Предоставление доступа к другим представлениям класса после успешной аутентификации Django REST

Вот мой API в Django REST.

Вот мой код:

from rest_framework.permissions import IsAuthenticated, AllowAny

class CreateItems(generics.CreateAPIView):
    permission_classes = [IsAuthenticated]
    
    queryset = SomeModel.objects.all()
    serializer_class = SomeSerializer
    
    


class AuthStatus(APIView):

    permission_classes = [AllowAny]

    def post(self, request, *args, **kwargs):
        
        
        token = self.request.data['itemsReturnedAuthAPI']['accessToken']
        if(token):
            return Response({"Token":token})

        else:
            return Response({"message":"No Token Found!"})
   
   

У меня есть микросервис аутентификации, от которого я получаю JWT-токен для пользователя, чтобы иметь доступ к представлениям. В классе AuthStatus я проверяю токен.

Мой вопрос: Я хочу предоставить пользователю доступ к классу CreateItems, после предоставления токена

Хороший вопрос. Вот мои рекомендации по реализации аутентификации на основе токенов.

  1. Let the user send their JWT in a HTTP header for every request.

  2. Use an authentication class in rest-framework. The authentication class looks at an incoming HTTP request and determines if that request is authenticated and which user the request comes from. Most authentication classes will look at tokens in HTTP headers, then make a call to a database or microservice to validate that token. Check out the documentation. You will probably need a custom or third-party authentication class.

  3. Keep using the permission class IsAuthenticated. This class will reject unauthenticated requests.

Вернуться на верх