authentication.py 3.9 KB

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