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

issue #145 fix to use any static backup device instead of strictly named #560

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions tests/test_views_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,40 @@ def test_with_backup_token(self, mock_signal):
# Check that the signal was fired.
mock_signal.assert_called_with(sender=mock.ANY, request=mock.ANY, user=user, device=device)

@mock.patch('two_factor.views.core.signals.user_verified.send')
def test_with_alter_backup_token(self, mock_signal):
user = self.create_user()
user.totpdevice_set.create(name='default', key=random_hex())
device = user.staticdevice_set.create(name='alter')
device.token_set.create(token='abcdef123')

# Backup phones should be listed on the login form
response = self._post({'auth-username': '[email protected]',
'auth-password': 'secret',
'login_view-current_step': 'auth'})
self.assertContains(response, 'Backup Token')

# Should be able to go to backup tokens step in wizard
response = self._post({'wizard_goto_step': 'backup'})
self.assertContains(response, 'backup tokens')

# Wrong codes should not be accepted
response = self._post({'backup-otp_token': 'WRONG',
'login_view-current_step': 'backup'})
self.assertEqual(response.context_data['wizard']['form'].errors,
{'__all__': ['Invalid token. Please make sure you '
'have entered it correctly.']})
# static devices are throttled
device.throttle_reset()

# Valid token should be accepted.
response = self._post({'backup-otp_token': 'abcdef123',
'login_view-current_step': 'backup'})
self.assertRedirects(response, resolve_url(settings.LOGIN_REDIRECT_URL))

# Check that the signal was fired.
mock_signal.assert_called_with(sender=mock.ANY, request=mock.ANY, user=user, device=device)

@mock.patch('two_factor.views.utils.logger')
def test_reset_wizard_state(self, mock_logger):
self.create_user()
Expand Down
23 changes: 23 additions & 0 deletions tests/test_yubikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ def test_show_correct_label(self):
self.assertNotContains(response, 'YubiKey:')
self.assertContains(response, 'Token:')

def test_show_correct_label_alter_backup(self):
"""
The token form replaces the input field when the user's device is a
YubiKey. However when the user decides to enter a backup token, the
normal backup token form should be shown. Refs #50.
"""
user = self.create_user()
service = ValidationService.objects.create(name='default', param_sl='', param_timeout='')
user.remoteyubikeydevice_set.create(service=service, name='default')
backup = user.staticdevice_set.create(name='alter')
backup.token_set.create(token='RANDOM')

response = self.client.post(reverse('two_factor:login'),
data={'auth-username': '[email protected]',
'auth-password': 'secret',
'login_view-current_step': 'auth'})
self.assertContains(response, 'YubiKey:')

response = self.client.post(reverse('two_factor:login'),
data={'wizard_goto_step': 'backup'})
self.assertNotContains(response, 'YubiKey:')
self.assertContains(response, 'Token:')

def test_missing_management_data(self):
# missing management data
response = self.client.post(reverse('two_factor:login'),
Expand Down
20 changes: 8 additions & 12 deletions two_factor/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from django.views.generic.base import View
from django_otp import devices_for_user
from django_otp.decorators import otp_required
from django_otp.plugins.otp_static.models import StaticDevice, StaticToken
from django_otp.plugins.otp_static.models import StaticToken
from django_otp.util import random_hex

from two_factor import signals
Expand Down Expand Up @@ -288,10 +288,7 @@ def get_device(self, step=None):
break

if step == 'backup':
try:
self.device_cache = self.get_user().staticdevice_set.get(name='backup')
except StaticDevice.DoesNotExist:
pass
self.device_cache = self.get_user().staticdevice_set.all().first()

if not self.device_cache:
self.device_cache = default_device(self.get_user())
Expand Down Expand Up @@ -344,12 +341,8 @@ def get_context_data(self, form, **kwargs):
device = self.get_device()
context['device'] = device
context['other_devices'] = self.get_other_devices(device)

try:
context['backup_tokens'] = self.get_user().staticdevice_set\
.get(name='backup').token_set.count()
except StaticDevice.DoesNotExist:
context['backup_tokens'] = 0
context['backup_tokens'] = self.get_user().staticdevice_set\
.all().values('token_set__token').count()

if getattr(settings, 'LOGOUT_REDIRECT_URL', None):
context['cancel_url'] = resolve_url(settings.LOGOUT_REDIRECT_URL)
Expand Down Expand Up @@ -642,7 +635,10 @@ class BackupTokensView(FormView):
number_of_tokens = 10

def get_device(self):
return self.request.user.staticdevice_set.get_or_create(name='backup')[0]
device = self.request.user.staticdevice_set.all().first()
if not device:
device = self.request.user.staticdevice_set.create(name='backup')
return device

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand Down