2013-05-11 43 views
8

Tôi đã đoạn mã sau:CSRF Miễn Failure - APIView CSRF django còn lại khung

Vấn đề là khi tôi cố gắng để truy cập người dùng đăng nhập/Tôi nhận được một lỗi: "CSRF Không: CSRF Cookie không được thiết lập."

Tôi có thể làm gì?

Tôi đang sử dụng khung công tác còn lại django.

urls.py: 

url(r'^user-login/$', 
    csrf_exempt(LoginView.as_view()), 
    name='user-login'), 

views.py: 

class LoginView(APIView): 
""" 
List all snippets, or create a new snippet. 
""" 
def get(self, request, format=None): 
    startups = Startup.objects.all() 
    serializer = StartupSerializer(startups, many=True) 
    return Response(serializer.data) 

def post(self, request, format=None): 
    profile = request.POST 

    if ('user_name' not in profile or 'email_address' not in profile or 'oauth_secret' not in profile): 
     return Response(
      {'error': 'No data'}, 
      status=status.HTTP_400_BAD_REQUEST) 

    username = 'l' + profile['user_name'] 
    email_address = profile['email_address'] 
    oauth_secret = profile['oauth_secret'] 
    password = oauth_secret 

Trả lời

14

Tôi giả sử bạn sử dụng khung công tác còn lại django SessionBackend. backend này thực hiện việc implicit CSRF check

Bạn có thể tránh điều này bằng cách:

from rest_framework.authentication import SessionAuthentication 

class UnsafeSessionAuthentication(SessionAuthentication): 

    def authenticate(self, request): 
     http_request = request._request 
     user = getattr(http_request, 'user', None) 

     if not user or not user.is_active: 
      return None 

     return (user, None) 

Và thiết lập này như authentication_classes trong Xem bạn

class UnsafeLogin(APIView): 
    permission_classes = (AllowAny,) #maybe not needed in your case 
    authentication_classes = (UnsafeSessionAuthentication,) 

    def post(self, request, *args, **kwargs): 

     username = request.DATA.get("u"); 
     password = request.DATA.get("p"); 

     user = authenticate(username=username, password=password) 
     if user is not None: 
      login(request, user) 

     return redirect("/") 
9

Trên thực tế, cách tốt hơn để vô hiệu hóa kiểm tra CSRF bên SessionAuthentication là:

from rest_framework.authentication import SessionAuthentication as OriginalSessionAuthentication 

class SessionAuthentication(OriginalSessionAuthentication): 
    def enforce_csrf(self, request): 
     return 
2

Cách dễ nhất để giải quyết vấn đề này lem:

Cho rằng có hai cách xác thực trong DRF see drf auth

BasicAuthentication

SessionAuthentication (mặc định)

SessionAuthentication có một tấm séc CSRF cưỡng bức, nhưng BasicAuthentication không. Vì vậy, cách của tôi là sử dụng BasicAuthentication trong chế độ xem của tôi thay vì SessionAuthentication.

from rest_framework.authentication import BasicAuthentication 

class UserLogin(generics.CreateAPIView): 
    permission_classes = (permissions.AllowAny,) 
    serializer_class = UserSerializer 
    authentication_classes = (BasicAuthentication,) 

    def post(self, request, *args, **kwargs): 
     return Response({})