Skip to content

Commit

Permalink
added cookie and token op
Browse files Browse the repository at this point in the history
  • Loading branch information
cateiru committed Aug 28, 2021
1 parent 6fc1fc1 commit e7c439b
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
85 changes: 85 additions & 0 deletions cookie.go
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
}
72 changes: 72 additions & 0 deletions tokenOp.go
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
}

0 comments on commit e7c439b

Please sign in to comment.