-
Notifications
You must be signed in to change notification settings - Fork 819
Add device authorization grant (device code flow - rfc 8628) #1539
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
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
2cf0166
Add Device model
duzumaki b601644
Adhere content-type request header to CGI standard
duzumaki 5a9b0ab
Add create device authorization response method
duzumaki ce76412
Update the grant type mapping to recognize device code
duzumaki d08be7e
Devices that are public should not need basic auth
duzumaki c5cb856
Add device settings
duzumaki d99ae78
Create device authorization view
duzumaki ac845ea
Ensure we import in the views module
duzumaki 0c43b33
Migrations
duzumaki bb23fb1
Add initial stage test
duzumaki b5043ae
Temp commit: Point oauthlib to master
duzumaki 4062b90
Add device poll change test
duzumaki 5f2d699
Add incorrect client id test
duzumaki 0145e43
Update authors and changelog
duzumaki f9c7f4f
Add the device user code form
duzumaki 5b5f2fd
Add approve deny form
duzumaki 63aeeb9
Update request body validator
duzumaki 0a04c3d
Update device imports
duzumaki 12416a4
Update token endpoint
duzumaki 3230a8e
Prep the tests: Create user_code_generator util
duzumaki 73b6907
Add user code generator to main settings
duzumaki dedc1b5
Add tests to test the whole flow
duzumaki 29a0ea5
Update idp requirements
duzumaki a0121b5
Add tutotial doc
duzumaki 6f7e02e
Update idp requirements
duzumaki 29bcfc9
Ensure the django user sets the oauthlib request user
duzumaki ccb780e
Ensure device token errors are returning 400
duzumaki ad4c187
Add device code to refresh token mapping
duzumaki 80c5160
chore: address documentation notes
dopry deac1fe
chore: add more details to the tutorial docs
cristiprg 9b117af
fix: addressed comments; enhanced preconditions and responses of views
cristiprg 22f149c
Use term DeviceGrant over Device
cristiprg b968a34
test: add missing unit test for user_code_generator
cristiprg 84dfbd5
Use latest oauthlib 3.3.0
cristiprg 0db05f8
Use class based views to make them more easily extensible
cristiprg 9e9390b
Bugfix: handle missing client_id
cristiprg 4ecbd91
Increase test coverage
cristiprg 213d772
Add missing test case in the validor class
cristiprg 6ab2722
Remove serialisation methods of DeviceGrant class
cristiprg 984fd72
Add tests that cover DeviceConfirmView for various values for action
cristiprg aec2211
Add instructions in the test IDP for device authorization flow
cristiprg 47a35bd
chore: add a device code demo to the tests/app/RP
dopry 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
|---|---|---|
|
|
@@ -9,4 +9,4 @@ Tutorials | |
| tutorial_03 | ||
| tutorial_04 | ||
| tutorial_05 | ||
|
|
||
| tutorial_06 | ||
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,126 @@ | ||
| Part 6 - Device authorization grant flow | ||
| ==================================================== | ||
|
|
||
| Scenario | ||
| -------- | ||
| In :doc:`Part 1 <tutorial_01>` you created your own :term:`Authorization Server` and it's running along just fine. | ||
| You have devices that your users have, and those users need to authenticate the device against your | ||
| :term:`Authorization Server` in order to make the required API calls. | ||
|
|
||
| Device Authorization | ||
| -------------------- | ||
| The OAuth 2.0 device authorization grant is designed for Internet | ||
| connected devices that either lack a browser to perform a user-agent | ||
| based authorization or are input-constrained to the extent that | ||
| requiring the user to input text in order to authenticate during the | ||
| authorization flow is impractical. It enables OAuth clients on such | ||
| devices (like smart TVs, media consoles, digital picture frames, and | ||
| printers) to obtain user authorization to access protected resources | ||
| by using a user agent on a separate device. | ||
|
|
||
| Point your browser to `http://127.0.0.1:8000/o/applications/register/` to create an application. | ||
|
|
||
| Fill the form as shown in the screenshot below, and before saving, take note of the ``Client id``. | ||
| Make sure the client type is set to "Public." There are cases where a confidential client makes sense, | ||
| but generally, it is assumed the device is unable to safely store the client secret. | ||
|
|
||
| .. image:: ../_images/application-register-device-code.png | ||
| :alt: Device Authorization application registration | ||
|
|
||
| Ensure the setting ``OAUTH_DEVICE_VERIFICATION_URI`` is set to a URI you want to return in the | ||
| `verification_uri` key in the response. This is what the device will display to the user. | ||
|
|
||
| 1. Navigate to the tests/app/idp directory: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| cd tests/app/idp | ||
dopry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| then start the server | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| python manage.py runserver | ||
|
|
||
| .. _RFC: https://www.rfc-editor.org/rfc/rfc8628 | ||
| .. _RFC section 3.5: https://datatracker.ietf.org/doc/html/rfc8628#section-3.5 | ||
|
|
||
| 2. To initiate device authorization, send this request (in the real world, the device | ||
| makes this request). In `RFC`_ Figure 1, this is step (A). | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| curl --location 'http://127.0.0.1:8000/o/device-authorization/' \ | ||
| --header 'Content-Type: application/x-www-form-urlencoded' \ | ||
| --data-urlencode 'client_id={your application client id}' | ||
|
|
||
| The OAuth2 provider will return the following response. In `RFC`_ Figure 1, this is step (B). | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| { | ||
| "verification_uri": "http://127.0.0.1:8000/o/device", | ||
| "expires_in": 1800, | ||
| "user_code": "A32RVADM", | ||
| "device_code": "G30j94v0kNfipD4KmGLTWeL4eZnKHm", | ||
| "interval": 5 | ||
| } | ||
|
|
||
| In the real world, the device will somehow make the value of the `user_code` available to the user (either on-screen display, | ||
| or Bluetooth, NFC, etc.). In `RFC`_ Figure 1, this is step (C). | ||
|
|
||
| 3. Go to `http://127.0.0.1:8000/o/device` in your browser. | ||
|
|
||
| .. image:: ../_images/device-enter-code-displayed.png | ||
|
|
||
| Enter the code, and it will redirect you to the device-confirm endpoint. In `RFC`_ Figure 1, this is step (D). | ||
|
|
||
| Device-confirm endpoint | ||
| ----------------------- | ||
| 4. Device polling occurs concurrently while the user approves or denies the request. | ||
|
|
||
| .. image:: ../_images/device-approve-deny.png | ||
|
|
||
| Device polling | ||
| -------------- | ||
| Send the following request (in the real world, the device makes this request). In `RFC`_ Figure 1, this is step (E). | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| curl --location 'http://localhost:8000/o/token/' \ | ||
| --header 'Content-Type: application/x-www-form-urlencoded' \ | ||
| --data-urlencode 'device_code={the device code from the device-authorization response}' \ | ||
| --data-urlencode 'client_id={your application client id}' \ | ||
| --data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:device_code' | ||
|
|
||
| In `RFC`_ Figure 1, there are multiple options for step (F), as per `RFC section 3.5`_. Until the user enters the code | ||
| in the browser and approves, the response will be 400: | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| {"error": "authorization_pending"} | ||
|
|
||
| Or if the user has denied the device, the response is 400: | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| {"error": "access_denied"} | ||
|
|
||
| Or if the token has expired, the response is 400: | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| {"error": "expired_token"} | ||
|
|
||
|
|
||
| However, after the user approves, the response will be 200: | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| { | ||
| "access_token": "SkJMgyL432P04nHDPyB63DEAM0nVxk", | ||
| "expires_in": 36000, | ||
| "token_type": "Bearer", | ||
| "scope": "openid", | ||
| "refresh_token": "Go6VumurDfFAeCeKrpCKPDtElV77id" | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
oauth2_provider/migrations/0013_alter_application_authorization_grant_type_device.py
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,41 @@ | ||
| # Generated by Django 5.1.5 on 2025-01-24 14:00 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('oauth2_provider', '0012_add_token_checksum'), | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='application', | ||
| name='authorization_grant_type', | ||
| field=models.CharField(choices=[('authorization-code', 'Authorization code'), ('urn:ietf:params:oauth:grant-type:device_code', 'Device Code'), ('implicit', 'Implicit'), ('password', 'Resource owner password-based'), ('client-credentials', 'Client credentials'), ('openid-hybrid', 'OpenID connect hybrid')], max_length=44), | ||
| ), | ||
| migrations.CreateModel( | ||
| name='DeviceGrant', | ||
| fields=[ | ||
| ('id', models.BigAutoField(primary_key=True, serialize=False)), | ||
| ('device_code', models.CharField(max_length=100, unique=True)), | ||
| ('user_code', models.CharField(max_length=100)), | ||
| ('scope', models.CharField(max_length=64, null=True)), | ||
| ('interval', models.IntegerField(default=5)), | ||
| ('expires', models.DateTimeField()), | ||
| ('status', models.CharField(blank=True, choices=[('authorized', 'Authorized'), ('authorization-pending', 'Authorization pending'), ('expired', 'Expired'), ('denied', 'Denied')], default='authorization-pending', max_length=64)), | ||
| ('client_id', models.CharField(db_index=True, max_length=100)), | ||
| ('last_checked', models.DateTimeField(auto_now=True)), | ||
| ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s', to=settings.AUTH_USER_MODEL)), | ||
| ], | ||
| options={ | ||
| 'abstract': False, | ||
| 'swappable': 'OAUTH2_PROVIDER_DEVICE_GRANT_MODEL', | ||
| 'constraints': [models.UniqueConstraint(fields=('device_code',), name='oauth2_provider_devicegrant_unique_device_code')], | ||
| }, | ||
| ), | ||
| ] |
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.
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.