-
Notifications
You must be signed in to change notification settings - Fork 17
/
source.go
57 lines (45 loc) · 1.05 KB
/
source.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
package jwks
import (
"context"
"encoding/json"
"fmt"
"gopkg.in/square/go-jose.v2"
"net/http"
)
type JWKSSource interface {
JSONWebKeySet(ctx context.Context) (*jose.JSONWebKeySet, error)
}
type WebSource struct {
client *http.Client
jwksUri string
}
func NewWebSource(jwksUri string, client *http.Client) *WebSource {
if client == nil {
client = new(http.Client)
}
return &WebSource{
client: client,
jwksUri: jwksUri,
}
}
func (s *WebSource) JSONWebKeySet(ctx context.Context) (*jose.JSONWebKeySet, error) {
logger.Printf("Fetching JWKS from %s", s.jwksUri)
req, err := http.NewRequest("GET", s.jwksUri, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed request, status: %d", resp.StatusCode)
}
jsonWebKeySet := new(jose.JSONWebKeySet)
if err = json.NewDecoder(resp.Body).Decode(jsonWebKeySet); err != nil {
return nil, err
}
return jsonWebKeySet, err
}