-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_jwt.go
688 lines (567 loc) · 17.9 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
package jwt
import (
"crypto/rsa"
"encoding/json"
"errors"
"net/http"
"os"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/kataras/iris/v12"
)
// MapClaims type that uses the map[string]interface{} for JSON decoding
type MapClaims map[string]interface{}
// IrisJWTMiddleware provides a JWT authentication implementation for Iris.
type IrisJWTMiddleware struct {
Realm string
SigningAlgorithm string
Key []byte
KeyFunc func(token *jwt.Token) (interface{}, error)
Timeout time.Duration
TimeoutFunc func(data interface{}) time.Duration
MaxRefresh time.Duration
Authenticator func(ctx iris.Context) (interface{}, error)
Authorizator func(data interface{}, ctx iris.Context) bool
PayloadFunc func(data interface{}) MapClaims
Unauthorized func(ctx iris.Context, code int, message string)
LoginResponse func(ctx iris.Context, code int, message string, time time.Time)
LogoutResponse func(ctx iris.Context, code int)
RefreshResponse func(ctx iris.Context, code int, message string, time time.Time)
IdentityHandler func(ctx iris.Context) interface{}
IdentityKey string
TokenLookup string
TokenHeadName string
TimeFunc func() time.Time
HTTPStatusMessageFunc func(e error, ctx iris.Context) string
PrivKeyFile string
PrivKeyBytes []byte
PubKeyFile string
PrivateKeyPassphrase string
PubKeyBytes []byte
privKey *rsa.PrivateKey
pubKey *rsa.PublicKey
SendCookie bool
CookieMaxAge time.Duration
SecureCookie bool
CookieHTTPOnly bool
CookieDomain string
SendAuthorization bool
DisabledAbort bool
CookieName string
CookieSameSite http.SameSite
ParseOptions []jwt.ParserOption
}
var (
// ErrMissingSecretKey indicates Secret key is required
ErrMissingSecretKey = errors.New("secret key is required")
// ErrForbidden when HTTP status 403 is given
ErrForbidden = errors.New("you don't have permission to access this resource")
// ErrMissingAuthenticatorFunc indicates Authenticator is required
ErrMissingAuthenticatorFunc = errors.New("irisJWTMiddleware.Authenticator func is undefined")
// ErrMissingLoginValues indicates a user tried to authenticate without username or password
ErrMissingLoginValues = errors.New("missing Username or Password")
// ErrFailedAuthentication indicates authentication failed, could be faulty username or password
ErrFailedAuthentication = errors.New("incorrect Username or Password")
// ErrFailedTokenCreation indicates JWT Token failed to create, reason unknown
ErrFailedTokenCreation = errors.New("failed to create JWT Token")
// ErrExpiredToken indicates JWT token has expired. Can't refresh.
ErrExpiredToken = errors.New("token is expired") // in practice, this is generated from the jwt library not by us
// ErrEmptyAuthHeader can be thrown if authing with a HTTP header, the Auth header needs to be set
ErrEmptyAuthHeader = errors.New("auth header is empty")
// ErrMissingExpField missing exp field in token
ErrMissingExpField = errors.New("missing exp field")
// ErrWrongFormatOfExp field must be float64 format
ErrWrongFormatOfExp = errors.New("exp must be float64 format")
// ErrInvalidAuthHeader indicates auth header is invalid, could for example have the wrong Realm name
ErrInvalidAuthHeader = errors.New("auth header is invalid")
// ErrEmptyQueryToken can be thrown if authing with URL Query, the query token variable is empty
ErrEmptyQueryToken = errors.New("query token is empty")
// ErrEmptyCookieToken can be thrown if authing with a cookie, the token cookie is empty
ErrEmptyCookieToken = errors.New("cookie token is empty")
// ErrEmptyParamToken can be thrown if authing with parameter in path, the parameter in path is empty
ErrEmptyParamToken = errors.New("parameter token is empty")
// ErrInvalidSigningAlgorithm indicates signing algorithm is invalid, needs to be HS256, HS384, HS512, RS256, RS384 or RS512
ErrInvalidSigningAlgorithm = errors.New("invalid signing algorithm")
// ErrNoPrivKeyFile indicates that the given private key is unreadable
ErrNoPrivKeyFile = errors.New("private key file unreadable")
// ErrNoPubKeyFile indicates that the given public key is unreadable
ErrNoPubKeyFile = errors.New("public key file unreadable")
// ErrInvalidPrivKey indicates that the given private key is invalid
ErrInvalidPrivKey = errors.New("private key invalid")
// ErrInvalidPubKey indicates the the given public key is invalid
ErrInvalidPubKey = errors.New("public key invalid")
// IdentityKey default identity key
IdentityKey = "identity"
)
// New for check error with IrisJWTMiddleware
func New(m *IrisJWTMiddleware) (*IrisJWTMiddleware, error) {
if err := m.MiddlewareInit(); err != nil {
return nil, err
}
return m, nil
}
func (mw *IrisJWTMiddleware) readKeys() error {
err := mw.privateKey()
if err != nil {
return err
}
return mw.publicKey()
}
func (mw *IrisJWTMiddleware) privateKey() error {
var keyData []byte
if mw.PrivKeyFile == "" {
keyData = mw.PrivKeyBytes
} else {
filecontent, err := os.ReadFile(mw.PrivKeyFile)
if err != nil {
return ErrNoPrivKeyFile
}
keyData = filecontent
}
if mw.PrivateKeyPassphrase != "" {
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 *IrisJWTMiddleware) publicKey() error {
var keyData []byte
if mw.PubKeyFile == "" {
keyData = mw.PubKeyBytes
} else {
filecontent, err := os.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 *IrisJWTMiddleware) usingPublicKeyAlgo() bool {
switch mw.SigningAlgorithm {
case "RS256", "RS512", "RS384":
return true
}
return false
}
// MiddlewareInit initialize jwt configs.
func (mw *IrisJWTMiddleware) MiddlewareInit() error {
if mw.TokenLookup == "" {
mw.TokenLookup = "header:Authorization"
}
if mw.SigningAlgorithm == "" {
mw.SigningAlgorithm = "HS256"
}
if mw.Timeout == 0 {
mw.Timeout = time.Hour
}
if mw.TimeoutFunc == nil {
mw.TimeoutFunc = func(data interface{}) time.Duration {
return mw.Timeout
}
}
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 iris.Context) bool {
return true
}
}
if mw.Unauthorized == nil {
mw.Unauthorized = func(ctx iris.Context, code int, message string) {
ctx.StatusCode(code)
ctx.JSON(iris.Map{
"code": code,
"message": message,
})
}
}
if mw.LoginResponse == nil {
mw.LoginResponse = func(ctx iris.Context, code int, token string, expire time.Time) {
ctx.JSON(iris.Map{
"code": iris.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
}
}
if mw.LogoutResponse == nil {
mw.LogoutResponse = func(ctx iris.Context, code int) {
ctx.JSON(iris.Map{
"code": iris.StatusOK,
})
}
}
if mw.RefreshResponse == nil {
mw.RefreshResponse = func(ctx iris.Context, code int, token string, expire time.Time) {
ctx.JSON(iris.Map{
"code": iris.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
}
}
if mw.IdentityKey == "" {
mw.IdentityKey = IdentityKey
}
if mw.IdentityHandler == nil {
mw.IdentityHandler = func(ctx iris.Context) interface{} {
claims := ExtractClaims(ctx)
return claims[mw.IdentityKey]
}
}
if mw.HTTPStatusMessageFunc == nil {
mw.HTTPStatusMessageFunc = func(e error, ctx iris.Context) string {
return e.Error()
}
}
if mw.Realm == "" {
mw.Realm = "iris jwt"
}
if mw.CookieMaxAge == 0 {
mw.CookieMaxAge = mw.Timeout
}
if mw.CookieName == "" {
mw.CookieName = "jwt"
}
if mw.KeyFunc != nil {
return nil
}
if mw.usingPublicKeyAlgo() {
return mw.readKeys()
}
if mw.Key == nil {
return ErrMissingSecretKey
}
return nil
}
// MiddlewareFunc makes IrisJWTMiddleware implement the Middleware interface.
func (mw *IrisJWTMiddleware) MiddlewareFunc() iris.Handler {
return func(ctx iris.Context) {
mw.middlewareImpl(ctx)
}
}
func (mw *IrisJWTMiddleware) middlewareImpl(ctx iris.Context) {
claims, err := mw.GetClaimsFromJWT(ctx)
if err != nil {
mw.unauthorized(ctx, iris.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
switch v := claims["exp"].(type) {
case nil:
mw.unauthorized(ctx, iris.StatusBadRequest, mw.HTTPStatusMessageFunc(ErrMissingExpField, ctx))
return
case float64:
if int64(v) < mw.TimeFunc().Unix() {
mw.unauthorized(ctx, iris.StatusUnauthorized, mw.HTTPStatusMessageFunc(ErrExpiredToken, ctx))
return
}
case json.Number:
n, err := v.Int64()
if err != nil {
mw.unauthorized(ctx, iris.StatusBadRequest, mw.HTTPStatusMessageFunc(ErrWrongFormatOfExp, ctx))
return
}
if n < mw.TimeFunc().Unix() {
mw.unauthorized(ctx, iris.StatusUnauthorized, mw.HTTPStatusMessageFunc(ErrExpiredToken, ctx))
return
}
default:
mw.unauthorized(ctx, iris.StatusBadRequest, mw.HTTPStatusMessageFunc(ErrWrongFormatOfExp, ctx))
return
}
ctx.Values().Set("JWT_PAYLOAD", claims)
identity := mw.IdentityHandler(ctx)
if identity != nil {
ctx.Values().Set(mw.IdentityKey, identity)
}
if !mw.Authorizator(identity, ctx) {
mw.unauthorized(ctx, iris.StatusForbidden, mw.HTTPStatusMessageFunc(ErrForbidden, ctx))
return
}
ctx.Next()
}
// GetClaimsFromJWT get claims from JWT token
func (mw *IrisJWTMiddleware) GetClaimsFromJWT(ctx iris.Context) (MapClaims, error) {
token, err := mw.ParseToken(ctx)
if err != nil {
return nil, err
}
if mw.SendAuthorization {
if v := ctx.Values().GetString("JWT_TOKEN"); v != "" {
ctx.Header("Authorization", mw.TokenHeadName+" "+v)
}
}
claims := MapClaims{}
for key, value := range token.Claims.(jwt.MapClaims) {
claims[key] = value
}
return claims, nil
}
// LoginHandler can be used by clients to get a jwt token.
func (mw *IrisJWTMiddleware) LoginHandler(ctx iris.Context) {
if mw.Authenticator == nil {
mw.unauthorized(ctx, iris.StatusInternalServerError, mw.HTTPStatusMessageFunc(ErrMissingAuthenticatorFunc, ctx))
return
}
data, err := mw.Authenticator(ctx)
if err != nil {
mw.unauthorized(ctx, iris.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
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().Add(mw.TimeoutFunc(claims))
claims["exp"] = expire.Unix()
claims["orig_iat"] = mw.TimeFunc().Unix()
tokenString, err := mw.signedString(token)
if err != nil {
mw.unauthorized(ctx, iris.StatusUnauthorized, mw.HTTPStatusMessageFunc(ErrFailedTokenCreation, ctx))
return
}
mw.SetCookie(ctx, tokenString)
mw.LoginResponse(ctx, iris.StatusOK, tokenString, expire)
}
// LogoutHandler can be used by clients to remove the jwt cookie (if set)
func (mw *IrisJWTMiddleware) LogoutHandler(ctx iris.Context) {
if mw.SendCookie {
ctx.RemoveCookie(mw.CookieName, iris.CookieExpires(0), iris.CookiePath("/"))
}
mw.LogoutResponse(ctx, iris.StatusOK)
}
func (mw *IrisJWTMiddleware) 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
}
// RefreshHandler can be used to refresh a token.
func (mw *IrisJWTMiddleware) RefreshHandler(ctx iris.Context) {
tokenString, expire, err := mw.RefreshToken(ctx)
if err != nil {
mw.unauthorized(ctx, iris.StatusUnauthorized, mw.HTTPStatusMessageFunc(err, ctx))
return
}
mw.RefreshResponse(ctx, iris.StatusOK, tokenString, expire)
}
// RefreshToken refresh token and check if token is expired
func (mw *IrisJWTMiddleware) RefreshToken(ctx iris.Context) (string, time.Time, error) {
claims, err := mw.CheckIfTokenExpire(ctx)
if err != nil {
return "", time.Now(), err
}
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.TimeoutFunc(claims))
newClaims["exp"] = expire.Unix()
newClaims["orig_iat"] = mw.TimeFunc().Unix()
tokenString, err := mw.signedString(newToken)
if err != nil {
return "", time.Now(), err
}
mw.SetCookie(ctx, tokenString)
return tokenString, expire, nil
}
// CheckIfTokenExpire check if token expire
func (mw *IrisJWTMiddleware) CheckIfTokenExpire(ctx iris.Context) (jwt.MapClaims, error) {
token, err := mw.ParseToken(ctx)
if err != nil {
validationErr, ok := err.(*jwt.ValidationError)
if !ok || validationErr.Errors != jwt.ValidationErrorExpired {
return nil, err
}
}
claims := token.Claims.(jwt.MapClaims)
origIat := int64(claims["orig_iat"].(float64))
if origIat < mw.TimeFunc().Add(-mw.MaxRefresh).Unix() {
return nil, ErrExpiredToken
}
return claims, nil
}
// TokenGenerator method that clients can use to get a jwt token.
func (mw *IrisJWTMiddleware) 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().Add(mw.TimeoutFunc(claims))
claims["exp"] = expire.Unix()
claims["orig_iat"] = mw.TimeFunc().Unix()
tokenString, err := mw.signedString(token)
if err != nil {
return "", time.Time{}, err
}
return tokenString, expire, nil
}
func (mw *IrisJWTMiddleware) jwtFromHeader(ctx iris.Context, key string) (string, error) {
authHeader := ctx.GetHeader(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 *IrisJWTMiddleware) jwtFromQuery(ctx iris.Context, key string) (string, error) {
token := ctx.URLParam(key)
if token == "" {
return "", ErrEmptyQueryToken
}
return token, nil
}
func (mw *IrisJWTMiddleware) jwtFromCookie(ctx iris.Context, key string) (string, error) {
cookie := ctx.GetCookie(key)
if cookie == "" {
return "", ErrEmptyCookieToken
}
return cookie, nil
}
func (mw *IrisJWTMiddleware) jwtFromParam(ctx iris.Context, key string) (string, error) {
token := ctx.Params().Get(key)
if token == "" {
return "", ErrEmptyParamToken
}
return token, nil
}
func (mw *IrisJWTMiddleware) jwtFromForm(ctx iris.Context, key string) (string, error) {
token := ctx.FormValue(key)
if token == "" {
return "", ErrEmptyParamToken
}
return token, nil
}
// ParseToken parse jwt token from iris context
func (mw *IrisJWTMiddleware) ParseToken(ctx iris.Context) (*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(ctx, v)
case "query":
token, err = mw.jwtFromQuery(ctx, v)
case "cookie":
token, err = mw.jwtFromCookie(ctx, v)
case "param":
token, err = mw.jwtFromParam(ctx, v)
case "form":
token, err = mw.jwtFromForm(ctx, v)
}
}
if err != nil {
return nil, err
}
if mw.KeyFunc != nil {
return jwt.Parse(token, mw.KeyFunc, mw.ParseOptions...)
}
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
}
ctx.Values().Set("JWT_TOKEN", token)
return mw.Key, nil
}, mw.ParseOptions...)
}
// ParseTokenString parse jwt token string
func (mw *IrisJWTMiddleware) ParseTokenString(token string) (*jwt.Token, error) {
if mw.KeyFunc != nil {
return jwt.Parse(token, mw.KeyFunc, mw.ParseOptions...)
}
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
}, mw.ParseOptions...)
}
func (mw *IrisJWTMiddleware) unauthorized(ctx iris.Context, code int, message string) {
ctx.Header("WWW-Authenticate", "JWT realm="+mw.Realm)
if !mw.DisabledAbort {
ctx.StopExecution()
}
mw.Unauthorized(ctx, code, message)
}
// ExtractClaims help to extract the JWT claims
func ExtractClaims(ctx iris.Context) MapClaims {
claims := ctx.Values().Get("JWT_PAYLOAD")
if claims == nil {
return make(MapClaims)
}
return claims.(MapClaims)
}
// ExtractClaimsFromToken helps to extract the JWT claims from a 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
}
// SetCookie sets the JWT token as a cookie
func (mw *IrisJWTMiddleware) SetCookie(ctx iris.Context, tokenString string) {
if mw.SendCookie {
ctx.SetCookie(&http.Cookie{
Name: mw.CookieName,
Value: tokenString,
MaxAge: int(mw.CookieMaxAge.Seconds()),
Path: "/",
Domain: mw.CookieDomain,
Secure: mw.SecureCookie,
HttpOnly: mw.CookieHTTPOnly,
SameSite: mw.CookieSameSite,
})
}
}