-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwks.go
123 lines (99 loc) · 2.6 KB
/
jwks.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
package jwks
import (
"encoding/json"
"fmt"
"sync"
"github.com/golang-jwt/jwt/v5"
)
// JWKS - JSON web key set.
type JWKS struct {
rwm sync.RWMutex
keys map[string]parsedJWK
}
type BaseJWKS struct {
Keys []JWK `json:"keys"`
}
// JWK - JSON web key, for more information see there https://openid.net/specs/draft-jones-json-web-key-03.html.
type JWK struct {
// The alg member identifies the cryptographic algorithm family used with the key.
Alg string `json:"alg"`
// The exp member contains the exponent value for the RSA public key.
// It is represented as the base64url encoding of the value's big endian representation.
Exp string `json:"e"`
// The kid (Key ID) member can be used to match a specific key. This can be used, for instance,
// to choose among a set of keys within the JWK during key rollover.
Kid string `json:"kid"`
// The "kty" (key type) parameter identifies the cryptographic algorithm
// family used with the key, such as "RSA" or "EC".
Kty string `json:"kty"`
// The mod member contains the modulus value for the RSA public key.
Mod string `json:"n"`
// The use member identifies the intended use of the key.
// Values defined by this specification are sig (signature) and enc (encryption).
// Other values MAY be used. The use value is case sensitive. This member is OPTIONAL.
Use string `json:"use"`
}
type parsedJWK struct {
pk any
alg string
use string
}
func NewFromJSONString(jwksString string) (*JWKS, error) {
var rawJWKS BaseJWKS
err := json.Unmarshal([]byte(jwksString), &rawJWKS)
if err != nil {
return nil, err
}
result := &JWKS{
keys: make(map[string]parsedJWK, len(rawJWKS.Keys)),
}
for _, key := range rawJWKS.Keys {
var pk any
switch key.Kty {
case ktyRSA:
pk, err = key.RSA()
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("%w: %q", ErrJWKAlgNotSupported, key.Kty)
}
result.keys[key.Kid] = parsedJWK{
alg: key.Alg,
use: key.Use,
pk: pk,
}
}
return result, nil
}
func (j *JWKS) KeyFunc(token *jwt.Token) (interface{}, error) {
kid, ok := token.Header["kid"]
if !ok {
return nil, ErrReqKid
}
kidRes, ok := kid.(string)
if !ok {
return nil, ErrKidConvert
}
alg, ok := token.Header["alg"]
if !ok {
return nil, ErrReqAlg
}
algRes, ok := alg.(string)
if !ok {
return nil, ErrAlgConvert
}
return j.getPublicKey(kidRes, algRes)
}
func (j *JWKS) getPublicKey(kid, alg string) (interface{}, error) {
j.rwm.RLock()
pk, ok := j.keys[kid]
j.rwm.RUnlock()
if !ok {
return nil, ErrPKNotFound
}
if pk.alg != "" && pk.alg != alg {
return nil, ErrAlgNotSupported
}
return pk.pk, nil
}