| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import dataclasses |
| 4 |
import uuid |
| 5 |
from typing import Any, Optional |
| 6 |
|
| 7 |
from django.contrib.auth import get_user_model |
| 8 |
from django.contrib.auth.base_user import AbstractBaseUser |
| 9 |
from django.db import models |
| 10 |
from django.forms.fields import Field |
| 11 |
from django.utils.module_loading import import_string |
| 12 |
|
| 13 |
from allauth.account import app_settings as account_settings |
| 14 |
from allauth.account.models import EmailAddress |
| 15 |
from allauth.account.utils import user_display, user_username |
| 16 |
from allauth.core.internal.adapter import BaseAdapter |
| 17 |
from allauth.core.internal.httpkit import default_get_frontend_url |
| 18 |
from allauth.headless import app_settings |
| 19 |
|
| 20 |
|
| 21 |
class DefaultHeadlessAdapter(BaseAdapter): |
| 22 |
"""The adapter class allows you to override various functionality of the |
| 23 |
``allauth.headless`` app. To do so, point ``settings.HEADLESS_ADAPTER`` to your own |
| 24 |
class that derives from ``DefaultHeadlessAdapter`` and override the behavior by |
| 25 |
altering the implementation of the methods according to your own need. |
| 26 |
""" |
| 27 |
|
| 28 |
error_messages = { |
| 29 |
# For the following error messages i18n is not an issue as these should not be |
| 30 |
# showing up in a UI. |
| 31 |
"account_not_found": "Unknown account.", |
| 32 |
"client_id_required": "`client_id` required.", |
| 33 |
"invalid_token": "Invalid token.", # nosec |
| 34 |
"token_authentication_not_supported": "Provider does not support token authentication.", |
| 35 |
"token_required": "`id_token` and/or `access_token` required.", |
| 36 |
"required": Field.default_error_messages["required"], |
| 37 |
"unknown_email": "Unknown email address.", |
| 38 |
"unknown_provider": "Unknown provider.", |
| 39 |
"invalid_url": "Invalid URL.", |
| 40 |
} |
| 41 |
|
| 42 |
def serialize_user(self, user: AbstractBaseUser) -> dict[str, Any]: |
| 43 |
""" |
| 44 |
Returns the basic user data. Note that this data is also exposed in |
| 45 |
partly authenticated scenario's (e.g. password reset, email |
| 46 |
verification). |
| 47 |
|
| 48 |
Do not override this method if you would like your customized user payloads |
| 49 |
to be reflected in the (dynamically rendered) OpenAPI specification. In that |
| 50 |
case, override ``get_user_dataclass()`` and ``user_as_dataclass`` instead. |
| 51 |
""" |
| 52 |
return { |
| 53 |
k: v |
| 54 |
for k, v in dataclasses.asdict(self.user_as_dataclass(user)).items() |
| 55 |
if v not in ("", None) |
| 56 |
} |
| 57 |
|
| 58 |
def get_frontend_url(self, urlname: str, **kwargs) -> str | None: |
| 59 |
"""Return the frontend URL for the given URL name.""" |
| 60 |
return default_get_frontend_url(self.request, urlname, **kwargs) |
| 61 |
|
| 62 |
def user_as_dataclass(self, user: AbstractBaseUser): |
| 63 |
""" |
| 64 |
See ``get_user_dataclass()``. This method returns an instance of |
| 65 |
that ``dataclass``, populated with the given ``user`` fields. |
| 66 |
""" |
| 67 |
UserDc = self.get_user_dataclass() |
| 68 |
kwargs = {} |
| 69 |
User = get_user_model() |
| 70 |
pk_field_class = type(User._meta.pk) |
| 71 |
if not user.pk: |
| 72 |
id_dc = None |
| 73 |
elif issubclass(pk_field_class, models.IntegerField): |
| 74 |
id_dc = user.pk |
| 75 |
else: |
| 76 |
id_dc = str(user.pk) |
| 77 |
if account_settings.USER_MODEL_USERNAME_FIELD: |
| 78 |
kwargs["username"] = user_username(user) |
| 79 |
if user.pk: |
| 80 |
email = EmailAddress.objects.get_primary_email(user) |
| 81 |
else: |
| 82 |
email = None |
| 83 |
kwargs.update( |
| 84 |
{ |
| 85 |
"id": id_dc, |
| 86 |
"email": email if email else None, |
| 87 |
"display": user_display(user), |
| 88 |
"has_usable_password": user.has_usable_password(), |
| 89 |
} |
| 90 |
) |
| 91 |
return UserDc(**kwargs) |
| 92 |
|
| 93 |
def get_user_dataclass(self): |
| 94 |
""" |
| 95 |
Basic user data payloads are exposed in some of the headless responses. If |
| 96 |
you need to customize these payloads in such a way that your custom user |
| 97 |
payload is also reflected in the OpenAPI specification, you wil need to |
| 98 |
provide a ``dataclass`` representing the schema of your custom payload, |
| 99 |
as well as method that takes a ``User`` instance and wraps it into your |
| 100 |
dataclass. This method returns that ``dataclass``. |
| 101 |
""" |
| 102 |
fields = [] |
| 103 |
User = get_user_model() |
| 104 |
pk_field_class = type(User._meta.pk) |
| 105 |
if issubclass(pk_field_class, models.UUIDField): |
| 106 |
id_type = str |
| 107 |
id_example = str(uuid.uuid4()) |
| 108 |
elif issubclass(pk_field_class, models.IntegerField): |
| 109 |
id_type = int |
| 110 |
id_example = 123 |
| 111 |
else: |
| 112 |
id_type = str |
| 113 |
id_example = "uid" |
| 114 |
|
| 115 |
def dc_field(attr, typ, description, example): |
| 116 |
return ( |
| 117 |
attr, |
| 118 |
typ, |
| 119 |
dataclasses.field( |
| 120 |
metadata={ |
| 121 |
"description": description, |
| 122 |
"example": example, |
| 123 |
} |
| 124 |
), |
| 125 |
) |
| 126 |
|
| 127 |
fields.extend( |
| 128 |
[ |
| 129 |
dc_field("id", Optional[id_type], "The user ID.", id_example), |
| 130 |
dc_field( |
| 131 |
"display", str, "The display name for the user.", "Magic Wizard" |
| 132 |
), |
| 133 |
dc_field( |
| 134 |
"email", Optional[str], "The email address.", "[email protected]" |
| 135 |
), |
| 136 |
dc_field( |
| 137 |
"has_usable_password", |
| 138 |
bool, |
| 139 |
"Whether or not the account has a password set.", |
| 140 |
True, |
| 141 |
), |
| 142 |
] |
| 143 |
) |
| 144 |
if account_settings.USER_MODEL_USERNAME_FIELD: |
| 145 |
fields.append(dc_field("username", str, "The username.", "wizard")) |
| 146 |
return dataclasses.make_dataclass("User", fields) |
| 147 |
|
| 148 |
|
| 149 |
def get_adapter() -> DefaultHeadlessAdapter: |
| 150 |
return import_string(app_settings.ADAPTER)() |