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, "/", 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")