feat: add Device Authorization Flow to the Authentication API - #794
Open
gagalago wants to merge 1 commit into
Open
feat: add Device Authorization Flow to the Authentication API#794gagalago wants to merge 1 commit into
gagalago wants to merge 1 commit into
Conversation
5 tasks
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.
Changes
Implements the Device Authorization Flow in
Auth0::Api::AuthenticationEndpoints. This is the proposal from #792, opened as a pull request per the general contributing guidelines ("PRs to our libraries are always welcome"); happy to close it if you would rather settle the design on the issue first.Two new methods, additive only — no existing method, endpoint or signature changes:
start_device_flow(scope:, audience:, client_id:)→POST /oauth/device/code, returning the parsed body (device_code,user_code,verification_uri,verification_uri_complete,expires_in,interval).exchange_device_code_for_tokens(device_code, client_id:)→POST /oauth/tokenwithgrant_type=urn:ietf:params:oauth:grant-type:device_code, returning anAuth0::AccessToken.They are named after the existing
start_passwordless_sms_flow/exchange_sms_otp_for_tokenspair so they read like their neighbours, and they follow the conventions already in the file: anAuth0::InvalidParameterguard on the required positional argument, arequest_paramshash,request_with_retry, and::Auth0::AccessToken.from_responsefor the token response.Note this is unrelated to
Auth0::Api::V2::DeviceCredentials, which is the Management API device-credentials resource.No new exception classes. While the user completes their part, Auth0 answers with an HTTP error whose body carries an
errorofauthorization_pendingorslow_down.Mixins::HTTPProxy#requestalready raisesAuth0::HTTPErrorsubclasses with the raw response body as the message —AccessDeniedfor 403 andBadRequestfor 400, bothHTTPError— so callers can distinguish the polling states with what is already there. A documented accessor for thaterrorcode would be a good follow-up, but it is deliberately not in this pull request.References
For prior art inside the SDK family, Auth0.NET exposes the same two operations as
StartDeviceFlowAsync(DeviceCodeRequest)andGetTokenAsync(DeviceCodeTokenRequest)onIAuthenticationApiClient.Motivation: we run a small Rails service that acts as an OAuth proxy for our command-line client. It starts the flow, hands
user_codeandverification_urito the CLI, and polls for the token. With this in place we can delete the hand-written Faraday client that exists only because the flow is not reachable from this SDK.Testing
Three cases added to
test/unit/authentication_endpoints_test.rb, alongside the existing ones and using the same WebMock body-matching style:test_start_device_flow_requests_a_device_code— assertsclient_id,scopeandaudienceare sent to/oauth/device/code, and that the response fields come backtest_exchange_device_code_for_tokens— asserts theurn:ietf:params:oauth:grant-type:device_codegrant type anddevice_codeare sent, and that anAuth0::AccessTokenis returnedtest_exchange_device_code_for_tokens_raises_on_empty— theInvalidParameterguardI checked the tests are not vacuous: pointing
start_device_flowat a different path makes its test fail, and restoring it makes it pass again.555 runs, 4585 assertions, 0 failures, 0 errors555 runs, 0 failures, 1 error— the pre-existingCGI.parsefailure on master, unrelated to this change and fixed by #793The three new tests pass on both (
3 runs, 7 assertions, 0 failures).bundle exec rubocop --force-exclusionreports no offences on the changed files. As with the rest ofauthentication_endpoints.rb, I followed the file's single-quoted style — it is listed underAllCops.Excludein.rubocop.ymlas ported verbatim from the legacy SDK.Documentation
Added a Device Authorization Flow section to
EXAMPLES.md, showing the full loop: start the flow, display the user code, poll on the returnedinterval, and treatauthorization_pending/slow_downas continue-conditions.Checklist