authentication.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from __future__ import unicode_literals
  2. import base64
  3. from rest_framework import exceptions, HTTP_HEADER_ENCODING
  4. from rest_framework.authentication import BaseAuthentication, get_authorization_header, authenticate
  5. from desecapi.models import Domain, Token
  6. from rest_framework.authentication import TokenAuthentication as RestFrameworkTokenAuthentication
  7. class TokenAuthentication(RestFrameworkTokenAuthentication):
  8. model = Token
  9. class BasicTokenAuthentication(BaseAuthentication):
  10. """
  11. HTTP Basic authentication that uses username and token.
  12. Clients should authenticate by passing the username and the token as a
  13. password in the "Authorization" HTTP header, according to the HTTP
  14. Basic Authentication Scheme
  15. Authorization: Basic dXNlcm5hbWU6dG9rZW4=
  16. For username "username" and password "token".
  17. """
  18. # A custom token model may be used, but must have the following properties.
  19. #
  20. # * key -- The string identifying the token
  21. # * user -- The user to which the token belongs
  22. model = Token
  23. def authenticate(self, request):
  24. auth = get_authorization_header(request).split()
  25. if not auth or auth[0].lower() != b'basic':
  26. return None
  27. if len(auth) == 1:
  28. msg = 'Invalid basic auth token header. No credentials provided.'
  29. raise exceptions.AuthenticationFailed(msg)
  30. elif len(auth) > 2:
  31. msg = 'Invalid basic auth token header. Basic authentication string should not contain spaces.'
  32. raise exceptions.AuthenticationFailed(msg)
  33. return self.authenticate_credentials(auth[1])
  34. def authenticate_credentials(self, basic):
  35. invalid_token_message = 'Invalid basic auth token'
  36. try:
  37. user, key = base64.b64decode(basic).decode(HTTP_HEADER_ENCODING).split(':')
  38. token = self.model.objects.get(key=key)
  39. except:
  40. raise exceptions.AuthenticationFailed(invalid_token_message)
  41. if not token.user.is_active:
  42. raise exceptions.AuthenticationFailed(invalid_token_message)
  43. if user:
  44. try:
  45. Domain.objects.get(owner=token.user.pk, name=user)
  46. except:
  47. raise exceptions.AuthenticationFailed('Invalid username')
  48. return token.user, token
  49. def authenticate_header(self, request):
  50. return 'Basic'
  51. class URLParamAuthentication(BaseAuthentication):
  52. """
  53. Authentication against username/password as provided in URL parameters.
  54. """
  55. model = Token
  56. def authenticate(self, request):
  57. """
  58. Returns a `User` if a correct username and password have been supplied
  59. using URL parameters. Otherwise returns `None`.
  60. """
  61. if not 'username' in request.query_params:
  62. msg = 'No username URL parameter provided.'
  63. raise exceptions.AuthenticationFailed(msg)
  64. if not 'password' in request.query_params:
  65. msg = 'No password URL parameter provided.'
  66. raise exceptions.AuthenticationFailed(msg)
  67. return self.authenticate_credentials(request.query_params['username'], request.query_params['password'])
  68. def authenticate_credentials(self, userid, key):
  69. try:
  70. token = self.model.objects.get(key=key)
  71. except self.model.DoesNotExist:
  72. raise exceptions.AuthenticationFailed('badauth')
  73. if not token.user.is_active:
  74. raise exceptions.AuthenticationFailed('badauth')
  75. return token.user, token