Skip to content

Commit d36b890

Browse files
refactor: Provided an Option to Set Cookie using *GinJWTMiddleware (#335)
* [FEATURE] SetCookie method should be publicly exposed * TestSetCookie test added to test the functionality of SetCookie method
1 parent 4339e81 commit d36b890

File tree

2 files changed

+62
-38
lines changed

2 files changed

+62
-38
lines changed

auth_jwt.go

+25-38
Original file line numberDiff line numberDiff line change
@@ -517,25 +517,7 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
517517
return
518518
}
519519

520-
// set cookie
521-
if mw.SendCookie {
522-
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
523-
maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())
524-
525-
if mw.CookieSameSite != 0 {
526-
c.SetSameSite(mw.CookieSameSite)
527-
}
528-
529-
c.SetCookie(
530-
mw.CookieName,
531-
tokenString,
532-
maxage,
533-
"/",
534-
mw.CookieDomain,
535-
mw.SecureCookie,
536-
mw.CookieHTTPOnly,
537-
)
538-
}
520+
mw.SetCookie(c, tokenString)
539521

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

612-
// set cookie
613-
if mw.SendCookie {
614-
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
615-
maxage := int(expireCookie.Unix() - time.Now().Unix())
616-
617-
if mw.CookieSameSite != 0 {
618-
c.SetSameSite(mw.CookieSameSite)
619-
}
620-
621-
c.SetCookie(
622-
mw.CookieName,
623-
tokenString,
624-
maxage,
625-
"/",
626-
mw.CookieDomain,
627-
mw.SecureCookie,
628-
mw.CookieHTTPOnly,
629-
)
630-
}
594+
mw.SetCookie(c, tokenString)
631595

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

846810
return token.(string)
847811
}
812+
813+
// SetCookie help to set the token in the cookie
814+
func (mw *GinJWTMiddleware) SetCookie(c *gin.Context, token string) {
815+
// set cookie
816+
if mw.SendCookie {
817+
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
818+
maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())
819+
820+
if mw.CookieSameSite != 0 {
821+
c.SetSameSite(mw.CookieSameSite)
822+
}
823+
824+
c.SetCookie(
825+
mw.CookieName,
826+
token,
827+
maxage,
828+
"/",
829+
mw.CookieDomain,
830+
mw.SecureCookie,
831+
mw.CookieHTTPOnly,
832+
)
833+
}
834+
}

auth_jwt_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8+
"net/http/httptest"
89
"os"
910
"reflect"
1011
"strings"
@@ -1322,3 +1323,39 @@ func TestLogout(t *testing.T) {
13221323
assert.Equal(t, fmt.Sprintf("%s=; Path=/; Domain=%s; Max-Age=0", cookieName, cookieDomain), r.HeaderMap.Get("Set-Cookie"))
13231324
})
13241325
}
1326+
1327+
func TestSetCookie(t *testing.T) {
1328+
w := httptest.NewRecorder()
1329+
c, _ := gin.CreateTestContext(w)
1330+
1331+
mw, _ := New(&GinJWTMiddleware{
1332+
Realm: "test zone",
1333+
Key: key,
1334+
Timeout: time.Hour,
1335+
Authenticator: defaultAuthenticator,
1336+
SendCookie: true,
1337+
CookieName: "jwt",
1338+
CookieMaxAge: time.Hour,
1339+
CookieDomain: "example.com",
1340+
SecureCookie: false,
1341+
CookieHTTPOnly: true,
1342+
TimeFunc: func() time.Time {
1343+
return time.Now()
1344+
},
1345+
})
1346+
1347+
token := makeTokenString("HS384", "admin")
1348+
1349+
mw.SetCookie(c, token)
1350+
1351+
cookies := w.Result().Cookies()
1352+
1353+
assert.Len(t, cookies, 1)
1354+
1355+
cookie := cookies[0]
1356+
assert.Equal(t, "jwt", cookie.Name)
1357+
assert.Equal(t, token, cookie.Value)
1358+
assert.Equal(t, "/", cookie.Path)
1359+
assert.Equal(t, "example.com", cookie.Domain)
1360+
assert.Equal(t, true, cookie.HttpOnly)
1361+
}

0 commit comments

Comments
 (0)