Debian Codegraph @

allauth/socialaccount/internal/flows/login.py

raw
1 from __future__ import annotations
2
3 from django.http import HttpRequest, HttpResponseRedirect
4 from django.shortcuts import render
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.utils import perform_login
9 from allauth.core.exceptions import ImmediateHttpResponse, SignupClosedException
10 from allauth.socialaccount import app_settings, signals
11 from allauth.socialaccount.adapter import get_adapter
12 from allauth.socialaccount.internal.flows.connect import connect, do_connect
13 from allauth.socialaccount.internal.flows.signup import (
14 clear_pending_signup,
15 process_signup,
16 )
17 from allauth.socialaccount.models import SocialLogin
18 from allauth.socialaccount.providers.base import AuthProcess
19
20
21 def _login(request: HttpRequest, sociallogin: SocialLogin):
22 sociallogin._accept_login(request)
23 record_authentication(request, sociallogin)
24 assert sociallogin.user # nosec
25 return perform_login(
26 request,
27 sociallogin.user,
28 email_verification=app_settings.EMAIL_VERIFICATION,
29 redirect_url=sociallogin.get_redirect_url(request),
30 signal_kwargs={"sociallogin": sociallogin},
31 )
32
33
34 def pre_social_login(request: HttpRequest, sociallogin: SocialLogin) -> None:
35 clear_pending_signup(request)
36 assert not sociallogin.is_existing # nosec
37 sociallogin.lookup()
38 get_adapter().pre_social_login(request, sociallogin)
39 signals.pre_social_login.send(
40 sender=SocialLogin, request=request, sociallogin=sociallogin
41 )
42
43
44 def complete_login(request: HttpRequest, sociallogin: SocialLogin, raises=False):
45 try:
46 pre_social_login(request, sociallogin)
47 process = sociallogin.state.get("process")
48 if process == AuthProcess.REDIRECT:
49 return _redirect(request, sociallogin)
50 elif process == AuthProcess.CONNECT:
51 if raises:
52 do_connect(request, sociallogin)
53 else:
54 return connect(request, sociallogin)
55 else:
56 return _authenticate(request, sociallogin)
57 except SignupClosedException:
58 if raises:
59 raise
60 return render(
61 request,
62 f"account/signup_closed.{account_settings.TEMPLATE_EXTENSION}",
63 )
64 except ImmediateHttpResponse as e:
65 if raises:
66 raise
67 return e.response
68
69
70 def _redirect(request: HttpRequest, sociallogin: SocialLogin):
71 next_url = sociallogin.get_redirect_url(request) or "/"
72 return HttpResponseRedirect(next_url)
73
74
75 def _authenticate(request: HttpRequest, sociallogin: SocialLogin):
76 if request.user.is_authenticated:
77 get_account_adapter(request).logout(request)
78 if sociallogin.is_existing:
79 # Login existing user
80 ret = _login(request, sociallogin)
81 else:
82 # New social user
83 ret = process_signup(request, sociallogin)
84 return ret
85
86
87 def record_authentication(request: HttpRequest, sociallogin: SocialLogin) -> None:
88 from allauth.account.internal.flows.login import record_authentication
89
90 record_authentication(
91 request,
92 sociallogin.user,
93 "socialaccount",
94 **{
95 "provider": sociallogin.account.provider,
96 "uid": sociallogin.account.uid,
97 },
98 )