-
Notifications
You must be signed in to change notification settings - Fork 14
/
jwt.go
555 lines (478 loc) · 13.9 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
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
package accesscontrol
import (
"context"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
goerrors "errors"
"fmt"
"net/http"
"strings"
"github.com/golang-jwt/jwt/v4"
"github.com/hashicorp/hcl/v2"
"github.com/sirupsen/logrus"
"github.com/avenga/couper/accesscontrol/jwk"
acjwt "github.com/avenga/couper/accesscontrol/jwt"
"github.com/avenga/couper/config/request"
"github.com/avenga/couper/errors"
"github.com/avenga/couper/eval"
"github.com/avenga/couper/internal/seetie"
)
const (
Invalid JWTSourceType = iota
Cookie
Header
Value
)
var (
_ AccessControl = &JWT{}
_ DisablePrivateCaching = &JWT{}
)
type (
JWTSourceType uint8
JWTSource struct {
Expr hcl.Expression
Name string
Type JWTSourceType
}
)
type JWT struct {
algorithm acjwt.Algorithm
claims hcl.Expression
claimsRequired []string
disablePrivateCaching bool
source JWTSource
hmacSecret []byte
name string
parser *jwt.Parser
pubKey interface{}
rolesClaim string
rolesMap map[string][]string
permissionsClaim string
permissionsMap map[string][]string
jwks *jwk.JWKS
}
type JWTOptions struct {
Algorithm string
Claims hcl.Expression
ClaimsRequired []string
DisablePrivateCaching bool
Name string // TODO: more generic (validate)
RolesClaim string
RolesMap map[string][]string
PermissionsClaim string
PermissionsMap map[string][]string
Source JWTSource
Key []byte
JWKS *jwk.JWKS
}
func NewJWTSource(cookie, header string, value hcl.Expression) JWTSource {
c, h := strings.TrimSpace(cookie), strings.TrimSpace(header)
if value != nil {
v, _ := value.Value(nil)
if !v.IsNull() {
if h != "" || c != "" {
return JWTSource{}
}
return JWTSource{
Name: "",
Type: Value,
Expr: value,
}
}
}
if c != "" && h == "" {
return JWTSource{
Name: c,
Type: Cookie,
}
}
if h != "" && c == "" {
return JWTSource{
Name: h,
Type: Header,
}
}
if h == "" && c == "" {
return JWTSource{
Name: "Authorization",
Type: Header,
}
}
return JWTSource{}
}
// NewJWT parses the key and creates Validation obj which can be referenced in related handlers.
func NewJWT(options *JWTOptions) (*JWT, error) {
jwtAC, err := newJWT(options)
if err != nil {
return nil, err
}
jwtAC.algorithm = acjwt.NewAlgorithm(options.Algorithm)
if jwtAC.algorithm == acjwt.AlgorithmUnknown {
return nil, fmt.Errorf("algorithm %q is not supported", options.Algorithm)
}
jwtAC.parser = newParser([]acjwt.Algorithm{jwtAC.algorithm})
if jwtAC.algorithm.IsHMAC() {
jwtAC.hmacSecret = options.Key
return jwtAC, nil
}
pubKey, err := parsePublicPEMKey(options.Key)
if err != nil {
return nil, err
}
jwtAC.pubKey = pubKey
return jwtAC, nil
}
func NewJWTFromJWKS(options *JWTOptions) (*JWT, error) {
jwtAC, err := newJWT(options)
if err != nil {
return nil, err
}
if options.JWKS == nil {
return nil, fmt.Errorf("invalid JWKS")
}
algorithms := append(acjwt.RSAAlgorithms, acjwt.ECDSAlgorithms...)
jwtAC.parser = newParser(algorithms)
jwtAC.jwks = options.JWKS
return jwtAC, nil
}
func newJWT(options *JWTOptions) (*JWT, error) {
if options.Source.Type == Invalid {
return nil, fmt.Errorf("token source is invalid")
}
if options.RolesClaim != "" && options.RolesMap == nil {
return nil, fmt.Errorf("missing beta_roles_map")
}
jwtAC := &JWT{
claims: options.Claims,
claimsRequired: options.ClaimsRequired,
disablePrivateCaching: options.DisablePrivateCaching,
name: options.Name,
rolesClaim: options.RolesClaim,
rolesMap: options.RolesMap,
permissionsClaim: options.PermissionsClaim,
permissionsMap: options.PermissionsMap,
source: options.Source,
}
return jwtAC, nil
}
func (j *JWT) DisablePrivateCaching() bool {
return j.disablePrivateCaching
}
// Validate reading the token from configured source and validates against the key.
func (j *JWT) Validate(req *http.Request) error {
var tokenValue string
var err error
switch j.source.Type {
case Cookie:
cookie, cerr := req.Cookie(j.source.Name)
if cerr != http.ErrNoCookie && cookie != nil {
tokenValue = cookie.Value
}
case Header:
if strings.ToLower(j.source.Name) == "authorization" {
if tokenValue = req.Header.Get(j.source.Name); tokenValue != "" {
if tokenValue, err = getBearer(tokenValue); err != nil {
return errors.JwtTokenMissing.With(err)
}
}
} else {
tokenValue = req.Header.Get(j.source.Name)
}
case Value:
requestContext := eval.ContextFromRequest(req).HCLContext()
value, diags := eval.Value(requestContext, j.source.Expr)
if diags != nil {
return diags
}
tokenValue = seetie.ValueToString(value)
}
if tokenValue == "" {
return errors.JwtTokenMissing.Message("token required")
}
expectedClaims, err := j.getConfiguredClaims(req)
if err != nil {
return err
}
if j.jwks != nil {
// load JWKS if needed
j.jwks.Data()
}
tokenClaims := jwt.MapClaims{}
_, err = j.parser.ParseWithClaims(tokenValue, tokenClaims, j.getValidationKey)
if err != nil {
if goerrors.Is(err, jwt.ErrTokenExpired) {
return errors.JwtTokenExpired.With(err)
}
return errors.JwtTokenInvalid.With(err)
}
err = j.validateClaims(tokenClaims, expectedClaims)
if err != nil {
return errors.JwtTokenInvalid.With(err)
}
ctx := req.Context()
acMap, ok := ctx.Value(request.AccessControls).(map[string]interface{})
if !ok {
acMap = make(map[string]interface{})
}
// treat token claims as map for context
acMap[j.name] = map[string]interface{}(tokenClaims)
ctx = context.WithValue(ctx, request.AccessControls, acMap)
log := req.Context().Value(request.LogEntry).(*logrus.Entry).WithContext(req.Context())
jwtGrantedPermissions := j.getGrantedPermissions(tokenClaims, log)
grantedPermissions, _ := ctx.Value(request.BetaGrantedPermissions).([]string)
grantedPermissions = append(grantedPermissions, jwtGrantedPermissions...)
ctx = context.WithValue(ctx, request.BetaGrantedPermissions, grantedPermissions)
*req = *req.WithContext(ctx)
return nil
}
func (j *JWT) getValidationKey(token *jwt.Token) (interface{}, error) {
if j.jwks != nil {
return j.jwks.GetSigKeyForToken(token)
}
switch j.algorithm {
case acjwt.AlgorithmRSA256, acjwt.AlgorithmRSA384, acjwt.AlgorithmRSA512:
return j.pubKey, nil
case acjwt.AlgorithmECDSA256, acjwt.AlgorithmECDSA384, acjwt.AlgorithmECDSA512:
return j.pubKey, nil
case acjwt.AlgorithmHMAC256, acjwt.AlgorithmHMAC384, acjwt.AlgorithmHMAC512:
return j.hmacSecret, nil
default: // this error case gets normally caught on configuration level
return nil, errors.Configuration.Message("algorithm is not supported")
}
}
// getConfiguredClaims evaluates the expected claim values from the configuration, and especially iss and aud
func (j *JWT) getConfiguredClaims(req *http.Request) (map[string]interface{}, error) {
claims := make(map[string]interface{})
if j.claims != nil {
val, verr := eval.Value(eval.ContextFromRequest(req).HCLContext(), j.claims)
if verr != nil {
return nil, verr
}
claims = seetie.ValueToMap(val)
var ok bool
if issVal, exists := claims["iss"]; exists {
_, ok = issVal.(string)
if !ok {
return nil, errors.Configuration.Message("invalid value type, string expected (claims / iss)")
}
}
if audVal, exists := claims["aud"]; exists {
_, ok = audVal.(string)
if !ok {
return nil, errors.Configuration.Message("invalid value type, string expected (claims / aud)")
}
}
}
return claims, nil
}
// validateClaims validates the token claims against the list of required claims and the expected claims values
func (j *JWT) validateClaims(tokenClaims jwt.MapClaims, expectedClaims map[string]interface{}) error {
for _, key := range j.claimsRequired {
if _, ok := tokenClaims[key]; !ok {
return fmt.Errorf("required claim is missing: " + key)
}
}
for k, v := range expectedClaims {
val, exist := tokenClaims[k]
if !exist {
return fmt.Errorf("required claim is missing: " + k)
}
if k == "iss" {
if !tokenClaims.VerifyIssuer(v.(string), true) {
return errors.JwtTokenInvalid.Message("invalid issuer")
}
continue
}
if k == "aud" {
if !tokenClaims.VerifyAudience(v.(string), true) {
return errors.JwtTokenInvalid.Message("invalid audience")
}
continue
}
if val != v {
return fmt.Errorf("unexpected value for claim %s, got %q, expected %q", k, val, v)
}
}
return nil
}
func (j *JWT) getGrantedPermissions(tokenClaims jwt.MapClaims, log *logrus.Entry) []string {
var grantedPermissions []string
grantedPermissions = j.addPermissionsFromPermissionsClaim(tokenClaims, grantedPermissions, log)
grantedPermissions = j.addPermissionsFromRoles(tokenClaims, grantedPermissions, log)
grantedPermissions = j.addMappedPermissions(grantedPermissions, grantedPermissions)
return grantedPermissions
}
const warnInvalidValueMsg = "invalid %s claim value type, ignoring claim, value %#v"
func (j *JWT) addPermissionsFromPermissionsClaim(tokenClaims jwt.MapClaims, permissions []string, log *logrus.Entry) []string {
if j.permissionsClaim == "" {
return permissions
}
permissionsFromClaim, exists := tokenClaims[j.permissionsClaim]
if !exists {
return permissions
}
// ["foo", "bar"] is stored as []interface{}, not []string, unfortunately
permissionsArray, ok := permissionsFromClaim.([]interface{})
if ok {
var vals []string
for _, v := range permissionsArray {
p, ok := v.(string)
if !ok {
log.Warn(fmt.Sprintf(warnInvalidValueMsg, "permissions", permissionsFromClaim))
return permissions
}
vals = append(vals, p)
}
for _, val := range vals {
permissions, _ = addPermission(permissions, val)
}
} else {
permissionsString, ok := permissionsFromClaim.(string)
if !ok {
log.Warn(fmt.Sprintf(warnInvalidValueMsg, "permissions", permissionsFromClaim))
return permissions
}
for _, p := range strings.Split(permissionsString, " ") {
permissions, _ = addPermission(permissions, p)
}
}
return permissions
}
func (j *JWT) getRoleValues(rolesClaimValue interface{}, log *logrus.Entry) []string {
var roleValues []string
// ["foo", "bar"] is stored as []interface{}, not []string, unfortunately
rolesArray, ok := rolesClaimValue.([]interface{})
if ok {
var vals []string
for _, v := range rolesArray {
r, ok := v.(string)
if !ok {
log.Warn(fmt.Sprintf(warnInvalidValueMsg, "roles", rolesClaimValue))
return roleValues
}
vals = append(vals, r)
}
return vals
}
rolesString, ok := rolesClaimValue.(string)
if !ok {
log.Warn(fmt.Sprintf(warnInvalidValueMsg, "roles", rolesClaimValue))
return roleValues
}
return strings.Split(rolesString, " ")
}
func (j *JWT) addPermissionsFromRoles(tokenClaims jwt.MapClaims, permissions []string, log *logrus.Entry) []string {
if j.rolesClaim == "" || j.rolesMap == nil {
return permissions
}
rolesClaimValue, exists := tokenClaims[j.rolesClaim]
if !exists {
return permissions
}
roleValues := j.getRoleValues(rolesClaimValue, log)
for _, r := range roleValues {
if perms, exist := j.rolesMap[r]; exist {
for _, p := range perms {
permissions, _ = addPermission(permissions, p)
}
}
}
if perms, exist := j.rolesMap["*"]; exist {
for _, p := range perms {
permissions, _ = addPermission(permissions, p)
}
}
return permissions
}
func (j *JWT) addMappedPermissions(source, target []string) []string {
if j.permissionsMap == nil {
return target
}
for _, val := range source {
mappedValues, exist := j.permissionsMap[val]
if !exist {
// no mapping for value
continue
}
var l []string
for _, mv := range mappedValues {
var added bool
// add value from mapping?
target, added = addPermission(target, mv)
if !added {
continue
}
l = append(l, mv)
}
// recursion: call only with values not already in target
target = j.addMappedPermissions(l, target)
}
return target
}
func addPermission(permissions []string, permission string) ([]string, bool) {
permission = strings.TrimSpace(permission)
if permission == "" {
return permissions, false
}
for _, p := range permissions {
if p == permission {
return permissions, false
}
}
return append(permissions, permission), true
}
func getBearer(val string) (string, error) {
const bearer = "bearer "
if strings.HasPrefix(strings.ToLower(val), bearer) {
return strings.Trim(val[len(bearer):], " "), nil
}
return "", fmt.Errorf("bearer required with authorization header")
}
// newParser creates a new parser
func newParser(algos []acjwt.Algorithm) *jwt.Parser {
var algorithms []string
for _, a := range algos {
algorithms = append(algorithms, a.String())
}
options := []jwt.ParserOption{
jwt.WithValidMethods(algorithms),
// no equivalent in new lib
// jwt.WithLeeway(time.Second),
}
return jwt.NewParser(options...)
}
// parsePublicPEMKey tries to parse all supported publicKey variations which
// must be given in PEM encoded format.
func parsePublicPEMKey(key []byte) (pub interface{}, err error) {
pemBlock, _ := pem.Decode(key)
if pemBlock == nil {
return nil, jwt.ErrKeyMustBePEMEncoded
}
pubKey, pubErr := x509.ParsePKCS1PublicKey(pemBlock.Bytes)
if pubErr != nil {
pkixKey, pkerr := x509.ParsePKIXPublicKey(pemBlock.Bytes)
if pkerr != nil {
cert, cerr := x509.ParseCertificate(pemBlock.Bytes)
if cerr != nil {
return nil, jwt.ErrNotRSAPublicKey
}
if k, ok := cert.PublicKey.(*rsa.PublicKey); ok {
return k, nil
}
if k, ok := cert.PublicKey.(*ecdsa.PublicKey); ok {
return k, nil
}
return nil, fmt.Errorf("invalid RSA/ECDSA public key")
}
if k, ok := pkixKey.(*rsa.PublicKey); ok {
return k, nil
}
if k, ok := pkixKey.(*ecdsa.PublicKey); ok {
return k, nil
}
return nil, fmt.Errorf("invalid RSA/ECDSA public key")
}
return pubKey, nil
}