Skip to content

Bug fix for custom email and mobile fields #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions drfpasswordless/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def alias_type(self):
# The alias type, either email or mobile
raise NotImplementedError

@property
def user_field_type(self):
# The user field, either email or mobile
raise NotImplementedError

def validate(self, attrs):
alias = attrs.get(self.alias_type)

Expand All @@ -44,15 +49,15 @@ def validate(self, attrs):
if api_settings.PASSWORDLESS_REGISTER_NEW_USERS is True:
# If new aliases should register new users.
try:
user = User.objects.get(**{self.alias_type+'__iexact': alias})
user = User.objects.get(**{self.user_field_type+'__iexact': alias})
except User.DoesNotExist:
user = User.objects.create(**{self.alias_type: alias})
user = User.objects.create(**{self.user_field_type: alias})
user.set_unusable_password()
user.save()
else:
# If new aliases should not register new users.
try:
user = User.objects.get(**{self.alias_type+'__iexact': alias})
user = User.objects.get(**{self.user_field_type+'__iexact': alias})
except User.DoesNotExist:
user = None

Expand All @@ -77,6 +82,10 @@ class EmailAuthSerializer(AbstractBaseAliasAuthenticationSerializer):
def alias_type(self):
return 'email'

@property
def user_field_type(self):
return api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME

email = serializers.EmailField()


Expand All @@ -85,6 +94,10 @@ class MobileAuthSerializer(AbstractBaseAliasAuthenticationSerializer):
def alias_type(self):
return 'mobile'

@property
def user_field_type(self):
return api_settings.PASSWORDLESS_USER_MOBILE_FIELD_NAME

phone_regex = RegexValidator(regex=r'^\+[1-9]\d{1,14}$',
message="Mobile number must be entered in the format:"
" '+999999999'. Up to 15 digits allowed.")
Expand All @@ -106,11 +119,16 @@ def alias_type(self):
# The alias type, either email or mobile
raise NotImplementedError

@property
def user_field_type(self):
# The user field from settings
raise NotImplementedError

def validate(self, attrs):

msg = _('There was a problem with your request.')

if self.alias_type:
if self.user_field_type:
# Get request.user
# Get their specified valid endpoint
# Validate
Expand All @@ -124,15 +142,15 @@ def validate(self, attrs):
msg = _('User account is disabled.')

else:
if hasattr(user, self.alias_type):
if hasattr(user, self.user_field_type):
# Has the appropriate alias type
attrs['user'] = user
return attrs
else:
msg = _('This user doesn\'t have an %s.' % self.alias_type)
msg = _('This user doesn\'t have an %s.' % self.user_field_type)
raise serializers.ValidationError(msg)
else:
msg = _('Missing %s.') % self.alias_type
msg = _('Missing %s.') % self.user_field_type
raise serializers.ValidationError(msg)


Expand All @@ -141,12 +159,20 @@ class EmailVerificationSerializer(AbstractBaseAliasVerificationSerializer):
def alias_type(self):
return 'email'

@property
def user_field_type(self):
return api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME


class MobileVerificationSerializer(AbstractBaseAliasVerificationSerializer):
@property
def alias_type(self):
return 'mobile'

@property
def user_field_type(self):
return api_settings.PASSWORDLESS_USER_MOBILE_FIELD_NAME


"""
Callback Token
Expand Down Expand Up @@ -178,6 +204,9 @@ class AbstractBaseCallbackTokenSerializer(serializers.Serializer):
token = TokenField(min_length=6, max_length=6, validators=[token_age_validator])

def validate_alias(self, attrs):
email_type = api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME
mobile_type = api_settings.PASSWORDLESS_USER_MOBILE_FIELD_NAME

email = attrs.get('email', None)
mobile = attrs.get('mobile', None)

Expand All @@ -188,9 +217,9 @@ def validate_alias(self, attrs):
raise serializers.ValidationError()

if email:
return 'email', email
return email_type, email
elif mobile:
return 'mobile', mobile
return mobile_type, mobile

return None

Expand Down