Skip to content

Commit a1a4621

Browse files
committed
Add docs for the Web API mode
1 parent e926c7f commit a1a4621

File tree

5 files changed

+101
-21
lines changed

5 files changed

+101
-21
lines changed

docs/app-vs-api.rst

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
.. note::
22

3-
Web Application (a.k.a. website) and Web API are different,
4-
and are supported by different Identity components.
5-
Make sure you are using the right component for your scenario.
3+
Web Application (a.k.a. website) and Web API are different scenarios,
4+
both supported by the same Identity Auth component with different decorators.
5+
Make sure you are using the right decorator for your scenario.
66

7-
+-------------------------+---------------------------------------------------+-------------------------------------------------------+
8-
| Aspects | Web Application (a.k.a. website) | Web API |
9-
+=========================+===================================================+=======================================================+
10-
| **Definition** | A complete solution that users interact with | A back-end system that provides data (typically in |
11-
| | directly through their browsers. | JSON format) to front-end or other system. |
12-
+-------------------------+---------------------------------------------------+-------------------------------------------------------+
13-
| **Functionality** | - Users interact with views (HTML user interfaces)| - Does not return views (in HTML); only provides data.|
14-
| | and data. | - Other systems (clients) hit its endpoints. |
15-
| | - Users sign in and establish their sessions. | - Clients presents a token to access your API. |
16-
| | | - Each request has no session. They are stateless. |
17-
+-------------------------+---------------------------------------------------+-------------------------------------------------------+
7+
.. list-table::
8+
:header-rows: 1
9+
:widths: 20 40 40
10+
11+
* - Aspects
12+
- Web Application (a.k.a. website)
13+
- Web API
14+
* - **Definition**
15+
- A complete solution that users interact with directly through their browsers.
16+
- A back-end system that provides data (typically in JSON format) to front-end or other system.
17+
* - **Functionality**
18+
- | - Users interact with views (HTML user interfaces) and data.
19+
| - Users sign in and establish their sessions.
20+
- | - Does not return views (in HTML); only provides data.
21+
| - Other systems (clients) hit its endpoints.
22+
| - Clients presents a token to access your API.
23+
| - Each request has no session. They are stateless.
24+
* - **Identity component**
25+
- Same ``Auth`` class
26+
- Same ``Auth`` class
27+
* - **Decorator to use**
28+
- ``@auth.login_required``
29+
- ``@auth.authorization_required``
1830

docs/django-webapi.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Django Web API protected by an access token
4545
-------------------------------------------
4646

4747
#. In your web project's ``views.py``, decorate some views with the
48-
:py:func:`identity.django.ApiAuth.authorization_required` decorator::
48+
:py:func:`identity.django.Auth.authorization_required` decorator::
4949

5050
from django.conf import settings
5151

@@ -69,7 +69,7 @@ All of the content above are demonstrated in
6969
API for Django web projects
7070
---------------------------
7171

72-
.. autoclass:: identity.django.ApiAuth
72+
.. autoclass:: identity.django.Auth
7373
:members:
7474
:inherited-members:
7575

docs/flask-webapi.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ Configuration
1515

1616
#. Install dependency by ``pip install identity[flask]``
1717

18-
#. Create an instance of the :py:class:`identity.Flask.ApiAuth` object,
18+
#. Create an instance of the :py:class:`identity.flask.Auth` object,
1919
and assign it to a global variable inside your ``app.py``::
2020

2121
import os
2222
from flask import Flask
23-
from identity.flask import ApiAuth
23+
from identity.flask import Auth
2424

2525
app = Flask(__name__)
26-
auth = ApiAuth(
26+
auth = Auth(
2727
client_id=os.getenv('CLIENT_ID'),
2828
...=..., # See below on how to feed in the authority url parameter
2929
)
@@ -35,7 +35,7 @@ Flask Web API protected by an access token
3535
------------------------------------------
3636

3737
#. In your web project's ``app.py``, decorate some views with the
38-
:py:func:`identity.flask.ApiAuth.authorization_required` decorator.
38+
:py:func:`identity.flask.Auth.authorization_required` decorator.
3939
It will automatically put validated token claims into the ``context`` dictionary,
4040
under the key ``claims``.
4141
or emit an HTTP 401 or 403 response if the token is missing or invalid.
@@ -59,7 +59,7 @@ All of the content above are demonstrated in
5959
API for Flask web API projects
6060
------------------------------
6161

62-
.. autoclass:: identity.flask.ApiAuth
62+
.. autoclass:: identity.flask.Auth
6363
:members:
6464
:inherited-members:
6565

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This Identity library is a Python authentication/authorization library that:
5151
flask
5252
flask-webapi
5353
quart
54+
quart-webapi
5455
abc
5556
generic
5657

docs/quart-webapi.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Identity for a Quart Web API
2+
==============================
3+
4+
.. include:: app-vs-api.rst
5+
6+
Prerequisite
7+
------------
8+
9+
Create `a hello world web project in Quart <https://quart.palletsprojects.com/en/latest/quick_start.html>`_.
10+
Here we assume the project's main file is named ``app.py``.
11+
12+
13+
Configuration
14+
-------------
15+
16+
#. Install dependency by ``pip install identity[quart]``
17+
18+
#. Create an instance of the :py:class:`identity.quart.Auth` object,
19+
and assign it to a global variable inside your ``app.py``::
20+
21+
import os
22+
from quart import Quart
23+
from identity.quart import Auth
24+
25+
app = Quart(__name__)
26+
auth = Auth(
27+
client_id=os.getenv('CLIENT_ID'),
28+
...=..., # See below on how to feed in the authority url parameter
29+
)
30+
31+
.. include:: auth.rst
32+
33+
34+
Quart Web API protected by an access token
35+
------------------------------------------
36+
37+
#. In your web project's ``app.py``, decorate some views with the
38+
:py:func:`identity.quart.Auth.authorization_required` decorator.
39+
It will automatically put validated token claims into the ``context`` dictionary,
40+
under the key ``claims``.
41+
or emit an HTTP 401 or 403 response if the token is missing or invalid.
42+
43+
::
44+
45+
@app.route("/")
46+
@auth.authorization_required(expected_scopes={
47+
"your_scope_1": "api://your_client_id/your_scope_1",
48+
"your_scope_2": "api://your_client_id/your_scope_2",
49+
})
50+
async def index(*, context):
51+
claims = context['claims']
52+
# The user is uniquely identified by claims['sub'] or claims["oid"],
53+
# claims['tid'] and/or claims['iss'].
54+
return {"message": f"Data for {claims['sub']}@{claims['tid']}"}
55+
56+
All of the content above follows the same pattern as
57+
`Flask web API sample <https://github.com/Azure-Samples/ms-identity-python-webapi-flask>`_
58+
but uses async/await syntax for Quart.
59+
60+
API for Quart web API projects
61+
------------------------------
62+
63+
.. autoclass:: identity.quart.Auth
64+
:members:
65+
:inherited-members:
66+
67+
.. automethod:: __init__

0 commit comments

Comments
 (0)