-
Notifications
You must be signed in to change notification settings - Fork 789
[13.x] Device Authorization Grant RFC8628 #1750
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
Merged
taylorotwell
merged 64 commits into
laravel:13.x
from
hafezdivandari:13.x-device-code-grant
Mar 7, 2025
Merged
[13.x] Device Authorization Grant RFC8628 #1750
taylorotwell
merged 64 commits into
laravel:13.x
from
hafezdivandari:13.x-device-code-grant
Mar 7, 2025
+1,135
−12
Conversation
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
|
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
This was referenced Jul 29, 2024
2 tasks
Contributor
Author
|
Hi @taylorotwell this is ready for review. |
…ssport into 13.x-device-code-grant
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.
This PR adds support for Device Authorization Grant RFC8628.
Additions
Grant type
urn:ietf:params:oauth:grant-type:device_codeORM
oauth_device_codestable.Laravel\Passport\DeviceCodemodel.Passport::useDeviceCodeModel()Routes
POST /device/code: Request device code / user code.GET /device: Display a form for entering the user code.GET /device/authorize: Display authorization consent.POST /device/authorize: Approve authorization.DELETE /device/authorize: Deny authorization.Views
Passport::deviceAuthorizationView()(Jestream / Inertia sample) (Jestream / Livewire sample)Passport::deviceUserCodeView()(Jestream / Inertia sample) (Jestream / Livewire sample)Commands
--deviceoption onpassport:clientcommand.passport:clientcommand.passport:purgecommand.Device Authorization Grant
The OAuth2 device authorization grant allows browserless or limited input devices, such as TVs and game consoles, to obtain an access token by exchanging a "device code". When using device flow, the device client will instruct the user to use a secondary device, such as a computer or a smartphone and connect to your server where they will enter the provided "user code" and either approve or deny the access request.
To get started, we need to instruct Passport how to return our "user code" and "authorization" views. Remember, Passport is a headless OAuth2 library. If you would like a frontend implementation of Laravel's OAuth features that are already completed for you, you should use an application starter kit.
All the authorization view's rendering logic may be customized using the appropriate methods available via the
Laravel\Passport\Passportclass. Typically, you should call this method from thebootmethod of your application'sApp\Providers\AppServiceProviderclass. Passport will take care of defining the routes that returns these views:Creating a Device Code Grant Client
Before your application can issue tokens via the device authorization grant, you will need to create a device flow enabled client. You may do this using the
passport:clientArtisan command with the--deviceoption. This command will create a first-party device flow enabled client and provide you with a client ID and secret:Additionally, you may use
createDeviceAuthorizationGrantClientmethod on theClientRepositoryclass to register a third-party client that belongs to the given user:Requesting Tokens
Requesting Device Code
Once a client has been created, developers may use their client ID to request a device code from your application. First, the consuming device should make a
POSTrequest to your application's/oauth/device/coderoute to request a device code:This will return a JSON response containing
device_code,user_code,verification_uri,intervalandexpires_inattributes. Theexpires_inattribute contains the number of seconds until the device code expires. Theintervalattribute contains the number of seconds, the consuming device should wait between requests, when polling\oauth\tokenroute to avoid rate limit errors.Note
Remember, the
/oauth/device/coderoute is already defined by Passport. You do not need to manually define this route.Displaying Verification URI and User Code
Once a device code request has been obtained, the consuming device should instruct the user to use another device and visit the provided
verification_uriand enter theuser_codein order to review the authorization request.Polling Token Request
Since the user will be using a separate device to grant (or deny) access, the consuming device should poll your application's
/oauth/tokenroute to determine when the user has responded to the request. The consuming device should use the minimum pollingintervalprovided in the JSON response when requesting device code to avoid rate limit errors:If the user has approved the authorization request, this will return a JSON response containing
access_token,refresh_token, andexpires_inattributes. Theexpires_inattribute contains the number of seconds until the access token expires.