-
Notifications
You must be signed in to change notification settings - Fork 192
/
auth_oidc.go
110 lines (87 loc) · 2.77 KB
/
auth_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
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
105
106
107
108
109
110
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
var _ azcore.TokenCredential = &OidcCredential{}
type OidcCredential struct {
requestToken string
requestUrl string
token string
cred *azidentity.ClientAssertionCredential
}
type OidcCredentialOptions struct {
azcore.ClientOptions
TenantID string
ClientID string
RequestToken string
RequestUrl string
Token string
}
func NewOidcCredential(options *OidcCredentialOptions) (*OidcCredential, error) {
w := &OidcCredential{
requestToken: options.RequestToken,
requestUrl: options.RequestUrl,
token: options.Token,
}
cred, err := azidentity.NewClientAssertionCredential(options.TenantID, options.ClientID, w.getAssertion, &azidentity.ClientAssertionCredentialOptions{ClientOptions: options.ClientOptions})
if err != nil {
return nil, err
}
w.cred = cred
return w, nil
}
func (w *OidcCredential) GetToken(ctx context.Context, opts policy.TokenRequestOptions) (azcore.AccessToken, error) {
return w.cred.GetToken(ctx, opts)
}
func (w *OidcCredential) getAssertion(ctx context.Context) (string, error) {
if w.token != "" {
return w.token, nil
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, w.requestUrl, http.NoBody)
if err != nil {
return "", fmt.Errorf("getAssertion: failed to build request")
}
query, err := url.ParseQuery(req.URL.RawQuery)
if err != nil {
return "", fmt.Errorf("getAssertion: cannot parse URL query")
}
if query.Get("audience") == "" {
query.Set("audience", "api://AzureADTokenExchange")
req.URL.RawQuery = query.Encode()
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", w.requestToken))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("getAssertion: cannot request token: %v", err)
}
// #nosec G307
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return "", fmt.Errorf("getAssertion: cannot parse response: %v", err)
}
if c := resp.StatusCode; c < 200 || c > 299 {
return "", fmt.Errorf("getAssertion: received HTTP status %d with response: %s", resp.StatusCode, body)
}
var tokenRes struct {
Count *int `json:"count"`
Value *string `json:"value"`
}
if err := json.Unmarshal(body, &tokenRes); err != nil {
return "", fmt.Errorf("getAssertion: cannot unmarshal response: %v", err)
}
if tokenRes.Value == nil {
return "", fmt.Errorf("getAssertion: nil JWT assertion received from OIDC provider")
}
return *tokenRes.Value, nil
}