# Path: oath_toolkit/django_otp/totp/models.py
# class OToolkitTOTPDevice(OToolkitDevice):
# 
#     """
#     TOTP-based django-otp_ ``Device``, using :mod:`oath_toolkit`.
# 
#     .. _django-otp: https://pypi.python.org/pypi/django-otp
# 
#     .. attribute:: time_step_size
# 
#         The time step, in seconds. In
#         :class:`django_otp.plugins.otp_totp.models.TOTPDevice`, this field is
#         named ``step``.
# 
#         Defaults to 30.
# 
#         :type: :class:`django.db.models.PositiveSmallIntegerField`
# 
#     .. attribute:: start_offset
# 
#         The UNIX timestamp of when to start counting time steps. In
#         :class:`django_otp.plugins.otp_totp.models.TOTPDevice`, this field is
#         named ``t0``.
# 
#         Defaults to ``0``.
# 
#         :type: :class:`django.db.models.BigIntegerField`
#     """
# 
#     select_name = __(u'Time-based OTP (TOTP) generator')
#     oath_type = b'totp'
# 
#     time_step_size = PositiveSmallIntegerField(default=30)
#     start_offset = BigIntegerField(default=0)
# 
#     class Meta:
#         verbose_name = u'OATH Toolkit TOTP Device'
# 
#     def verify_token(self, token):
#         verified = self._do_verify_token(token, self.oath.totp_validate,
#                                          time(), self.time_step_size,
#                                          self.start_offset)
#         if verified is not False:
#             verified = True
#         return verified
# 
# Path: examples/django/example/forms.py
# class DeviceValidationForm(forms.Form):
#     '''Validates that a OTP device is working properly.'''
#     token = forms.IntegerField(label=_(u'Token'), min_value=1,
#                                max_value=999999)
# 
#     def __init__(self, device, *args, **kwargs):
#         super(DeviceValidationForm, self).__init__(*args, **kwargs)
#         self.device = device
# 
#     def clean_token(self):
#         token = self.cleaned_data['token']
#         if not self.device.verify_token(token):
#             raise forms.ValidationError(_(u'Entered token is not valid'))
#         return token


from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render, resolve_url
from django.utils.http import is_safe_url
from django.utils.translation import ugettext as u_, ugettext_lazy as ul_
from django_otp import devices_for_user, login as otp_login
from django_otp.decorators import otp_required
from django_otp.forms import OTPTokenForm
from oath_toolkit.django_otp.totp.models import (
    OToolkitTOTPDevice as TOTPDevice)
from .forms import DeviceValidationForm
        form = DeviceValidationForm(device)
        return render(request, 'otp_setup.html', {
            'device': device,
            'form': form,
        })
    elif request.method == 'POST':
        if request.POST.get(u'enable') and not tfa_enabled:
            device = TOTPDevice.objects.create(user=request.user,
                                               name=u'TOTP',
                                               confirmed=False)
            form = DeviceValidationForm(device)
            return render(request, 'otp_setup.html', {
                'device': device,
                'form': form,
            })
        elif request.POST.get(u'disable') and tfa_enabled:
            for device in confirmed_devices:
                device.delete()
            messages.success(request, u_(
                u'Disabled two-factor authentication for this account.'))
        else:
            messages.error(request, u_(u'Unknown error.'))
    return redirect('otp_status')


@login_required
def otp_setup_verify(request):
    if request.method == 'POST':
        unconfirmed_devices = list(devices_for_user(request.user, False))
        device = unconfirmed_devices[0]

# Path: oath_toolkit/django_otp/totp/models.py
# class OToolkitTOTPDevice(OToolkitDevice):
# 
#     """
#     TOTP-based django-otp_ ``Device``, using :mod:`oath_toolkit`.
# 
#     .. _django-otp: https://pypi.python.org/pypi/django-otp
# 
#     .. attribute:: time_step_size
# 
#         The time step, in seconds. In
#         :class:`django_otp.plugins.otp_totp.models.TOTPDevice`, this field is
#         named ``step``.
# 
#         Defaults to 30.
# 
#         :type: :class:`django.db.models.PositiveSmallIntegerField`
# 
#     .. attribute:: start_offset
# 
#         The UNIX timestamp of when to start counting time steps. In
#         :class:`django_otp.plugins.otp_totp.models.TOTPDevice`, this field is
#         named ``t0``.
# 
#         Defaults to ``0``.
# 
#         :type: :class:`django.db.models.BigIntegerField`
#     """
# 
#     select_name = __(u'Time-based OTP (TOTP) generator')
#     oath_type = b'totp'
# 
#     time_step_size = PositiveSmallIntegerField(default=30)
#     start_offset = BigIntegerField(default=0)
# 
#     class Meta:
#         verbose_name = u'OATH Toolkit TOTP Device'
# 
#     def verify_token(self, token):
#         verified = self._do_verify_token(token, self.oath.totp_validate,
#                                          time(), self.time_step_size,
#                                          self.start_offset)
#         if verified is not False:
#             verified = True
#         return verified
# 
# Path: examples/django/example/forms.py
# class DeviceValidationForm(forms.Form):
#     '''Validates that a OTP device is working properly.'''
#     token = forms.IntegerField(label=_(u'Token'), min_value=1,
#                                max_value=999999)
# 
#     def __init__(self, device, *args, **kwargs):
#         super(DeviceValidationForm, self).__init__(*args, **kwargs)
#         self.device = device
# 
#     def clean_token(self):
#         token = self.cleaned_data['token']
#         if not self.device.verify_token(token):
#             raise forms.ValidationError(_(u'Entered token is not valid'))
#         return token


# Path: examples/django/example/views.py
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render, resolve_url
from django.utils.http import is_safe_url
from django.utils.translation import ugettext as u_, ugettext_lazy as ul_
from django_otp import devices_for_user, login as otp_login
from django_otp.decorators import otp_required
from django_otp.forms import OTPTokenForm
from oath_toolkit.django_otp.totp.models import (
    OToolkitTOTPDevice as TOTPDevice)
from .forms import DeviceValidationForm

        form = DeviceValidationForm(device)
        return render(request, 'otp_setup.html', {
            'device': device,
            'form': form,
        })
    elif request.method == 'POST':
        if request.POST.get(u'enable') and not tfa_enabled:
            device = TOTPDevice.objects.create(user=request.user,
                                               name=u'TOTP',
                                               confirmed=False)
            form = DeviceValidationForm(device)
            return render(request, 'otp_setup.html', {
                'device': device,
                'form': form,
            })
        elif request.POST.get(u'disable') and tfa_enabled:
            for device in confirmed_devices:
                device.delete()
            messages.success(request, u_(
                u'Disabled two-factor authentication for this account.'))
        else:
            messages.error(request, u_(u'Unknown error.'))
    return redirect('otp_status')


@login_required
def otp_setup_verify(request):
    if request.method == 'POST':
        unconfirmed_devices = list(devices_for_user(request.user, False))
        device = unconfirmed_devices[0]

        form = DeviceValidationForm(device, request.POST)