|
| 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 | +To initiate device authorization, send this request: |
| 40 | + |
| 41 | +.. code-block:: sh |
| 42 | +
|
| 43 | + curl --location 'http://127.0.0.1:8000/o/device-authorization/' \ |
| 44 | + --header 'Content-Type: application/x-www-form-urlencoded' \ |
| 45 | + --data-urlencode 'client_id={your application\'s client id}' |
| 46 | +
|
| 47 | +The OAuth2 provider will return the following response: |
| 48 | +
|
| 49 | +.. code-block:: json |
| 50 | +
|
| 51 | + { |
| 52 | + "verification_uri": "http://127.0.0.1:8000/o/device", |
| 53 | + "expires_in": 1800, |
| 54 | + "user_code": "A32RVADM", |
| 55 | + "device_code": "G30j94v0kNfipD4KmGLTWeL4eZnKHm", |
| 56 | + "interval": 5 |
| 57 | + } |
| 58 | +
|
| 59 | +Go to `http://127.0.0.1:8000/o/device` in your browser. |
| 60 | +
|
| 61 | +.. image:: _images/device-enter-code-displayed.png |
| 62 | +
|
| 63 | +Enter the code, and it will redirect you to the device-confirm endpoint. |
| 64 | +
|
| 65 | +Device-confirm endpoint |
| 66 | +----------------------- |
| 67 | +Device polling occurs concurrently while the user approves or denies the request. |
| 68 | +
|
| 69 | +.. image:: _images/device-approve-deny.png |
| 70 | +
|
| 71 | +Device polling |
| 72 | +-------------- |
| 73 | +Note: You should already have the `/token` endpoint implemented in your authorization server before this. |
| 74 | +
|
| 75 | +Send the following request (in the real world, the device makes this request): |
| 76 | +
|
| 77 | +.. code-block:: sh |
| 78 | +
|
| 79 | + curl --location 'http://localhost:8000/o/token/' \ |
| 80 | + --header 'Content-Type: application/x-www-form-urlencoded' \ |
| 81 | + --data-urlencode 'device_code={the device code from the device-authorization response}' \ |
| 82 | + --data-urlencode 'client_id={your application\'s client id}' \ |
| 83 | + --data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:device_code' |
| 84 | +
|
| 85 | +The response will be similar to this: |
| 86 | +
|
| 87 | +.. code-block:: json |
| 88 | +
|
| 89 | + { |
| 90 | + "access_token": "SkJMgyL432P04nHDPyB63DEAM0nVxk", |
| 91 | + "expires_in": 36000, |
| 92 | + "token_type": "Bearer", |
| 93 | + "scope": "openid", |
| 94 | + "refresh_token": "Go6VumurDfFAeCeKrpCKPDtElV77id" |
| 95 | + } |
0 commit comments