Skip to content

Commit

Permalink
refactor: Provided an Option to Set Cookie using *GinJWTMiddleware (#335
Browse files Browse the repository at this point in the history
)

* [FEATURE] SetCookie method should be publicly exposed

* TestSetCookie test added to test the functionality of SetCookie method
  • Loading branch information
maniSHarma7575 authored Jul 14, 2024
1 parent 4339e81 commit d36b890
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 38 deletions.
63 changes: 25 additions & 38 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,25 +517,7 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
return
}

// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
tokenString,
maxage,
"/",
mw.CookieDomain,
mw.SecureCookie,
mw.CookieHTTPOnly,
)
}
mw.SetCookie(c, tokenString)

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

// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - time.Now().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
tokenString,
maxage,
"/",
mw.CookieDomain,
mw.SecureCookie,
mw.CookieHTTPOnly,
)
}
mw.SetCookie(c, tokenString)

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

return token.(string)
}

// SetCookie help to set the token in the cookie
func (mw *GinJWTMiddleware) SetCookie(c *gin.Context, token string) {
// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
token,
maxage,
"/",
mw.CookieDomain,
mw.SecureCookie,
mw.CookieHTTPOnly,
)
}
}
37 changes: 37 additions & 0 deletions auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -1322,3 +1323,39 @@ func TestLogout(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%s=; Path=/; Domain=%s; Max-Age=0", cookieName, cookieDomain), r.HeaderMap.Get("Set-Cookie"))
})
}

func TestSetCookie(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)

mw, _ := New(&GinJWTMiddleware{
Realm: "test zone",
Key: key,
Timeout: time.Hour,
Authenticator: defaultAuthenticator,
SendCookie: true,
CookieName: "jwt",
CookieMaxAge: time.Hour,
CookieDomain: "example.com",
SecureCookie: false,
CookieHTTPOnly: true,
TimeFunc: func() time.Time {
return time.Now()
},
})

token := makeTokenString("HS384", "admin")

mw.SetCookie(c, token)

cookies := w.Result().Cookies()

assert.Len(t, cookies, 1)

cookie := cookies[0]
assert.Equal(t, "jwt", cookie.Name)
assert.Equal(t, token, cookie.Value)
assert.Equal(t, "/", cookie.Path)
assert.Equal(t, "example.com", cookie.Domain)
assert.Equal(t, true, cookie.HttpOnly)
}

0 comments on commit d36b890

Please sign in to comment.