我正在研究django-otp模块,并希望在我的项目中实现它。但我面临着几个问题。
1)根据文档(文档中给出的方法),有三个级别的身份验证:Anonymous、Authenticated和Authenticated + Verified。如果用户已经通过django的身份验证系统进行了身份验证,那么他将被要求进行otp验证(双向身份验证)。
现在我想跳过它,只通过otp认证/验证用户。而不是登录提示,用户将输入一个电话号码,并将收到一个用于验证的otp。(我想绕过django的身份验证)。
2)另外,我只想在选定的页面上使用otp_required。也就是说,我将在我的网站上有匿名和验证用户。
3)我找不到任何关于实现的例子。
我的问题是如何在我当前的场景中实现它。
编辑: Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
'django_otp',
'django_otp.plugins.otp_totp',
'django_otp.plugins.otp_static',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_otp.middleware.OTPMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]发布于 2017-09-27 05:13:40
您可以编写自己的基于类的视图混合,类似于LoginRequired mixin。
class AuthenticationVerificationMixin(AccessMixin):
"""
CBV mixin which verifies that the current user is authenticated,
and has a placeholder for checking if user verified.
"""
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return self.handle_no_permission()
elif not request.user.is_verified():
# If you need a verification logic it will go here,
# for example here's a redirect if you're not verified...
# return redirect_to_login(self.request.get_full_path(), '/verify/'), self.get_redirect_field_name())
return super().dispatch(request, *args, **kwargs)然后将这些混合添加到视图中,如下所示
class MyView(AuthenticationVerificationMixin, TemplateView):
...https://stackoverflow.com/questions/36564973
复制相似问题