Skip to content

Commit c8c5292

Browse files
committed
add expire time testing.
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 3bff0bf commit c8c5292

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

auth_jwt.go

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ func (mw *GinJWTMiddleware) TokenGenerator(userID string) string {
251251

252252
token.Claims["id"] = userID
253253
token.Claims["exp"] = time.Now().Add(mw.Timeout).Unix()
254+
token.Claims["orig_iat"] = time.Now().Unix()
254255

255256
tokenString, _ := token.SignedString(mw.Key)
256257

auth_jwt_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,36 @@ func TestUnauthorized(t *testing.T) {
550550
assert.Equal(t, http.StatusUnauthorized, r.Code)
551551
})
552552
}
553+
554+
func TestTokenExpire(t *testing.T) {
555+
// the middleware to test
556+
authMiddleware := &GinJWTMiddleware{
557+
Realm: "test zone",
558+
Key: key,
559+
Timeout: time.Hour,
560+
MaxRefresh: -time.Second,
561+
Authenticator: func(userId string, password string, c *gin.Context) (string, bool) {
562+
if userId == "admin" && password == "admin" {
563+
return userId, true
564+
}
565+
return userId, false
566+
},
567+
Unauthorized: func(c *gin.Context, code int, message string) {
568+
c.String(code, message)
569+
},
570+
}
571+
572+
handler := ginHandler(authMiddleware)
573+
574+
r := gofight.New()
575+
576+
userToken := authMiddleware.TokenGenerator("admin")
577+
578+
r.GET("/auth/refresh_token").
579+
SetHeader(gofight.H{
580+
"Authorization": "Bearer " + userToken,
581+
}).
582+
Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
583+
assert.Equal(t, http.StatusUnauthorized, r.Code)
584+
})
585+
}

0 commit comments

Comments
 (0)