|
5 | 5 |
|
6 | 6 | from rest_framework.views import APIView
|
7 | 7 | from rest_framework.response import Response
|
8 |
| -from rest_framework.permissions import AllowAny |
| 8 | +from rest_framework.permissions import (AllowAny, |
| 9 | + IsAuthenticated) |
| 10 | +from rest_framework.decorators import detail_route |
| 11 | +from rest_framework.viewsets import GenericViewSet |
9 | 12 | from rest_framework.generics import CreateAPIView
|
10 | 13 | from rest_framework import status
|
11 | 14 |
|
12 |
| -from allauth.account.adapter import get_adapter |
13 | 15 | from allauth.account.views import ConfirmEmailView
|
14 | 16 | from allauth.account.utils import complete_signup
|
15 | 17 | from allauth.account import app_settings as allauth_settings
|
|
19 | 21 | create_token)
|
20 | 22 | from rest_auth.models import TokenModel
|
21 | 23 | from rest_auth.registration.serializers import (SocialLoginSerializer,
|
22 |
| - VerifyEmailSerializer) |
| 24 | + VerifyEmailSerializer, |
| 25 | + SocialConnectSerializer) |
23 | 26 | from rest_auth.utils import jwt_encode
|
24 | 27 | from rest_auth.views import LoginView
|
25 | 28 | from .app_settings import RegisterSerializer, register_permission_classes
|
@@ -91,31 +94,98 @@ def post(self, request, *args, **kwargs):
|
91 | 94 | return Response({'detail': _('ok')}, status=status.HTTP_200_OK)
|
92 | 95 |
|
93 | 96 |
|
94 |
| -class SocialLoginView(LoginView): |
95 |
| - """ |
96 |
| - class used for social authentications |
97 |
| - example usage for facebook with access_token |
98 |
| - ------------- |
99 |
| - from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
| 97 | +if 'allauth.socialaccount' in settings.INSTALLED_APPS: |
| 98 | + from allauth.socialaccount import signals |
| 99 | + from allauth.socialaccount.models import SocialAccount |
| 100 | + from allauth.socialaccount.adapter import get_adapter |
100 | 101 |
|
101 |
| - class FacebookLogin(SocialLoginView): |
102 |
| - adapter_class = FacebookOAuth2Adapter |
103 |
| - ------------- |
| 102 | + from rest_auth.registration.serializers import SocialAccountSerializer |
104 | 103 |
|
105 |
| - example usage for facebook with code |
106 | 104 |
|
107 |
| - ------------- |
108 |
| - from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
109 |
| - from allauth.socialaccount.providers.oauth2.client import OAuth2Client |
| 105 | + class SocialLoginView(LoginView): |
| 106 | + """ |
| 107 | + class used for social authentications |
| 108 | + example usage for facebook with access_token |
| 109 | + ------------- |
| 110 | + from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
110 | 111 |
|
111 |
| - class FacebookLogin(SocialLoginView): |
112 |
| - adapter_class = FacebookOAuth2Adapter |
113 |
| - client_class = OAuth2Client |
114 |
| - callback_url = 'localhost:8000' |
115 |
| - ------------- |
116 |
| - """ |
| 112 | + class FacebookLogin(SocialLoginView): |
| 113 | + adapter_class = FacebookOAuth2Adapter |
| 114 | + ------------- |
117 | 115 |
|
118 |
| - serializer_class = SocialLoginSerializer |
| 116 | + example usage for facebook with code |
119 | 117 |
|
120 |
| - def process_login(self): |
121 |
| - get_adapter(self.request).login(self.request, self.user) |
| 118 | + ------------- |
| 119 | + from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
| 120 | + from allauth.socialaccount.providers.oauth2.client import OAuth2Client |
| 121 | +
|
| 122 | + class FacebookLogin(SocialLoginView): |
| 123 | + adapter_class = FacebookOAuth2Adapter |
| 124 | + client_class = OAuth2Client |
| 125 | + callback_url = 'localhost:8000' |
| 126 | + ------------- |
| 127 | + """ |
| 128 | + |
| 129 | + serializer_class = SocialLoginSerializer |
| 130 | + |
| 131 | + def process_login(self): |
| 132 | + get_adapter(self.request).login(self.request, self.user) |
| 133 | + |
| 134 | + |
| 135 | + class SocialConnectView(SocialLoginView): |
| 136 | + """ |
| 137 | + class used for social account linking |
| 138 | +
|
| 139 | + example usage for facebook with access_token |
| 140 | + ------------- |
| 141 | + from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
| 142 | +
|
| 143 | + class FacebookConnect(SocialConnectView): |
| 144 | + adapter_class = FacebookOAuth2Adapter |
| 145 | + ------------- |
| 146 | + """ |
| 147 | + |
| 148 | + serializer_class = SocialConnectSerializer |
| 149 | + permission_classes = (IsAuthenticated,) |
| 150 | + |
| 151 | + |
| 152 | + class SocialAccountViewSet(GenericViewSet): |
| 153 | + """ |
| 154 | + allauth SocialAccount REST API read and disconnect views for logged in users |
| 155 | +
|
| 156 | + Refer to the django-allauth package implementation of the models and |
| 157 | + account handling logic for more details, this viewset emulates the allauth web UI. |
| 158 | + """ |
| 159 | + |
| 160 | + serializer_class = SocialAccountSerializer |
| 161 | + permission_classes = (IsAuthenticated,) |
| 162 | + queryset = SocialAccount.objects.none() |
| 163 | + |
| 164 | + def get_queryset(self): |
| 165 | + return SocialAccount.objects.filter(user=self.request.user) |
| 166 | + |
| 167 | + def list(self, request): |
| 168 | + """ |
| 169 | + list SocialAccounts for the currently logged in user |
| 170 | + """ |
| 171 | + |
| 172 | + return Response(self.get_serializer(self.get_queryset(), many=True).data) |
| 173 | + |
| 174 | + @detail_route(methods=['POST']) |
| 175 | + def disconnect(self, request, pk): |
| 176 | + """ |
| 177 | + disconnect SocialAccount from remote service for the currently logged in user |
| 178 | + """ |
| 179 | + |
| 180 | + accounts = self.get_queryset() |
| 181 | + account = accounts.get(pk=pk) |
| 182 | + get_adapter(self.request).validate_disconnect(account, accounts) |
| 183 | + |
| 184 | + account.delete() |
| 185 | + signals.social_account_removed.send( |
| 186 | + sender=SocialAccount, |
| 187 | + request=self.request, |
| 188 | + socialaccount=account |
| 189 | + ) |
| 190 | + |
| 191 | + return Response(self.get_serializer(account).data) |
0 commit comments