| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from django.core.exceptions import PermissionDenied, ValidationError |
| 6 |
from django.http import HttpRequest, HttpResponseRedirect |
| 7 |
|
| 8 |
from allauth import app_settings as allauth_settings |
| 9 |
from allauth.core.exceptions import ( |
| 10 |
ImmediateHttpResponse, |
| 11 |
ReauthenticationRequired, |
| 12 |
SignupClosedException, |
| 13 |
) |
| 14 |
from allauth.core.internal import httpkit |
| 15 |
from allauth.headless.internal.authkit import AuthenticationStatus |
| 16 |
from allauth.socialaccount.internal import flows, statekit |
| 17 |
from allauth.socialaccount.providers.base.constants import AuthError, AuthProcess |
| 18 |
|
| 19 |
|
| 20 |
if TYPE_CHECKING: |
| 21 |
from allauth.socialaccount.models import SocialLogin |
| 22 |
|
| 23 |
|
| 24 |
def on_authentication_error( |
| 25 |
request: HttpRequest, |
| 26 |
provider, |
| 27 |
error=None, |
| 28 |
exception=None, |
| 29 |
extra_context=None, |
| 30 |
state_id=None, |
| 31 |
) -> None: |
| 32 |
""" |
| 33 |
Called at a time when it is not clear whether or not this is a headless flow. |
| 34 |
""" |
| 35 |
state = None |
| 36 |
if extra_context: |
| 37 |
state = extra_context.get("state") |
| 38 |
if state is None: |
| 39 |
state_id = extra_context.get("state_id") |
| 40 |
if state_id: |
| 41 |
state = statekit.unstash_state(request, state_id) |
| 42 |
params = {"error": error} |
| 43 |
if state is not None: |
| 44 |
headless = state.get("headless") |
| 45 |
next_url = state.get("next") |
| 46 |
params["error_process"] = state["process"] |
| 47 |
else: |
| 48 |
headless = allauth_settings.HEADLESS_ONLY |
| 49 |
next_url = None |
| 50 |
params["error_process"] = AuthProcess.LOGIN |
| 51 |
if not headless: |
| 52 |
return |
| 53 |
if not next_url: |
| 54 |
next_url = httpkit.get_frontend_url(request, "socialaccount_login_error") or "/" |
| 55 |
next_url = httpkit.add_query_params(next_url, params) |
| 56 |
raise ImmediateHttpResponse(HttpResponseRedirect(next_url)) |
| 57 |
|
| 58 |
|
| 59 |
def complete_token_login(request: HttpRequest, sociallogin: SocialLogin): |
| 60 |
return flows.login.complete_login(request, sociallogin, raises=True) |
| 61 |
|
| 62 |
|
| 63 |
def complete_login( |
| 64 |
request: HttpRequest, sociallogin: SocialLogin |
| 65 |
) -> HttpResponseRedirect: |
| 66 |
""" |
| 67 |
Called when `sociallogin.is_headless`. |
| 68 |
""" |
| 69 |
error = None |
| 70 |
try: |
| 71 |
flows.login.complete_login(request, sociallogin, raises=True) |
| 72 |
except ReauthenticationRequired: |
| 73 |
error = "reauthentication_required" |
| 74 |
except SignupClosedException: |
| 75 |
error = "signup_closed" |
| 76 |
except PermissionDenied: |
| 77 |
error = "permission_denied" |
| 78 |
except ValidationError as e: |
| 79 |
error = e.code |
| 80 |
else: |
| 81 |
# At this stage, we're either: |
| 82 |
# 1) logged in (or in of the login pipeline stages, such as email verification) |
| 83 |
# 2) auto signed up -- a pipeline stage, so see 1) |
| 84 |
# 3) performing a social signup |
| 85 |
|
| 86 |
# 4) Stopped, due to not being open-for-signup |
| 87 |
# It would be good to refactor the above into a more generic social login |
| 88 |
# pipeline with clear stages, but for now the /auth endpoint properly responds |
| 89 |
status = AuthenticationStatus(request) |
| 90 |
if all( |
| 91 |
[ |
| 92 |
not status.is_authenticated, |
| 93 |
not status.has_pending_signup, |
| 94 |
not status.get_pending_stage(), |
| 95 |
] |
| 96 |
): |
| 97 |
error = AuthError.UNKNOWN |
| 98 |
next_url = sociallogin.state["next"] |
| 99 |
if error: |
| 100 |
next_url = httpkit.add_query_params( |
| 101 |
next_url, |
| 102 |
{"error": error, "error_process": sociallogin.state["process"]}, |
| 103 |
) |
| 104 |
return HttpResponseRedirect(next_url) |