api.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from django.conf import settings
  2. from django.utils import timezone
  3. from rest_framework import authentication, exceptions
  4. from rest_framework.exceptions import APIException
  5. from rest_framework.permissions import DjangoModelPermissions, SAFE_METHODS
  6. from rest_framework.serializers import Field
  7. from users.models import Token
  8. WRITE_OPERATIONS = ['create', 'update', 'partial_update', 'delete']
  9. class ServiceUnavailable(APIException):
  10. status_code = 503
  11. default_detail = "Service temporarily unavailable, please try again later."
  12. class TokenAuthentication(authentication.TokenAuthentication):
  13. """
  14. A custom authentication scheme which enforces Token expiration times.
  15. """
  16. model = Token
  17. def authenticate_credentials(self, key):
  18. model = self.get_model()
  19. try:
  20. token = model.objects.select_related('user').get(key=key)
  21. except model.DoesNotExist:
  22. raise exceptions.AuthenticationFailed("Invalid token")
  23. # Enforce the Token's expiration time, if one has been set.
  24. if token.expires and token.expires < timezone.now():
  25. raise exceptions.AuthenticationFailed("Token expired")
  26. if not token.user.is_active:
  27. raise exceptions.AuthenticationFailed("User inactive")
  28. return token.user, token
  29. class TokenPermissions(DjangoModelPermissions):
  30. """
  31. Custom permissions handler which extends the built-in DjangoModelPermissions to validate a Token's write ability
  32. for unsafe requests (POST/PUT/PATCH/DELETE).
  33. """
  34. def __init__(self):
  35. # LOGIN_REQUIRED determines whether read-only access is provided to anonymous users.
  36. self.authenticated_users_only = settings.LOGIN_REQUIRED
  37. super(TokenPermissions, self).__init__()
  38. def has_permission(self, request, view):
  39. # If token authentication is in use, verify that the token allows write operations (for unsafe methods).
  40. if request.method not in SAFE_METHODS and isinstance(request.auth, Token):
  41. if not request.auth.write_enabled:
  42. return False
  43. return super(TokenPermissions, self).has_permission(request, view)
  44. class ChoiceFieldSerializer(Field):
  45. """
  46. Represent a ChoiceField as (value, label).
  47. """
  48. def __init__(self, choices, **kwargs):
  49. self._choices = {k: v for k, v in choices}
  50. super(ChoiceFieldSerializer, self).__init__(**kwargs)
  51. def to_representation(self, obj):
  52. return obj, self._choices[obj]
  53. def to_internal_value(self, data):
  54. return self._choices.get(data)
  55. class WritableSerializerMixin(object):
  56. """
  57. Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).
  58. """
  59. def get_serializer_class(self):
  60. if self.action in WRITE_OPERATIONS and hasattr(self, 'write_serializer_class'):
  61. return self.write_serializer_class
  62. return self.serializer_class