-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
104 lines (85 loc) · 2.61 KB
/
client.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
package onelogin
import (
"context"
"crypto/ecdsa"
"errors"
"net/http"
"net/url"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
)
var expectedError = errors.New("err")
type Doer interface {
Do(r *http.Request) (*http.Response, error)
}
type Logger interface {
WarnContext(ctx context.Context, msg string, args ...any)
}
type SecretsClient interface {
SecretBytes(ctx context.Context, name string) ([]byte, error)
}
type IdentityPublicKeyFunc func(context.Context) (*ecdsa.PublicKey, error)
type Client struct {
ctx context.Context
logger Logger
httpClient Doer
openidConfiguration *configurationClient
secretsClient SecretsClient
randomString func(int) string
identityPublicKeyFunc IdentityPublicKeyFunc
clientID string
redirectURL string
}
func New(ctx context.Context, logger Logger, httpClient *http.Client, secretsClient SecretsClient, issuer, clientID, redirectURL string, identityPublicKeyFunc IdentityPublicKeyFunc) *Client {
return &Client{
ctx: ctx,
logger: logger,
httpClient: httpClient,
secretsClient: secretsClient,
randomString: random.String,
identityPublicKeyFunc: identityPublicKeyFunc,
clientID: clientID,
redirectURL: redirectURL,
openidConfiguration: getConfiguration(ctx, logger, httpClient, issuer),
}
}
func (c *Client) AuthCodeURL(state, nonce, locale string, identity bool) (string, error) {
q := url.Values{
"response_type": {"code"},
"scope": {"openid email"},
"redirect_uri": {c.redirectURL},
"client_id": {c.clientID},
"state": {state},
"nonce": {nonce},
"ui_locales": {locale},
}
if identity {
q.Add("vtr", `["Cl.Cm.P2"]`)
q.Add("claims", `{"userinfo":{"https://vocab.account.gov.uk/v1/coreIdentityJWT": null}}`)
}
endpoint, err := c.openidConfiguration.AuthorizationEndpoint()
if err != nil {
return "", err
}
return endpoint + "?" + q.Encode(), nil
}
func (c *Client) EndSessionURL(idToken, postLogoutURL string) (string, error) {
endpoint, err := c.openidConfiguration.EndSessionEndpoint()
if err != nil {
return "", err
}
return endpoint + "?" + url.Values{
"id_token_hint": {idToken},
"post_logout_redirect_uri": {postLogoutURL},
}.Encode(), nil
}
func (c *Client) CheckHealth(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.openidConfiguration.issuer, nil)
if err != nil {
return err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
return resp.Body.Close()
}