-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DXCDT-296: Supporting additional scopes when authenticating as user #561
Changes from 7 commits
253f880
4cea50f
2cb6f19
06723ec
73ea1c9
d55fd0d
1aef346
b96c834
c3541b0
607a197
e8df004
7d58fa9
cb0e08f
a2f5860
9be2ed0
d5caf06
976f9a2
e23b9b2
d823184
03bea62
cc62501
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,18 +133,6 @@ func New() *Authenticator { | |
} | ||
} | ||
|
||
// Start 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 (a *Authenticator) Start(ctx context.Context) (State, error) { | ||
state, err := a.getDeviceCode(ctx) | ||
if err != nil { | ||
return State{}, fmt.Errorf("failed to get the device code: %w", err) | ||
} | ||
|
||
return state, nil | ||
} | ||
|
||
// Wait waits until the user is logged in on the browser. | ||
func (a *Authenticator) Wait(ctx context.Context, state State) (Result, error) { | ||
t := time.NewTicker(state.IntervalDuration()) | ||
|
@@ -205,49 +193,61 @@ func (a *Authenticator) Wait(ctx context.Context, state State) (Result, error) { | |
} | ||
} | ||
|
||
func (a *Authenticator) getDeviceCode(ctx context.Context) (State, error) { | ||
data := url.Values{ | ||
"client_id": []string{a.ClientID}, | ||
"scope": []string{strings.Join(requiredScopes, " ")}, | ||
"audience": []string{a.Audience}, | ||
} | ||
// 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 (a *Authenticator) GetDeviceCode(ctx context.Context, additionalScopes []string) (State, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only functional change here is the addition of the |
||
state, err := func() (State, error) { | ||
|
||
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) | ||
} | ||
scopesToRequest := append(requiredScopes, additionalScopes...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to note – combining the required scopes with additional scopes. |
||
|
||
data := url.Values{ | ||
"client_id": []string{a.ClientID}, | ||
"scope": []string{strings.Join(scopesToRequest, " ")}, | ||
"audience": []string{a.Audience}, | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
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) | ||
} | ||
|
||
response, err := http.DefaultClient.Do(request) | ||
if err != nil { | ||
return State{}, fmt.Errorf("failed to send the request: %w", err) | ||
} | ||
defer response.Body.Close() | ||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest { | ||
bodyBytes, err := io.ReadAll(response.Body) | ||
response, err := http.DefaultClient.Do(request) | ||
if err != nil { | ||
return State{}, fmt.Errorf( | ||
"received a %d response and failed to read the response", | ||
response.StatusCode, | ||
) | ||
return State{}, fmt.Errorf("failed to send the request: %w", err) | ||
} | ||
defer response.Body.Close() | ||
|
||
return State{}, fmt.Errorf("received a %d response: %s", response.StatusCode, bodyBytes) | ||
} | ||
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest { | ||
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 | ||
err = json.NewDecoder(response.Body).Decode(&state) | ||
var state State | ||
err = json.NewDecoder(response.Body).Decode(&state) | ||
if err != nil { | ||
return State{}, fmt.Errorf("failed to decode the response: %w", err) | ||
} | ||
|
||
return state, nil | ||
}() | ||
if err != nil { | ||
return State{}, fmt.Errorf("failed to decode the response: %w", err) | ||
fmt.Errorf("failed to get the device code: %w", err) | ||
} | ||
|
||
return state, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of a refactor here. Removing the
Start
function because it only exists as a function wrapper forgetDeviceCode
. Further, the name wasn't descriptive of what the function did.