ExamHoot

Token-Based Authentication Implementation for API Security

from rest_framework.authtoken.models import Token
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated

@api_view(['GET'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def protected_endpoint(request):
    return Response({'message': 'Success'})
  1. TokenAuthentication requires the token to be sent in the request body

  2. The decorators specify that the endpoint requires a valid token in the Authorization header and the user must be authenticated

  3. TokenAuthentication is less secure than session-based authentication for APIs

  4. The endpoint can be accessed by any user without authentication if they know the URL

Show answer & explanation

Correct answer

The decorators specify that the endpoint requires a valid token in the Authorization header and the user must be authenticated

Explanation

TokenAuthentication uses tokens stored in the database to authenticate API requests. The @authentication_classes decorator specifies which authentication method to use, and @permission_classes ensures only authenticated users can access the endpoint.

Written by ExamHoot EditorialPublished · Updated

All 30 Django Authentication and Permissions questions

  1. 1.What is the primary purpose of Django's authentication system?
  2. 2.Which Django decorator is used to restrict access to a view to authenticated users only?
  3. 3.What is the difference between authentication and authorization in Django?
  4. 4.Which model in Django stores user information and authentication credentials?
  5. 5.What does the `is_authenticated` attribute of a User object return for an anonymous user?
  6. 6.Which method is used to authenticate a user with username and password in Django?
  7. 7.What is the purpose of the `login()` function in Django?
  8. 8.In Django, what is a Permission object used for?
  9. 9.What is the purpose of a Group in Django's permission system?
  10. 10.Which method checks if a user has a specific permission?
  11. 11.Scenario:
  12. 12.What will be the output of the following code?
  13. 13.Which decorator would you use to ensure a user has the 'can_publish_article' permission before accessing a view?
  14. 14.Scenario:
  15. 15.What is the output of the following code when user 'hari' has the 'blog.change_post' permission?
  16. 16.How would you programmatically assign a permission to a user in Django?
  17. 17.Scenario:
  18. 18.What will be the output of the following code?
  19. 19.Coding Question:
  20. 20.Scenario:
  21. 21.Understanding Django's Authentication Backend Architecture
  22. 22.Implementing Custom Authentication Backend for Company Access Control
  23. 23.Permission Inheritance in Django Group-Based Authorization
  24. 24.Scenario:
  25. 25.Using Django's @permission_required Decorator for View Protection
  26. 26.Implementing Object-Level Permissions for Hari's Document Sharing Platform
  27. 27.Token-Based Authentication Implementation for API Security
  28. 28.Debugging Permission Issues:
  29. 29.Implementing Session-Based Authentication with CSRF Protection
  30. 30.Advanced: