Debian Codegraph @

allauth/socialaccount/providers/dummy/views.py

raw
1 from __future__ import annotations
2
3 from typing import Any
4
5 from django.core.exceptions import PermissionDenied
6 from django.http import HttpRequest, HttpResponse, HttpResponseBase
7 from django.urls import reverse
8 from django.utils.decorators import method_decorator
9 from django.utils.http import urlencode
10 from django.views.generic.edit import FormView
11
12 from allauth.account.internal.decorators import login_not_required
13 from allauth.socialaccount.adapter import get_adapter
14 from allauth.socialaccount.helpers import (
15 complete_social_login,
16 render_authentication_error,
17 )
18 from allauth.socialaccount.models import SocialLogin
19 from allauth.socialaccount.providers.base.constants import AuthError
20 from allauth.socialaccount.providers.base.views import BaseLoginView
21 from allauth.socialaccount.providers.dummy.forms import AuthenticateForm
22 from allauth.socialaccount.providers.dummy.provider import DummyProvider
23
24
25 @method_decorator(login_not_required, name="dispatch")
26 class LoginView(BaseLoginView):
27 provider_id = DummyProvider.id
28
29
30 login = LoginView.as_view()
31
32
33 class AuthenticateView(FormView):
34 form_class = AuthenticateForm
35 template_name = "dummy/authenticate_form.html"
36
37 @method_decorator(login_not_required)
38 def dispatch(
39 self, request: HttpRequest, *args: Any, **kwargs: Any
40 ) -> HttpResponseBase:
41 state_id = request.GET.get("state")
42 if not state_id:
43 raise PermissionDenied()
44 self.state_id = state_id
45 self.provider = get_adapter().get_provider(self.request, DummyProvider.id)
46 if request.method == "POST" and request.POST.get("action") == "cancel":
47 return render_authentication_error(
48 request,
49 self.provider,
50 error=AuthError.CANCELLED,
51 extra_context={"state_id": self.state_id},
52 )
53
54 return super().dispatch(request, *args, **kwargs)
55
56 def form_valid(self, form) -> HttpResponse:
57 login = self.provider.sociallogin_from_response(self.request, form.cleaned_data)
58 login.state = SocialLogin.unstash_state(self.request)
59 return complete_social_login(self.request, login)
60
61 def get_context_data(self, **kwargs) -> dict:
62 ret = super().get_context_data(**kwargs)
63 ret["action_url"] = (
64 f"{reverse('dummy_authenticate')}?{urlencode({'state': self.state_id})}"
65 )
66 return ret
67
68
69 authenticate = AuthenticateView.as_view()