-
Notifications
You must be signed in to change notification settings - Fork 351
/
jwt_validation.go
172 lines (136 loc) · 3.47 KB
/
jwt_validation.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
package auth
import (
"fmt"
"sync"
"time"
"github.com/MicahParks/keyfunc"
jwt "github.com/golang-jwt/jwt/v4"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters"
)
const (
// Deprecated, use filters.JwtValidationName instead
JwtValidationName = filters.JwtValidationName
)
type (
jwtValidationSpec struct {
options TokenintrospectionOptions
}
jwtValidationFilter struct {
jwksUri string
}
)
var m sync.RWMutex
var refreshInterval = time.Hour
var refreshRateLimit = time.Minute * 5
var refreshTimeout = time.Second * 10
var refreshUnknownKID = true
// the map of jwks keyfunctions stored per jwksUri
var jwksMap map[string]*keyfunc.JWKS = make(map[string]*keyfunc.JWKS)
func NewJwtValidationWithOptions(o TokenintrospectionOptions) filters.Spec {
return &jwtValidationSpec{
options: o,
}
}
func (s *jwtValidationSpec) Name() string {
return filters.JwtValidationName
}
func (s *jwtValidationSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) != 1 {
return nil, filters.ErrInvalidFilterParameters
}
sargs, err := getStrings(args)
if err != nil {
return nil, err
}
issuerURL := sargs[0]
cfg, err := getOpenIDConfig(issuerURL)
if err != nil {
return nil, err
}
err = registerKeyFunction(cfg.JwksURI)
if err != nil {
return nil, err
}
f := &jwtValidationFilter{
jwksUri: cfg.JwksURI,
}
return f, nil
}
func hasKeyFunction(url string) bool {
m.RLock()
defer m.RUnlock()
_, ok := jwksMap[url]
return ok
}
func putKeyFunction(url string, jwks *keyfunc.JWKS) {
m.Lock()
defer m.Unlock()
jwksMap[url] = jwks
}
func registerKeyFunction(url string) (err error) {
if hasKeyFunction(url) {
return nil
}
options := keyfunc.Options{
RefreshErrorHandler: func(err error) {
log.Errorf("There was an error on key refresh for the given URL %s\nError:%s\n", url, err.Error())
},
RefreshInterval: refreshInterval,
RefreshRateLimit: refreshRateLimit,
RefreshTimeout: refreshTimeout,
RefreshUnknownKID: refreshUnknownKID,
}
jwks, err := keyfunc.Get(url, options)
if err != nil {
return fmt.Errorf("failed to get the JWKS from the given URL %s Error:%w", url, err)
}
putKeyFunction(url, jwks)
return nil
}
func getKeyFunction(url string) (jwks *keyfunc.JWKS) {
m.RLock()
defer m.RUnlock()
return jwksMap[url]
}
func (f *jwtValidationFilter) Request(ctx filters.FilterContext) {
r := ctx.Request()
var info tokenContainer
infoTemp, ok := ctx.StateBag()[oidcClaimsCacheKey]
if !ok {
token, ok := getToken(r)
if !ok || token == "" {
unauthorized(ctx, "", missingToken, "", "")
return
}
claims, err := parseToken(token, f.jwksUri)
if err != nil {
ctx.Logger().Errorf("Error while parsing jwt token : %v.", err)
unauthorized(ctx, "", invalidToken, "", "")
return
}
info.Claims = claims
} else {
info = infoTemp.(tokenContainer)
}
sub, ok := info.Claims["sub"].(string)
if !ok {
unauthorized(ctx, sub, invalidSub, "", "")
return
}
authorized(ctx, sub)
ctx.StateBag()[oidcClaimsCacheKey] = info
}
func (f *jwtValidationFilter) Response(filters.FilterContext) {}
func parseToken(token string, jwksUri string) (map[string]interface{}, error) {
jwks := getKeyFunction(jwksUri)
var claims jwt.MapClaims
parsedToken, err := jwt.ParseWithClaims(token, &claims, jwks.Keyfunc)
if err != nil {
return nil, fmt.Errorf("error while parsing jwt token : %w", err)
} else if !parsedToken.Valid {
return nil, fmt.Errorf("invalid token")
} else {
return claims, nil
}
}