Debian Codegraph @

allauth/headless/tokens/strategies/base.py

raw
1 import abc
2 from typing import Any
3
4 from django.contrib.sessions.backends.base import SessionBase
5 from django.http import HttpRequest
6
7
8 class AbstractTokenStrategy(abc.ABC):
9 def get_session_token(self, request: HttpRequest) -> str | None:
10 """
11 Returns the session token, if any.
12 """
13 token = request.headers.get("x-session-token")
14 return token
15
16 def create_access_token_payload(
17 self, request: HttpRequest
18 ) -> dict[str, Any] | None:
19 """
20 After authenticating, this method is called to create the access
21 token response payload, exposing the access token and possibly other
22 information such as a ``refresh_token`` and ``expires_in``.
23 """
24 at = self.create_access_token(request)
25 if not at:
26 return None
27 return {"access_token": at}
28
29 def create_access_token(self, request: HttpRequest) -> str | None:
30 """Create an access token.
31
32 While session tokens are required to handle the authentication process,
33 depending on your requirements, a different type of token may be needed
34 once authenticated.
35
36 For example, your app likely needs access to other APIs as well. These
37 APIs may even be implemented using different technologies, in which case
38 having a stateless token, possibly a JWT encoding the user ID, might be
39 a good fit.
40
41 We make no assumptions in this regard. If you need access tokens, you
42 will have to implement a token strategy that returns an access token
43 here.
44 """
45 return None
46
47 @abc.abstractmethod
48 def create_session_token(self, request: HttpRequest) -> str:
49 """
50 Create a session token for the `request.session`.
51 """
52 ...
53
54 @abc.abstractmethod
55 def lookup_session(self, session_token: str) -> SessionBase | None:
56 """
57 Looks up the Django session given the session token. Returns `None`
58 if the session does not / no longer exist.
59 """
60 ...
61
62 def refresh_token(self, refresh_token: str) -> tuple[str, str] | None:
63 """
64 Validates the given refresh token, and if valid, returns a new
65 access token and refresh token pair.
66 """
67 return None