Debian Codegraph @

allauth/socialaccount/internal/flows/signup.py

raw
1 from __future__ import annotations
2
3 from django.forms import ValidationError
4 from django.http import HttpRequest
5
6 from allauth.account import app_settings as account_settings
7 from allauth.account.adapter import get_adapter as get_account_adapter
8 from allauth.account.internal.flows.manage_email import assess_unique_email
9 from allauth.account.internal.flows.signup import complete_signup, prevent_enumeration
10 from allauth.account.utils import user_username
11 from allauth.core.exceptions import SignupClosedException
12 from allauth.core.internal.httpkit import headed_redirect_response
13 from allauth.socialaccount import app_settings
14 from allauth.socialaccount.adapter import get_adapter
15 from allauth.socialaccount.models import SocialLogin
16
17
18 def get_pending_signup(request: HttpRequest) -> SocialLogin | None:
19 if data := request.session.get("socialaccount_sociallogin"):
20 try:
21 return SocialLogin.deserialize(data)
22 except ValueError:
23 return None
24 return None
25
26
27 def redirect_to_signup(request: HttpRequest, sociallogin: SocialLogin):
28 request.session["socialaccount_sociallogin"] = sociallogin.serialize()
29 return headed_redirect_response("socialaccount_signup")
30
31
32 def clear_pending_signup(request: HttpRequest) -> None:
33 request.session.pop("socialaccount_sociallogin", None)
34
35
36 def signup_by_form(request: HttpRequest, sociallogin: SocialLogin, form):
37 clear_pending_signup(request)
38 user, resp = form.try_save(request)
39 if not resp:
40 resp = complete_social_signup(request, sociallogin)
41 return resp
42
43
44 def process_auto_signup(request: HttpRequest, sociallogin: SocialLogin):
45 auto_signup = get_adapter().is_auto_signup_allowed(request, sociallogin)
46 if not auto_signup:
47 return False, None
48 auto_signup, resp = process_auto_signup_email(request, sociallogin)
49 if not auto_signup:
50 return False, resp
51 auto_signup, resp = process_auto_signup_phone(request, sociallogin)
52 return auto_signup, resp
53
54
55 def process_auto_signup_email(request: HttpRequest, sociallogin: SocialLogin):
56 email = None
57 if sociallogin.email_addresses:
58 email = sociallogin.email_addresses[0].email
59 # Let's check if auto_signup is really possible...
60 if email:
61 assessment = assess_unique_email(email)
62 if assessment is True:
63 # Auto signup is fine.
64 auto_signup = True
65 elif assessment is False:
66 # Oops, another user already has this address. We cannot simply
67 # connect this social account to the existing user. Reason is
68 # that the email address may not be verified, meaning, the user
69 # may be a hacker that has added your email address to their
70 # account in the hope that you fall in their trap. We cannot
71 # check on 'email_address.verified' either, because
72 # 'email_address' is not guaranteed to be verified.
73 auto_signup = False
74 # TODO: We redirect to signup form -- user will see email
75 # address conflict only after posting whereas we detected it
76 # here already.
77 else:
78 assert assessment is None # nosec
79 # Prevent enumeration is properly turned on, meaning, we cannot
80 # show the signup form to allow the user to input another email
81 # address. Instead, we're going to send the user an email that
82 # the account already exists, and on the outside make it appear
83 # as if an email verification mail was sent.
84 resp = prevent_enumeration(request, email=email)
85 return False, resp
86 elif app_settings.EMAIL_REQUIRED:
87 # Nope, email is required and we don't have it yet...
88 auto_signup = False
89 else:
90 auto_signup = True
91 return auto_signup, None
92
93
94 def process_auto_signup_phone(request: HttpRequest, sociallogin: SocialLogin):
95 # At this point, email is not required, or, we have a unique email. Let's
96 # check the phone number.
97 phone_field = account_settings.SIGNUP_FIELDS.get("phone")
98 if not phone_field or not phone_field["required"]:
99 return True, None
100 if not sociallogin.phone:
101 return False, None
102 # If the phone is already taken?
103 existing_user = get_account_adapter().get_user_by_phone(sociallogin.phone)
104 if existing_user:
105 return False, None
106 return True, None
107
108
109 def process_signup(request: HttpRequest, sociallogin: SocialLogin):
110 if not get_adapter().is_open_for_signup(request, sociallogin):
111 raise SignupClosedException()
112 auto_signup, resp = process_auto_signup(request, sociallogin)
113 if resp:
114 return resp
115 if not auto_signup:
116 resp = redirect_to_signup(request, sociallogin)
117 else:
118 user = sociallogin.user
119 assert user # nosec
120 # Ok, auto signup it is, at least the email address is ok.
121 # We still need to check the username though...
122 if account_settings.USER_MODEL_USERNAME_FIELD:
123 username = user_username(user)
124 try:
125 get_account_adapter(request).clean_username(username)
126 except ValidationError:
127 # This username is no good ...
128 user_username(user, "")
129 # TODO: This part contains a lot of duplication of logic
130 # ("closed" rendering, create user, send email, in active
131 # etc..)
132 get_adapter().save_user(request, sociallogin, form=None)
133 resp = complete_social_signup(request, sociallogin)
134 return resp
135
136
137 def complete_social_signup(request: HttpRequest, sociallogin: SocialLogin):
138 from allauth.socialaccount.internal.flows.login import record_authentication
139
140 record_authentication(request, sociallogin)
141 return complete_signup(
142 request,
143 user=sociallogin.user,
144 email_verification=app_settings.EMAIL_VERIFICATION,
145 redirect_url=sociallogin.get_redirect_url(request),
146 signal_kwargs={"sociallogin": sociallogin},
147 )