|
| 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