-
Notifications
You must be signed in to change notification settings - Fork 55
/
auth.go
255 lines (224 loc) · 7.32 KB
/
auth.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//go:generate mockgen -source auth.go -destination mock/auth.go -package mock
package auth
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"golang.org/x/oauth2/clientcredentials"
)
const (
audiencePath = "/api/v2/"
waitThresholdInSeconds = 3
)
// Credentials is used to facilitate the login process.
type Credentials struct {
Audience string
ClientID string
DeviceCodeEndpoint string
OauthTokenEndpoint string
}
type Result struct {
Tenant string
Domain string
RefreshToken string
AccessToken string
ExpiresAt time.Time
}
type State struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri_complete"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}
func (s *State) IntervalDuration() time.Duration {
return time.Duration(s.Interval+waitThresholdInSeconds) * time.Second
}
var credentials = &Credentials{
Audience: "https://*.auth0.com/api/v2/",
ClientID: "2iZo3Uczt5LFHacKdM0zzgUO2eG2uDjT",
DeviceCodeEndpoint: "https://auth0.auth0.com/oauth/device/code",
OauthTokenEndpoint: "https://auth0.auth0.com/oauth/token",
}
// WaitUntilUserLogsIn waits until the user is logged in on the browser.
func WaitUntilUserLogsIn(ctx context.Context, httpClient *http.Client, state State) (Result, error) {
t := time.NewTicker(state.IntervalDuration())
for {
select {
case <-ctx.Done():
return Result{}, ctx.Err()
case <-t.C:
data := url.Values{
"client_id": []string{credentials.ClientID},
"grant_type": []string{"urn:ietf:params:oauth:grant-type:device_code"},
"device_code": []string{state.DeviceCode},
}
r, err := httpClient.PostForm(credentials.OauthTokenEndpoint, data)
if err != nil {
return Result{}, fmt.Errorf("cannot get device code: %w", err)
}
defer func() {
_ = r.Body.Close()
}()
var res struct {
AccessToken string `json:"access_token"`
IDToken string `json:"id_token"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
ExpiresIn int64 `json:"expires_in"`
TokenType string `json:"token_type"`
Error *string `json:"error,omitempty"`
ErrorDescription string `json:"error_description,omitempty"`
}
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return Result{}, fmt.Errorf("cannot decode response: %w", err)
}
if res.Error != nil {
if *res.Error == "authorization_pending" {
continue
}
return Result{}, errors.New(res.ErrorDescription)
}
ten, domain, err := parseTenant(res.AccessToken)
if err != nil {
return Result{}, fmt.Errorf("cannot parse tenant from the given access token: %w", err)
}
return Result{
RefreshToken: res.RefreshToken,
AccessToken: res.AccessToken,
ExpiresAt: time.Now().Add(
time.Duration(res.ExpiresIn) * time.Second,
),
Tenant: ten,
Domain: domain,
}, nil
}
}
}
var RequiredScopes = []string{
"openid",
"offline_access", // For retrieving refresh token.
"create:clients", "delete:clients", "read:clients", "update:clients",
"read:client_grants",
"create:resource_servers", "delete:resource_servers", "read:resource_servers", "update:resource_servers",
"create:roles", "delete:roles", "read:roles", "update:roles",
"create:rules", "delete:rules", "read:rules", "update:rules",
"create:users", "delete:users", "read:users", "update:users",
"read:branding", "update:branding",
"read:email_templates", "update:email_templates",
"read:email_provider",
"read:connections", "update:connections",
"read:client_keys", "read:logs", "read:tenant_settings",
"read:custom_domains", "create:custom_domains", "update:custom_domains", "delete:custom_domains",
"read:anomaly_blocks", "delete:anomaly_blocks",
"create:log_streams", "delete:log_streams", "read:log_streams", "update:log_streams",
"create:actions", "delete:actions", "read:actions", "update:actions",
"create:organizations", "delete:organizations", "read:organizations", "update:organizations", "read:organization_members", "read:organization_member_roles", "read:organization_connections",
"read:prompts", "update:prompts",
"read:attack_protection", "update:attack_protection",
}
// GetDeviceCode kicks-off the device authentication flow by requesting
// a device code from Auth0. The returned state contains the
// URI for the next step of the flow.
func GetDeviceCode(ctx context.Context, httpClient *http.Client, additionalScopes []string) (State, error) {
a := credentials
data := url.Values{
"client_id": []string{a.ClientID},
"scope": []string{strings.Join(append(RequiredScopes, additionalScopes...), " ")},
"audience": []string{a.Audience},
}
request, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
a.DeviceCodeEndpoint,
strings.NewReader(data.Encode()),
)
if err != nil {
return State{}, fmt.Errorf("failed to create the request: %w", err)
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := httpClient.Do(request)
if err != nil {
return State{}, fmt.Errorf("failed to send the request: %w", err)
}
defer func() {
_ = response.Body.Close()
}()
if response.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return State{}, fmt.Errorf(
"received a %d response and failed to read the response",
response.StatusCode,
)
}
return State{}, fmt.Errorf("received a %d response: %s", response.StatusCode, bodyBytes)
}
var state State
if err = json.NewDecoder(response.Body).Decode(&state); err != nil {
return State{}, fmt.Errorf("failed to decode the response: %w", err)
}
return state, nil
}
func parseTenant(accessToken string) (tenant, domain string, err error) {
parts := strings.Split(accessToken, ".")
v, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return "", "", err
}
var payload struct {
AUDs []string `json:"aud"`
}
if err := json.Unmarshal(v, &payload); err != nil {
return "", "", err
}
for _, aud := range payload.AUDs {
u, err := url.Parse(aud)
if err != nil {
return "", "", err
}
if u.Path == audiencePath {
parts := strings.Split(u.Host, ".")
return parts[0], u.Host, nil
}
}
return "", "", fmt.Errorf("audience not found for %s", audiencePath)
}
// ClientCredentials encapsulates all data to facilitate access token creation with client credentials (client ID and client secret).
type ClientCredentials struct {
ClientID string
ClientSecret string
Domain string
}
// GetAccessTokenFromClientCreds generates an access token from client credentials.
func GetAccessTokenFromClientCreds(ctx context.Context, args ClientCredentials) (Result, error) {
u, err := url.Parse("https://" + args.Domain)
if err != nil {
return Result{}, err
}
credsConfig := &clientcredentials.Config{
ClientID: args.ClientID,
ClientSecret: args.ClientSecret,
TokenURL: u.String() + "/oauth/token",
EndpointParams: url.Values{
"client_id": {args.ClientID},
"audience": {u.String() + "/api/v2/"},
},
}
resp, err := credsConfig.Token(ctx)
if err != nil {
return Result{}, err
}
return Result{
AccessToken: resp.AccessToken,
ExpiresAt: resp.Expiry,
}, nil
}