Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit 9ae04f9

Browse files
authored
Merge pull request #27 from mingzaily/v2
Supports millisecond timestamp and user-defined blacklist keys
2 parents 40503f0 + dd5dd66 commit 9ae04f9

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

auth_jwt.go

+25-16
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package jwt
33
import (
44
"context"
55
"crypto/rsa"
6-
"github.com/gogf/gf/v2/crypto/gmd5"
7-
"github.com/gogf/gf/v2/frame/g"
8-
"github.com/gogf/gf/v2/net/ghttp"
9-
"github.com/gogf/gf/v2/os/gcache"
106
"io/ioutil"
117
"net/http"
128
"strings"
139
"time"
1410

11+
"github.com/gogf/gf/v2/crypto/gmd5"
12+
"github.com/gogf/gf/v2/frame/g"
13+
"github.com/gogf/gf/v2/net/ghttp"
14+
"github.com/gogf/gf/v2/os/gcache"
1515
"github.com/golang-jwt/jwt/v4"
1616
)
1717

@@ -145,6 +145,9 @@ type GfJWTMiddleware struct {
145145

146146
// CacheAdapter
147147
CacheAdapter gcache.Adapter
148+
149+
// BlacklistPrefix
150+
BlacklistPrefix string
148151
}
149152

150153
var (
@@ -245,6 +248,10 @@ func New(mw *GfJWTMiddleware) *GfJWTMiddleware {
245248
blacklist.SetAdapter(mw.CacheAdapter)
246249
}
247250

251+
if mw.BlacklistPrefix == "" {
252+
mw.BlacklistPrefix = "JWT:BLACKLIST:"
253+
}
254+
248255
return mw
249256
}
250257

@@ -311,8 +318,8 @@ func (mw *GfJWTMiddleware) LoginHandler(ctx context.Context) (tokenString string
311318
}
312319

313320
expire = mw.TimeFunc().Add(mw.Timeout)
314-
claims["exp"] = expire.Unix()
315-
claims["orig_iat"] = mw.TimeFunc().Unix()
321+
claims["exp"] = expire.UnixNano() / 1e6
322+
claims["orig_iat"] = mw.TimeFunc().UnixNano() / 1e6
316323

317324
tokenString, err = mw.signedString(token)
318325
if err != nil {
@@ -323,7 +330,7 @@ func (mw *GfJWTMiddleware) LoginHandler(ctx context.Context) (tokenString string
323330
// set cookie
324331
if mw.SendCookie {
325332
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
326-
maxAge := int(expireCookie.Unix() - mw.TimeFunc().Unix())
333+
maxAge := (expireCookie.UnixNano() - mw.TimeFunc().UnixNano()) / 1e6
327334
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", time.Duration(maxAge)*time.Second)
328335
}
329336

@@ -385,8 +392,8 @@ func (mw *GfJWTMiddleware) RefreshToken(ctx context.Context) (string, time.Time,
385392
}
386393

387394
expire := mw.TimeFunc().Add(mw.Timeout)
388-
newClaims["exp"] = expire.Unix()
389-
newClaims["orig_iat"] = mw.TimeFunc().Unix()
395+
newClaims["exp"] = expire.UnixNano() / 1e6
396+
newClaims["orig_iat"] = mw.TimeFunc().UnixNano() / 1e6
390397
tokenString, err := mw.signedString(newToken)
391398
if err != nil {
392399
return "", time.Now(), err
@@ -395,7 +402,7 @@ func (mw *GfJWTMiddleware) RefreshToken(ctx context.Context) (string, time.Time,
395402
// set cookie
396403
if mw.SendCookie {
397404
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
398-
maxAge := int(expireCookie.Unix() - time.Now().Unix())
405+
maxAge := (expireCookie.UnixNano() - time.Now().UnixNano()) / 1e6
399406
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", time.Duration(maxAge)*time.Second)
400407
}
401408

@@ -437,7 +444,7 @@ func (mw *GfJWTMiddleware) CheckIfTokenExpire(ctx context.Context) (jwt.MapClaim
437444

438445
origIat := int64(claims["orig_iat"].(float64))
439446

440-
if origIat < mw.TimeFunc().Add(-mw.MaxRefresh).Unix() {
447+
if origIat < (mw.TimeFunc().Add(-mw.MaxRefresh).UnixNano() / 1e6) {
441448
return nil, "", ErrExpiredToken
442449
}
443450

@@ -456,8 +463,8 @@ func (mw *GfJWTMiddleware) TokenGenerator(data interface{}) (string, time.Time,
456463
}
457464

458465
expire := mw.TimeFunc().UTC().Add(mw.Timeout)
459-
claims["exp"] = expire.Unix()
460-
claims["orig_iat"] = mw.TimeFunc().Unix()
466+
claims["exp"] = expire.UnixNano() / 1e6
467+
claims["orig_iat"] = mw.TimeFunc().UnixNano() / 1e6
461468
tokenString, err := mw.signedString(token)
462469
if err != nil {
463470
return "", time.Time{}, err
@@ -732,7 +739,7 @@ func (mw *GfJWTMiddleware) middlewareImpl(ctx context.Context) {
732739
return
733740
}
734741

735-
if int64(claims["exp"].(float64)) < mw.TimeFunc().Unix() {
742+
if int64(claims["exp"].(float64)) < (mw.TimeFunc().UnixNano() / 1e6) {
736743
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(ErrExpiredToken, ctx))
737744
return
738745
}
@@ -776,8 +783,9 @@ func (mw *GfJWTMiddleware) setBlacklist(ctx context.Context, token string, claim
776783
// save duration time = (exp + max_refresh) - now
777784
duration := time.Unix(exp, 0).Add(mw.MaxRefresh).Sub(mw.TimeFunc()).Truncate(time.Second)
778785

786+
key := mw.BlacklistPrefix + token
779787
// global gcache
780-
err = blacklist.Set(ctx, token, true, duration)
788+
err = blacklist.Set(ctx, key, true, duration)
781789

782790
if err != nil {
783791
return err
@@ -794,8 +802,9 @@ func (mw *GfJWTMiddleware) inBlacklist(ctx context.Context, token string) (bool,
794802
return false, nil
795803
}
796804

805+
key := mw.BlacklistPrefix + tokenRaw
797806
// Global gcache
798-
if in, err := blacklist.Contains(ctx, tokenRaw); err != nil {
807+
if in, err := blacklist.Contains(ctx, key); err != nil {
799808
return false, nil
800809
} else {
801810
return in, nil

0 commit comments

Comments
 (0)