本文整理汇总了Python中two_factor.utils.default_device函数的典型用法代码示例。如果您正苦于以下问题:Python default_device函数的具体用法?Python default_device怎么用?Python default_device使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了default_device函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_default_device
def test_default_device(self):
user = self.create_user()
self.assertEqual(default_device(user), None)
user.phonedevice_set.create(name='backup', number='+1')
self.assertEqual(default_device(user), None)
default = user.phonedevice_set.create(name='default', number='+1')
self.assertEqual(default_device(user).pk, default.pk)
示例2: test_default_device
def test_default_device(self):
user = User.objects.create_user('bouke')
self.assertEqual(default_device(user), None)
user.phonedevice_set.create(name='backup')
self.assertEqual(default_device(user), None)
default = user.phonedevice_set.create(name='default')
self.assertEqual(default_device(user).pk, default.pk)
示例3: test_default_device
def test_default_device(self):
user = self.create_user()
self.assertEqual(default_device(user), None)
PhoneDevice.objects.create(user=user, name='backup')
self.assertEqual(default_device(user), None)
default = PhoneDevice.objects.create(user=user, name='default')
self.assertEqual(default_device(user).pk, default.pk)
示例4: login_user
def login_user(self, user=None):
if not user:
user = list(self._passwords.keys())[0]
username = user.get_username()
assert self.client.login(username=username, password=self._passwords[user])
if default_device(user):
session = self.client.session
session[DEVICE_ID_SESSION_KEY] = default_device(user).persistent_id
session.save()
示例5: account
def account(request):
try:
backup_tokens = request.user.staticdevice_set.all()[0].token_set.count()
except Exception:
backup_tokens = 0
status = {
'default_device': default_device(request.user),
'default_device_type': default_device(request.user).__class__.__name__,
'backup_phones': backup_phones(request.user),
'backup_tokens': backup_tokens,
'site_user': request.user
}
return render_to_response('account.html', status, context_instance=RequestContext(request))
示例6: wrapped
def wrapped(request, *args, **kwargs):
u = request.user
if (not hasattr(u, 'userprofile') or not hasattr(u.userprofile, 'teacher') or
(not u.is_verified() and default_device(u))):
return HttpResponseRedirect(reverse_lazy('teach'))
return view_func(request, *args, **kwargs)
示例7: profile
def profile(request):
# Get a list of the users API Keys
keys = ApiKey.objects.filter(user=request.user)
try:
backup_tokens = request.user.staticdevice_set.all()[0].token_set.count()
except IndexError:
backup_tokens = 0
# Get a list of the users current sessions
sessions = request.user.session_set.filter(expire_date__gt=now())
# Get the current session key
session_key = request.session.session_key
# Process the form if we have data coming in
if request.method == 'POST':
form = UserProfileForm(request.POST, instance=request.user.profile)
if form.is_valid():
form.save()
else:
form = UserProfileForm(instance=request.user.profile)
# Show the template
return render(request, 'account_profile.html', {
'keys': keys,
'sessions': sessions,
'session_key': session_key,
'form': form,
'user': request.user,
'default_device': default_device(request.user),
'backup_tokens': backup_tokens,
})
示例8: dispatch
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated() or \
(not request.user.is_verified() and default_device(request.user)):
# If the user has not authenticated raise or redirect to the login
# page. Also if the user just enabled two-factor authentication and
# has not yet logged in since should also have the same result. If
# the user receives a 'you need to enable TFA' by now, he gets
# confuses as TFA has just been enabled. So we either raise or
# redirect to the login page.
if self.raise_anonymous:
raise PermissionDenied()
else:
return redirect('%s?%s' % (
self.get_login_url(),
urlencode({self.redirect_field_name: request.get_full_path()})
))
if not request.user.is_verified():
if self.raise_unverified:
raise PermissionDenied()
elif self.get_verification_url():
return redirect('%s?%s' % (
self.verification_url,
urlencode({self.redirect_field_name: request.get_full_path()})
))
else:
return TemplateResponse(
request=request,
template='two_factor/core/otp_required.html',
status=403,
)
return super(OTPRequiredMixin, self).dispatch(request, *args, **kwargs)
示例9: teacher_edit_account
def teacher_edit_account(request):
teacher = request.user.userprofile.teacher
backup_tokens = 0
# For teachers using 2FA, find out how many backup tokens they have
if default_device(request.user):
try:
backup_tokens = request.user.staticdevice_set.all()[0].token_set.count()
except Exception:
backup_tokens = 0
if request.method == "POST":
form = TeacherEditAccountForm(request.user, request.POST)
if form.is_valid():
data = form.cleaned_data
changing_email = False
# check not default value for CharField
if data["password"] != "":
teacher.user.user.set_password(data["password"])
teacher.user.user.save()
update_session_auth_hash(request, form.user)
teacher.title = data["title"]
teacher.user.user.first_name = data["first_name"]
teacher.user.user.last_name = data["last_name"]
new_email = data["email"]
if new_email != "" and new_email != teacher.user.user.email:
# new email to set and verify
changing_email = True
send_verification_email(request, teacher.user, new_email)
teacher.save()
teacher.user.user.save()
if changing_email:
logout(request)
messages.success(
request,
"Your account details have been successfully changed. Your email will be changed once you have verified it, until then you can still log in with your old email.",
)
return render(
request, "portal/email_verification_needed.html", {"userprofile": teacher.user, "email": new_email}
)
messages.success(request, "Your account details have been successfully changed.")
return HttpResponseRedirect(reverse_lazy("teacher_home"))
else:
form = TeacherEditAccountForm(
request.user,
initial={
"title": teacher.title,
"first_name": teacher.user.user.first_name,
"last_name": teacher.user.user.last_name,
"school": teacher.school,
},
)
return render(request, "portal/teach/teacher_edit_account.html", {"form": form, "backup_tokens": backup_tokens})
示例10: two_form_authentication_warnings
def two_form_authentication_warnings(request, teacher):
# For teachers using 2FA, warn if they don't have any backup tokens set, and warn solo-admins to set up another admin
if default_device(request.user):
# check backup tokens
try:
backup_tokens = request.user.staticdevice_set.all()[0].token_set.count()
except Exception:
backup_tokens = 0
if not backup_tokens > 0:
link = reverse('two_factor:profile')
messages.warning(request,
'You do not have any backup tokens set up for two factor authentication, so could lose '
'access to your account if you have problems with your smartphone or tablet. '
'<a href="{link}">Set up backup tokens now</a>.'.format(link = link), extra_tags='safe')
# check admin
if teacher.is_admin:
admins = Teacher.objects.filter(school=teacher.school, is_admin=True)
manageSchoolLink = reverse('organisation_manage')
if len(admins) == 1:
messages.warning(request,
'You are the only administrator in your school and are using Two Factor Authentication '
'(2FA). We recommend you <a href="{manageSchoolLink}">set up another '
'administrator</a> who will be able to disable your 2FA should you have problems with '
'your smartphone or tablet.'.format(manageSchoolLink = manageSchoolLink),
extra_tags='safe')
示例11: login
def login(self, request, redirect_url=None):
# prevent admin users hijacking this login page to circumvent
# two factor authentication
if site_settings.ADMIN_ENABLED:
if default_device(self.user):
raise Http404
if request.limited:
return render(request, 'account/ratelimit_triggered.html', {})
return super(RatelimitedLoginForm, self).login(request, redirect_url)
示例12: process_request
def process_request(self, request):
if settings.ADMIN_ENABLED:
allowed_urls = [reverse('admin:logout')]
requested_url = request.get_full_path()
two_factor_base = reverse('two_factor:profile')
if two_factor_base not in requested_url \
and requested_url not in allowed_urls \
and request.user and any([request.user.is_superuser, request.user.is_staff]) \
and not default_device(request.user):
return HttpResponseRedirect(reverse('two_factor:setup'))
示例13: teacher_edit_account
def teacher_edit_account(request):
teacher = request.user.userprofile.teacher
backup_tokens = 0
# For teachers using 2FA, find out how many backup tokens they have
if default_device(request.user):
try:
backup_tokens = request.user.staticdevice_set.all()[0].token_set.count()
except Exception:
backup_tokens = 0
if request.method == 'POST':
form = TeacherEditAccountForm(request.user, request.POST)
if form.is_valid():
data = form.cleaned_data
changing_email=False
# check not default value for CharField
if (data['password'] != ''):
teacher.user.user.set_password(data['password'])
teacher.user.user.save()
update_session_auth_hash(request, form.user)
teacher.title = data['title']
teacher.user.user.first_name = data['first_name']
teacher.user.user.last_name = data['last_name']
new_email = data['email']
if new_email != '' and new_email != teacher.user.user.email:
# new email to set and verify
changing_email=True
send_verification_email(request, teacher.user, new_email)
teacher.save()
teacher.user.user.save()
if changing_email:
logout(request)
messages.success(request, 'Your account details have been successfully changed. Your email will be changed once you have verified it, until then you can still log in with your old email.')
return render(request, 'portal/email_verification_needed.html', { 'userprofile': teacher.user, 'email': new_email })
messages.success(request, 'Your account details have been successfully changed.')
return HttpResponseRedirect(reverse_lazy('teacher_home'))
else:
form = TeacherEditAccountForm(request.user, initial={
'title' : teacher.title,
'first_name': teacher.user.user.first_name,
'last_name': teacher.user.user.last_name,
'school': teacher.school,
})
return render(request, 'portal/teach/teacher_edit_account.html', { 'form': form, 'backup_tokens': backup_tokens })
示例14: list
def list(self, request):
try:
token_set = request.user.staticdevice_set.first().token_set.all()
except AttributeError:
token_set = []
return Response({
'default': get_info_text_for_device(default_device(request.user)),
'backup_phone_numbers': [
{
'id': phone.pk,
'number': mask_phone_number(format_phone_number(phone.number)),
} for phone in backup_phones(request.user)
],
'backup_tokens': [token.token for token in token_set],
})
示例15: do_two_factor_login
def do_two_factor_login(request: HttpRequest, user_profile: UserProfile) -> None:
device = default_device(user_profile)
if device:
django_otp.login(request, device)