2017-03-02 14 views
0

現在の認証ビューapi/v1/o /トークンを無効にして、ユーザー名とパスワードに基づいてカスタムエラーメッセージを追加する必要があります。 Django OAuthカスタム認証クラス

3.

status = 'error' 
detail= 'Incorrect username or password' 

status = 'not_active' 
detail= 'user not activated' 

2.

{ 
     "status = ok // need to add this 
     "access_token": "xxxx", 
     "token_type": "Bearer", 
     "expires_in": 60, 
     "refresh_token": "xxxxaaaxxxx", 
     "scope": "read write" 
    } 

1.私は、アプリケーションが私の生産ホスティング上で作成を無効にしたいです。 どうすればいいですか?

+0

私はTokenViewがトークンを与えることができなかった場合、私は、ユーザー名に基づいてユーザの状態を確認するために、別の自分のREST呼び出しを行う必要があると思うカスタム認証クラスを指すように設定してDEFAULT_AUTHENTICATION_CLASSESを変更。 またはそれを行うための最善の方法。 ? –

+0

あなたの認証バックエンドは何ですか?なぜカスタム認証バックエンドが本当に必要なのですか? –

+0

私のバックエンドは、クライアント(React web and mobile)のための単純なREST APIです。私はUIの機能を追加することに基づいてこの条件をチェックする必要があります。 –

答えて

0

Django Rest Frameworkを使用してカスタム認証クラスを作成する方法です。サブクラスBaseAuthentication.authenticate(self, request)メソッドをオーバーライドします。

from django.contrib.auth.models import User 
from rest_framework import authentication 
from rest_framework import exceptions 

class CustomAuthentication(authentication.BaseAuthentication): 
    def authenticate(self, request): 
     """ 
     Consider the method validate_access_token() takes an access token, 
     verify it and return the User.username if the token is valid else None 
     """ 
     username = validate_access_token(request.META.get('X_ACCESS_TOKEN')) 
     if not username: 
      return None #return None if User is not authenticated. 

     try: 
      user = User.objects.get(username=username) 
     except User.DoesNotExist: 
      raise exceptions.AuthenticationFailed('No such user') 

     return (user, None) 

その後、

REST_FRAMEWORK = { 
    'DEFAULT_AUTHENTICATION_CLASSES': (
     'api.core.auth.CustomAuthentication', 
    ), 
} 
+0

こんにちは、oauthプロバイダの中でTokenViewをオーバーライドすることについて話していました。 –

関連する問題