-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
fetcher.go
65 lines (55 loc) · 1.44 KB
/
fetcher.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
package jwksx
import (
"encoding/json"
"net/http"
"sync"
"github.com/pkg/errors"
"gopkg.in/square/go-jose.v2"
)
// Fetcher is a small helper for fetching JSON Web Keys from remote endpoints.
type Fetcher struct {
sync.RWMutex
remote string
c *http.Client
keys map[string]jose.JSONWebKey
}
// NewFetcher returns a new fetcher that can download JSON Web Keys from remote endpoints.
func NewFetcher(remote string) *Fetcher {
return &Fetcher{
remote: remote,
c: http.DefaultClient,
keys: make(map[string]jose.JSONWebKey),
}
}
// GetKey retrieves a JSON Web Key from the cache, fetches it from a remote if it is not yet cached or returns an error.
func (f *Fetcher) GetKey(kid string) (*jose.JSONWebKey, error) {
f.RLock()
if k, ok := f.keys[kid]; ok {
f.RUnlock()
return &k, nil
}
f.RUnlock()
res, err := f.c.Get(f.remote)
if err != nil {
return nil, errors.WithStack(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.Errorf("expected status code 200 but got %d when requesting %s", res.StatusCode, f.remote)
}
var set jose.JSONWebKeySet
if err := json.NewDecoder(res.Body).Decode(&set); err != nil {
return nil, errors.WithStack(err)
}
for _, k := range set.Keys {
f.Lock()
f.keys[k.KeyID] = k
f.Unlock()
}
f.RLock()
defer f.RUnlock()
if k, ok := f.keys[kid]; ok {
return &k, nil
}
return nil, errors.Errorf("unable to find JSON Web Key with ID: %s", kid)
}