| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
from http import HTTPStatus |
| 5 |
from types import SimpleNamespace |
| 6 |
|
| 7 |
from django.http import HttpRequest, HttpResponseRedirect |
| 8 |
from django.urls import NoReverseMatch, reverse |
| 9 |
from django.utils.decorators import sync_and_async_middleware |
| 10 |
|
| 11 |
from asgiref.sync import iscoroutinefunction, sync_to_async |
| 12 |
|
| 13 |
from allauth.account.adapter import get_adapter |
| 14 |
from allauth.account.internal import flows |
| 15 |
from allauth.core import context |
| 16 |
from allauth.core.exceptions import ImmediateHttpResponse, ReauthenticationRequired |
| 17 |
|
| 18 |
|
| 19 |
@sync_and_async_middleware |
| 20 |
def AccountMiddleware(get_response): |
| 21 |
if iscoroutinefunction(get_response): |
| 22 |
|
| 23 |
async def async_middleware(request: HttpRequest): |
| 24 |
request.allauth = SimpleNamespace() # type:ignore[attr-defined] |
| 25 |
with context.request_context(request): |
| 26 |
response = await get_response(request) |
| 27 |
if _should_redirect_accounts(request, response): |
| 28 |
response = await _aredirect_accounts(request) |
| 29 |
return response |
| 30 |
|
| 31 |
middleware = async_middleware |
| 32 |
else: |
| 33 |
|
| 34 |
def sync_middleware(request: HttpRequest): |
| 35 |
request.allauth = SimpleNamespace() # type:ignore[attr-defined] |
| 36 |
with context.request_context(request): |
| 37 |
response = get_response(request) |
| 38 |
if _should_redirect_accounts(request, response): |
| 39 |
response = _redirect_accounts(request) |
| 40 |
return response |
| 41 |
|
| 42 |
middleware = sync_middleware |
| 43 |
|
| 44 |
def process_exception(request: HttpRequest, exception): |
| 45 |
if isinstance(exception, ImmediateHttpResponse): |
| 46 |
return exception.response |
| 47 |
elif isinstance(exception, ReauthenticationRequired): |
| 48 |
redirect_url = reverse("account_login") |
| 49 |
methods = get_adapter().get_reauthentication_methods(request.user) |
| 50 |
if methods: |
| 51 |
redirect_url = methods[0]["url"] |
| 52 |
return flows.reauthentication.suspend_request(request, redirect_url) |
| 53 |
|
| 54 |
middleware.process_exception = process_exception |
| 55 |
return middleware |
| 56 |
|
| 57 |
|
| 58 |
def _should_redirect_accounts(request: HttpRequest, response) -> bool: |
| 59 |
""" |
| 60 |
URLs should be hackable. Yet, assuming allauth is included like this... |
| 61 |
|
| 62 |
path("accounts/", include("allauth.urls")), |
| 63 |
|
| 64 |
... and a user would attempt to navigate to /accounts/, a 404 would be |
| 65 |
presented. This code catches that 404, and redirects to either the email |
| 66 |
management overview or the login page, depending on whether or not the user |
| 67 |
is authenticated. |
| 68 |
""" |
| 69 |
if response.status_code != HTTPStatus.NOT_FOUND: |
| 70 |
return False |
| 71 |
try: |
| 72 |
login_path = reverse("account_login") |
| 73 |
email_path = reverse("account_email") |
| 74 |
except NoReverseMatch: |
| 75 |
# Project might have deviated URLs, let's keep out of the way. |
| 76 |
return False |
| 77 |
prefix = os.path.commonprefix([login_path, email_path]) |
| 78 |
if len(prefix) <= 1 or prefix != request.path: |
| 79 |
return False |
| 80 |
# If we have a prefix that is not just '/', and that is what our request is |
| 81 |
# pointing to, redirect. |
| 82 |
return True |
| 83 |
|
| 84 |
|
| 85 |
@sync_to_async |
| 86 |
def _async_get_user(request: HttpRequest): |
| 87 |
return request.user |
| 88 |
|
| 89 |
|
| 90 |
async def _aredirect_accounts(request: HttpRequest) -> HttpResponseRedirect: |
| 91 |
email_path = reverse("account_email") |
| 92 |
login_path = reverse("account_login") |
| 93 |
if hasattr(request, "auser"): |
| 94 |
user = await request.auser() |
| 95 |
else: |
| 96 |
# Django <5 |
| 97 |
user = await _async_get_user(request) |
| 98 |
return HttpResponseRedirect(email_path if user.is_authenticated else login_path) |
| 99 |
|
| 100 |
|
| 101 |
def _redirect_accounts(request: HttpRequest) -> HttpResponseRedirect: |
| 102 |
email_path = reverse("account_email") |
| 103 |
login_path = reverse("account_login") |
| 104 |
user = request.user |
| 105 |
return HttpResponseRedirect(email_path if user.is_authenticated else login_path) |