首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现django otp?

如何实现django otp?
EN

Stack Overflow用户
提问于 2016-04-12 14:17:22
回答 1查看 5.3K关注 0票数 8

我正在研究django-otp模块,并希望在我的项目中实现它。但我面临着几个问题。

1)根据文档(文档中给出的方法),有三个级别的身份验证:AnonymousAuthenticatedAuthenticated + Verified。如果用户已经通过django的身份验证系统进行了身份验证,那么他将被要求进行otp验证(双向身份验证)。

现在我想跳过它,只通过otp认证/验证用户。而不是登录提示,用户将输入一个电话号码,并将收到一个用于验证的otp。(我想绕过django的身份验证)。

2)另外,我只想在选定的页面上使用otp_required。也就是说,我将在我的网站上有匿名和验证用户。

3)我找不到任何关于实现的例子。

我的问题是如何在我当前的场景中实现它。

编辑: Settings.py

代码语言:javascript
复制
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',
]
EN

回答 1

Stack Overflow用户

发布于 2017-09-27 05:13:40

您可以编写自己的基于类的视图混合,类似于LoginRequired mixin

代码语言:javascript
复制
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)

然后将这些混合添加到视图中,如下所示

代码语言:javascript
复制
class MyView(AuthenticationVerificationMixin, TemplateView):
    ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36564973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档