Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Provided an Option to Set Cookie using *GinJWTMiddleware #335

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 25 additions & 38 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,25 +517,7 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
return
}

// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
tokenString,
maxage,
"/",
mw.CookieDomain,
mw.SecureCookie,
mw.CookieHTTPOnly,
)
}
mw.SetCookie(c, tokenString)

mw.LoginResponse(c, http.StatusOK, tokenString, expire)
}
Expand Down Expand Up @@ -609,25 +591,7 @@ func (mw *GinJWTMiddleware) RefreshToken(c *gin.Context) (string, time.Time, err
return "", time.Now(), err
}

// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - time.Now().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
tokenString,
maxage,
"/",
mw.CookieDomain,
mw.SecureCookie,
mw.CookieHTTPOnly,
)
}
mw.SetCookie(c, tokenString)

return tokenString, expire, nil
}
Expand Down Expand Up @@ -845,3 +809,26 @@ func GetToken(c *gin.Context) string {

return token.(string)
}

// SetCookie help to set the token in the cookie
func (mw *GinJWTMiddleware) SetCookie(c *gin.Context, token string) {
// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
token,
maxage,
"/",
mw.CookieDomain,
mw.SecureCookie,
mw.CookieHTTPOnly,
)
}
}
37 changes: 37 additions & 0 deletions auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -1322,3 +1323,39 @@ func TestLogout(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%s=; Path=/; Domain=%s; Max-Age=0", cookieName, cookieDomain), r.HeaderMap.Get("Set-Cookie"))
})
}

func TestSetCookie(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)

mw, _ := New(&GinJWTMiddleware{
Realm: "test zone",
Key: key,
Timeout: time.Hour,
Authenticator: defaultAuthenticator,
SendCookie: true,
CookieName: "jwt",
CookieMaxAge: time.Hour,
CookieDomain: "example.com",
SecureCookie: false,
CookieHTTPOnly: true,
TimeFunc: func() time.Time {
return time.Now()
},
})

token := makeTokenString("HS384", "admin")

mw.SetCookie(c, token)

cookies := w.Result().Cookies()

assert.Len(t, cookies, 1)

cookie := cookies[0]
assert.Equal(t, "jwt", cookie.Name)
assert.Equal(t, token, cookie.Value)
assert.Equal(t, "/", cookie.Path)
assert.Equal(t, "example.com", cookie.Domain)
assert.Equal(t, true, cookie.HttpOnly)
}
Loading