authentication.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from __future__ import unicode_literals
  2. import base64, os
  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. try:
  36. user, key = base64.b64decode(basic).decode(HTTP_HEADER_ENCODING).split(':')
  37. token = self.model.objects.get(key=key)
  38. except:
  39. raise exceptions.AuthenticationFailed('Invalid basic auth token')
  40. if not token.user.is_active:
  41. raise exceptions.AuthenticationFailed('User inactive or deleted')
  42. if user:
  43. try:
  44. Domain.objects.get(owner=token.user.pk, name=user)
  45. except:
  46. raise exceptions.AuthenticationFailed('Invalid username')
  47. return token.user, token
  48. def authenticate_header(self, request):
  49. return 'Basic'
  50. class URLParamAuthentication(BaseAuthentication):
  51. """
  52. Authentication against username/password as provided in URL parameters.
  53. """
  54. model = Token
  55. def authenticate(self, request):
  56. """
  57. Returns a `User` if a correct username and password have been supplied
  58. using URL parameters. Otherwise returns `None`.
  59. """
  60. if not 'username' in request.query_params:
  61. msg = 'No username URL parameter provided.'
  62. raise exceptions.AuthenticationFailed(msg)
  63. if not 'password' in request.query_params:
  64. msg = 'No password URL parameter provided.'
  65. raise exceptions.AuthenticationFailed(msg)
  66. return self.authenticate_credentials(request.query_params['username'], request.query_params['password'])
  67. def authenticate_credentials(self, userid, key):
  68. try:
  69. token = self.model.objects.get(key=key)
  70. except self.model.DoesNotExist:
  71. raise exceptions.AuthenticationFailed('badauth')
  72. if not token.user.is_active:
  73. raise exceptions.AuthenticationFailed('User inactive or deleted')
  74. return token.user, token
  75. class IPAuthentication(BaseAuthentication):
  76. """
  77. Authentication against remote IP address for dedyn.io management by nslord
  78. """
  79. def authenticate(self, request):
  80. nslord = '%s.1.11' % os.environ['DESECSTACK_IPV4_REAR_PREFIX16']
  81. # Make sure this is dual-stack safe
  82. if request.META.get('REMOTE_ADDR') in [nslord, '::ffff:%s' % nslord]:
  83. try:
  84. domain = Domain.objects.get(name='dedyn.io')
  85. return (domain.owner, None)
  86. except Domain.DoesNotExist:
  87. return None
  88. return None