Debian Codegraph @

allauth/socialaccount/internal/flows/email_authentication.py

raw
1 from __future__ import annotations
2
3 from django.contrib.auth.base_user import AbstractBaseUser
4 from django.http import HttpRequest
5
6 from allauth import app_settings as allauth_settings
7 from allauth.account.models import EmailAddress
8
9
10 def wipe_password(request: HttpRequest, user: AbstractBaseUser, email: str) -> None:
11 """
12 Consider a scenario where an attacker signs up for an account using the
13 email address of a victim. Obviously, the email address cannot be
14 verified, yet the attacker -- knowing the password -- can wait until the
15 victim appears. When the victim signs in using email authentication, it
16 is not obvious that the victim is signing into an account that was not
17 created by the victim. As a result, both the attacker and the victim now
18 have access to the account. To prevent this, we wipe the password of the
19 account in case the email address was not verified, effectively locking
20 out the attacker.
21 """
22 try:
23 address = EmailAddress.objects.get_for_user(user, email)
24 except EmailAddress.DoesNotExist:
25 address = None
26 if address and address.verified:
27 # Verified email address, no reason to worry.
28 return
29 if user.has_usable_password():
30 user.set_unusable_password()
31 user.save(update_fields=["password"])
32 # Also wipe any other sessions (upstream integrators may hook up to the
33 # ending of the sessions to trigger e.g. backchannel logout.
34 if allauth_settings.USERSESSIONS_ENABLED:
35 from allauth.usersessions.internal.flows.sessions import end_other_sessions
36
37 end_other_sessions(request, user)