test_authentication.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from datetime import timedelta
  2. import json
  3. from unittest import mock
  4. from django.utils import timezone
  5. from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED
  6. from desecapi.models import Token
  7. from desecapi.tests.base import DynDomainOwnerTestCase
  8. class DynUpdateAuthenticationTestCase(DynDomainOwnerTestCase):
  9. NUM_OWNED_DOMAINS = 1
  10. def _get_dyndns12(self):
  11. with self.assertPdnsNoRequestsBut(self.requests_desec_rr_sets_update()):
  12. return self.client.get(self.reverse('v1:dyndns12update'))
  13. def assertDynDNS12Status(self, code=HTTP_200_OK, authorization=None):
  14. if authorization:
  15. self.client.set_credentials_basic_auth(authorization)
  16. self.assertStatus(self._get_dyndns12(), code)
  17. def test_username_password(self):
  18. # noinspection PyPep8Naming
  19. def assertDynDNS12AuthenticationStatus(username, token, code):
  20. self.client.set_credentials_basic_auth(username, token)
  21. self.assertDynDNS12Status(code)
  22. assertDynDNS12AuthenticationStatus('', self.token.plain, HTTP_200_OK)
  23. assertDynDNS12AuthenticationStatus(self.owner.get_username(), self.token.plain, HTTP_200_OK)
  24. assertDynDNS12AuthenticationStatus(self.my_domain.name, self.token.plain, HTTP_200_OK)
  25. assertDynDNS12AuthenticationStatus(' ' + self.my_domain.name, self.token.plain, HTTP_401_UNAUTHORIZED)
  26. assertDynDNS12AuthenticationStatus('wrong', self.token.plain, HTTP_401_UNAUTHORIZED)
  27. assertDynDNS12AuthenticationStatus('', 'wrong', HTTP_401_UNAUTHORIZED)
  28. assertDynDNS12AuthenticationStatus(self.user.get_username(), 'wrong', HTTP_401_UNAUTHORIZED)
  29. def test_malformed_basic_auth(self):
  30. for authorization in [
  31. 'asdf:asdf:sadf',
  32. 'asdf',
  33. 'bull[%]shit',
  34. '你好',
  35. '💩💩💩💩',
  36. '💩💩:💩💩',
  37. ]:
  38. self.assertDynDNS12Status(authorization=authorization, code=HTTP_401_UNAUTHORIZED)
  39. class TokenAuthenticationTestCase(DynDomainOwnerTestCase):
  40. def setUp(self):
  41. super().setUp()
  42. # Refresh token from database, but keep plain value
  43. self.token, self.token.plain = Token.objects.get(pk=self.token.pk), self.token.plain
  44. def assertAuthenticationStatus(self, code, plain=None, expired=False ,**kwargs):
  45. plain = plain or self.token.plain
  46. self.client.set_credentials_token_auth(plain)
  47. # only forward REMOTE_ADDR if not None
  48. if kwargs.get('REMOTE_ADDR') is None:
  49. kwargs.pop('REMOTE_ADDR', None)
  50. response = self.client.get(self.reverse('v1:root'), **kwargs)
  51. body = json.dumps({'detail': 'Invalid token.'}) if code == HTTP_401_UNAUTHORIZED else None
  52. self.assertResponse(response, code, body)
  53. if expired:
  54. key = Token.make_hash(plain)
  55. self.assertFalse(Token.objects.get(key=key).is_valid)
  56. def test_token_case_sensitive(self):
  57. self.assertAuthenticationStatus(HTTP_200_OK)
  58. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.plain.upper())
  59. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.plain.lower())
  60. def test_token_subnets(self):
  61. datas = [ # Format: allowed_subnets, status, client_ip | None, [client_ip, ...]
  62. ([], HTTP_401_UNAUTHORIZED, None),
  63. (['127.0.0.1'], HTTP_200_OK, None),
  64. (['1.2.3.4'], HTTP_401_UNAUTHORIZED, None),
  65. (['1.2.3.4'], HTTP_200_OK, '1.2.3.4'),
  66. (['1.2.3.0/24'], HTTP_200_OK, '1.2.3.4'),
  67. (['1.2.3.0/24'], HTTP_401_UNAUTHORIZED, 'bade::affe'),
  68. (['bade::/64'], HTTP_200_OK, 'bade::affe'),
  69. (['bade::/64', '1.2.3.0/24'], HTTP_200_OK, 'bade::affe', '1.2.3.66'),
  70. ]
  71. for allowed_subnets, status, client_ips in ((*data[:2], data[2:]) for data in datas):
  72. self.token.allowed_subnets = allowed_subnets
  73. self.token.save()
  74. for client_ip in client_ips:
  75. self.assertAuthenticationStatus(status, REMOTE_ADDR=client_ip)
  76. def test_token_max_age(self):
  77. # No maximum age: can use now and in ten years
  78. self.token.max_age = None
  79. self.token.save()
  80. self.assertAuthenticationStatus(HTTP_200_OK)
  81. with mock.patch('desecapi.models.timezone.now', return_value=timezone.now() + timedelta(days=3650)):
  82. self.assertAuthenticationStatus(HTTP_200_OK)
  83. # Maximum age zero: token cannot be used
  84. self.token.max_age = timedelta(0)
  85. self.token.save()
  86. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  87. # Maximum age 10 10:10:10: can use one second before, but not once second after
  88. period = timedelta(days=10, hours=10, minutes=10, seconds=10)
  89. self.token.max_age = period
  90. self.token.save()
  91. second = timedelta(seconds=1)
  92. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + period - second):
  93. self.assertAuthenticationStatus(HTTP_200_OK)
  94. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + period + second):
  95. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  96. def test_token_max_unused_period(self):
  97. plain = self.token.plain
  98. second = timedelta(seconds=1)
  99. # Maximum unused period zero: token cannot be used
  100. self.token.max_unused_period = timedelta(0)
  101. self.token.save()
  102. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  103. # Maximum unused period
  104. period = timedelta(days=10, hours=10, minutes=10, seconds=10)
  105. self.token.max_unused_period = period
  106. self.token.save()
  107. # Can't use after period if token was never used (last_used is None)
  108. self.assertIsNone(self.token.last_used)
  109. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + period + second):
  110. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, plain=plain, expired=True)
  111. self.assertIsNone(Token.objects.get(pk=self.token.pk).last_used) # unchanged
  112. # Can use after half the period
  113. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + period/2):
  114. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  115. self.token = Token.objects.get(pk=self.token.pk) # update last_used field
  116. # Can't use once another period is over
  117. with mock.patch('desecapi.models.timezone.now', return_value=self.token.last_used + period + second):
  118. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, plain=plain, expired=True)
  119. self.assertEqual(self.token.last_used, Token.objects.get(pk=self.token.pk).last_used) # unchanged
  120. # ... but one second before, and also for one more period
  121. with mock.patch('desecapi.models.timezone.now', return_value=self.token.last_used + period - second):
  122. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  123. with mock.patch('desecapi.models.timezone.now', return_value=self.token.last_used + 2*period - 2*second):
  124. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  125. # No maximum age: can use now and in ten years
  126. self.token.max_unused_period = None
  127. self.token.save()
  128. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  129. with mock.patch('desecapi.models.timezone.now', return_value=timezone.now() + timedelta(days=3650)):
  130. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  131. def test_token_max_age_max_unused_period(self):
  132. hour = timedelta(hours=1)
  133. self.token.max_age = 3 * hour
  134. self.token.max_unused_period = hour
  135. self.token.save()
  136. # max_unused_period wins if tighter than max_age
  137. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + 1.25*hour):
  138. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  139. # Can use immediately
  140. self.assertAuthenticationStatus(HTTP_200_OK)
  141. # Can use continuously within max_unused_period
  142. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + 0.75*hour):
  143. self.assertAuthenticationStatus(HTTP_200_OK)
  144. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + 1.5*hour):
  145. self.assertAuthenticationStatus(HTTP_200_OK)
  146. # max_unused_period wins again if tighter than max_age
  147. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + 2.75*hour):
  148. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  149. # Can use continuously within max_unused_period
  150. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + 2.25*hour):
  151. self.assertAuthenticationStatus(HTTP_200_OK)
  152. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + 2.75*hour):
  153. self.assertAuthenticationStatus(HTTP_200_OK)
  154. # max_age wins again if tighter than max_unused_period
  155. with mock.patch('desecapi.models.timezone.now', return_value=self.token.created + 3.25*hour):
  156. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)