forked from s12v/go-jwks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
45 lines (37 loc) · 849 Bytes
/
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
package jwks
import (
"encoding/json"
"fmt"
"github.com/square/go-jose"
"log"
"net/http"
)
type JWKSSource interface {
JSONWebKeySet() (*jose.JSONWebKeySet, error)
}
type WebSource struct {
client *http.Client
jwksUri string
}
func NewWebSource(jwksUri string) *WebSource {
return &WebSource{
client: new(http.Client),
jwksUri: jwksUri,
}
}
func (s *WebSource) JSONWebKeySet() (*jose.JSONWebKeySet, error) {
log.Printf("Fetchng JWKS from %s", s.jwksUri)
resp, err := s.client.Get(s.jwksUri)
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
}