Skip to content

Commit 32107c0

Browse files
committed
Fix serializer logic
1 parent 48c5d27 commit 32107c0

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

drfpasswordless/serializers.py

+18-24
Original file line numberDiff line numberDiff line change
@@ -201,40 +201,35 @@ def validate(self, attrs):
201201
try:
202202
alias_type, alias = self.validate_alias(attrs)
203203
callback_token = attrs.get('token', None)
204-
token = CallbackToken.objects.get(**{alias_type: alias,
205-
'key': callback_token,
204+
token = CallbackToken.objects.get(**{'key': callback_token,
206205
'type': CallbackToken.TOKEN_TYPE_AUTH,
207206
'is_active': True})
208207

209-
if token:
208+
user = User.objects.get(**{alias_type: alias})
209+
210+
if token.user == user:
210211
# Check the token type for our uni-auth method.
211212
# authenticates and checks the expiry of the callback token.
212-
user = authenticate_by_token(token)
213-
if user:
214-
if not user.is_active:
215-
msg = _('User account is disabled.')
216-
raise serializers.ValidationError(msg)
213+
if not user.is_active:
214+
msg = _('User account is disabled.')
215+
raise serializers.ValidationError(msg)
217216

218-
if api_settings.PASSWORDLESS_USER_MARK_EMAIL_VERIFIED \
219-
or api_settings.PASSWORDLESS_USER_MARK_MOBILE_VERIFIED:
220-
# Mark this alias as verified
221-
user = User.objects.get(pk=token.user.pk)
222-
success = verify_user_alias(user, token)
217+
if api_settings.PASSWORDLESS_USER_MARK_EMAIL_VERIFIED \
218+
or api_settings.PASSWORDLESS_USER_MARK_MOBILE_VERIFIED:
219+
# Mark this alias as verified
220+
user = User.objects.get(pk=token.user.pk)
221+
success = verify_user_alias(user, token)
223222

224-
if success is False:
225-
msg = _('Error validating user alias.')
226-
raise serializers.ValidationError(msg)
223+
if success is False:
224+
msg = _('Error validating user alias.')
225+
raise serializers.ValidationError(msg)
227226

228-
attrs['user'] = user
229-
return attrs
227+
attrs['user'] = user
228+
return attrs
230229

231-
else:
232-
msg = _('Invalid Token')
233-
raise serializers.ValidationError(msg)
234230
else:
235-
msg = _('Missing authentication token.')
231+
msg = _('Invalid Token')
236232
raise serializers.ValidationError(msg)
237-
238233
except serializers.ValidationError():
239234
msg = _('Invalid alias parameters provided.')
240235
raise serializers.ValidationError(msg)
@@ -254,7 +249,6 @@ def validate(self, attrs):
254249
callback_token = attrs.get('token', None)
255250

256251
token = CallbackToken.objects.get(**{'user': user,
257-
alias_type: alias,
258252
'key': callback_token,
259253
'type': CallbackToken.TOKEN_TYPE_VERIFY,
260254
'is_active': True})

0 commit comments

Comments
 (0)