-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package networkutil | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type CookieOperation struct { | ||
Path string | ||
Domain string | ||
Secure bool | ||
HttpOnly bool | ||
} | ||
|
||
func NewCookieOp() *CookieOperation { | ||
path := "/" | ||
domain := "hello-slide.jp" | ||
|
||
return &CookieOperation{ | ||
Path: path, | ||
Domain: domain, | ||
Secure: true, | ||
HttpOnly: true, | ||
} | ||
} | ||
|
||
// Set cookie. | ||
// | ||
// Arguments: | ||
// w {http.ResponseWriter} - http writer. | ||
// name {string} - cookie key. | ||
// value {string} - cookie value. | ||
// exp {int} - date of expiry. It is an hourly unit. | ||
func (c *CookieOperation) Set(w http.ResponseWriter, name string, value string, exp int) { | ||
expires := time.Now().Add(time.Duration(exp) * time.Hour) | ||
maxAge := 60 * 60 * exp | ||
|
||
cookie := &http.Cookie{ | ||
Name: name, | ||
Value: value, | ||
|
||
Expires: expires, | ||
MaxAge: maxAge, | ||
|
||
Secure: c.Secure, | ||
Path: c.Path, | ||
Domain: c.Domain, | ||
HttpOnly: c.HttpOnly, | ||
} | ||
|
||
http.SetCookie(w, cookie) | ||
} | ||
|
||
// Delete cookie | ||
// | ||
// Arguments: | ||
// w {http.ResponseWriter} - http writer. | ||
// req {http.Request} - http request. | ||
// name {string} - cookie key. | ||
func (c *CookieOperation) Delete(w http.ResponseWriter, req *http.Request, name string) error { | ||
cookie, err := req.Cookie(name) | ||
if err != nil { | ||
return err | ||
} | ||
cookie.MaxAge = -1 | ||
http.SetCookie(w, cookie) | ||
return nil | ||
} | ||
|
||
// Get cookie. | ||
// | ||
// Arguments: | ||
// req {http.Request} - http request. | ||
// name {string} - cookie key. | ||
// | ||
// Retruns: | ||
// {string} - cookie value. | ||
func (c *CookieOperation) Get(req *http.Request, name string) (string, error) { | ||
cookie, err := req.Cookie(name) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
return cookie.Value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package networkutil | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type TokenOperation struct { | ||
CookieOp CookieOperation | ||
} | ||
|
||
func NewTokenOp() *TokenOperation { | ||
cookieOp := NewCookieOp() | ||
return &TokenOperation{ | ||
CookieOp: *cookieOp, | ||
} | ||
} | ||
|
||
// Set the refresh token. | ||
// | ||
// Arguments: | ||
// w {http.ResponseWriter} - http writer. | ||
// refreshToken {string} - refresh token. | ||
func (t *TokenOperation) SetRefreshToken(w http.ResponseWriter, refreshToken string) { | ||
t.CookieOp.Set(w, "refresh_token", refreshToken, 24*30) | ||
} | ||
|
||
// Set the session token. | ||
// | ||
// Arguments: | ||
// w {http.ResponseWriter} - http writer. | ||
// sessionToken {string} - session token. | ||
func (t *TokenOperation) SetSessionToken(w http.ResponseWriter, sessionToken string) { | ||
t.CookieOp.Set(w, "session_token", sessionToken, 6) | ||
} | ||
|
||
// Get the refresh token. | ||
// | ||
// Arguments: | ||
// r {*http.Request} - http requests. | ||
// | ||
// Returns: | ||
// {string} - refresh token. | ||
func (t *TokenOperation) GetRefreshToken(r *http.Request) (string, error) { | ||
return t.CookieOp.Get(r, "refresh_token") | ||
} | ||
|
||
// Get the session token. | ||
// | ||
// Arguments: | ||
// r {*http.Request} - http requests. | ||
// | ||
// Returns: | ||
// {string} - session token. | ||
func (t *TokenOperation) GetSessionToken(r *http.Request) (string, error) { | ||
return t.CookieOp.Get(r, "session_token") | ||
} | ||
|
||
// Delete session and refresh tokens. | ||
// | ||
// Arguments: | ||
// w {http.ResponseWriter} - http writer. | ||
// r {*http.Request} - http requests. | ||
func (t *TokenOperation) DeleteToken(w http.ResponseWriter, r *http.Request) error { | ||
if err := t.CookieOp.Delete(w, r, "session_token"); err != nil { | ||
return err | ||
} | ||
|
||
if err := t.CookieOp.Delete(w, r, "refresh_token"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |