@@ -3,15 +3,15 @@ package jwt
3
3
import (
4
4
"context"
5
5
"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"
10
6
"io/ioutil"
11
7
"net/http"
12
8
"strings"
13
9
"time"
14
10
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"
15
15
"github.com/golang-jwt/jwt/v4"
16
16
)
17
17
@@ -145,6 +145,9 @@ type GfJWTMiddleware struct {
145
145
146
146
// CacheAdapter
147
147
CacheAdapter gcache.Adapter
148
+
149
+ // BlacklistPrefix
150
+ BlacklistPrefix string
148
151
}
149
152
150
153
var (
@@ -245,6 +248,10 @@ func New(mw *GfJWTMiddleware) *GfJWTMiddleware {
245
248
blacklist .SetAdapter (mw .CacheAdapter )
246
249
}
247
250
251
+ if mw .BlacklistPrefix == "" {
252
+ mw .BlacklistPrefix = "JWT:BLACKLIST:"
253
+ }
254
+
248
255
return mw
249
256
}
250
257
@@ -311,8 +318,8 @@ func (mw *GfJWTMiddleware) LoginHandler(ctx context.Context) (tokenString string
311
318
}
312
319
313
320
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
316
323
317
324
tokenString , err = mw .signedString (token )
318
325
if err != nil {
@@ -323,7 +330,7 @@ func (mw *GfJWTMiddleware) LoginHandler(ctx context.Context) (tokenString string
323
330
// set cookie
324
331
if mw .SendCookie {
325
332
expireCookie := mw .TimeFunc ().Add (mw .CookieMaxAge )
326
- maxAge := int (expireCookie .Unix () - mw .TimeFunc ().Unix ())
333
+ maxAge := (expireCookie .UnixNano () - mw .TimeFunc ().UnixNano ()) / 1e6
327
334
r .Cookie .SetCookie (mw .CookieName , tokenString , mw .CookieDomain , "/" , time .Duration (maxAge )* time .Second )
328
335
}
329
336
@@ -385,8 +392,8 @@ func (mw *GfJWTMiddleware) RefreshToken(ctx context.Context) (string, time.Time,
385
392
}
386
393
387
394
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
390
397
tokenString , err := mw .signedString (newToken )
391
398
if err != nil {
392
399
return "" , time .Now (), err
@@ -395,7 +402,7 @@ func (mw *GfJWTMiddleware) RefreshToken(ctx context.Context) (string, time.Time,
395
402
// set cookie
396
403
if mw .SendCookie {
397
404
expireCookie := mw .TimeFunc ().Add (mw .CookieMaxAge )
398
- maxAge := int (expireCookie .Unix () - time .Now ().Unix ())
405
+ maxAge := (expireCookie .UnixNano () - time .Now ().UnixNano ()) / 1e6
399
406
r .Cookie .SetCookie (mw .CookieName , tokenString , mw .CookieDomain , "/" , time .Duration (maxAge )* time .Second )
400
407
}
401
408
@@ -437,7 +444,7 @@ func (mw *GfJWTMiddleware) CheckIfTokenExpire(ctx context.Context) (jwt.MapClaim
437
444
438
445
origIat := int64 (claims ["orig_iat" ].(float64 ))
439
446
440
- if origIat < mw .TimeFunc ().Add (- mw .MaxRefresh ).Unix ( ) {
447
+ if origIat < ( mw .TimeFunc ().Add (- mw .MaxRefresh ).UnixNano () / 1e6 ) {
441
448
return nil , "" , ErrExpiredToken
442
449
}
443
450
@@ -456,8 +463,8 @@ func (mw *GfJWTMiddleware) TokenGenerator(data interface{}) (string, time.Time,
456
463
}
457
464
458
465
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
461
468
tokenString , err := mw .signedString (token )
462
469
if err != nil {
463
470
return "" , time.Time {}, err
@@ -732,7 +739,7 @@ func (mw *GfJWTMiddleware) middlewareImpl(ctx context.Context) {
732
739
return
733
740
}
734
741
735
- if int64 (claims ["exp" ].(float64 )) < mw .TimeFunc ().Unix ( ) {
742
+ if int64 (claims ["exp" ].(float64 )) < ( mw .TimeFunc ().UnixNano () / 1e6 ) {
736
743
mw .unauthorized (ctx , http .StatusUnauthorized , mw .HTTPStatusMessageFunc (ErrExpiredToken , ctx ))
737
744
return
738
745
}
@@ -776,8 +783,9 @@ func (mw *GfJWTMiddleware) setBlacklist(ctx context.Context, token string, claim
776
783
// save duration time = (exp + max_refresh) - now
777
784
duration := time .Unix (exp , 0 ).Add (mw .MaxRefresh ).Sub (mw .TimeFunc ()).Truncate (time .Second )
778
785
786
+ key := mw .BlacklistPrefix + token
779
787
// global gcache
780
- err = blacklist .Set (ctx , token , true , duration )
788
+ err = blacklist .Set (ctx , key , true , duration )
781
789
782
790
if err != nil {
783
791
return err
@@ -794,8 +802,9 @@ func (mw *GfJWTMiddleware) inBlacklist(ctx context.Context, token string) (bool,
794
802
return false , nil
795
803
}
796
804
805
+ key := mw .BlacklistPrefix + tokenRaw
797
806
// Global gcache
798
- if in , err := blacklist .Contains (ctx , tokenRaw ); err != nil {
807
+ if in , err := blacklist .Contains (ctx , key ); err != nil {
799
808
return false , nil
800
809
} else {
801
810
return in , nil
0 commit comments