| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from urllib.parse import quote |
| 4 |
|
| 5 |
from django.contrib import messages |
| 6 |
from django.contrib.auth.models import AbstractBaseUser |
| 7 |
from django.http import HttpRequest, HttpResponse |
| 8 |
from django.urls import reverse |
| 9 |
|
| 10 |
from allauth.account import app_settings, signals |
| 11 |
from allauth.account.adapter import get_adapter |
| 12 |
from allauth.account.app_settings import LoginMethod |
| 13 |
from allauth.account.internal.flows.login import perform_login, record_authentication |
| 14 |
from allauth.account.internal.flows.signup import send_unknown_account_mail |
| 15 |
from allauth.account.models import EmailAddress, Login |
| 16 |
from allauth.core.internal.httpkit import get_frontend_url |
| 17 |
from allauth.utils import build_absolute_uri |
| 18 |
|
| 19 |
|
| 20 |
def reset_password(user: AbstractBaseUser, password: str) -> None: |
| 21 |
get_adapter().set_password(user, password) |
| 22 |
|
| 23 |
|
| 24 |
def perform_password_reset_login( |
| 25 |
request: HttpRequest, |
| 26 |
user: AbstractBaseUser, |
| 27 |
phone: str | None = None, |
| 28 |
email: str | None = None, |
| 29 |
) -> HttpResponse: |
| 30 |
extra_data = {} |
| 31 |
if phone: |
| 32 |
extra_data["phone"] = phone |
| 33 |
elif email: |
| 34 |
extra_data["email"] = email |
| 35 |
record_authentication(request, user, method="password_reset", **extra_data) |
| 36 |
login = Login(user=user, email=email) |
| 37 |
return perform_login(request, login) |
| 38 |
|
| 39 |
|
| 40 |
def finalize_password_reset( |
| 41 |
request: HttpRequest, user: AbstractBaseUser, email: str | None = None |
| 42 |
) -> HttpResponse | None: |
| 43 |
adapter = get_adapter() |
| 44 |
if user: |
| 45 |
# User successfully reset the password, clear any |
| 46 |
# possible cache entries for all email addresses. |
| 47 |
for address in EmailAddress.objects.filter(user_id=user.pk): |
| 48 |
adapter._delete_login_attempts_cached_email(request, email=address.email) |
| 49 |
|
| 50 |
adapter.add_message( |
| 51 |
request, |
| 52 |
messages.SUCCESS, |
| 53 |
"account/messages/password_changed.txt", |
| 54 |
) |
| 55 |
signals.password_reset.send( |
| 56 |
sender=user.__class__, |
| 57 |
request=request, |
| 58 |
user=user, |
| 59 |
) |
| 60 |
adapter.send_notification_mail("account/email/password_reset", user) |
| 61 |
if app_settings.LOGIN_ON_PASSWORD_RESET: |
| 62 |
return perform_password_reset_login(request, user, email=email) |
| 63 |
return None |
| 64 |
|
| 65 |
|
| 66 |
def get_reset_password_url(request: HttpRequest) -> str: |
| 67 |
url = get_frontend_url(request, "account_reset_password") |
| 68 |
if not url: |
| 69 |
url = build_absolute_uri(request, reverse("account_reset_password")) |
| 70 |
return url |
| 71 |
|
| 72 |
|
| 73 |
def get_reset_password_from_key_url(request: HttpRequest, key: str) -> str: |
| 74 |
""" |
| 75 |
Method intented to be overriden in case the password reset email |
| 76 |
needs to point to your frontend/SPA. |
| 77 |
""" |
| 78 |
url = get_frontend_url(request, "account_reset_password_from_key", key=key) |
| 79 |
if not url: |
| 80 |
# We intentionally accept an opaque `key` on the interface here, and not |
| 81 |
# implementation details such as a separate `uidb36` and `key. Ideally, |
| 82 |
# this should have done on `urls` level as well. |
| 83 |
path = reverse( |
| 84 |
"account_reset_password_from_key", kwargs={"uidb36": "UID", "key": "KEY"} |
| 85 |
) |
| 86 |
path = path.replace("UID-KEY", quote(key)) |
| 87 |
url = build_absolute_uri(request, path) |
| 88 |
return url |
| 89 |
|
| 90 |
|
| 91 |
def request_password_reset( |
| 92 |
request: HttpRequest, email: str, users, token_generator |
| 93 |
) -> None: |
| 94 |
from allauth.account.utils import user_pk_to_url_str, user_username |
| 95 |
|
| 96 |
if not users: |
| 97 |
send_unknown_account_mail(request, email) |
| 98 |
return |
| 99 |
adapter = get_adapter() |
| 100 |
for user in users: |
| 101 |
temp_key = ( |
| 102 |
token_generator or app_settings.PASSWORD_RESET_TOKEN_GENERATOR() |
| 103 |
).make_token(user) |
| 104 |
|
| 105 |
# send the password reset email |
| 106 |
uid = user_pk_to_url_str(user) |
| 107 |
# We intentionally pass an opaque `key` on the interface here, and |
| 108 |
# not implementation details such as a separate `uidb36` and |
| 109 |
# `key. Ideally, this should have done on `urls` level as well. |
| 110 |
key = f"{uid}-{temp_key}" |
| 111 |
url = adapter.get_reset_password_from_key_url(key) |
| 112 |
context = { |
| 113 |
"user": user, |
| 114 |
"password_reset_url": url, |
| 115 |
"uid": uid, |
| 116 |
"key": temp_key, |
| 117 |
"request": request, |
| 118 |
} |
| 119 |
|
| 120 |
if LoginMethod.USERNAME in app_settings.LOGIN_METHODS: |
| 121 |
context["username"] = user_username(user) |
| 122 |
adapter.send_password_reset_mail(user, email, context) |