Django Rest Knox


1. Create a Token API View

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_knox.models import AuthToken

class TokenObtainPairView(APIView):
    def post(self, request, format=None):
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()
            token = AuthToken.objects.create(user)
            return Response({'token': token})
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

2. Use the Knox Authentication Middleware

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_knox.authentication.KnoxAuthentication',
    ),
}

3. Set the Token Model

4. Customize the Token Expiration

5. Disable the Token Limit

6. Use the Knox TokenSerializer

7. Use the Knox UserSerializer

8. Create a Refresh Token API View

9. Customize the Refresh Token Expiration

10. Use the Knox RefreshTokenSerializer

11. Create a Logout API View

12. Use the Knox LogoutSerializer

13. Create a Knox Serializer Mixin

14. Use the Knox Serializer Mixin in a ModelSerializer

15. Create a Custom Knox Authentication

16. Use the Custom Knox Authentication in a View

17. Create a Custom Knox Permission

18. Use the Custom Knox Permission in a View

19. Create a Knox Token Manager

20. Use the Knox Token Manager in the Token Model

21. Create a Custom Knox API View

22. Use the Custom Knox API View in a URL Pattern

23. Create a Knox Token Field

24. Use the Knox Token Field in a ModelSerializer

25. Create a Knox Serializer Wrapper

26. Use the Knox Serializer Wrapper in a ViewSet

27. Create a Knox Scoped Permission

28. Use the Knox Scoped Permission in a View

29. Create a Knox Token Generator

30. Use the Knox Token Generator in the Token Model

31. Create a Knox Token Expire Handler

32. Use the Knox Token Expire Handler in the Token Model

33. Create a Knox User Serializer Mixin

34. Use the Knox User Serializer Mixin in a ModelSerializer

35. Create a Knox API Exception Handler

36. Use the Knox API Exception Handler in a View