Skip to content

Commit

Permalink
feat(headless): Support for exposing refresh_token
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Jul 30, 2024
1 parent 2cc8f19 commit e60d1ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 3 additions & 3 deletions allauth/headless/internal/authkit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing
from contextlib import contextmanager
from typing import Any, Dict, Optional

from django.utils.functional import SimpleLazyObject, empty

Expand Down Expand Up @@ -77,7 +77,7 @@ def authentication_context(request):
request.META["CSRF_COOKIE_NEEDS_UPDATE"] = False


def expose_access_token(request) -> typing.Optional[str]:
def expose_access_token(request) -> Optional[Dict[str, Any]]:
"""
Determines if a new access token needs to be exposed.
"""
Expand All @@ -89,4 +89,4 @@ def expose_access_token(request) -> typing.Optional[str]:
if pre_user.is_authenticated and pre_user.pk == request.user.pk:
return None
strategy = app_settings.TOKEN_STRATEGY
return strategy.create_access_token(request)
return strategy.create_access_token_payload(request)
6 changes: 3 additions & 3 deletions allauth/headless/internal/restkit/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def __init__(

def _add_session_meta(self, request, meta: Optional[Dict]) -> Optional[Dict]:
session_token = sessionkit.expose_session_token(request)
access_token = authkit.expose_access_token(request)
access_token_payload = authkit.expose_access_token(request)
if session_token:
meta = meta or {}
meta["session_token"] = session_token
if access_token:
if access_token_payload:
meta = meta or {}
meta["access_token"] = access_token
meta.update(access_token_payload)
return meta


Expand Down
21 changes: 17 additions & 4 deletions allauth/headless/tokens/base.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import abc
import typing
from typing import Any, Dict, Optional

from django.contrib.sessions.backends.base import SessionBase
from django.http import HttpRequest


class AbstractTokenStrategy(abc.ABC):
def get_session_token(self, request: HttpRequest) -> typing.Optional[str]:
def get_session_token(self, request: HttpRequest) -> Optional[str]:
"""
Returns the session token, if any.
"""
token = request.headers.get("x-session-token")
return token

def create_access_token(self, request: HttpRequest) -> typing.Optional[str]:
def create_access_token_payload(
self, request: HttpRequest
) -> Optional[Dict[str, Any]]:
"""
After authenticating, this method is called to create the access
token response payload, exposing the access token and possibly other
information such as a ``refresh_token`` and ``expires_in``.
"""
at = self.create_access_token(request)
if not at:
return None
return {"access_token": at}

def create_access_token(self, request: HttpRequest) -> Optional[str]:
"""Create an access token.
While session tokens are required to handle the authentication process,
Expand All @@ -39,7 +52,7 @@ def create_session_token(self, request: HttpRequest) -> str:
...

@abc.abstractmethod
def lookup_session(self, session_token: str) -> typing.Optional[SessionBase]:
def lookup_session(self, session_token: str) -> Optional[SessionBase]:
"""
Looks up the Django session given the session token. Returns `None`
if the session does not / no longer exist.
Expand Down

0 comments on commit e60d1ab

Please sign in to comment.