-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
jwk.go
91 lines (76 loc) · 1.84 KB
/
jwk.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
package jwt
import (
"context"
"fmt"
"net/http"
"sync"
"github.com/golang-jwt/jwt/v4"
"github.com/lestrrat-go/jwx/v2/jwk"
)
type KeyCache struct {
url string
http *http.Client
m sync.Mutex
keys jwk.Set
}
func NewKeyCache(url string, client *http.Client) *KeyCache {
return &KeyCache{url: url, http: client}
}
func (c *KeyCache) GetOrFetchKeyWithContext(ctx context.Context, token *jwt.Token) (interface{}, error) {
if k, err := c.GetKey(token); err == nil {
return k, nil
}
if err := c.FetchKeysWithContext(ctx); err != nil {
return nil, err
}
return c.GetKey(token)
}
func (c *KeyCache) GetOrFetchKey(token *jwt.Token) (interface{}, error) {
if k, err := c.GetKey(token); err == nil {
return k, nil
}
if err := c.FetchKeys(); err != nil {
return nil, err
}
return c.GetKey(token)
}
func (c *KeyCache) GetKey(token *jwt.Token) (interface{}, error) {
key, err := c.LookupKey(token)
if err != nil {
return nil, err
}
var v interface{}
return v, key.Raw(&v)
}
func (c *KeyCache) LookupKey(token *jwt.Token) (jwk.Key, error) {
id, ok := token.Header["kid"].(string)
if !ok {
return nil, fmt.Errorf("missing key id in token")
}
c.m.Lock()
defer c.m.Unlock()
if c.keys == nil {
return nil, fmt.Errorf("empty JWK cache")
}
if key, ok := c.keys.LookupKeyID(id); ok {
if key.Algorithm().String() == token.Method.Alg() {
return key, nil
}
}
return nil, fmt.Errorf("could not find JWK")
}
func (c *KeyCache) FetchKeys() error {
ctx, cancel := context.WithTimeout(context.Background(), c.http.Timeout)
defer cancel()
return c.FetchKeysWithContext(ctx)
}
func (c *KeyCache) FetchKeysWithContext(ctx context.Context) error {
keys, err := jwk.Fetch(ctx, c.url, jwk.WithHTTPClient(c.http))
if err != nil {
return fmt.Errorf("could not fetch JWK: %w", err)
}
c.m.Lock()
defer c.m.Unlock()
c.keys = keys
return nil
}