-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoidc.go
67 lines (58 loc) · 1.53 KB
/
oidc.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
package main
// TODO(glrf) Maybe switch to a full blown OIDC client, which would enable us to
// get the actual expiration time of the token.
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type oidcProvider struct {
client *http.Client
tokenUrl string
clientId string
clientSecret string
}
func (p *oidcProvider) GetToken(context.Context) ([]byte, error) {
raw, err := p.client.PostForm(p.tokenUrl, url.Values{
"grant_type": {"client_credentials"},
"client_id": {p.clientId},
"client_secret": {p.clientSecret},
})
if err != nil {
return nil, err
}
if raw.Body == nil {
return nil, fmt.Errorf("missing body in response")
}
defer raw.Body.Close()
body, err := io.ReadAll(raw.Body)
if err != nil {
return nil, fmt.Errorf("error reading body: %w", err)
}
if raw.StatusCode != http.StatusOK {
errResp := struct {
Error string `json:"error"`
Description string `json:"error_description"`
}{}
err := json.Unmarshal(body, &errResp)
if err != nil {
return nil, fmt.Errorf("error %s: uable to parse error response %w", raw.Status, err)
}
return nil, fmt.Errorf("error %s: %s", raw.Status, errResp.Description)
}
resp := struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}{}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, fmt.Errorf("uable to parse response %w", err)
}
if resp.AccessToken == "" {
return nil, fmt.Errorf("invalid response, no token provided")
}
return []byte(resp.AccessToken), nil
}