authentication.py 841 B

123456789101112131415161718192021222324252627
  1. from django.utils.translation import gettext_lazy as _
  2. from rest_framework import exceptions
  3. from rest_framework.authentication import TokenAuthentication
  4. from api_tokens.models import APIToken
  5. class BearerTokenAuthentication(TokenAuthentication):
  6. """
  7. Customized TokenAuthentication to support the APIToken model
  8. and sentry-cli's usage of bearer
  9. """
  10. keyword = "Bearer"
  11. model = APIToken
  12. def authenticate_credentials(self, key):
  13. model = self.get_model()
  14. try:
  15. token = model.objects.select_related("user").get(token=key)
  16. except model.DoesNotExist:
  17. raise exceptions.AuthenticationFailed(_("Invalid token."))
  18. if not token.user.is_active:
  19. raise exceptions.AuthenticationFailed(_("User inactive or deleted."))
  20. return (token.user, token)