feat: Implementation of OAuth Device Code Flow#245
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for Device Access Tokens using OAuth Device Code Flow to the Helix Twitch API client. This enables authentication for standalone devices like game consoles and CLIs that may not have easy access to a web browser.
- Added
DeviceAccessTokenfield to theOptionsstruct and corresponding getter/setter methods - Implemented
RequestDeviceVerificationURIandRequestDeviceAccessTokenmethods for the device code flow - Updated token precedence logic in
setRequestHeadersto include device tokens (between app and user tokens)
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| helix.go | Added DeviceAccessToken field to Options struct, implemented getter/setter methods, updated token precedence logic, and modified canRefreshToken logic |
| helix_test.go | Added comprehensive test coverage for device access token functionality including getter/setter tests and token header priority tests |
| authentication.go | Implemented device code flow with RequestDeviceVerificationURI and RequestDeviceAccessToken methods and associated types |
| authentication_test.go | Added test cases for device verification URI and device access token requests |
| docs/authentication_docs.md | Added documentation for device access token flow including examples for getting and refreshing device tokens |
| docs/README.md | Updated documentation to include device access tokens in the Options struct and access token priority explanation |
| SUPPORTED_ENDPOINTS.md | Added device access token OAuth flow to the list of supported endpoints |
Comments suppressed due to low confidence (1)
helix.go:442
- The
refreshToken()method only handles user access token refresh by callingRefreshUserAccessTokenand updatingUserAccessToken. However, thecanRefreshToken()method now allows device tokens to pass through (line 423). When a device token is being used without a user token, the refresh will still callRefreshUserAccessToken, which may not be appropriate for device tokens. This could cause incorrect token refresh behavior.
func (c *Client) refreshToken() error {
resp, err := c.RefreshUserAccessToken(c.opts.RefreshToken)
if err != nil || resp.StatusCode != http.StatusOK {
statusCode := -1
var errorMessage string
if resp != nil {
statusCode = resp.StatusCode
errorMessage = resp.ErrorMessage
}
return fmt.Errorf("failed to refresh token: (%d: %s) %v", statusCode, errorMessage, err)
}
c.mu.Lock()
c.opts.UserAccessToken = resp.Data.AccessToken
c.opts.RefreshToken = resp.Data.RefreshToken
c.mu.Unlock()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Contributor
Author
|
Reviewed Copilot suggestions and fixed them. |
Pull Request Test Coverage Report for Build 19297569553Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added two new methods for generating DCF tokens, also tests and docs for them:
RequestDeviceVerificationURI()RequestDeviceAccessToken()New structs for requests/responses as well.
New methods
SetDeviceAccessToken()/GetDeviceAccessToken()forDeviceAccessTokeninOptions.Also tests for them too.
UserAccessTokenstill prioritized in headers overDeviceAccessToken, which is also prioritized overAppAccessToken.Argued against adding new method for refreshing DCF tokens, when existing
RefreshTokenalready works, as it doesn't return an error for missingClientSecret. Still open for consideration. Also updatedcanRefreshToken()to support both ACF and DCF at the same time.