Skip to content

feat: sc-240964/fetch token#326

Merged
dbolson merged 8 commits into
mainfrom
sc-240964/fetch-token
Jun 24, 2024
Merged

feat: sc-240964/fetch token#326
dbolson merged 8 commits into
mainfrom
sc-240964/fetch-token

Conversation

@dbolson
Copy link
Copy Markdown
Contributor

@dbolson dbolson commented Jun 24, 2024

Attempt to fetch an access token from the POST /tokens endpoint. It retries every second if the request is pending up to two minutes while the user logs in to LD to approve their request. Other errors show a specific error message so the user knows what to do.
Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

Provide links to any issues in this repository or elsewhere relating to this pull request.

Describe the solution you've provided

Provide a clear and concise description of what you expect to happen.

Describe alternatives you've considered

Provide a clear and concise description of any alternative solutions or features you've considered.

Additional context

Add any other context about the pull request here.

@dbolson dbolson marked this pull request as ready for review June 24, 2024 16:51
@dbolson dbolson force-pushed the sc-240964/fetch-token branch from 247ce7e to d46bc6f Compare June 24, 2024 16:51
@dbolson dbolson force-pushed the sc-240964/fetch-token branch from d46bc6f to 19715a8 Compare June 24, 2024 16:52
Comment thread cmd/login/login.go
)
if err != nil {
return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag))
return err
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only run by a user, so we don't need to return anything but plain text.

Comment thread cmd/login/login.go
return err
}

fmt.Fprintf(cmd.OutOrStdout(), "Your token is %s\n", deviceAuthorizationToken.AccessToken)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another PR will set this value in the config instead of showing it.

Comment thread internal/login/login.go
maxAttempts int,
) (DeviceAuthorizationToken, error) {
var attempts int
for {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can block since we're not doing anything else while the user logs into LD.

Comment thread internal/login/login.go Outdated
case "authorization_pending":
attempts += 1
case "access_denied":
return DeviceAuthorizationToken{}, errors.NewError("Your request has been denied. Please try logging in again.")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion on these messages if you think of something better.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also no strong opinion.

in the happy path, the individual denying the request is the same as the individual running this CLI command, which means please try logging in again may be presumptuous of their requirements.

Comment thread internal/login/login.go
deviceCode,
)
res, err := client.MakeRequest("POST", path, []byte(body))
body, _ := json.Marshal(map[string]string{
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marshal this instead of using a string since the test mock complains if the whitespace differs from the test setup. Marshaling removes extra whitespace to match the tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me. you could also remove all whitespace at each location.

@dbolson dbolson requested a review from moribellamy June 24, 2024 16:59
Comment thread internal/login/login.go Outdated
var attempts int
for {
if attempts > maxAttempts {
return DeviceAuthorizationToken{}, errors.NewError("The request timed-out after too many attempts.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i usually see it spelled timed out

Comment thread internal/login/login.go Outdated
case "authorization_pending":
attempts += 1
case "access_denied":
return DeviceAuthorizationToken{}, errors.NewError("Your request has been denied. Please try logging in again.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also no strong opinion.

in the happy path, the individual denying the request is the same as the individual running this CLI command, which means please try logging in again may be presumptuous of their requirements.

Comment thread internal/login/login.go
deviceCode,
)
res, err := client.MakeRequest("POST", path, []byte(body))
body, _ := json.Marshal(map[string]string{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me. you could also remove all whitespace at each location.

mockClient.On(
"MakeRequest",
"POST",
"http://test.com/internal/device-authorization/token",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like example.com or example.org since there are reserved by ICANN and guaranteed to never be real webapps. maybe test.com is similar? i doubt it matters.

@dbolson dbolson merged commit 58dc226 into main Jun 24, 2024
@dbolson dbolson deleted the sc-240964/fetch-token branch June 24, 2024 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants