Skip to content

Commit ee9497f

Browse files
committed
Add tutotial doc
1 parent 7ff0d9b commit ee9497f

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed
Loading

docs/_images/device-approve-deny.png

16.5 KB
Loading
18.1 KB
Loading

docs/tutorial/tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Tutorials
99
tutorial_03
1010
tutorial_04
1111
tutorial_05
12-
12+
tutorial_06

docs/tutorial/tutorial_06.rst

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
Device authorization grant flow
2+
====================================================
3+
4+
Scenario
5+
--------
6+
In :doc:`Part 1 <tutorial_01>` you created your own :term:`Authorization Server` and it's running along just fine.
7+
You have devices that your users have, and those users need to authenticate the device against your
8+
:term:`Authorization Server` in order to make the required API calls.
9+
10+
Device Authorization
11+
--------------------
12+
The OAuth 2.0 device authorization grant is designed for Internet
13+
connected devices that either lack a browser to perform a user-agent
14+
based authorization or are input-constrained to the extent that
15+
requiring the user to input text in order to authenticate during the
16+
authorization flow is impractical. It enables OAuth clients on such
17+
devices (like smart TVs, media consoles, digital picture frames, and
18+
printers) to obtain user authorization to access protected resources
19+
by using a user agent on a separate device.
20+
21+
Point your browser to `http://127.0.0.1:8000/o/applications/register/` to create an application.
22+
23+
Fill the form as shown in the screenshot below, and before saving, take note of the ``Client id``.
24+
Make sure the client type is set to "Public." There are cases where a confidential client makes sense,
25+
but generally, it is assumed the device is unable to safely store the client secret.
26+
27+
.. image:: _images/application-register-device-code.png
28+
:alt: Device Authorization application registration
29+
30+
Ensure the setting ``OAUTH_DEVICE_VERIFICATION_URI`` is set to a URI you want to return in the
31+
`verification_uri` key in the response. This is what the device will display to the user.
32+
33+
1: Navigate to the tests/app/idp directory:
34+
35+
.. code-block:: sh
36+
37+
cd tests/app/idp
38+
39+
then start the server
40+
.. code-block:: sh
41+
42+
python manage.py runserver
43+
44+
To initiate device authorization, send this request:
45+
46+
.. code-block:: sh
47+
48+
curl --location 'http://127.0.0.1:8000/o/device-authorization/' \
49+
--header 'Content-Type: application/x-www-form-urlencoded' \
50+
--data-urlencode 'client_id={your application\'s client id}'
51+
52+
The OAuth2 provider will return the following response:
53+
54+
.. code-block:: json
55+
56+
{
57+
"verification_uri": "http://127.0.0.1:8000/o/device",
58+
"expires_in": 1800,
59+
"user_code": "A32RVADM",
60+
"device_code": "G30j94v0kNfipD4KmGLTWeL4eZnKHm",
61+
"interval": 5
62+
}
63+
64+
Go to `http://127.0.0.1:8000/o/device` in your browser.
65+
66+
.. image:: _images/device-enter-code-displayed.png
67+
68+
Enter the code, and it will redirect you to the device-confirm endpoint.
69+
70+
Device-confirm endpoint
71+
-----------------------
72+
Device polling occurs concurrently while the user approves or denies the request.
73+
74+
.. image:: _images/device-approve-deny.png
75+
76+
Device polling
77+
--------------
78+
Note: You should already have the `/token` endpoint implemented in your authorization server before this.
79+
80+
Send the following request (in the real world, the device makes this request):
81+
82+
.. code-block:: sh
83+
84+
curl --location 'http://localhost:8000/o/token/' \
85+
--header 'Content-Type: application/x-www-form-urlencoded' \
86+
--data-urlencode 'device_code={the device code from the device-authorization response}' \
87+
--data-urlencode 'client_id={your application\'s client id}' \
88+
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:device_code'
89+
90+
The response will be similar to this:
91+
92+
.. code-block:: json
93+
94+
{
95+
"access_token": "SkJMgyL432P04nHDPyB63DEAM0nVxk",
96+
"expires_in": 36000,
97+
"token_type": "Bearer",
98+
"scope": "openid",
99+
"refresh_token": "Go6VumurDfFAeCeKrpCKPDtElV77id"
100+
}

0 commit comments

Comments
 (0)