2016-03-02 22:33:27 +03:00
|
|
|
from django.conf import settings
|
2016-01-13 10:13:12 +03:00
|
|
|
from django.http import HttpRequest
|
|
|
|
from rest_framework import serializers
|
|
|
|
# Import is needed only if we are using social login, in which
|
|
|
|
# case the allauth.socialaccount will be declared
|
2016-03-02 22:33:27 +03:00
|
|
|
if 'allauth.socialaccount' in settings.INSTALLED_APPS:
|
2016-01-13 10:13:12 +03:00
|
|
|
from allauth.socialaccount.helpers import complete_social_login
|
2016-03-02 22:33:27 +03:00
|
|
|
from allauth.socialaccount.models import SocialToken
|
2016-03-02 22:59:01 +03:00
|
|
|
from allauth.socialaccount.providers.oauth.client import OAuthError
|
2016-01-13 10:13:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TwitterLoginSerializer(serializers.Serializer):
|
|
|
|
access_token = serializers.CharField(required=True)
|
|
|
|
token_secret = serializers.CharField(required=True)
|
|
|
|
|
|
|
|
def _get_request(self):
|
|
|
|
request = self.context.get('request')
|
|
|
|
if not isinstance(request, HttpRequest):
|
|
|
|
request = request._request
|
|
|
|
return request
|
|
|
|
|
|
|
|
def get_social_login(self, adapter, app, token, response):
|
|
|
|
"""
|
|
|
|
|
|
|
|
:param adapter: allauth.socialaccount Adapter subclass. Usually OAuthAdapter or Auth2Adapter
|
|
|
|
:param app: `allauth.socialaccount.SocialApp` instance
|
|
|
|
:param token: `allauth.socialaccount.SocialToken` instance
|
|
|
|
:param response: Provider's response for OAuth1. Not used in the
|
2016-10-25 00:23:44 +03:00
|
|
|
:returns: A populated instance of the `allauth.socialaccount.SocialLoginView` instance
|
2016-01-13 10:13:12 +03:00
|
|
|
"""
|
|
|
|
request = self._get_request()
|
|
|
|
social_login = adapter.complete_login(request, app, token, response=response)
|
|
|
|
social_login.token = token
|
|
|
|
return social_login
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
view = self.context.get('view')
|
|
|
|
request = self._get_request()
|
|
|
|
|
|
|
|
if not view:
|
|
|
|
raise serializers.ValidationError(
|
|
|
|
'View is not defined, pass it as a context variable'
|
|
|
|
)
|
|
|
|
|
|
|
|
adapter_class = getattr(view, 'adapter_class', None)
|
|
|
|
if not adapter_class:
|
|
|
|
raise serializers.ValidationError('Define adapter_class in view')
|
|
|
|
|
2016-07-18 08:06:28 +03:00
|
|
|
adapter = adapter_class(request)
|
2016-01-13 10:13:12 +03:00
|
|
|
app = adapter.get_provider().get_app(request)
|
|
|
|
|
2016-07-18 08:06:28 +03:00
|
|
|
access_token = attrs.get('access_token')
|
|
|
|
token_secret = attrs.get('token_secret')
|
2016-01-13 10:13:12 +03:00
|
|
|
|
|
|
|
request.session['oauth_api.twitter.com_access_token'] = {
|
2016-02-23 15:22:44 +03:00
|
|
|
'oauth_token': access_token,
|
|
|
|
'oauth_token_secret': token_secret,
|
2016-01-13 10:13:12 +03:00
|
|
|
}
|
|
|
|
token = SocialToken(token=access_token, token_secret=token_secret)
|
|
|
|
token.app = app
|
|
|
|
|
|
|
|
try:
|
|
|
|
login = self.get_social_login(adapter, app, token, access_token)
|
|
|
|
complete_social_login(request, login)
|
2016-03-02 22:59:01 +03:00
|
|
|
except OAuthError as e:
|
|
|
|
raise serializers.ValidationError(str(e))
|
2016-01-13 10:13:12 +03:00
|
|
|
|
|
|
|
if not login.is_existing:
|
|
|
|
login.lookup()
|
|
|
|
login.save(request, connect=True)
|
|
|
|
attrs['user'] = login.account.user
|
|
|
|
|
|
|
|
return attrs
|