2015-09-29 29 views
8

http://www.django-rest-framework.org/api-guide/authentication/#custom-authenticationDjango बाकी फ्रेमवर्क कस्टम प्रमाणीकरण

मैं अपने Django ऐप्लिकेशन में कस्टम प्रमाणीकरण का उपयोग करना चाहते हैं लेकिन यह कैसे लागू करने के लिए नहीं मिल रहा। क्या कोई इसके साथ मेरी मदद कर सकता है। उदाहरण दस्तावेज में दिए गए मेरे लिए स्पष्ट है लेकिन उन्होंने यह नहीं बताया कि इस नए वर्ग को कहां बनाना है और इसका उपयोग कैसे करें।

अग्रिम धन्यवाद!

उत्तर

0

आपके एपीआई फाइलों वाले फ़ोल्डर में, अपनी कस्टम प्रमाणीकरण कक्षा, जैसे authentication.py रखने के लिए एक और फ़ाइल बनाएं। फिर अपनी सेटिंग्स में, DEFAULT_AUTHENTICATION_CLASSES के तहत, अपनी कस्टम प्रमाणीकरण कक्षा को इंगित करें।

8

डीआरएफ में कस्टम प्रमाणीकरण योजना कैसे कार्यान्वित करें?

कस्टम प्रमाणीकरण योजना को लागू करने के लिए, हमें डीआरएफ की BaseAuthentication कक्षा को उप-वर्गीकृत करने और .authenticate(self, request) विधि को ओवरराइड करने की आवश्यकता है।

यदि प्रमाणीकरण सफल होता है, या None अन्यथा (user, auth) के दो-टुपल को विधि को वापस करना चाहिए। कुछ परिस्थितियों में, हम .authenticate() विधि से AuthenticationFailed अपवाद बढ़ा सकते हैं।

(DRF docs से) उदाहरण:

चलें कहते हैं कि हम एक कस्टम अनुरोध 'X_USERNAME' नामित शीर्षक में username द्वारा दिए गए उपयोगकर्ता के रूप में किसी भी भेजे अनुरोध प्रमाणित करने के लिए चाहते हैं।

चरण 1: कस्टम प्रमाणीकरण वर्ग

कि ऐसा करने के लिए, हम my_app में एक authentication.py फ़ाइल पैदा करेगा बनाएँ।

# my_app/authentication.py 
from django.contrib.auth.models import User 
from rest_framework import authentication 
from rest_framework import exceptions 

class ExampleAuthentication(authentication.BaseAuthentication): 
    def authenticate(self, request): 
     username = request.META.get('X_USERNAME') # get the username request header 
     if not username: # no username passed in request headers 
      return None # authentication did not succeed 

     try: 
      user = User.objects.get(username=username) # get the user 
     except User.DoesNotExist: 
      raise exceptions.AuthenticationFailed('No such user') # raise exception if user does not exist 

     return (user, None) # authentication successful 

चरण 2: कस्टम प्रमाणीकरण वर्ग

निर्दिष्ट करें कस्टम प्रमाणीकरण वर्ग बनाने के बाद, हम अपने डीआरएफ सेटिंग में इस प्रमाणीकरण वर्ग को परिभाषित करने की जरूरत है। ऐसा करने से, सभी प्रमाणीकरण इस प्रमाणीकरण योजना के आधार पर प्रमाणित किए जाएंगे।

'DEFAULT_AUTHENTICATION_CLASSES': (  
    'my_app.authentication.ExampleAuthentication', # custom authentication class 
    ... 
), 

नोट: आप पर-व्यू के आधार पर या प्रति-viewset आधार और वैश्विक स्तर पर इस कस्टम प्रमाणीकरण वर्ग उपयोग करना चाहते हैं, तो आप इस प्रमाणीकरण वर्ग स्पष्ट रूप से अपने विचारों में परिभाषित कर सकते हैं।

class MyView(APIView): 

    authentication_classes = (ExampleAuthentication,) # specify this authentication class in your view 

    ... 
+0

आप कैसे इस कर्ल का उपयोग कर का उपयोग करने पर एक उदाहरण देना कृपया कर सकते हैं? – momokjaaaaa

+1

@momokjaaaaa एक पोस्ट अनुरोध में शीर्षलेख भेजने के लिए इस SO लिंक को जांचें। http://stackoverflow.com/questions/356705/how-to-send-a-header-using-a-http-request-through-a-curl-call –

3

बोलो एक साधारण उदाहरण है जिसका उपयोग कस्टम प्रमाणीकरण प्राप्त करने के लिए किया जा सकता है। एंडपॉइंट तक पहुंचने के उदाहरण में आपको POST डेटा पर उपयोगकर्ता नाम & पासवर्ड पास करना होगा।

urls.py

urlpatterns = [ 
    url(r'^stuff/', views.MyView.as_view()), 
    ... 
] 

बार देखा गया।py

from django.contrib.auth.models import User 
    from rest_framework import viewsets 
    from rest_framework.response import Response 
    from rest_framework.views import APIView  
    from rest_framework.permissions import IsAuthenticated 
    from rest_framework import exceptions 
    from rest_framework import authentication 
    from django.contrib.auth import authenticate, get_user_model 
    from rest_framework.authentication import BasicAuthentication, 
SessionAuthentication 


class ExampleAuthentication(authentication.BaseAuthentication): 

    def authenticate(self, request): 

     # Get the username and password 
     username = request.data.get('username', None) 
     password = request.data.get('password', None) 

     if not username or not password: 
      raise exceptions.AuthenticationFailed(_('No credentials provided.')) 

     credentials = { 
      get_user_model().USERNAME_FIELD: username, 
      'password': password 
     } 

     user = authenticate(**credentials) 

     if user is None: 
      raise exceptions.AuthenticationFailed(_('Invalid username/password.')) 

     if not user.is_active: 
      raise exceptions.AuthenticationFailed(_('User inactive or deleted.')) 


    return (user, None) # authentication successful 


class MyView(APIView): 
    authentication_classes = (SessionAuthentication, ExampleAuthentication,) 
    permission_classes = (IsAuthenticated,) 

    def post(self, request, format=None):  
     content = { 
      'user': unicode(request.user), 
      'auth': unicode(request.auth), # None 
     } 
     return Response(content) 

कर्ल

curl -v -X POST http://localhost:8000/stuff/ -d 'username=my_username&password=my_password' 
संबंधित मुद्दे