-
Notifications
You must be signed in to change notification settings - Fork 92
feat: Add support for device code grant #229
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
Open
SimonVanacco
wants to merge
3
commits into
thephpleague:master
Choose a base branch
from
SimonVanacco:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Device grant handling | ||
|
||
The device code grant type is designed for devices without a browser or with limited input capabilities. In this flow, the user authenticates on another device—like a smartphone or computer—and receives a code to enter on the original device. | ||
|
||
Initially, the device sends a request to /device-code with its client ID and scope. The server then returns a device code, a user code, and a verification URL. The user takes the code to a secondary device, opens the verification URL in a browser, and enters the user code. | ||
|
||
Meanwhile, the original device continuously polls the /token endpoint with the device code. Once the user approves the request on the secondary device, the token endpoint returns the access token to the polling device. | ||
|
||
## Requirements | ||
|
||
You need to implement the verification URL yourself and handle the user code input : this bundle does not provide a route or UI for this. | ||
|
||
## Example | ||
|
||
### Controller | ||
|
||
This is a sample Symfony 7 controller to handle the user code input | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use League\Bundle\OAuth2ServerBundle\Repository\DeviceCodeRepository; | ||
use League\OAuth2\Server\Exception\OAuthServerException; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
public function __construct( | ||
private readonly DeviceCodeRepository $deviceCodeRepository | ||
) { | ||
} | ||
|
||
#[Route(path: '/verify-device', name: 'app_verify_device', methods: ['GET', 'POST'])] | ||
public function verifyDevice( | ||
Request $request | ||
): Response { | ||
$form = $this->createFormBuilder() | ||
->add('userCode', TextType::class, [ | ||
'required' => true, | ||
]) | ||
->getForm() | ||
->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
try { | ||
$this->deviceCodeRepository->approveDeviceCode($form->get('userCode')->getData(), $this->getUser()->getId()); | ||
// Device code approved, show success message to user | ||
} catch (OAuthServerException $e) { | ||
// Handle exception (invalid code or missing user ID) | ||
} | ||
} | ||
|
||
// Render the form to the user | ||
} | ||
``` | ||
|
||
### Configuration | ||
|
||
```yaml | ||
league_oauth2_server: | ||
authorization_server: | ||
device_code_verification_uri: 'https://your-domain.com/verify-device' | ||
``` |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.