| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from django.core.exceptions import ValidationError |
| 6 |
from django.http import HttpRequest, HttpResponse, HttpResponseBase |
| 7 |
|
| 8 |
from allauth.core.exceptions import SignupClosedException |
| 9 |
from allauth.headless.base.response import ( |
| 10 |
AuthenticationResponse, |
| 11 |
ConflictResponse, |
| 12 |
ForbiddenResponse, |
| 13 |
) |
| 14 |
from allauth.headless.base.views import APIView, AuthenticatedAPIView |
| 15 |
from allauth.headless.internal.restkit.response import ErrorResponse |
| 16 |
from allauth.headless.socialaccount.forms import RedirectToProviderForm |
| 17 |
from allauth.headless.socialaccount.inputs import ( |
| 18 |
DeleteProviderAccountInput, |
| 19 |
ProviderTokenInput, |
| 20 |
SignupInput, |
| 21 |
) |
| 22 |
from allauth.headless.socialaccount.internal import complete_token_login |
| 23 |
from allauth.headless.socialaccount.response import ( |
| 24 |
SocialAccountsResponse, |
| 25 |
SocialLoginResponse, |
| 26 |
) |
| 27 |
from allauth.socialaccount.adapter import get_adapter as get_socialaccount_adapter |
| 28 |
from allauth.socialaccount.helpers import render_authentication_error |
| 29 |
from allauth.socialaccount.internal import flows |
| 30 |
from allauth.socialaccount.models import SocialAccount, SocialLogin |
| 31 |
|
| 32 |
|
| 33 |
class ProviderSignupView(APIView): |
| 34 |
input_class = SignupInput |
| 35 |
sociallogin: SocialLogin |
| 36 |
|
| 37 |
def handle( |
| 38 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 39 |
) -> HttpResponseBase: |
| 40 |
sociallogin = flows.signup.get_pending_signup(self.request) |
| 41 |
if not sociallogin: |
| 42 |
return ConflictResponse(request) |
| 43 |
self.sociallogin = sociallogin |
| 44 |
if not get_socialaccount_adapter().is_open_for_signup( |
| 45 |
request, self.sociallogin |
| 46 |
): |
| 47 |
return ForbiddenResponse(request) |
| 48 |
return super().handle(request, *args, **kwargs) |
| 49 |
|
| 50 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 51 |
return SocialLoginResponse(request, self.sociallogin) |
| 52 |
|
| 53 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 54 |
response = flows.signup.signup_by_form( |
| 55 |
self.request, self.sociallogin, self.input |
| 56 |
) |
| 57 |
return AuthenticationResponse.from_response(request, response) |
| 58 |
|
| 59 |
def get_input_kwargs(self) -> dict: |
| 60 |
return {"sociallogin": self.sociallogin} |
| 61 |
|
| 62 |
|
| 63 |
class RedirectToProviderView(APIView): |
| 64 |
handle_json_input = False |
| 65 |
|
| 66 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 67 |
form = RedirectToProviderForm(request.POST) |
| 68 |
if not form.is_valid(): |
| 69 |
return render_authentication_error( |
| 70 |
request, |
| 71 |
provider=request.POST.get("provider"), |
| 72 |
exception=ValidationError(form.errors), |
| 73 |
) |
| 74 |
provider = form.cleaned_data["provider"] |
| 75 |
next_url = form.cleaned_data["callback_url"] |
| 76 |
process = form.cleaned_data["process"] |
| 77 |
return provider.redirect( |
| 78 |
request, |
| 79 |
process, |
| 80 |
next_url=next_url, |
| 81 |
headless=True, |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
class ManageProvidersView(AuthenticatedAPIView): |
| 86 |
input_class = { |
| 87 |
"DELETE": DeleteProviderAccountInput, |
| 88 |
} |
| 89 |
|
| 90 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 91 |
return self.respond_provider_accounts(request) |
| 92 |
|
| 93 |
@classmethod |
| 94 |
def respond_provider_accounts(self, request: HttpRequest) -> SocialAccountsResponse: |
| 95 |
assert request.user.is_authenticated # nosec |
| 96 |
accounts = SocialAccount.objects.filter(user=request.user) |
| 97 |
return SocialAccountsResponse(request, accounts) |
| 98 |
|
| 99 |
def delete(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 100 |
flows.connect.disconnect(request, self.input.cleaned_data["account"]) |
| 101 |
return self.respond_provider_accounts(request) |
| 102 |
|
| 103 |
def get_input_kwargs(self) -> dict: |
| 104 |
return {"user": self.request.user} |
| 105 |
|
| 106 |
|
| 107 |
class ProviderTokenView(APIView): |
| 108 |
input_class = ProviderTokenInput |
| 109 |
|
| 110 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 111 |
sociallogin = self.input.cleaned_data["sociallogin"] |
| 112 |
response = None |
| 113 |
try: |
| 114 |
response = complete_token_login(request, sociallogin) |
| 115 |
except ValidationError as e: |
| 116 |
return ErrorResponse(self.request, exception=e) |
| 117 |
except SignupClosedException: |
| 118 |
return ForbiddenResponse(self.request) |
| 119 |
return AuthenticationResponse.from_response(self.request, response) |