Debian Codegraph @

docs/headless/token-strategies/session-tokens.rst

raw
1 Session Tokens
2 ==============
3
4 Introduction
5 ------------
6
7 When using allauth headless in non-browser contexts, such as mobile apps, a
8 session token is used to keep track of the authentication state. This session
9 token is handed over by the app by providing the ``X-Session-Token`` request
10 header.
11
12 If you do not have any requirements that prescribe a specific token strategy,
13 the simplest way forward is to use the ``X-Session-Token`` authentication
14 mechanism for your own APIs as well. In order to do so, integration with Django
15 Ninja and Django REST framework is offered out of the box.
16
17
18 Securing Your API Endpoints
19 ---------------------------
20
21 Django Ninja
22 ~~~~~~~~~~~~
23
24 For Django Ninja, the following security class is available:
25
26 .. autoclass:: allauth.headless.contrib.ninja.security.XSessionTokenAuth
27 :members:
28
29 An example on how to use that security class in your own code is listed below:
30
31 .. code-block:: python
32
33 from allauth.headless.contrib.ninja.security import x_session_token_auth
34 from ninja import NinjaAPI
35
36 api = NinjaAPI()
37
38 @api.get("/your/own/api", auth=[x_session_token_auth])
39 def your_own_api(request):
40 ...
41
42
43 Django REST framework
44 ~~~~~~~~~~~~~~~~~~~~~
45
46 For Django REST framework, the following authentication class is available:
47
48 .. autoclass:: allauth.headless.contrib.rest_framework.authentication.XSessionTokenAuthentication
49 :members:
50
51 An example on how to use that authentication class in your own code is listed below:
52
53 .. code-block:: python
54
55 from allauth.headless.contrib.rest_framework.authentication import (
56 XSessionTokenAuthentication,
57 )
58 from rest_framework import permissions
59 from rest_framework.views import APIView
60
61 class YourOwnAPIView(APIView):
62
63 authentication_classes = [
64 XSessionTokenAuthentication,
65 ]
66 permission_classes = [permissions.IsAuthenticated]
67
68 def get(self, request):
69 ...