| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
|
| 5 |
from django.http import HttpRequest, HttpResponseRedirect |
| 6 |
from django.urls import reverse |
| 7 |
from django.utils.http import urlencode |
| 8 |
|
| 9 |
from allauth.account.models import EmailAddress |
| 10 |
from allauth.socialaccount.adapter import get_adapter |
| 11 |
from allauth.socialaccount.providers.base import Provider, ProviderAccount |
| 12 |
from allauth.socialaccount.providers.dummy.forms import AuthenticateForm |
| 13 |
|
| 14 |
|
| 15 |
class DummyAccount(ProviderAccount): |
| 16 |
pass |
| 17 |
|
| 18 |
|
| 19 |
class DummyProvider(Provider): |
| 20 |
id = "dummy" |
| 21 |
name = "Dummy" |
| 22 |
account_class = DummyAccount |
| 23 |
uses_apps = False |
| 24 |
supports_redirect = True |
| 25 |
supports_token_authentication = True |
| 26 |
|
| 27 |
def get_login_url(self, request: HttpRequest, **kwargs): |
| 28 |
url = reverse("dummy_login") |
| 29 |
if kwargs: |
| 30 |
url = f"{url}?{urlencode(kwargs)}" |
| 31 |
return url |
| 32 |
|
| 33 |
def extract_uid(self, data): |
| 34 |
return str(data["id"]) |
| 35 |
|
| 36 |
def extract_common_fields(self, data): |
| 37 |
ret = {} |
| 38 |
if first_name := data.get("first_name"): |
| 39 |
ret["first_name"] = first_name |
| 40 |
if last_name := data.get("last_name"): |
| 41 |
ret["last_name"] = last_name |
| 42 |
if username := data.get("username"): |
| 43 |
ret["username"] = username |
| 44 |
if phone := data.get("phone"): |
| 45 |
ret["phone"] = phone |
| 46 |
if phone_verified := data.get("phone_verified"): |
| 47 |
ret["phone_verified"] = phone_verified |
| 48 |
return ret |
| 49 |
|
| 50 |
def redirect( |
| 51 |
self, request: HttpRequest, process, next_url=None, data=None, **kwargs |
| 52 |
): |
| 53 |
state_id = self.stash_redirect_state( |
| 54 |
request, |
| 55 |
process, |
| 56 |
next_url=next_url, |
| 57 |
data=data, |
| 58 |
**kwargs, |
| 59 |
) |
| 60 |
return HttpResponseRedirect( |
| 61 |
f"{reverse('dummy_authenticate')}?{urlencode({'state': state_id})}" |
| 62 |
) |
| 63 |
|
| 64 |
def extract_email_addresses(self, data): |
| 65 |
addresses = [] |
| 66 |
email = data.get("email") |
| 67 |
if email: |
| 68 |
email_verified = data.get("email_verified") |
| 69 |
addresses.append( |
| 70 |
EmailAddress( |
| 71 |
email=email, |
| 72 |
verified=email_verified, |
| 73 |
primary=True, |
| 74 |
) |
| 75 |
) |
| 76 |
return addresses |
| 77 |
|
| 78 |
def verify_token(self, request: HttpRequest, token): |
| 79 |
# Our ID token is just a JSON payload that can be handed over |
| 80 |
# to the `AuthenticateForm`. |
| 81 |
id_token = token.get("id_token") |
| 82 |
if id_token: |
| 83 |
try: |
| 84 |
data = json.loads(id_token) |
| 85 |
except json.JSONDecodeError: |
| 86 |
pass |
| 87 |
else: |
| 88 |
form = AuthenticateForm(data=data) |
| 89 |
if form.is_valid(): |
| 90 |
return self.sociallogin_from_response(request, form.cleaned_data) |
| 91 |
raise get_adapter().validation_error("invalid_token") |
| 92 |
|
| 93 |
|
| 94 |
provider_classes = [DummyProvider] |