django-rest-auth/rest_auth/registration/serializers.py

43 lines
1.4 KiB
Python
Raw Normal View History

from rest_framework import serializers
from requests.exceptions import HTTPError
from allauth.socialaccount.helpers import complete_social_login
class SocialLoginSerializer(serializers.Serializer):
access_token = serializers.CharField(required=True)
def validate_access_token(self, value):
view = self.context.get('view')
request = self.context.get('request')
if not view:
2014-10-09 13:41:52 +04:00
raise serializers.ValidationError('View is not defined, pass it ' +
'as a context variable')
self.adapter_class = getattr(view, 'adapter_class', None)
if not self.adapter_class:
raise serializers.ValidationError('Define adapter_class in view')
self.adapter = self.adapter_class()
app = self.adapter.get_provider().get_app(request)
token = self.adapter.parse_token({'access_token': value})
token.app = app
try:
login = self.adapter.complete_login(request, app, token,
response=value)
token.account = login.account
login.token = token
complete_social_login(request, login)
except HTTPError:
raise serializers.ValidationError('Incorrect value')
if not login.is_existing:
login.lookup()
login.save(request, connect=True)
self.object = {'user': login.account.user}
return value