Skip to content

Commit 00b96ae

Browse files
authored
feat: add TimeFunc provides the current time. (#67)
* feat: add TimeFunc provides the current time. * fix: example * fix: update readme.
1 parent cdf2348 commit 00b96ae

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func main() {
108108

109109
// TokenHeadName is a string in the header. Default value is "Bearer"
110110
TokenHeadName: "Bearer",
111+
112+
// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
113+
TimeFunc: time.Now,
111114
}
112115

113116
r.POST("/login", authMiddleware.LoginHandler)

auth_jwt.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ type GinJWTMiddleware struct {
7070

7171
// TokenHeadName is a string in the header. Default value is "Bearer"
7272
TokenHeadName string
73+
74+
// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
75+
TimeFunc func() time.Time
7376
}
7477

7578
// Login form structure.
@@ -93,6 +96,10 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
9396
mw.Timeout = time.Hour
9497
}
9598

99+
if mw.TimeFunc == nil {
100+
mw.TimeFunc = time.Now
101+
}
102+
96103
mw.TokenHeadName = strings.TrimSpace(mw.TokenHeadName)
97104
if len(mw.TokenHeadName) == 0 {
98105
mw.TokenHeadName = "Bearer"
@@ -207,10 +214,10 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
207214
userID = loginVals.Username
208215
}
209216

210-
expire := time.Now().Add(mw.Timeout)
217+
expire := mw.TimeFunc().Add(mw.Timeout)
211218
claims["id"] = userID
212219
claims["exp"] = expire.Unix()
213-
claims["orig_iat"] = time.Now().Unix()
220+
claims["orig_iat"] = mw.TimeFunc().Unix()
214221

215222
tokenString, err := token.SignedString(mw.Key)
216223

@@ -234,7 +241,7 @@ func (mw *GinJWTMiddleware) RefreshHandler(c *gin.Context) {
234241

235242
origIat := int64(claims["orig_iat"].(float64))
236243

237-
if origIat < time.Now().Add(-mw.MaxRefresh).Unix() {
244+
if origIat < mw.TimeFunc().Add(-mw.MaxRefresh).Unix() {
238245
mw.unauthorized(c, http.StatusUnauthorized, "Token is expired.")
239246
return
240247
}
@@ -247,7 +254,7 @@ func (mw *GinJWTMiddleware) RefreshHandler(c *gin.Context) {
247254
newClaims[key] = claims[key]
248255
}
249256

250-
expire := time.Now().Add(mw.Timeout)
257+
expire := mw.TimeFunc().Add(mw.Timeout)
251258
newClaims["id"] = claims["id"]
252259
newClaims["exp"] = expire.Unix()
253260
newClaims["orig_iat"] = origIat
@@ -290,8 +297,8 @@ func (mw *GinJWTMiddleware) TokenGenerator(userID string) string {
290297
}
291298

292299
claims["id"] = userID
293-
claims["exp"] = time.Now().Add(mw.Timeout).Unix()
294-
claims["orig_iat"] = time.Now().Unix()
300+
claims["exp"] = mw.TimeFunc().Add(mw.Timeout).Unix()
301+
claims["orig_iat"] = mw.TimeFunc().Unix()
295302

296303
tokenString, _ := token.SignedString(mw.Key)
297304

example/server.go

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func main() {
6464

6565
// TokenHeadName is a string in the header. Default value is "Bearer"
6666
TokenHeadName: "Bearer",
67+
68+
// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
69+
TimeFunc: time.Now,
6770
}
6871

6972
r.POST("/login", authMiddleware.LoginHandler)

0 commit comments

Comments
 (0)