From 0a73fc86d1819869347fcc21a62239d45f412e36 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 4 May 2020 15:20:40 +0300 Subject: [PATCH] implements CookieMaxAge value --- auth_jwt.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/auth_jwt.go b/auth_jwt.go index 567d831..1a7c3cd 100644 --- a/auth_jwt.go +++ b/auth_jwt.go @@ -111,6 +111,9 @@ type GinJWTMiddleware struct { // Optionally return the token as a cookie SendCookie bool + // Duration that a cookie is valid. Optional, by default equals to Timeout value. + CookieMaxAge time.Duration + // Allow insecure cookies for development over http SecureCookie bool @@ -335,6 +338,10 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error { mw.Realm = "gin jwt" } + if mw.CookieMaxAge == 0 { + mw.CookieMaxAge = mw.Timeout + } + if mw.CookieName == "" { mw.CookieName = "jwt" } @@ -453,7 +460,8 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) { // set cookie if mw.SendCookie { - maxage := int(expire.Unix() - time.Now().Unix()) + expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge) + maxage := int(expireCookie.Unix() - time.Now().Unix()) c.SetCookie( mw.CookieName, tokenString, @@ -536,7 +544,8 @@ func (mw *GinJWTMiddleware) RefreshToken(c *gin.Context) (string, time.Time, err // set cookie if mw.SendCookie { - maxage := int(expire.Unix() - time.Now().Unix()) + expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge) + maxage := int(expireCookie.Unix() - time.Now().Unix()) c.SetCookie( mw.CookieName, tokenString,