| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.core.exceptions import ValidationError |
| 4 |
|
| 5 |
from allauth.core import context |
| 6 |
from allauth.headless.adapter import get_adapter |
| 7 |
from allauth.headless.internal.restkit import inputs |
| 8 |
from allauth.socialaccount.adapter import get_adapter as get_socialaccount_adapter |
| 9 |
from allauth.socialaccount.forms import SignupForm |
| 10 |
from allauth.socialaccount.internal.flows.connect import validate_disconnect |
| 11 |
from allauth.socialaccount.models import SocialAccount, SocialApp |
| 12 |
from allauth.socialaccount.providers import registry |
| 13 |
from allauth.socialaccount.providers.base.constants import AuthProcess |
| 14 |
|
| 15 |
|
| 16 |
class SignupInput(SignupForm, inputs.Input): |
| 17 |
pass |
| 18 |
|
| 19 |
|
| 20 |
class DeleteProviderAccountInput(inputs.Input): |
| 21 |
provider = inputs.CharField() |
| 22 |
account = inputs.CharField() |
| 23 |
|
| 24 |
def __init__(self, *args, **kwargs) -> None: |
| 25 |
self.user = kwargs.pop("user") |
| 26 |
super().__init__(*args, **kwargs) |
| 27 |
|
| 28 |
def clean(self): |
| 29 |
cleaned_data = super().clean() |
| 30 |
uid = cleaned_data.get("account") |
| 31 |
provider_id = cleaned_data.get("provider") |
| 32 |
if uid and provider_id: |
| 33 |
accounts = SocialAccount.objects.filter(user=self.user) |
| 34 |
account = accounts.filter( |
| 35 |
uid=uid, |
| 36 |
provider=provider_id, |
| 37 |
).first() |
| 38 |
if not account: |
| 39 |
raise get_adapter().validation_error("account_not_found") |
| 40 |
validate_disconnect(context.request, account) |
| 41 |
self.cleaned_data["account"] = account |
| 42 |
return cleaned_data |
| 43 |
|
| 44 |
|
| 45 |
class ProviderTokenInput(inputs.Input): |
| 46 |
provider = inputs.CharField() |
| 47 |
process = inputs.ChoiceField( |
| 48 |
choices=[ |
| 49 |
(AuthProcess.LOGIN, AuthProcess.LOGIN), |
| 50 |
(AuthProcess.CONNECT, AuthProcess.CONNECT), |
| 51 |
] |
| 52 |
) |
| 53 |
token = inputs.Field() |
| 54 |
|
| 55 |
def clean(self): |
| 56 |
cleaned_data = super().clean() |
| 57 |
token = self.data.get("token") |
| 58 |
adapter = get_adapter() |
| 59 |
if not isinstance(token, dict): |
| 60 |
self.add_error("token", adapter.validation_error("invalid_token")) |
| 61 |
token = None |
| 62 |
|
| 63 |
provider_id = cleaned_data.get("provider") |
| 64 |
provider = None |
| 65 |
if provider_id and token: |
| 66 |
provider_class = registry.get_class(provider_id) |
| 67 |
# If `provider_id` is a sub provider ID we won't find it by class. |
| 68 |
client_id_required = provider_class is None or provider_class.uses_apps |
| 69 |
client_id = token.get("client_id") |
| 70 |
if client_id_required and not isinstance(client_id, str): |
| 71 |
self.add_error("token", adapter.validation_error("client_id_required")) |
| 72 |
else: |
| 73 |
try: |
| 74 |
provider = get_socialaccount_adapter().get_provider( |
| 75 |
context.request, provider_id, client_id=client_id |
| 76 |
) |
| 77 |
except SocialApp.DoesNotExist: |
| 78 |
self.add_error("token", adapter.validation_error("invalid_token")) |
| 79 |
else: |
| 80 |
if not provider.supports_token_authentication: |
| 81 |
self.add_error( |
| 82 |
"provider", |
| 83 |
adapter.validation_error( |
| 84 |
"token_authentication_not_supported" |
| 85 |
), |
| 86 |
) |
| 87 |
elif ( |
| 88 |
provider.uses_apps |
| 89 |
and client_id |
| 90 |
and provider.app.client_id != client_id |
| 91 |
): |
| 92 |
self.add_error( |
| 93 |
"token", adapter.validation_error("client_id_mismatch") |
| 94 |
) |
| 95 |
else: |
| 96 |
id_token = token.get("id_token") |
| 97 |
access_token = token.get("access_token") |
| 98 |
if ( |
| 99 |
(id_token is not None and not isinstance(id_token, str)) |
| 100 |
or ( |
| 101 |
access_token is not None |
| 102 |
and not isinstance(access_token, str) |
| 103 |
) |
| 104 |
or (not id_token and not access_token) |
| 105 |
): |
| 106 |
self.add_error( |
| 107 |
"token", adapter.validation_error("token_required") |
| 108 |
) |
| 109 |
if not self.errors: |
| 110 |
cleaned_data["provider"] = provider |
| 111 |
try: |
| 112 |
login = provider.verify_token(context.request, token) |
| 113 |
login.state["process"] = cleaned_data["process"] |
| 114 |
cleaned_data["sociallogin"] = login |
| 115 |
except ValidationError as e: |
| 116 |
self.add_error("token", e) |
| 117 |
return cleaned_data |