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

custom cookie name #175

Merged
merged 2 commits into from
Sep 30, 2018
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
11 changes: 9 additions & 2 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ type GinJWTMiddleware struct {

// Disable abort() of context.
DisabledAbort bool

// Allow cookie domain change for development
CookieName string
}

var (
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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,
"/",
Expand Down Expand Up @@ -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,
"/",
Expand Down
11 changes: 6 additions & 5 deletions auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestLoginHandler(t *testing.T) {

r.POST("/login").
SetCookie(gofight.H{
"JWTToken": "JWTToken",
"jwt": "jwt",
}).
SetJSON(gofight.D{
"username": "admin",
Expand All @@ -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())
})
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down