Skip to content
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

Limit OTP Attempts #11

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion drfpasswordless/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (1, 6, 3)
VERSION = (1, 6, 4)

__version__ = '.'.join(map(str, VERSION))
18 changes: 18 additions & 0 deletions drfpasswordless/migrations/0006_callbacktoken_attempts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.25 on 2025-03-20 05:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('drfpasswordless', '0005_auto_20201117_0410'),
]

operations = [
migrations.AddField(
model_name='callbacktoken',
name='attempts',
field=models.IntegerField(default=0),
),
]
10 changes: 10 additions & 0 deletions drfpasswordless/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ class CallbackToken(AbstractBaseCallbackToken):

key = models.CharField(default=generate_numeric_token, max_length=6)
type = models.CharField(max_length=20, choices=TOKEN_TYPES)
attempts = models.IntegerField(default=0)

class Meta(AbstractBaseCallbackToken.Meta):
verbose_name = 'Callback Token'

def increment_attempts(self):
"""
Increment the number of attempts and deactivate if max attempts reached
"""
self.attempts += 1
if self.attempts > 3: # Max 3 attempts
self.is_active = False
self.save()
15 changes: 13 additions & 2 deletions drfpasswordless/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ class AbstractBaseCallbackTokenSerializer(serializers.Serializer):
token = TokenField(
min_length=api_settings.PASSWORDLESS_TOKEN_LENGTH,
max_length=api_settings.PASSWORDLESS_TOKEN_LENGTH,
validators=[token_age_validator]
)

def validate_alias(self, attrs):
Expand Down Expand Up @@ -209,6 +208,18 @@ def validate(self, attrs):
alias_type, alias = self.validate_alias(attrs)
callback_token = attrs.get('token', None)
user = User.objects.get(**{alias_type+'__iexact': alias})


#increment the attempt
token = CallbackToken.objects.filter(**{'user': user,
'type': CallbackToken.TOKEN_TYPE_AUTH,
'is_active': True}).first()

if token:
token.increment_attempts()

validate_token_age(callback_token)

token = CallbackToken.objects.get(**{'user': user,
'key': callback_token,
'type': CallbackToken.TOKEN_TYPE_AUTH,
Expand Down Expand Up @@ -238,7 +249,7 @@ def validate(self, attrs):
msg = _('Invalid Token')
raise serializers.ValidationError(msg)
except CallbackToken.DoesNotExist:
msg = _('Invalid alias parameters provided.')
msg = _('Invalid OTP or OTP may be expired')
raise serializers.ValidationError(msg)
except User.DoesNotExist:
msg = _('Invalid user alias parameters provided.')
Expand Down