| 1 |
Integrations |
| 2 |
============ |
| 3 |
|
| 4 |
In order to simplify implementing your own OIDC based API endpoints, allauth |
| 5 |
offers out of the box support for authenticating and authorizing requests with |
| 6 |
Django Ninja as well as Django REST framework. |
| 7 |
|
| 8 |
|
| 9 |
Django Ninja |
| 10 |
------------ |
| 11 |
|
| 12 |
For Django Ninja, the following security class is available: |
| 13 |
|
| 14 |
.. autoclass:: allauth.idp.oidc.contrib.ninja.security.TokenAuth |
| 15 |
:special-members: __init__ |
| 16 |
:members: |
| 17 |
|
| 18 |
An example on how to use that security class in your own code is listed below: |
| 19 |
|
| 20 |
.. code-block:: python |
| 21 |
|
| 22 |
from allauth.idp.oidc.contrib.ninja.security import TokenAuth |
| 23 |
from ninja import NinjaAPI |
| 24 |
|
| 25 |
api = NinjaAPI() |
| 26 |
|
| 27 |
@api.get("/api/resource", auth=[TokenAuth(scope=["view-resource"])]) |
| 28 |
def resource(request): |
| 29 |
... |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
Django REST framework |
| 35 |
--------------------- |
| 36 |
|
| 37 |
For Django REST framework, the following authentication class is available: |
| 38 |
|
| 39 |
.. autoclass:: allauth.idp.oidc.contrib.rest_framework.authentication.TokenAuthentication |
| 40 |
|
| 41 |
.. autoclass:: allauth.idp.oidc.contrib.rest_framework.permissions.TokenPermission |
| 42 |
:members: has_scope |
| 43 |
|
| 44 |
An example on how to use that authentication class in your own code is listed below: |
| 45 |
|
| 46 |
.. code-block:: python |
| 47 |
|
| 48 |
from rest_framework.views import APIView |
| 49 |
|
| 50 |
from allauth.idp.oidc.contrib.rest_framework.authentication import TokenAuthentication |
| 51 |
from allauth.idp.oidc.contrib.rest_framework.permissions import TokenPermission |
| 52 |
|
| 53 |
|
| 54 |
class ResourceView(APIView): |
| 55 |
authentication_classes = [TokenAuthentication] |
| 56 |
permission_classes = [TokenPermission.has_scope(["view-resource"])] |
| 57 |
|
| 58 |
def get(request, *args, **kwargs): |
| 59 |
... |