-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery_document.go
68 lines (53 loc) · 1.29 KB
/
discovery_document.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
package oidc
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func NewDiscoveryDocument(discoveryURL url.URL) *DiscoveryDocument {
return &DiscoveryDocument{
DiscoveryURL: discoveryURL,
}
}
func (document *DiscoveryDocument) Initialize() error {
err := loadDiscoveryDocument(document)
if err != nil {
return fmt.Errorf("error loading discovery document: %w", err)
}
err = loadJwksData(document)
if err != nil {
return fmt.Errorf("error loading jwks data: %w", err)
}
return nil
}
func loadDiscoveryDocument(document *DiscoveryDocument) error {
resp, err := http.Get(document.DiscoveryURL.String())
if err != nil {
return fmt.Errorf("could not fetch discovery url: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
err = json.NewDecoder(resp.Body).Decode(document)
if err != nil {
return fmt.Errorf("error decoding discovery document: %w", err)
}
return nil
}
func loadJwksData(document *DiscoveryDocument) error {
resp, err := http.Get(document.JWKSURL)
if err != nil {
return fmt.Errorf("could not fetch jwks URL")
}
defer func() {
_ = resp.Body.Close()
}()
jwks := JSONWebKeyResponse{}
err = json.NewDecoder(resp.Body).Decode(&jwks)
if err != nil {
return fmt.Errorf("error decoding jwks data: %w", err)
}
document.KeyResponse = &jwks
return nil
}