2020-03-01 08:56:21 +03:00
|
|
|
from allauth.account import app_settings as allauth_settings
|
2018-01-19 05:08:41 +03:00
|
|
|
from allauth.account.adapter import get_adapter
|
2014-10-01 16:13:21 +04:00
|
|
|
from allauth.account.utils import complete_signup
|
2020-03-01 08:56:21 +03:00
|
|
|
from allauth.account.views import ConfirmEmailView
|
2018-01-19 05:08:41 +03:00
|
|
|
from allauth.socialaccount import signals
|
|
|
|
from allauth.socialaccount.adapter import get_adapter as get_social_adapter
|
|
|
|
from allauth.socialaccount.models import SocialAccount
|
2020-03-01 08:56:21 +03:00
|
|
|
from dj_rest_auth.app_settings import (JWTSerializer, TokenSerializer,
|
|
|
|
create_token)
|
2020-03-01 04:54:53 +03:00
|
|
|
from dj_rest_auth.models import TokenModel
|
2020-03-01 08:56:21 +03:00
|
|
|
from dj_rest_auth.registration.serializers import (SocialAccountSerializer,
|
|
|
|
SocialConnectSerializer,
|
|
|
|
SocialLoginSerializer,
|
|
|
|
VerifyEmailSerializer)
|
2020-03-01 04:54:53 +03:00
|
|
|
from dj_rest_auth.utils import jwt_encode
|
|
|
|
from dj_rest_auth.views import LoginView
|
2020-03-01 08:56:21 +03:00
|
|
|
from django.conf import settings
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.views.decorators.debug import sensitive_post_parameters
|
|
|
|
from rest_framework import status
|
2020-06-01 23:53:33 +03:00
|
|
|
from rest_framework.exceptions import NotFound, MethodNotAllowed
|
2020-03-01 08:56:21 +03:00
|
|
|
from rest_framework.generics import CreateAPIView, GenericAPIView, ListAPIView
|
|
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2017-02-13 02:35:28 +03:00
|
|
|
from .app_settings import RegisterSerializer, register_permission_classes
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2016-12-31 23:55:19 +03:00
|
|
|
sensitive_post_parameters_m = method_decorator(
|
|
|
|
sensitive_post_parameters('password1', 'password2')
|
|
|
|
)
|
2016-01-04 20:45:33 +03:00
|
|
|
|
2016-03-01 14:51:01 +03:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
class RegisterView(CreateAPIView):
|
|
|
|
serializer_class = RegisterSerializer
|
2017-02-13 02:35:28 +03:00
|
|
|
permission_classes = register_permission_classes()
|
2016-01-01 00:10:52 +03:00
|
|
|
token_model = TokenModel
|
2020-06-03 16:17:15 +03:00
|
|
|
throttle_scope = 'dj_rest_auth'
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2016-12-31 23:55:19 +03:00
|
|
|
@sensitive_post_parameters_m
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(RegisterView, self).dispatch(*args, **kwargs)
|
|
|
|
|
2016-01-12 00:33:14 +03:00
|
|
|
def get_response_data(self, user):
|
|
|
|
if allauth_settings.EMAIL_VERIFICATION == \
|
|
|
|
allauth_settings.EmailVerificationMethod.MANDATORY:
|
2016-12-22 01:08:56 +03:00
|
|
|
return {"detail": _("Verification e-mail sent.")}
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2016-02-16 08:42:18 +03:00
|
|
|
if getattr(settings, 'REST_USE_JWT', False):
|
|
|
|
data = {
|
|
|
|
'user': user,
|
2020-03-11 13:05:11 +03:00
|
|
|
'access_token': self.access_token,
|
|
|
|
'refresh_token': self.refresh_token
|
2016-02-16 08:42:18 +03:00
|
|
|
}
|
2020-04-21 18:17:57 +03:00
|
|
|
return JWTSerializer(data, context=self.get_serializer_context()).data
|
2016-02-16 08:42:18 +03:00
|
|
|
else:
|
2020-04-21 18:17:57 +03:00
|
|
|
return TokenSerializer(user.auth_token, context=self.get_serializer_context()).data
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
user = self.perform_create(serializer)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
2014-11-12 05:52:06 +03:00
|
|
|
|
2016-12-22 01:08:56 +03:00
|
|
|
return Response(self.get_response_data(user),
|
|
|
|
status=status.HTTP_201_CREATED,
|
|
|
|
headers=headers)
|
2015-11-06 16:07:12 +03:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
user = serializer.save(self.request)
|
2016-02-16 08:42:18 +03:00
|
|
|
if getattr(settings, 'REST_USE_JWT', False):
|
2020-03-11 13:05:11 +03:00
|
|
|
self.access_token, self.refresh_token = jwt_encode(user)
|
2016-02-16 08:42:18 +03:00
|
|
|
else:
|
|
|
|
create_token(self.token_model, user, serializer)
|
2016-07-28 21:14:26 +03:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
complete_signup(self.request._request, user,
|
|
|
|
allauth_settings.EMAIL_VERIFICATION,
|
2015-11-24 17:16:39 +03:00
|
|
|
None)
|
2015-11-24 13:11:46 +03:00
|
|
|
return user
|
2014-10-01 16:13:21 +04:00
|
|
|
|
|
|
|
|
2015-08-07 13:54:45 +03:00
|
|
|
class VerifyEmailView(APIView, ConfirmEmailView):
|
2014-10-01 18:34:51 +04:00
|
|
|
permission_classes = (AllowAny,)
|
2014-11-12 12:33:29 +03:00
|
|
|
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
|
|
|
|
2016-12-09 12:53:09 +03:00
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
return VerifyEmailSerializer(*args, **kwargs)
|
|
|
|
|
2020-06-01 23:53:33 +03:00
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
raise MethodNotAllowed('GET')
|
|
|
|
|
2014-10-01 18:34:51 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
2016-12-09 12:53:09 +03:00
|
|
|
serializer = self.get_serializer(data=request.data)
|
2015-11-24 13:11:46 +03:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
self.kwargs['key'] = serializer.validated_data['key']
|
2014-10-01 18:34:51 +04:00
|
|
|
confirmation = self.get_object()
|
|
|
|
confirmation.confirm(self.request)
|
2016-03-31 11:58:14 +03:00
|
|
|
return Response({'detail': _('ok')}, status=status.HTTP_200_OK)
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialLoginView(LoginView):
|
|
|
|
"""
|
|
|
|
class used for social authentications
|
|
|
|
example usage for facebook with access_token
|
|
|
|
-------------
|
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
2014-10-02 18:54:55 +04:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class FacebookLogin(SocialLoginView):
|
|
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
-------------
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
example usage for facebook with code
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
-------------
|
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
|
|
|
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class FacebookLogin(SocialLoginView):
|
|
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
client_class = OAuth2Client
|
|
|
|
callback_url = 'localhost:8000'
|
|
|
|
-------------
|
|
|
|
"""
|
|
|
|
serializer_class = SocialLoginSerializer
|
2014-10-02 18:54:55 +04:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def process_login(self):
|
|
|
|
get_adapter(self.request).login(self.request, self.user)
|
2016-07-18 08:06:28 +03:00
|
|
|
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialConnectView(LoginView):
|
|
|
|
"""
|
|
|
|
class used for social account linking
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
example usage for facebook with access_token
|
|
|
|
-------------
|
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class FacebookConnect(SocialConnectView):
|
|
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
-------------
|
|
|
|
"""
|
|
|
|
serializer_class = SocialConnectSerializer
|
|
|
|
permission_classes = (IsAuthenticated,)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def process_login(self):
|
|
|
|
get_adapter(self.request).login(self.request, self.user)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialAccountListView(ListAPIView):
|
|
|
|
"""
|
|
|
|
List SocialAccounts for the currently logged in user
|
|
|
|
"""
|
|
|
|
serializer_class = SocialAccountSerializer
|
|
|
|
permission_classes = (IsAuthenticated,)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def get_queryset(self):
|
|
|
|
return SocialAccount.objects.filter(user=self.request.user)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialAccountDisconnectView(GenericAPIView):
|
|
|
|
"""
|
|
|
|
Disconnect SocialAccount from remote service for
|
|
|
|
the currently logged in user
|
|
|
|
"""
|
|
|
|
serializer_class = SocialConnectSerializer
|
|
|
|
permission_classes = (IsAuthenticated,)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def get_queryset(self):
|
|
|
|
return SocialAccount.objects.filter(user=self.request.user)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
accounts = self.get_queryset()
|
|
|
|
account = accounts.filter(pk=kwargs['pk']).first()
|
|
|
|
if not account:
|
|
|
|
raise NotFound
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
get_social_adapter(self.request).validate_disconnect(account, accounts)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
account.delete()
|
|
|
|
signals.social_account_removed.send(
|
|
|
|
sender=SocialAccount,
|
|
|
|
request=self.request,
|
|
|
|
socialaccount=account
|
|
|
|
)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
return Response(self.get_serializer(account).data)
|