forked from cookieY/yee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
120 lines (105 loc) 路 3.16 KB
/
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
package middleware
import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/xuji-cny/yee"
"github.com/golang-jwt/jwt"
)
// JwtConfig defines the config of JWT middleware
type JwtConfig struct {
GetKey string
AuthScheme string
SigningKey interface{}
SigningMethod string
TokenLookup string
Claims jwt.Claims
keyFunc jwt.Keyfunc
ErrorHandler JWTErrorHandler
SuccessHandler JWTSuccessHandler
}
type jwtExtractor func(yee.Context) (string, error)
// JWTErrorHandler defines a function which is error for a valid token.
type JWTErrorHandler func(error) error
// JWTSuccessHandler defines a function which is executed for a valid token.
type JWTSuccessHandler func(yee.Context)
const algorithmHS256 = "HS256"
// DefaultJwtConfig is the default config of JWT middleware
var DefaultJwtConfig = JwtConfig{
GetKey: "auth",
SigningMethod: algorithmHS256,
AuthScheme: "Bearer",
TokenLookup: "header:" + yee.HeaderAuthorization,
Claims: jwt.MapClaims{},
}
// JWTWithConfig is the custom implementation CORS middleware
func JWTWithConfig(config JwtConfig) yee.HandlerFunc {
if config.SigningKey == nil {
panic("yee: jwt middleware requires signing key")
}
if config.SigningMethod == "" {
config.SigningMethod = DefaultJwtConfig.SigningMethod
}
if config.GetKey == "" {
config.GetKey = DefaultJwtConfig.GetKey
}
if config.AuthScheme == "" {
config.AuthScheme = DefaultJwtConfig.AuthScheme
}
if config.Claims == nil {
config.Claims = DefaultJwtConfig.Claims
}
if config.TokenLookup == "" {
config.TokenLookup = DefaultJwtConfig.TokenLookup
}
config.keyFunc = func(token *jwt.Token) (interface{}, error) {
if token.Method.Alg() != config.SigningMethod {
return nil, fmt.Errorf("unexpected jwt signing method=%v", token.Header["alg"])
}
return config.SigningKey, nil
}
parts := strings.Split(config.TokenLookup, ":")
extractor := jwtFromHeader(parts[1], config.AuthScheme)
return func(c yee.Context) (err error) {
// cause upgrade websocket will clear custom header
// when header add jwt bearer that panic
if c.IsWebsocket() {
return
}
auth, err := extractor(c)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
token := new(jwt.Token)
if _, ok := config.Claims.(jwt.MapClaims); ok {
token, err = jwt.Parse(auth, config.keyFunc)
if err != nil {
return c.JSON(http.StatusUnauthorized, err.Error())
}
} else {
t := reflect.ValueOf(config.Claims).Type().Elem()
claims := reflect.New(t).Interface().(jwt.Claims)
token, err = jwt.ParseWithClaims(auth, claims, config.keyFunc)
}
if err == nil && token.Valid {
c.Put(config.GetKey, token)
return
}
// bug fix
// if invalid or expired jwt,
// we must intercept all handlers and return serverError
return c.JSON(http.StatusUnauthorized, "invalid or expired jwt")
}
}
func jwtFromHeader(header string, authScheme string) jwtExtractor {
return func(c yee.Context) (string, error) {
auth := c.Request().Header.Get(header)
l := len(authScheme)
if len(auth) > l+1 && auth[:l] == authScheme {
return auth[l+1:], nil
}
return "", errors.New("missing or malformed jwt")
}
}