| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.account.models import EmailAddress |
| 6 |
from allauth.socialaccount import app_settings |
| 7 |
from allauth.socialaccount.providers.authentiq.views import AuthentiqOAuth2Adapter |
| 8 |
from allauth.socialaccount.providers.base import AuthAction, ProviderAccount |
| 9 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 10 |
|
| 11 |
|
| 12 |
class Scope: |
| 13 |
NAME = "aq:name" |
| 14 |
EMAIL = "email" |
| 15 |
PHONE = "phone" |
| 16 |
ADDRESS = "address" |
| 17 |
LOCATION = "aq:location" |
| 18 |
PUSH = "aq:push" |
| 19 |
|
| 20 |
|
| 21 |
IDENTITY_CLAIMS = frozenset( |
| 22 |
[ |
| 23 |
"sub", |
| 24 |
"name", |
| 25 |
"given_name", |
| 26 |
"family_name", |
| 27 |
"middle_name", |
| 28 |
"nickname", |
| 29 |
"preferred_username", |
| 30 |
"profile", |
| 31 |
"picture", |
| 32 |
"website", |
| 33 |
"email", |
| 34 |
"email_verified", |
| 35 |
"gender", |
| 36 |
"birthdate", |
| 37 |
"zoneinfo", |
| 38 |
"locale", |
| 39 |
"phone_number", |
| 40 |
"phone_number_verified", |
| 41 |
"address", |
| 42 |
"updated_at", |
| 43 |
"aq:location", |
| 44 |
] |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
class AuthentiqAccount(ProviderAccount): |
| 49 |
def get_profile_url(self): |
| 50 |
return self.account.extra_data.get("profile") |
| 51 |
|
| 52 |
def get_avatar_url(self): |
| 53 |
return self.account.extra_data.get("picture") |
| 54 |
|
| 55 |
|
| 56 |
class AuthentiqProvider(OAuth2Provider): |
| 57 |
id = "authentiq" |
| 58 |
name = "Authentiq" |
| 59 |
account_class = AuthentiqAccount |
| 60 |
oauth2_adapter_class = AuthentiqOAuth2Adapter |
| 61 |
|
| 62 |
def get_scope_from_request(self, request: HttpRequest): |
| 63 |
scope = set(super().get_scope_from_request(request)) |
| 64 |
scope.add("openid") |
| 65 |
|
| 66 |
if Scope.EMAIL in scope: |
| 67 |
modifiers = "" |
| 68 |
if app_settings.EMAIL_REQUIRED: |
| 69 |
modifiers += "r" |
| 70 |
if app_settings.EMAIL_VERIFICATION: |
| 71 |
modifiers += "s" |
| 72 |
if modifiers: |
| 73 |
scope.add(f"{Scope.EMAIL}~{modifiers}") |
| 74 |
scope.remove(Scope.EMAIL) |
| 75 |
return list(scope) |
| 76 |
|
| 77 |
def get_default_scope(self): |
| 78 |
scope = [Scope.NAME, Scope.PUSH] |
| 79 |
if app_settings.QUERY_EMAIL: |
| 80 |
scope.append(Scope.EMAIL) |
| 81 |
return scope |
| 82 |
|
| 83 |
def get_auth_params_from_request(self, request: HttpRequest, action): |
| 84 |
ret = super().get_auth_params_from_request(request, action) |
| 85 |
if action == AuthAction.REAUTHENTICATE: |
| 86 |
ret["prompt"] = "select_account" |
| 87 |
return ret |
| 88 |
|
| 89 |
def extract_uid(self, data): |
| 90 |
return str(data["sub"]) |
| 91 |
|
| 92 |
def extract_common_fields(self, data): |
| 93 |
return dict( |
| 94 |
username=data.get("preferred_username", data.get("given_name")), |
| 95 |
email=data.get("email"), |
| 96 |
name=data.get("name"), |
| 97 |
first_name=data.get("given_name"), |
| 98 |
last_name=data.get("family_name"), |
| 99 |
) |
| 100 |
|
| 101 |
def extract_extra_data(self, data): |
| 102 |
return {k: v for k, v in data.items() if k in IDENTITY_CLAIMS} |
| 103 |
|
| 104 |
def extract_email_addresses(self, data): |
| 105 |
ret = [] |
| 106 |
email = data.get("email") |
| 107 |
if email and data.get("email_verified"): |
| 108 |
ret.append(EmailAddress(email=email, verified=True, primary=True)) |
| 109 |
return ret |
| 110 |
|
| 111 |
|
| 112 |
provider_classes = [AuthentiqProvider] |