-
Notifications
You must be signed in to change notification settings - Fork 11
/
jwt.go
473 lines (422 loc) · 12.6 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
package j8a
import (
"context"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/itchyny/gojq"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/rs/zerolog/log"
"golang.org/x/sync/semaphore"
"strconv"
"strings"
"time"
)
type KeySet []KidPair
func (ks *KeySet) Upsert(kp KidPair) {
updated := false
for _, k := range *ks {
if k.Kid == kp.Kid {
k.Key = kp.Key
updated = true
}
}
if !updated {
*ks = append(*ks, kp)
}
}
func (ks *KeySet) Find(kid string) interface{} {
for _, k := range *ks {
if k.Kid == kid {
return k.Key
}
}
return nil
}
type KidPair struct {
Kid string
Key interface{}
}
type Jwt struct {
Name string
Alg string
// Jwt key supports pem encoding for public keys, certificates unencoded secrets for hmac.
Key string
// JwksUrl loads remotely.
JwksUrl string
RSAPublic KeySet
ECDSAPublic KeySet
Secret KeySet
AcceptableSkewSeconds string
Claims []string
claimsVal []*gojq.Code
lock *semaphore.Weighted
updateCount int
}
var validAlgNoNone = []string{"RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "HS256", "HS384", "HS512", "ES256", "ES384", "ES512"}
var validAlg = append(validAlgNoNone, "none")
const pemOverflow = "jwt key [%s] only type PUBLIC KEY or CERTIFICATE allowed but found additional or invalid data, check your PEM block"
const pemTypeBad = "jwt key [%s] is not of type PUBLIC KEY or CERTIFICATE, check your PEM Block preamble"
const pemAsn1Bad = "jwt key [%s] asn data not valid, check your PEM Block"
const pemRsaNotFound = "jwt key [%s] RSA public key not found in your certificate, check your PEM Block"
const pemEcdsaNotFound = "jwt key [%s] ECDSA public key not found in your certificate, check your PEM Block"
const keyTypeInvalid = "jwt [%s] unable to determine key type. Must be one of %s"
const unknownAlg = "jwt [%s] unknown alg [%s]. Must be one of %s"
const missingAlg = "jwt [%s] missing mandatory alg parameter next to jwksUrl. Must be one of %s"
const noneWithKeyData = "jwt [%s] none type signature does not allow key data, check your configuration"
const missingKeyOrJwks = "jwt [%s] alg [%s] must specify one of key or jwksUrl"
const skewInvalid = "jwt [%s] acceptable skew seconds, must be 0 or greater, was %s"
const ecdsaKeySizeBad = "jwt [%s] invalid key size for alg [%s], parsed bitsize %d, check your configuration"
const defaultSkew = "120"
const jwksRefreshSlowwait = time.Second * 10
func NewJwt(name string, alg string, key string, jwksUrl string, acceptableSkewSeconds string, claims ...string) *Jwt {
jwt := Jwt{
Name: name,
Alg: alg,
Key: key,
JwksUrl: jwksUrl,
AcceptableSkewSeconds: acceptableSkewSeconds,
Claims: claims,
updateCount: 0,
}
jwt.Init()
return &jwt
}
// we need this separate because the JSON unmarshaller creates this object without asking us.
func (jwt *Jwt) Init() {
jwt.RSAPublic = make([]KidPair, 0)
jwt.ECDSAPublic = make([]KidPair, 0)
jwt.Secret = make([]KidPair, 0)
jwt.lock = semaphore.NewWeighted(1)
jwt.claimsVal = make([]*gojq.Code, 0)
}
func (j *Jwt) UnmarshalJSON(data []byte) error {
var value interface{}
if err := json.Unmarshal(data, &value); err != nil {
return err
}
switch v := value.(type) {
case map[string]interface{}:
if v["acceptableSkewSeconds"] != nil {
j.AcceptableSkewSeconds = fmt.Sprintf("%v", v["acceptableSkewSeconds"])
}
if v["alg"] != nil {
j.Alg = fmt.Sprintf("%v", v["alg"])
}
if v["key"] != nil {
j.Key = fmt.Sprintf("%v", v["key"])
}
if v["jwksUrl"] != nil {
j.JwksUrl = fmt.Sprintf("%v", v["jwksUrl"])
}
if v["claims"] != nil {
vc, ok := v["claims"].([]interface{})
if !ok {
return fmt.Errorf("unexpected JSON value type: %T", value)
}
for _, v1 := range vc {
s, ok := v1.(string)
if ok {
j.Claims = append(j.Claims, s)
} else {
return fmt.Errorf("unexpected JSON value type: %T", value)
}
}
}
default:
return fmt.Errorf("unexpected JSON value type: %T", value)
}
return nil
}
func (jwt *Jwt) Validate() error {
var err error
alg := *new(jwa.SignatureAlgorithm)
err = alg.Accept(jwt.Alg)
if len(jwt.Name) == 0 {
return errors.New("invalid jwt name not specified")
}
if len(jwt.Alg) > 0 {
matched := false
for _, alg := range validAlg {
if alg == jwt.Alg {
matched = true
}
}
if !matched {
return errors.New(fmt.Sprintf(unknownAlg, jwt.Name, jwt.Alg, validAlg))
}
}
if len(jwt.Alg) == 0 && len(jwt.JwksUrl) > 0 {
return errors.New(fmt.Sprintf(missingAlg, jwt.Name, validAlgNoNone))
}
if len(jwt.Alg) == 0 && len(jwt.Key) > 0 {
return errors.New(fmt.Sprintf(missingAlg, jwt.Name, validAlgNoNone))
}
if alg == jwa.NoSignature && len(jwt.Key) > 0 {
return errors.New(fmt.Sprintf(noneWithKeyData, jwt.Name))
}
if alg != jwa.NoSignature && len(jwt.Key) == 0 && len(jwt.JwksUrl) == 0 {
return errors.New(fmt.Sprintf(missingKeyOrJwks, jwt.Name, alg))
}
if len(jwt.AcceptableSkewSeconds) > 0 {
secs, nonnumeric := strconv.Atoi(jwt.AcceptableSkewSeconds)
if nonnumeric != nil || secs < 0 {
err = errors.New(fmt.Sprintf(skewInvalid, jwt.Name, jwt.AcceptableSkewSeconds))
return err
}
} else {
jwt.AcceptableSkewSeconds = defaultSkew
}
if len(jwt.Claims) > 0 {
jwt.claimsVal = make([]*gojq.Code, len(jwt.Claims))
for i, claim := range jwt.Claims {
//poor mans jq query conversion
if len(claim) > 0 &&
!strings.Contains(claim, " ") &&
string(claim[0]) != "." {
claim = "." + claim
jwt.Claims[i] = claim
}
q, e := gojq.Parse(claim)
if e != nil {
err = e
break
} else {
var c *gojq.Code
c, err = gojq.Compile(q)
if err == nil {
jwt.claimsVal[i] = c
} else {
break
}
}
}
}
if len(jwt.Key) > 0 {
err = jwt.parseKey(alg)
} else if len(jwt.JwksUrl) > 0 {
err = jwt.LoadJwks()
}
return err
}
// TODO this method needs a refactor and has high cognitive complexity
func (jwt *Jwt) LoadJwks() error {
var err error
//acquires the lock with true else skips
if jwt.lock.TryAcquire(1) {
var keyset jwk.Set
keyset, err = jwk.Fetch(context.Background(), jwt.JwksUrl)
if err == nil {
log.Info().Msgf("jwt [%s] fetched %d jwk from jwks URL %s", jwt.Name, keyset.Len(), jwt.JwksUrl)
} else {
log.Warn().Msgf("jwt [%s] unable to fetch jwk from jwks URL %s, cause: %v", jwt.Name, jwt.JwksUrl, err)
}
if keyset == nil || keyset.Len() == 0 {
err = errors.New(fmt.Sprintf("jwt [%s] unable to parse keys in keyset", jwt.Name))
} else {
keys := keyset.Iterate(context.Background())
Keyrange:
for keys.Next(context.Background()) {
key := keys.Pair().Value.(jwk.Key)
alg := *new(jwa.SignatureAlgorithm)
err = alg.Accept(key.Algorithm())
//check alg conforms to what's configured. J8a does not support rotating key algos for security.
if jwt.Alg != key.Algorithm() {
msg := "jwt [%s] key algorithm [%s] in jwks keyset does not match configured alg [%s]."
err = errors.New(fmt.Sprintf(msg, jwt.Name, key.Algorithm(), jwt.Alg))
log.Warn().
Str("jwt", jwt.Name).
Str("jwtAlg", jwt.Alg).
Str("keyAlg", key.Algorithm()).
Msgf(msg, jwt.Name, key.Algorithm(), jwt.Alg)
}
if err == nil {
switch alg {
case jwa.RS256, jwa.RS384, jwa.RS512, jwa.PS256, jwa.PS384, jwa.PS512:
k := KidPair{
Kid: key.KeyID(),
Key: &rsa.PublicKey{
N: nil,
E: 0,
},
}
err = key.Raw(k.Key)
if err == nil {
jwt.RSAPublic.Upsert(k)
}
//Note, removed support for HS256, secret keys make no sense for JWKS even over TLS.
case jwa.ES256, jwa.ES384, jwa.ES512:
k := KidPair{
Kid: key.KeyID(),
Key: &ecdsa.PublicKey{
Curve: nil,
X: nil,
Y: nil,
},
}
err = key.Raw(k.Key)
err = jwt.checkECDSABitSize(alg, k.Key.(*ecdsa.PublicKey))
if err == nil {
jwt.ECDSAPublic.Upsert(k)
}
default:
err = errors.New(fmt.Sprintf("unknown key type in Jwks %v", alg.String()))
}
log.Info().Msgf("jwt [%s] successfully parsed %s key from remote jwk", jwt.Name, alg)
} else {
break Keyrange
}
}
}
//slow down JWKS updates to once every 10 seconds per route to prevent DOS attacks
if jwt.updateCount > 0 {
time.Sleep(jwksRefreshSlowwait)
}
jwt.updateCount++
//release here, don't use defer
jwt.lock.Release(1)
} else {
log.Info().
Str("jwt", jwt.Name).
Msgf("jwt [%s] already updating within 10s, skipping attempt.", jwt.Name)
}
return err
}
func (jwt *Jwt) parseKey(alg jwa.SignatureAlgorithm) error {
var p *pem.Block
var p1 []byte
var err error
switch alg {
case jwa.RS256, jwa.RS384, jwa.RS512, jwa.PS256, jwa.PS384, jwa.PS512:
p, p1 = pem.Decode([]byte(jwt.Key))
if len(p1) > 0 {
return errors.New(fmt.Sprintf(pemOverflow, jwt.Name))
}
if p.Type != "PUBLIC KEY" && p.Type != "RSA PUBLIC KEY" && p.Type != "CERTIFICATE" {
return errors.New(fmt.Sprintf(pemTypeBad, jwt.Name))
}
switch p.Type {
case "PUBLIC KEY", "RSA PUBLIC KEY":
var pub interface{}
pub, err = x509.ParsePKIXPublicKey(p.Bytes)
switch pub.(type) {
case *rsa.PublicKey:
jwt.RSAPublic.Upsert(
KidPair{
Kid: fmt.Sprintf("%s-%s", alg, uuid.New()),
Key: pub.(*rsa.PublicKey),
})
default:
return errors.New(fmt.Sprintf(pemAsn1Bad, jwt.Name))
}
case "CERTIFICATE":
var cert interface{}
cert, err = x509.ParseCertificate(p.Bytes)
switch cert.(type) {
case *x509.Certificate:
key := cert.(*x509.Certificate).PublicKey
switch key.(type) {
case *rsa.PublicKey:
jwt.RSAPublic.Upsert(
KidPair{
Kid: fmt.Sprintf("%s-%s", alg, uuid.New()),
Key: key.(*rsa.PublicKey),
})
default:
return errors.New(fmt.Sprintf(pemRsaNotFound, jwt.Name))
}
default:
return errors.New(fmt.Sprintf(pemAsn1Bad, jwt.Name))
}
}
case jwa.HS256, jwa.HS384, jwa.HS512:
if len(jwt.Key) > 0 {
jwt.Secret.Upsert(
KidPair{
Kid: fmt.Sprintf("%s-%s", alg, uuid.New()),
Key: []byte(jwt.Key),
})
} else {
err = errors.New("jwt secret not found, check your configuration")
}
case jwa.ES256, jwa.ES384, jwa.ES512:
p, p1 = pem.Decode([]byte(jwt.Key))
if len(p1) > 0 {
return errors.New(fmt.Sprintf(pemOverflow, jwt.Name))
}
if p.Type != "PUBLIC KEY" && p.Type != "CERTIFICATE" {
return errors.New(fmt.Sprintf(pemTypeBad, jwt.Name))
}
switch p.Type {
case "PUBLIC KEY":
var pub interface{}
pub, err = x509.ParsePKIXPublicKey(p.Bytes)
switch pub.(type) {
case *ecdsa.PublicKey:
parsed := pub.(*ecdsa.PublicKey)
err = jwt.checkECDSABitSize(alg, parsed)
if err == nil {
jwt.ECDSAPublic.Upsert(
KidPair{
Kid: fmt.Sprintf("%s-%s", alg, uuid.New()),
Key: parsed,
})
}
default:
return errors.New(fmt.Sprintf(pemAsn1Bad, jwt.Name))
}
case "CERTIFICATE":
var cert interface{}
cert, err = x509.ParseCertificate(p.Bytes)
switch cert.(type) {
case *x509.Certificate:
key := cert.(*x509.Certificate).PublicKey
switch key.(type) {
case *ecdsa.PublicKey:
parsed := key.(*ecdsa.PublicKey)
err = jwt.checkECDSABitSize(alg, parsed)
if err == nil {
jwt.ECDSAPublic.Upsert(
KidPair{
Kid: fmt.Sprintf("%s-%s", alg, uuid.New()),
Key: parsed,
})
}
default:
return errors.New(fmt.Sprintf(pemEcdsaNotFound, jwt.Name))
}
default:
return errors.New(fmt.Sprintf(pemAsn1Bad, jwt.Name))
}
}
case jwa.NoSignature:
if len(jwt.Key) > 0 {
return errors.New(fmt.Sprintf("jwt [%s] none type signature does not allow key data, check your configuration", jwt.Name))
}
default:
return errors.New(fmt.Sprintf(keyTypeInvalid, jwt.Name, validAlg))
}
log.Info().Msgf("jwt [%s] successfully parsed %s key", jwt.Name, alg)
return err
}
func (jwt *Jwt) checkECDSABitSize(alg jwa.SignatureAlgorithm, parsed *ecdsa.PublicKey) error {
bitsize := parsed.Curve.Params().BitSize
var err error
if alg == jwa.ES256 && (bitsize != 256) {
err = errors.New(fmt.Sprintf(ecdsaKeySizeBad, jwt.Name, alg, bitsize))
} else if alg == jwa.ES384 && (bitsize != 384) {
err = errors.New(fmt.Sprintf(ecdsaKeySizeBad, jwt.Name, alg, bitsize))
} else if alg == jwa.ES512 && (bitsize != 521) {
err = errors.New(fmt.Sprintf(ecdsaKeySizeBad, jwt.Name, alg, bitsize))
}
return err
}
func (jwt *Jwt) hasMandatoryClaims() bool {
return len(jwt.Claims) > 0 && len(jwt.Claims[0]) > 0
}