generated from rayluo/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authorization: Web API protected by access token
- Loading branch information
Showing
12 changed files
with
507 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
.. note:: | ||
|
||
Web Application (a.k.a. website) and Web API are different, | ||
and are supported by different Identity components. | ||
Make sure you are using the right component for your scenario. | ||
|
||
+-------------------------+---------------------------------------------------+-------------------------------------------------------+ | ||
| Aspects | Web Application (a.k.a. website) | Web API | | ||
+=========================+===================================================+=======================================================+ | ||
| **Definition** | A complete solution that users interact with | A back-end system that provides data (typically in | | ||
| | directly through their browsers. | JSON format) to front-end or other system. | | ||
+-------------------------+---------------------------------------------------+-------------------------------------------------------+ | ||
| **Functionality** | - Users interact with views (HTML user interfaces)| - Does not return views (in HTML); only provides data.| | ||
| | and data. | - Other systems (clients) hit its endpoints. | | ||
| | - Users sign in and establish their sessions. | - Clients presents a token to access your API. | | ||
| | | - Each request has no session. They are stateless. | | ||
+-------------------------+---------------------------------------------------+-------------------------------------------------------+ | ||
|
This file contains 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,77 @@ | ||
Identity for a Django Web API | ||
============================= | ||
|
||
.. include:: app-vs-api.rst | ||
|
||
Prerequisite | ||
------------ | ||
|
||
Create a hello world web project in Django. | ||
|
||
You can use | ||
`Django's own tutorial, part 1 <https://docs.djangoproject.com/en/5.0/intro/tutorial01/>`_ | ||
as a reference. What we need are basically these steps: | ||
|
||
#. ``django-admin startproject mysite`` | ||
#. ``python manage.py migrate`` (Optinoal if your project does not use a database) | ||
#. ``python manage.py runserver localhost:5000`` | ||
|
||
#. Now, add a new `mysite/views.py` file with an `index` view to your project. | ||
For now, it can simply return a "hello world" page to any visitor:: | ||
|
||
from django.http import JsonResponse | ||
def index(request): | ||
return JsonResponse({"message": "Hello, world!"}) | ||
|
||
Configuration | ||
------------- | ||
|
||
#. Install dependency by ``pip install identity[django]`` | ||
|
||
#. Create an instance of the :py:class:`identity.django.Auth` object, | ||
and assign it to a global variable inside your ``settings.py``:: | ||
|
||
import os | ||
from identity.django import Auth | ||
AUTH = Auth( | ||
client_id=os.getenv('CLIENT_ID'), | ||
...=..., # See below on how to feed in the authority url parameter | ||
) | ||
|
||
.. include:: auth.rst | ||
|
||
|
||
Django Web API protected by an access token | ||
------------------------------------------- | ||
|
||
#. In your web project's ``views.py``, decorate some views with the | ||
:py:func:`identity.django.ApiAuth.authorization_required` decorator:: | ||
|
||
from django.conf import settings | ||
|
||
@settings.AUTH.authorization_required(expected_scopes={ | ||
"your_scope_1": "api://your_client_id/your_scope_1", | ||
"your_scope_2": "api://your_client_id/your_scope_2", | ||
}) | ||
def index(request, *, context): | ||
claims = context['claims'] | ||
# The user is uniquely identified by claims['sub'] or claims["oid"], | ||
# claims['tid'] and/or claims['iss']. | ||
return JsonResponse( | ||
{"message": f"Data for {claims['sub']}@{claims['tid']}"} | ||
) | ||
|
||
|
||
All of the content above are demonstrated in | ||
`this django web app sample <https://github.com/Azure-Samples/ms-identity-python-webapi-django>`_. | ||
|
||
|
||
API for Django web projects | ||
--------------------------- | ||
|
||
.. autoclass:: identity.django.ApiAuth | ||
:members: | ||
:inherited-members: | ||
|
||
.. automethod:: __init__ | ||
|
This file contains 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 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,67 @@ | ||
Identity for a Flask Web API | ||
============================ | ||
|
||
.. include:: app-vs-api.rst | ||
|
||
Prerequisite | ||
------------ | ||
|
||
Create `a hello world web project in Flask <https://flask.palletsprojects.com/en/3.0.x/quickstart/#a-minimal-application>`_. | ||
Here we assume the project's main file is named ``app.py``. | ||
|
||
|
||
Configuration | ||
------------- | ||
|
||
#. Install dependency by ``pip install identity[flask]`` | ||
|
||
#. Create an instance of the :py:class:`identity.Flask.ApiAuth` object, | ||
and assign it to a global variable inside your ``app.py``:: | ||
|
||
import os | ||
from flask import Flask | ||
from identity.flask import ApiAuth | ||
|
||
app = Flask(__name__) | ||
auth = ApiAuth( | ||
client_id=os.getenv('CLIENT_ID'), | ||
...=..., # See below on how to feed in the authority url parameter | ||
) | ||
|
||
.. include:: auth.rst | ||
|
||
|
||
Flask Web API protected by an access token | ||
------------------------------------------ | ||
|
||
#. In your web project's ``app.py``, decorate some views with the | ||
:py:func:`identity.flask.ApiAuth.authorization_required` decorator. | ||
It will automatically put validated token claims into the ``context`` dictionary, | ||
under the key ``claims``. | ||
or emit an HTTP 401 or 403 response if the token is missing or invalid. | ||
|
||
:: | ||
|
||
@app.route("/") | ||
@auth.authorization_required(expected_scopes={ | ||
"your_scope_1": "api://your_client_id/your_scope_1", | ||
"your_scope_2": "api://your_client_id/your_scope_2", | ||
}) | ||
def index(*, context): | ||
claims = context['claims'] | ||
# The user is uniquely identified by claims['sub'] or claims["oid"], | ||
# claims['tid'] and/or claims['iss']. | ||
return {"message": f"Data for {claims['sub']}@{claims['tid']}"} | ||
|
||
All of the content above are demonstrated in | ||
`this Flask web API sample <https://github.com/Azure-Samples/ms-identity-python-webapi-flask>`_. | ||
|
||
API for Flask web API projects | ||
------------------------------ | ||
|
||
.. autoclass:: identity.flask.ApiAuth | ||
:members: | ||
:inherited-members: | ||
|
||
.. automethod:: __init__ | ||
|
This file contains 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 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 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 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.