-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwk.go
201 lines (169 loc) · 4.68 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
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
package jwkset
import (
"bytes"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/go-jose/go-jose/v4"
"github.com/patrickmn/go-cache"
)
// Response represents a response of JWK Set.
// This contains a TTL (Time to Live) for caching purpose.
type Response struct {
Keys []jose.JSONWebKey
TTL time.Duration // This would be used as TTL for caching.
}
// ALBFetcher fetchs a public key from AWS's Application Load Balancer and decodes it into JWK.
type ALBFetcher struct {
Client *http.Client
Region string
Algo jose.SignatureAlgorithm
}
func (f *ALBFetcher) keyURL(kid string) string {
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-authenticate-users.html
return fmt.Sprintf("https://public-keys.auth.elb.%s.amazonaws.com/%s", f.Region, kid)
}
func (f *ALBFetcher) FetchJWKs(kid string) (*Response, error) {
resp, err := f.Client.Get(f.keyURL(kid))
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
jwks, err := DecodeSigPublicKey(data, kid, f.Algo)
return &Response{
Keys: jwks,
}, err
}
// HTTPFetcher fetches JWKs over HTTP.
type HTTPFetcher struct {
Client *http.Client
}
// InMemoryFetcher fetches JWKs from its memory.
type InMemoryFetcher struct {
RAWJWKs []byte
}
// FetchJWKs implements Fetcher interface by using internal JWKs.
func (f *InMemoryFetcher) FetchJWKs(_ string) (*Response, error) {
jwks, err := Decode(bytes.NewReader(f.RAWJWKs))
if err != nil {
return nil, err
}
return &Response{
Keys: jwks,
}, nil
}
// FetchJWKs implements Fetcher interface by using http.Client.
// FetchJWKs tries to retrieve JWKSet from uri.
func (f *HTTPFetcher) FetchJWKs(uri string) (*Response, error) {
resp, err := f.Client.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jwks, err := Decode(resp.Body)
return &Response{
Keys: jwks,
}, err
}
// S3Fetcher fetches JWKs via S3.
type S3Fetcher struct {
S3Svc s3iface.S3API
}
// FetchJWKs implements JWKsS3Fetcher by using S3. It tries to retrieve an S3 object from path.
// path must be in s3://<bucket>/<key>.
func (f *S3Fetcher) FetchJWKs(path string) (*Response, error) {
s3url, err := url.Parse(path)
if err != nil {
return nil, err
}
params := &s3.GetObjectInput{
Bucket: aws.String(s3url.Host),
Key: aws.String(s3url.Path),
}
resp, err := f.S3Svc.GetObject(params)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jwks, err := Decode(resp.Body)
return &Response{
Keys: jwks,
}, err
}
// Cacher fetches JWKs via Cache if available.
type Cacher struct {
fetcher Fetcher
cache *cache.Cache
defaultExpiration time.Duration
cleanupInterval time.Duration
}
// NewCacher returns Cacher with initializing cache store.
func NewCacher(defaultExpiration, cleanupInterval time.Duration, f Fetcher) *Cacher {
c := cache.New(defaultExpiration, cleanupInterval)
return &Cacher{
fetcher: f,
cache: c,
defaultExpiration: defaultExpiration,
cleanupInterval: cleanupInterval,
}
}
// FetchJWKs tries to retrieve JWKs from Cache. If the cache is not available,
// it will call Fetcher.FetchJWKs and cache the result for future request.
func (c *Cacher) FetchJWKs(cacheKey string) (*Response, error) {
if keys, found := c.cache.Get(cacheKey); found {
return &Response{Keys: keys.([]jose.JSONWebKey)}, nil
}
jwksresp, err := c.fetcher.FetchJWKs(cacheKey)
if err != nil {
return nil, err
}
ttl := jwksresp.TTL
// If TTL is larger than cleanupInterval, we should subtract cleanupInterval from TTL to
// make sure that the latest jwks is obtained.
if ttl > c.cleanupInterval {
ttl -= c.cleanupInterval
}
c.cache.Set(cacheKey, jwksresp.Keys, ttl)
return jwksresp, nil
}
// DecodeSigPublicKey decodes the plain public key into JWKs used for sigining.
// https://github.com/square/go-jose/blob/v2.4.1/jose-util/utils.go#L42
func DecodeSigPublicKey(data []byte, kid string, algo jose.SignatureAlgorithm) ([]jose.JSONWebKey, error) {
input := data
block, _ := pem.Decode(data)
if block != nil {
input = block.Bytes
}
pub, err := x509.ParsePKIXPublicKey(input)
if err != nil {
return nil, err
}
return []jose.JSONWebKey{
{
Key: pub,
KeyID: kid,
Algorithm: string(algo),
Use: "sig",
},
}, nil
}
// Decode decodes the data with reading from r into JWKs.
func Decode(r io.Reader) ([]jose.JSONWebKey, error) {
keyset := jose.JSONWebKeySet{}
if err := json.NewDecoder(r).Decode(&keyset); err != nil && err != io.EOF {
return nil, err
}
return keyset.Keys, nil
}