| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import time |
| 4 |
|
| 5 |
from django.contrib.auth import REDIRECT_FIELD_NAME |
| 6 |
from django.contrib.auth.base_user import AbstractBaseUser |
| 7 |
from django.http import HttpRequest, HttpResponseRedirect |
| 8 |
from django.urls import reverse |
| 9 |
from django.utils.http import urlencode |
| 10 |
|
| 11 |
from allauth import app_settings as allauth_settings |
| 12 |
from allauth.account import app_settings |
| 13 |
from allauth.account.authentication import get_authentication_records |
| 14 |
from allauth.account.internal.flows.login import record_authentication |
| 15 |
from allauth.core.exceptions import ReauthenticationRequired |
| 16 |
from allauth.core.internal.httpkit import deserialize_request, serialize_request |
| 17 |
from allauth.core.internal.urlkit import script_aware_resolve |
| 18 |
from allauth.utils import import_callable |
| 19 |
|
| 20 |
|
| 21 |
STATE_SESSION_KEY = "account_reauthentication_state" |
| 22 |
|
| 23 |
|
| 24 |
def reauthenticate_by_password(request: HttpRequest) -> None: |
| 25 |
record_authentication( |
| 26 |
request, request.user, method="password", reauthenticated=True |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def stash_and_reauthenticate( |
| 31 |
request: HttpRequest, state: dict, callback: str |
| 32 |
) -> HttpResponseRedirect: |
| 33 |
request.session[STATE_SESSION_KEY] = { |
| 34 |
"state": state, |
| 35 |
"callback": callback, |
| 36 |
} |
| 37 |
return HttpResponseRedirect(reverse("account_reauthenticate")) |
| 38 |
|
| 39 |
|
| 40 |
def suspend_request(request: HttpRequest, redirect_to: str) -> HttpResponseRedirect: |
| 41 |
path = request.get_full_path() |
| 42 |
if request.method == "POST": |
| 43 |
request.session[STATE_SESSION_KEY] = {"request": serialize_request(request)} |
| 44 |
return HttpResponseRedirect( |
| 45 |
f"{redirect_to}?{urlencode({REDIRECT_FIELD_NAME: path})}" |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
def resume_request(request: HttpRequest) -> HttpResponseRedirect | None: |
| 50 |
from allauth.account.utils import get_next_redirect_url |
| 51 |
|
| 52 |
state = request.session.pop(STATE_SESSION_KEY, None) |
| 53 |
if state and "callback" in state: |
| 54 |
callback = import_callable(state["callback"]) |
| 55 |
return callback(request, state["state"]) |
| 56 |
|
| 57 |
url = get_next_redirect_url(request, REDIRECT_FIELD_NAME) |
| 58 |
if not url: |
| 59 |
return None |
| 60 |
if state and "request" in state: |
| 61 |
suspended_request = deserialize_request(state["request"], request) |
| 62 |
if suspended_request.path == url: |
| 63 |
resolved = script_aware_resolve(suspended_request.path) |
| 64 |
return resolved.func(suspended_request, *resolved.args, **resolved.kwargs) |
| 65 |
return HttpResponseRedirect(url) |
| 66 |
|
| 67 |
|
| 68 |
def raise_if_reauthentication_required(request: HttpRequest) -> None: |
| 69 |
if not did_recently_authenticate(request): |
| 70 |
raise ReauthenticationRequired() |
| 71 |
|
| 72 |
|
| 73 |
def did_recently_authenticate(request: HttpRequest) -> bool: |
| 74 |
if request.user.is_anonymous: |
| 75 |
return False |
| 76 |
if not get_reauthentication_flows(request.user): |
| 77 |
# TODO: This user only has social accounts attached. Now, ideally, you |
| 78 |
# would want to reauthenticate over at the social account provider. For |
| 79 |
# now, this is not implemented. Although definitely suboptimal, this |
| 80 |
# method is currently used for reauthentication checks over at MFA, and, |
| 81 |
# users that delegate the security of their account to an external |
| 82 |
# provider like Google typically use MFA over there anyway. |
| 83 |
return True |
| 84 |
methods = get_authentication_records(request) |
| 85 |
if not methods: |
| 86 |
return False |
| 87 |
authenticated_at = methods[-1]["at"] |
| 88 |
return time.time() - authenticated_at < app_settings.REAUTHENTICATION_TIMEOUT |
| 89 |
|
| 90 |
|
| 91 |
def get_reauthentication_flows(user: AbstractBaseUser) -> list[dict]: |
| 92 |
ret: list[dict] = [] |
| 93 |
if not user.is_authenticated: |
| 94 |
return ret |
| 95 |
if user.has_usable_password(): |
| 96 |
entry = { |
| 97 |
"id": "reauthenticate", |
| 98 |
} |
| 99 |
ret.append(entry) |
| 100 |
if allauth_settings.MFA_ENABLED: |
| 101 |
from allauth.mfa.models import Authenticator |
| 102 |
from allauth.mfa.utils import is_mfa_enabled |
| 103 |
|
| 104 |
types = [] |
| 105 |
for typ in Authenticator.Type: |
| 106 |
if is_mfa_enabled(user, types=[typ]): |
| 107 |
types.append(typ) |
| 108 |
if types: |
| 109 |
ret.append({"id": "mfa_reauthenticate", "types": types}) |
| 110 |
return ret |