-
Notifications
You must be signed in to change notification settings - Fork 44
/
auth_jwt.go
812 lines (655 loc) · 20.6 KB
/
auth_jwt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
package jwt
import (
"context"
"crypto/rsa"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/gogf/gf/v2/crypto/gmd5"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcache"
"github.com/golang-jwt/jwt/v4"
)
// MapClaims type that uses the map[string]interface{} for JSON decoding
// This is the default claims type if you don't supply one
type MapClaims map[string]interface{}
// GfJWTMiddleware provides a Json-Web-Token authentication implementation. On failure, a 401 HTTP response
// is returned. On success, the wrapped middleware is called, and the userID is made available as
// c.Get("userID").(string).
// Users can get a token by posting a json request to LoginHandler. The token then needs to be passed in
// the Authentication header. Example: Authorization:Bearer XXX_TOKEN_XXX
type GfJWTMiddleware struct {
// Realm name to display to the user. Required.
Realm string
// signing algorithm - possible values are HS256, HS384, HS512, RS256, RS384 or RS512
// Optional, default is HS256.
SigningAlgorithm string
// Secret key used for signing. Required.
Key []byte
// Callback to retrieve key used for signing. Setting KeyFunc will bypass
// all other key settings
KeyFunc func(token *jwt.Token) (interface{}, error)
// Duration that a jwt token is valid. Optional, defaults to one hour.
Timeout time.Duration
// This field allows clients to refresh their token until MaxRefresh has passed.
// Note that clients can refresh their token in the last moment of MaxRefresh.
// This means that the maximum validity timespan for a token is TokenTime + MaxRefresh.
// Optional, defaults to 0 meaning not refreshable.
MaxRefresh time.Duration
// Callback function that should perform the authentication of the user based on login info.
// Must return user data as user identifier, it will be stored in Claim Array. Required.
// Check error (e) to determine the appropriate error message.
Authenticator func(ctx context.Context) (interface{}, error)
// Callback function that should perform the authorization of the authenticated user. Called
// only after an authentication success. Must return true on success, false on failure.
// Optional, default to success.
Authorizator func(data interface{}, ctx context.Context) bool
// Callback function that will be called during login.
// Using this function it is possible to add additional payload data to the web token.
// The data is then made available during requests via c.Get(jwt.PayloadKey).
// Note that the payload is not encrypted.
// The attributes mentioned on jwt.io can't be used as keys for the map.
// Optional, by default no additional data will be set.
PayloadFunc func(data interface{}) MapClaims
// User can define own Unauthorized func.
Unauthorized func(ctx context.Context, code int, message string)
// Set the identity handler function
IdentityHandler func(ctx context.Context) interface{}
// Set the identity key
IdentityKey string
// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
// Optional. Default value "header:Authorization".
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "cookie:<name>"
TokenLookup string
// TokenHeadName is a string in the header. Default value is "Bearer"
TokenHeadName string
// 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.
TimeFunc func() time.Time
// HTTP Status messages for when something in the JWT middleware fails.
// Check error (e) to determine the appropriate error message.
HTTPStatusMessageFunc func(e error, ctx context.Context) string
// Private key file for asymmetric algorithms
PrivKeyFile string
// Private Key bytes for asymmetric algorithms
//
// Note: PrivKeyFile takes precedence over PrivKeyBytes if both are set
PrivKeyBytes []byte
// Public key file for asymmetric algorithms
PubKeyFile string
// Private key passphrase
PrivateKeyPassphrase string
// Public key bytes for asymmetric algorithms.
//
// Note: PubKeyFile takes precedence over PubKeyBytes if both are set
PubKeyBytes []byte
// Private key
privKey *rsa.PrivateKey
// Public key
pubKey *rsa.PublicKey
// Optionally return the token as a cookie
SendCookie bool
// Duration that a cookie is valid. Optional, by default equals to Timeout value.
CookieMaxAge time.Duration
// Allow insecure cookies for development over http
SecureCookie bool
// Allow cookies to be accessed client side for development
CookieHTTPOnly bool
// Allow cookie domain change for development
CookieDomain string
// SendAuthorization allow return authorization header for every request
SendAuthorization bool
// Disable abort() of context.
DisabledAbort bool
// CookieName allow cookie name change for development
CookieName string
// CacheAdapter
CacheAdapter gcache.Adapter
// BlacklistPrefix
BlacklistPrefix string
}
var (
// TokenKey default jwt token key in params
TokenKey = "JWT_TOKEN"
// PayloadKey default jwt payload key in params
PayloadKey = "JWT_PAYLOAD"
// IdentityKey default identity key
IdentityKey = "identity"
// The blacklist stores tokens that have not expired but have been deactivated.
blacklist = gcache.New()
)
// New for check error with GfJWTMiddleware
func New(mw *GfJWTMiddleware) *GfJWTMiddleware {
if mw.TokenLookup == "" {
mw.TokenLookup = "header:Authorization"
}
if mw.SigningAlgorithm == "" {
mw.SigningAlgorithm = "HS256"
}
if mw.Timeout == 0 {
mw.Timeout = time.Hour
}
if mw.TimeFunc == nil {
mw.TimeFunc = time.Now
}
mw.TokenHeadName = strings.TrimSpace(mw.TokenHeadName)
if len(mw.TokenHeadName) == 0 {
mw.TokenHeadName = "Bearer"
}
if mw.Authorizator == nil {
mw.Authorizator = func(data interface{}, ctx context.Context) bool {
return true
}
}
if mw.Unauthorized == nil {
mw.Unauthorized = func(ctx context.Context, code int, message string) {
r := g.RequestFromCtx(ctx)
r.Response.WriteJson(g.Map{
"code": code,
"message": message,
})
}
}
if mw.IdentityKey == "" {
mw.IdentityKey = IdentityKey
}
if mw.IdentityHandler == nil {
mw.IdentityHandler = func(ctx context.Context) interface{} {
claims := ExtractClaims(ctx)
return claims[mw.IdentityKey]
}
}
if mw.HTTPStatusMessageFunc == nil {
mw.HTTPStatusMessageFunc = func(e error, ctx context.Context) string {
return e.Error()
}
}
if mw.Realm == "" {
mw.Realm = "gf jwt"
}
if mw.CookieMaxAge == 0 {
mw.CookieMaxAge = mw.Timeout
}
if mw.CookieName == "" {
mw.CookieName = "jwt"
}
// bypass other key settings if KeyFunc is set
if mw.KeyFunc != nil {
return nil
}
if mw.usingPublicKeyAlgo() {
if err := mw.readKeys(); err != nil {
panic(err)
}
}
if mw.Key == nil {
panic(ErrMissingSecretKey)
}
if mw.CacheAdapter != nil {
blacklist.SetAdapter(mw.CacheAdapter)
}
if mw.BlacklistPrefix == "" {
mw.BlacklistPrefix = "JWT:BLACKLIST:"
}
return mw
}
// MiddlewareFunc makes GfJWTMiddleware implement the Middleware interface.
func (mw *GfJWTMiddleware) MiddlewareFunc() ghttp.HandlerFunc {
return func(r *ghttp.Request) {
mw.middlewareImpl(r.GetCtx())
}
}
// GetClaimsFromJWT get claims from JWT token
func (mw *GfJWTMiddleware) GetClaimsFromJWT(ctx context.Context) (MapClaims, string, error) {
r := g.RequestFromCtx(ctx)
token, err := mw.parseToken(r)
if err != nil {
return nil, "", err
}
if mw.SendAuthorization {
token := r.Get(TokenKey).String()
if len(token) > 0 {
r.Header.Set("Authorization", mw.TokenHeadName+" "+token)
}
}
claims := MapClaims{}
for key, value := range token.Claims.(jwt.MapClaims) {
claims[key] = value
}
return claims, token.Raw, nil
}
// LoginHandler can be used by clients to get a jwt token.
// Payload needs to be json in the form of {"username": "USERNAME", "password": "PASSWORD"}.
// Reply will be of the form {"token": "TOKEN"}.
func (mw *GfJWTMiddleware) LoginHandler(ctx context.Context) (tokenString string, expire time.Time) {
if mw.Authenticator == nil {
mw.unauthorized(ctx, http.StatusInternalServerError, mw.HTTPStatusMessageFunc(ErrMissingAuthenticatorFunc, ctx))
return
}
data, err := mw.Authenticator(ctx)
if err != nil {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
r := g.RequestFromCtx(ctx)
// Create the token
token := jwt.New(jwt.GetSigningMethod(mw.SigningAlgorithm))
claims := token.Claims.(jwt.MapClaims)
if mw.PayloadFunc != nil {
for key, value := range mw.PayloadFunc(data) {
claims[key] = value
}
}
if _, ok := claims[mw.IdentityKey]; !ok {
mw.unauthorized(ctx, http.StatusInternalServerError, mw.HTTPStatusMessageFunc(ErrMissingIdentity, ctx))
return
}
expire = mw.TimeFunc().Add(mw.Timeout)
claims["exp"] = expire.UnixNano() / 1e6
claims["orig_iat"] = mw.TimeFunc().UnixNano() / 1e6
tokenString, err = mw.signedString(token)
if err != nil {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(ErrFailedTokenCreation, ctx))
return
}
// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxAge := (expireCookie.UnixNano() - mw.TimeFunc().UnixNano()) / 1e6
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", time.Duration(maxAge)*time.Second)
}
return
}
// LogoutHandler can be used by clients to remove the jwt cookie (if set)
func (mw *GfJWTMiddleware) LogoutHandler(ctx context.Context) {
r := g.RequestFromCtx(ctx)
// delete auth cookie
if mw.SendCookie {
r.Cookie.SetCookie(mw.CookieName, "", mw.CookieDomain, "/", -1)
}
claims, token, err := mw.CheckIfTokenExpire(ctx)
if err != nil {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
err = mw.setBlacklist(ctx, token, claims)
if err != nil {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
return
}
// RefreshHandler can be used to refresh a token. The token still needs to be valid on refresh.
// Shall be put under an endpoint that is using the GfJWTMiddleware.
// Reply will be of the form {"token": "TOKEN"}.
func (mw *GfJWTMiddleware) RefreshHandler(ctx context.Context) (tokenString string, expire time.Time) {
tokenString, expire, err := mw.RefreshToken(ctx)
if err != nil {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
return
}
// RefreshToken refresh token and check if token is expired
func (mw *GfJWTMiddleware) RefreshToken(ctx context.Context) (string, time.Time, error) {
claims, token, err := mw.CheckIfTokenExpire(ctx)
if err != nil {
return "", time.Now(), err
}
r := g.RequestFromCtx(ctx)
// Create the token
newToken := jwt.New(jwt.GetSigningMethod(mw.SigningAlgorithm))
newClaims := newToken.Claims.(jwt.MapClaims)
for key := range claims {
newClaims[key] = claims[key]
}
expire := mw.TimeFunc().Add(mw.Timeout)
newClaims["exp"] = expire.UnixNano() / 1e6
newClaims["orig_iat"] = mw.TimeFunc().UnixNano() / 1e6
tokenString, err := mw.signedString(newToken)
if err != nil {
return "", time.Now(), err
}
// set cookie
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxAge := (expireCookie.UnixNano() - time.Now().UnixNano()) / 1e6
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", time.Duration(maxAge)*time.Second)
}
// set old token in blacklist
err = mw.setBlacklist(ctx, token, claims)
if err != nil {
return "", time.Now(), err
}
return tokenString, expire, nil
}
// CheckIfTokenExpire check if token expire
func (mw *GfJWTMiddleware) CheckIfTokenExpire(ctx context.Context) (jwt.MapClaims, string, error) {
r := g.RequestFromCtx(ctx)
token, err := mw.parseToken(r)
if err != nil {
// If we receive an error, and the error is anything other than a single
// ValidationErrorExpired, we want to return the error.
// If the error is just ValidationErrorExpired, we want to continue, as we can still
// refresh the token if it's within the MaxRefresh time.
// (see https://github.com/appleboy/gin-jwt/issues/176)
validationErr, ok := err.(*jwt.ValidationError)
if !ok || validationErr.Errors != jwt.ValidationErrorExpired {
return nil, "", err
}
}
in, err := mw.inBlacklist(ctx, token.Raw)
if err != nil {
return nil, "", err
}
if in {
return nil, "", ErrInvalidToken
}
claims := token.Claims.(jwt.MapClaims)
exp := int64(claims["exp"].(float64))
if exp < (mw.TimeFunc().Add(-mw.MaxRefresh).UnixNano() / 1e6) {
return nil, "", ErrExpiredToken
}
return claims, token.Raw, nil
}
// TokenGenerator method that clients can use to get a jwt token.
func (mw *GfJWTMiddleware) TokenGenerator(data interface{}) (string, time.Time, error) {
token := jwt.New(jwt.GetSigningMethod(mw.SigningAlgorithm))
claims := token.Claims.(jwt.MapClaims)
if mw.PayloadFunc != nil {
for key, value := range mw.PayloadFunc(data) {
claims[key] = value
}
}
expire := mw.TimeFunc().UTC().Add(mw.Timeout)
claims["exp"] = expire.UnixNano() / 1e6
claims["orig_iat"] = mw.TimeFunc().UnixNano() / 1e6
tokenString, err := mw.signedString(token)
if err != nil {
return "", time.Time{}, err
}
return tokenString, expire, nil
}
// GetToken help to get the JWT token string
func (mw *GfJWTMiddleware) GetToken(ctx context.Context) string {
r := g.RequestFromCtx(ctx)
token := r.Get(TokenKey).String()
if len(token) == 0 {
return ""
}
return token
}
// GetPayload help to get the payload map
func (mw *GfJWTMiddleware) GetPayload(ctx context.Context) string {
r := g.RequestFromCtx(ctx)
token := r.Get(PayloadKey).String()
if len(token) == 0 {
return ""
}
return token
}
// GetIdentity help to get the identity
func (mw *GfJWTMiddleware) GetIdentity(ctx context.Context) interface{} {
r := g.RequestFromCtx(ctx)
return r.Get(mw.IdentityKey)
}
// ExtractClaims help to extract the JWT claims
func ExtractClaims(ctx context.Context) MapClaims {
r := g.RequestFromCtx(ctx)
claims := r.GetParam(PayloadKey).Interface()
return claims.(MapClaims)
}
// ExtractClaimsFromToken help to extract the JWT claims from token
func ExtractClaimsFromToken(token *jwt.Token) MapClaims {
if token == nil {
return make(MapClaims)
}
claims := MapClaims{}
for key, value := range token.Claims.(jwt.MapClaims) {
claims[key] = value
}
return claims
}
// ================= private func =================
func (mw *GfJWTMiddleware) readKeys() error {
err := mw.privateKey()
if err != nil {
return err
}
err = mw.publicKey()
if err != nil {
return err
}
return nil
}
func (mw *GfJWTMiddleware) privateKey() error {
var keyData []byte
if mw.PrivKeyFile == "" {
keyData = mw.PrivKeyBytes
} else {
fileContent, err := ioutil.ReadFile(mw.PrivKeyFile)
if err != nil {
return ErrNoPrivKeyFile
}
keyData = fileContent
}
if mw.PrivateKeyPassphrase != "" {
//nolint:static check
key, err := jwt.ParseRSAPrivateKeyFromPEMWithPassword(keyData, mw.PrivateKeyPassphrase)
if err != nil {
return ErrInvalidPrivKey
}
mw.privKey = key
return nil
}
key, err := jwt.ParseRSAPrivateKeyFromPEM(keyData)
if err != nil {
return ErrInvalidPrivKey
}
mw.privKey = key
return nil
}
func (mw *GfJWTMiddleware) publicKey() error {
var keyData []byte
if mw.PubKeyFile == "" {
keyData = mw.PubKeyBytes
} else {
fileContent, err := ioutil.ReadFile(mw.PubKeyFile)
if err != nil {
return ErrNoPubKeyFile
}
keyData = fileContent
}
key, err := jwt.ParseRSAPublicKeyFromPEM(keyData)
if err != nil {
return ErrInvalidPubKey
}
mw.pubKey = key
return nil
}
func (mw *GfJWTMiddleware) usingPublicKeyAlgo() bool {
switch mw.SigningAlgorithm {
case "RS256", "RS512", "RS384":
return true
}
return false
}
func (mw *GfJWTMiddleware) signedString(token *jwt.Token) (string, error) {
var tokenString string
var err error
if mw.usingPublicKeyAlgo() {
tokenString, err = token.SignedString(mw.privKey)
} else {
tokenString, err = token.SignedString(mw.Key)
}
return tokenString, err
}
func (mw *GfJWTMiddleware) jwtFromHeader(r *ghttp.Request, key string) (string, error) {
authHeader := r.Header.Get(key)
if authHeader == "" {
return "", ErrEmptyAuthHeader
}
parts := strings.SplitN(authHeader, " ", 2)
if !(len(parts) == 2 && parts[0] == mw.TokenHeadName) {
return "", ErrInvalidAuthHeader
}
return parts[1], nil
}
func (mw *GfJWTMiddleware) jwtFromQuery(r *ghttp.Request, key string) (string, error) {
token := r.Get(key).String()
if token == "" {
return "", ErrEmptyQueryToken
}
return token, nil
}
func (mw *GfJWTMiddleware) jwtFromCookie(r *ghttp.Request, key string) (string, error) {
cookie := r.Cookie.Get(key).String()
if cookie == "" {
return "", ErrEmptyCookieToken
}
return cookie, nil
}
func (mw *GfJWTMiddleware) jwtFromParam(r *ghttp.Request, key string) (string, error) {
token := r.Get(key).String()
if token == "" {
return "", ErrEmptyParamToken
}
return token, nil
}
func (mw *GfJWTMiddleware) parseToken(r *ghttp.Request) (*jwt.Token, error) {
var token string
var err error
methods := strings.Split(mw.TokenLookup, ",")
for _, method := range methods {
if len(token) > 0 {
break
}
parts := strings.Split(strings.TrimSpace(method), ":")
k := strings.TrimSpace(parts[0])
v := strings.TrimSpace(parts[1])
switch k {
case "header":
token, err = mw.jwtFromHeader(r, v)
case "query":
token, err = mw.jwtFromQuery(r, v)
case "cookie":
token, err = mw.jwtFromCookie(r, v)
case "param":
token, err = mw.jwtFromParam(r, v)
}
}
if err != nil {
return nil, err
}
if mw.KeyFunc != nil {
return jwt.Parse(token, mw.KeyFunc)
}
return jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
if jwt.GetSigningMethod(mw.SigningAlgorithm) != t.Method {
return nil, ErrInvalidSigningAlgorithm
}
if mw.usingPublicKeyAlgo() {
return mw.pubKey, nil
}
// save token string if valid
r.SetParam(TokenKey, token)
return mw.Key, nil
})
}
func (mw *GfJWTMiddleware) parseTokenString(token string) (*jwt.Token, error) {
if mw.KeyFunc != nil {
return jwt.Parse(token, mw.KeyFunc)
}
return jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
if jwt.GetSigningMethod(mw.SigningAlgorithm) != t.Method {
return nil, ErrInvalidSigningAlgorithm
}
if mw.usingPublicKeyAlgo() {
return mw.pubKey, nil
}
return mw.Key, nil
})
}
func (mw *GfJWTMiddleware) unauthorized(ctx context.Context, code int, message string) {
r := g.RequestFromCtx(ctx)
r.Header.Set("WWW-Authenticate", "JWT realm="+mw.Realm)
mw.Unauthorized(ctx, code, message)
if !mw.DisabledAbort {
r.ExitAll()
}
}
func (mw *GfJWTMiddleware) middlewareImpl(ctx context.Context) {
r := g.RequestFromCtx(ctx)
claims, token, err := mw.GetClaimsFromJWT(ctx)
if err != nil {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
if claims["exp"] == nil {
mw.unauthorized(ctx, http.StatusBadRequest, mw.HTTPStatusMessageFunc(ErrMissingExpField, ctx))
return
}
if _, ok := claims["exp"].(float64); !ok {
mw.unauthorized(ctx, http.StatusBadRequest, mw.HTTPStatusMessageFunc(ErrWrongFormatOfExp, ctx))
return
}
if int64(claims["exp"].(float64)) < (mw.TimeFunc().UnixNano() / 1e6) {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(ErrExpiredToken, ctx))
return
}
in, err := mw.inBlacklist(ctx, token)
if err != nil {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
if in {
mw.unauthorized(ctx, http.StatusUnauthorized, mw.HTTPStatusMessageFunc(ErrInvalidToken, ctx))
return
}
r.SetParam(PayloadKey, claims)
identity := mw.IdentityHandler(ctx)
if identity != nil {
r.SetParam(mw.IdentityKey, identity)
}
if !mw.Authorizator(identity, ctx) {
mw.unauthorized(ctx, http.StatusForbidden, mw.HTTPStatusMessageFunc(ErrForbidden, ctx))
return
}
//c.Next() todo
}
func (mw *GfJWTMiddleware) setBlacklist(ctx context.Context, token string, claims jwt.MapClaims) error {
// The goal of MD5 is to reduce the key length.
token, err := gmd5.EncryptString(token)
if err != nil {
return err
}
exp := int64(claims["exp"].(float64))
// save duration time = (exp + max_refresh) - now
duration := time.Unix(exp, 0).Add(mw.MaxRefresh).Sub(mw.TimeFunc()).Truncate(time.Second)
key := mw.BlacklistPrefix + token
// global gcache
err = blacklist.Set(ctx, key, true, duration)
if err != nil {
return err
}
return nil
}
func (mw *GfJWTMiddleware) inBlacklist(ctx context.Context, token string) (bool, error) {
// The goal of MD5 is to reduce the key length.
tokenRaw, err := gmd5.EncryptString(token)
if err != nil {
return false, nil
}
key := mw.BlacklistPrefix + tokenRaw
// Global gcache
if in, err := blacklist.Contains(ctx, key); err != nil {
return false, nil
} else {
return in, nil
}
}