2016-04-12 10 views
2

私はdjango-otpモジュールを見ていて、それを私のプロジェクトに実装したいと思っていました。しかし、私はいくつかの問題に直面しています。django otpを実装するには?

1)docs(彼らがdocsで示した方法)によると、認証のレベルはAnonymous,AuthenticatedおよびAuthenticated + Verifiedです。ユーザが既にdjangoの認証システムですでに認証されている場合は、otp認証(双方向認証)を求められます。

今、私はそれをスキップし、otpを介してのみユーザーを認証/検証したいと思います。ログインプロンプトの代わりに、ユーザーは電話番号を入力し、確認のためにotpを受信します(私はdjangoの認証を迂回します)。

2)また、otp_requiredを選択したページでのみ使用します。つまり、私のウェブサイトには匿名ユーザーと確認ユーザーの両方がいます。

3)実装に関する例は見つかりませんでした。

私の質問は、現在のシナリオでそれを実装する方法です。

EDIT: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', 
] 
+0

のようなあなたのビューにこれらのミックスインを追加します。カスタムのDjango認証バックエンドを書いているようです。 – psagers

答えて

1

あなたは、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) 

とこれはジャンゴ-OTPの目的外にはかなり遠いです

class MyView(AuthenticationVerificationMixin, TemplateView): 
    ... 
関連する問題