From ce308a67b0238de4163bda470dae6b2019f1f735 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 30 Sep 2018 08:50:20 +0800 Subject: [PATCH 1/2] feat: custom cookie name. --- auth_jwt.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/auth_jwt.go b/auth_jwt.go index c659615..9057780 100644 --- a/auth_jwt.go +++ b/auth_jwt.go @@ -122,6 +122,9 @@ type GinJWTMiddleware struct { // Disable abort() of context. DisabledAbort bool + + // Allow cookie domain change for development + CookieName string } var ( @@ -315,6 +318,10 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error { mw.Realm = "gin jwt" } + if mw.CookieName == "" { + mw.CookieName = "jwt" + } + if mw.usingPublicKeyAlgo() { return mw.readKeys() } @@ -421,7 +428,7 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) { if mw.SendCookie { maxage := int(expire.Unix() - time.Now().Unix()) c.SetCookie( - "JWTToken", + mw.CookieName, tokenString, maxage, "/", @@ -486,7 +493,7 @@ func (mw *GinJWTMiddleware) RefreshToken(c *gin.Context) (string, time.Time, err if mw.SendCookie { maxage := int(expire.Unix() - time.Now().Unix()) c.SetCookie( - "JWTToken", + mw.CookieName, tokenString, maxage, "/", From 88e18a67b29f8cf5284247646445f26c6c49f81d Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 30 Sep 2018 09:01:14 +0800 Subject: [PATCH 2/2] update cookie testing Signed-off-by: Bo-Yi Wu --- auth_jwt_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/auth_jwt_test.go b/auth_jwt_test.go index 59704f7..a7e183b 100644 --- a/auth_jwt_test.go +++ b/auth_jwt_test.go @@ -221,7 +221,7 @@ func TestLoginHandler(t *testing.T) { return true }, LoginResponse: func(c *gin.Context, code int, token string, t time.Time) { - cookie, err := c.Cookie("JWTToken") + cookie, err := c.Cookie("jwt") if err != nil { log.Println(err) } @@ -268,7 +268,7 @@ func TestLoginHandler(t *testing.T) { r.POST("/login"). SetCookie(gofight.H{ - "JWTToken": "JWTToken", + "jwt": "jwt", }). SetJSON(gofight.D{ "username": "admin", @@ -279,7 +279,7 @@ func TestLoginHandler(t *testing.T) { cookie := gjson.Get(r.Body.String(), "cookie") assert.Equal(t, "login successfully", message.String()) assert.Equal(t, http.StatusOK, r.Code) - assert.Equal(t, "JWTToken", cookie.String()) + assert.Equal(t, "jwt", cookie.String()) }) } @@ -404,9 +404,10 @@ func TestRefreshHandlerRS256(t *testing.T) { PrivKeyFile: "testdata/jwtRS256.key", PubKeyFile: "testdata/jwtRS256.key.pub", SendCookie: true, + CookieName: "jwt", Authenticator: defaultAuthenticator, RefreshResponse: func(c *gin.Context, code int, token string, t time.Time) { - cookie, err := c.Cookie("JWTToken") + cookie, err := c.Cookie("jwt") if err != nil { log.Println(err) } @@ -452,7 +453,7 @@ func TestRefreshHandlerRS256(t *testing.T) { "Authorization": "Bearer " + makeTokenString("RS256", "admin"), }). SetCookie(gofight.H{ - "JWTToken": makeTokenString("RS256", "admin"), + "jwt": makeTokenString("RS256", "admin"), }). Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { message := gjson.Get(r.Body.String(), "message")