2015-02-25 18:52:51 +03:00
|
|
|
from django.http import HttpRequest
|
2015-10-04 13:41:07 +03:00
|
|
|
from django.conf import settings
|
2016-02-02 17:29:16 +03:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2015-10-04 13:41:07 +03:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
try:
|
|
|
|
from allauth.account import app_settings as allauth_settings
|
|
|
|
from allauth.utils import (email_address_exists,
|
|
|
|
get_username_max_length)
|
|
|
|
from allauth.account.adapter import get_adapter
|
|
|
|
from allauth.account.utils import setup_user_email
|
|
|
|
except ImportError:
|
2016-12-22 01:08:56 +03:00
|
|
|
raise ImportError("allauth needs to be added to INSTALLED_APPS.")
|
2015-11-24 13:11:46 +03:00
|
|
|
|
2014-10-02 18:54:55 +04:00
|
|
|
from rest_framework import serializers
|
|
|
|
from requests.exceptions import HTTPError
|
2015-08-07 11:25:40 +03:00
|
|
|
# Import is needed only if we are using social login, in which
|
|
|
|
# case the allauth.socialaccount will be declared
|
2015-11-26 12:38:25 +03:00
|
|
|
if 'allauth.socialaccount' in settings.INSTALLED_APPS:
|
2016-03-02 22:33:27 +03:00
|
|
|
from allauth.socialaccount.helpers import complete_social_login
|
2015-11-26 12:38:25 +03:00
|
|
|
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
class SocialLoginSerializer(serializers.Serializer):
|
2015-08-17 13:34:59 +03:00
|
|
|
access_token = serializers.CharField(required=False, allow_blank=True)
|
|
|
|
code = serializers.CharField(required=False, allow_blank=True)
|
2014-10-02 18:54:55 +04:00
|
|
|
|
2015-07-23 22:22:39 +03:00
|
|
|
def _get_request(self):
|
2014-10-02 18:54:55 +04:00
|
|
|
request = self.context.get('request')
|
2015-02-25 18:52:51 +03:00
|
|
|
if not isinstance(request, HttpRequest):
|
|
|
|
request = request._request
|
2015-07-23 22:22:39 +03:00
|
|
|
return request
|
|
|
|
|
2015-07-23 22:33:48 +03:00
|
|
|
def get_social_login(self, adapter, app, token, response):
|
2015-07-23 22:22:39 +03:00
|
|
|
"""
|
2016-12-22 01:08:56 +03:00
|
|
|
:param adapter: allauth.socialaccount Adapter subclass.
|
|
|
|
Usually OAuthAdapter or Auth2Adapter
|
2015-07-23 22:22:39 +03:00
|
|
|
:param app: `allauth.socialaccount.SocialApp` instance
|
|
|
|
:param token: `allauth.socialaccount.SocialToken` instance
|
|
|
|
:param response: Provider's response for OAuth1. Not used in the
|
2016-12-22 01:08:56 +03:00
|
|
|
:returns: A populated instance of the
|
|
|
|
`allauth.socialaccount.SocialLoginView` instance
|
2015-07-23 22:22:39 +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()
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
if not view:
|
2015-04-28 11:22:08 +03:00
|
|
|
raise serializers.ValidationError(
|
2016-12-22 01:08:56 +03:00
|
|
|
_("View is not defined, pass it as a context variable")
|
2015-04-28 11:22:08 +03:00
|
|
|
)
|
|
|
|
|
2015-07-23 22:22:39 +03:00
|
|
|
adapter_class = getattr(view, 'adapter_class', None)
|
|
|
|
if not adapter_class:
|
2016-12-22 01:08:56 +03:00
|
|
|
raise serializers.ValidationError(_("Define adapter_class in view"))
|
2014-10-02 18:54:55 +04:00
|
|
|
|
2016-03-14 15:19:06 +03:00
|
|
|
adapter = adapter_class(request)
|
2015-07-23 22:22:39 +03:00
|
|
|
app = adapter.get_provider().get_app(request)
|
2015-08-07 13:26:57 +03:00
|
|
|
|
|
|
|
# More info on code vs access_token
|
|
|
|
# http://stackoverflow.com/questions/8666316/facebook-oauth-2-0-code-and-token
|
|
|
|
|
|
|
|
# Case 1: We received the access_token
|
2016-11-28 09:12:06 +03:00
|
|
|
if attrs.get('access_token'):
|
2015-08-07 13:26:57 +03:00
|
|
|
access_token = attrs.get('access_token')
|
|
|
|
|
|
|
|
# Case 2: We received the authorization code
|
2016-11-28 09:12:06 +03:00
|
|
|
elif attrs.get('code'):
|
2015-08-07 13:26:57 +03:00
|
|
|
self.callback_url = getattr(view, 'callback_url', None)
|
|
|
|
self.client_class = getattr(view, 'client_class', None)
|
|
|
|
|
|
|
|
if not self.callback_url:
|
|
|
|
raise serializers.ValidationError(
|
2016-12-22 01:08:56 +03:00
|
|
|
_("Define callback_url in view")
|
2015-08-07 13:26:57 +03:00
|
|
|
)
|
|
|
|
if not self.client_class:
|
|
|
|
raise serializers.ValidationError(
|
2016-12-22 01:08:56 +03:00
|
|
|
_("Define client_class in view")
|
2015-08-07 13:26:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
code = attrs.get('code')
|
|
|
|
|
2015-08-10 11:24:21 +03:00
|
|
|
provider = adapter.get_provider()
|
2015-08-07 13:26:57 +03:00
|
|
|
scope = provider.get_scope(request)
|
|
|
|
client = self.client_class(
|
|
|
|
request,
|
|
|
|
app.client_id,
|
|
|
|
app.secret,
|
2015-08-10 11:24:21 +03:00
|
|
|
adapter.access_token_method,
|
|
|
|
adapter.access_token_url,
|
2015-08-07 13:26:57 +03:00
|
|
|
self.callback_url,
|
|
|
|
scope
|
|
|
|
)
|
|
|
|
token = client.get_access_token(code)
|
|
|
|
access_token = token['access_token']
|
2015-08-14 14:49:47 +03:00
|
|
|
|
2015-08-13 10:56:25 +03:00
|
|
|
else:
|
2016-12-22 01:08:56 +03:00
|
|
|
raise serializers.ValidationError(
|
|
|
|
_("Incorrect input. access_token or code is required."))
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2016-12-03 03:35:13 +03:00
|
|
|
social_token = adapter.parse_token({'access_token': access_token})
|
|
|
|
social_token.app = app
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
try:
|
2016-12-03 03:35:13 +03:00
|
|
|
login = self.get_social_login(adapter, app, social_token, access_token)
|
2014-10-02 18:54:55 +04:00
|
|
|
complete_social_login(request, login)
|
|
|
|
except HTTPError:
|
2016-02-02 17:29:16 +03:00
|
|
|
raise serializers.ValidationError(_('Incorrect value'))
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
if not login.is_existing:
|
|
|
|
login.lookup()
|
|
|
|
login.save(request, connect=True)
|
2015-01-09 14:05:14 +03:00
|
|
|
attrs['user'] = login.account.user
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
return attrs
|
2015-11-24 13:11:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
class RegisterSerializer(serializers.Serializer):
|
|
|
|
username = serializers.CharField(
|
2016-01-05 16:56:11 +03:00
|
|
|
max_length=get_username_max_length(),
|
|
|
|
min_length=allauth_settings.USERNAME_MIN_LENGTH,
|
|
|
|
required=allauth_settings.USERNAME_REQUIRED
|
|
|
|
)
|
2015-11-24 13:11:46 +03:00
|
|
|
email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED)
|
2016-12-22 00:47:24 +03:00
|
|
|
password1 = serializers.CharField(write_only=True)
|
|
|
|
password2 = serializers.CharField(write_only=True)
|
2015-11-24 13:11:46 +03:00
|
|
|
|
|
|
|
def validate_username(self, username):
|
|
|
|
username = get_adapter().clean_username(username)
|
|
|
|
return username
|
|
|
|
|
|
|
|
def validate_email(self, email):
|
|
|
|
email = get_adapter().clean_email(email)
|
|
|
|
if allauth_settings.UNIQUE_EMAIL:
|
|
|
|
if email and email_address_exists(email):
|
|
|
|
raise serializers.ValidationError(
|
2016-02-02 17:29:16 +03:00
|
|
|
_("A user is already registered with this e-mail address."))
|
2015-11-24 13:11:46 +03:00
|
|
|
return email
|
|
|
|
|
2016-01-05 16:56:11 +03:00
|
|
|
def validate_password1(self, password):
|
2015-11-24 13:11:46 +03:00
|
|
|
return get_adapter().clean_password(password)
|
|
|
|
|
2016-01-05 16:56:11 +03:00
|
|
|
def validate(self, data):
|
|
|
|
if data['password1'] != data['password2']:
|
2016-02-02 17:29:16 +03:00
|
|
|
raise serializers.ValidationError(_("The two password fields didn't match."))
|
2016-01-05 16:56:11 +03:00
|
|
|
return data
|
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
def custom_signup(self, request, user):
|
|
|
|
pass
|
|
|
|
|
2015-11-24 18:04:57 +03:00
|
|
|
def get_cleaned_data(self):
|
|
|
|
return {
|
|
|
|
'username': self.validated_data.get('username', ''),
|
2016-01-05 16:56:11 +03:00
|
|
|
'password1': self.validated_data.get('password1', ''),
|
2015-11-24 18:04:57 +03:00
|
|
|
'email': self.validated_data.get('email', '')
|
|
|
|
}
|
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
def save(self, request):
|
|
|
|
adapter = get_adapter()
|
|
|
|
user = adapter.new_user(request)
|
2015-11-24 18:04:57 +03:00
|
|
|
self.cleaned_data = self.get_cleaned_data()
|
2015-11-24 13:11:46 +03:00
|
|
|
adapter.save_user(request, user, self)
|
|
|
|
self.custom_signup(request, user)
|
|
|
|
setup_user_email(request, user, [])
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
class VerifyEmailSerializer(serializers.Serializer):
|
|
|
|
key = serializers.CharField()
|