test_authentication.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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(
  24. self.owner.get_username(), self.token.plain, HTTP_200_OK
  25. )
  26. assertDynDNS12AuthenticationStatus(
  27. self.my_domain.name, self.token.plain, HTTP_200_OK
  28. )
  29. assertDynDNS12AuthenticationStatus(
  30. " " + self.my_domain.name, self.token.plain, HTTP_401_UNAUTHORIZED
  31. )
  32. assertDynDNS12AuthenticationStatus(
  33. "wrong", self.token.plain, HTTP_401_UNAUTHORIZED
  34. )
  35. assertDynDNS12AuthenticationStatus("", "wrong", HTTP_401_UNAUTHORIZED)
  36. assertDynDNS12AuthenticationStatus(
  37. self.user.get_username(), "wrong", HTTP_401_UNAUTHORIZED
  38. )
  39. def test_malformed_basic_auth(self):
  40. for authorization in [
  41. "asdf:asdf:sadf",
  42. "asdf",
  43. "bull[%]shit",
  44. "你好",
  45. "💩💩💩💩",
  46. "💩💩:💩💩",
  47. ]:
  48. self.assertDynDNS12Status(
  49. authorization=authorization, code=HTTP_401_UNAUTHORIZED
  50. )
  51. class TokenAuthenticationTestCase(DynDomainOwnerTestCase):
  52. def setUp(self):
  53. super().setUp()
  54. # Refresh token from database, but keep plain value
  55. self.token, self.token.plain = (
  56. Token.objects.get(pk=self.token.pk),
  57. self.token.plain,
  58. )
  59. def assertAuthenticationStatus(self, code, plain=None, expired=False, **kwargs):
  60. plain = plain or self.token.plain
  61. self.client.set_credentials_token_auth(plain)
  62. # only forward REMOTE_ADDR if not None
  63. if kwargs.get("REMOTE_ADDR") is None:
  64. kwargs.pop("REMOTE_ADDR", None)
  65. response = self.client.get(self.reverse("v1:root"), **kwargs)
  66. body = (
  67. json.dumps({"detail": "Invalid token."})
  68. if code == HTTP_401_UNAUTHORIZED
  69. else None
  70. )
  71. self.assertResponse(response, code, body)
  72. if expired:
  73. key = Token.make_hash(plain)
  74. self.assertFalse(Token.objects.get(key=key).is_valid)
  75. def test_token_case_sensitive(self):
  76. self.assertAuthenticationStatus(HTTP_200_OK)
  77. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.plain.upper())
  78. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, self.token.plain.lower())
  79. def test_token_subnets(self):
  80. datas = [ # Format: allowed_subnets, status, client_ip | None, [client_ip, ...]
  81. ([], HTTP_401_UNAUTHORIZED, None),
  82. (["127.0.0.1"], HTTP_200_OK, None),
  83. (["1.2.3.4"], HTTP_401_UNAUTHORIZED, None),
  84. (["1.2.3.4"], HTTP_200_OK, "1.2.3.4"),
  85. (["1.2.3.0/24"], HTTP_200_OK, "1.2.3.4"),
  86. (["1.2.3.0/24"], HTTP_401_UNAUTHORIZED, "bade::affe"),
  87. (["bade::/64"], HTTP_200_OK, "bade::affe"),
  88. (["bade::/64", "1.2.3.0/24"], HTTP_200_OK, "bade::affe", "1.2.3.66"),
  89. ]
  90. for allowed_subnets, status, client_ips in (
  91. (*data[:2], data[2:]) for data in datas
  92. ):
  93. self.token.allowed_subnets = allowed_subnets
  94. self.token.save()
  95. for client_ip in client_ips:
  96. self.assertAuthenticationStatus(status, REMOTE_ADDR=client_ip)
  97. def test_token_max_age(self):
  98. # No maximum age: can use now and in ten years
  99. self.token.max_age = None
  100. self.token.save()
  101. self.assertAuthenticationStatus(HTTP_200_OK)
  102. with mock.patch(
  103. "desecapi.models.timezone.now",
  104. return_value=timezone.now() + timedelta(days=3650),
  105. ):
  106. self.assertAuthenticationStatus(HTTP_200_OK)
  107. # Maximum age zero: token cannot be used
  108. self.token.max_age = timedelta(0)
  109. self.token.save()
  110. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  111. # Maximum age 10 10:10:10: can use one second before, but not once second after
  112. period = timedelta(days=10, hours=10, minutes=10, seconds=10)
  113. self.token.max_age = period
  114. self.token.save()
  115. second = timedelta(seconds=1)
  116. with mock.patch(
  117. "desecapi.models.timezone.now",
  118. return_value=self.token.created + period - second,
  119. ):
  120. self.assertAuthenticationStatus(HTTP_200_OK)
  121. with mock.patch(
  122. "desecapi.models.timezone.now",
  123. return_value=self.token.created + period + second,
  124. ):
  125. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  126. def test_token_max_unused_period(self):
  127. plain = self.token.plain
  128. second = timedelta(seconds=1)
  129. # Maximum unused period zero: token cannot be used
  130. self.token.max_unused_period = timedelta(0)
  131. self.token.save()
  132. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  133. # Maximum unused period
  134. period = timedelta(days=10, hours=10, minutes=10, seconds=10)
  135. self.token.max_unused_period = period
  136. self.token.save()
  137. # Can't use after period if token was never used (last_used is None)
  138. self.assertIsNone(self.token.last_used)
  139. with mock.patch(
  140. "desecapi.models.timezone.now",
  141. return_value=self.token.created + period + second,
  142. ):
  143. self.assertAuthenticationStatus(
  144. HTTP_401_UNAUTHORIZED, plain=plain, expired=True
  145. )
  146. self.assertIsNone(
  147. Token.objects.get(pk=self.token.pk).last_used
  148. ) # unchanged
  149. # Can use after half the period
  150. with mock.patch(
  151. "desecapi.models.timezone.now", return_value=self.token.created + period / 2
  152. ):
  153. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  154. self.token = Token.objects.get(pk=self.token.pk) # update last_used field
  155. # Can't use once another period is over
  156. with mock.patch(
  157. "desecapi.models.timezone.now",
  158. return_value=self.token.last_used + period + second,
  159. ):
  160. self.assertAuthenticationStatus(
  161. HTTP_401_UNAUTHORIZED, plain=plain, expired=True
  162. )
  163. self.assertEqual(
  164. self.token.last_used, Token.objects.get(pk=self.token.pk).last_used
  165. ) # unchanged
  166. # ... but one second before, and also for one more period
  167. with mock.patch(
  168. "desecapi.models.timezone.now",
  169. return_value=self.token.last_used + period - second,
  170. ):
  171. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  172. with mock.patch(
  173. "desecapi.models.timezone.now",
  174. return_value=self.token.last_used + 2 * period - 2 * second,
  175. ):
  176. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  177. # No maximum age: can use now and in ten years
  178. self.token.max_unused_period = None
  179. self.token.save()
  180. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  181. with mock.patch(
  182. "desecapi.models.timezone.now",
  183. return_value=timezone.now() + timedelta(days=3650),
  184. ):
  185. self.assertAuthenticationStatus(HTTP_200_OK, plain=plain)
  186. def test_token_max_age_max_unused_period(self):
  187. hour = timedelta(hours=1)
  188. self.token.max_age = 3 * hour
  189. self.token.max_unused_period = hour
  190. self.token.save()
  191. # max_unused_period wins if tighter than max_age
  192. with mock.patch(
  193. "desecapi.models.timezone.now",
  194. return_value=self.token.created + 1.25 * hour,
  195. ):
  196. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  197. # Can use immediately
  198. self.assertAuthenticationStatus(HTTP_200_OK)
  199. # Can use continuously within max_unused_period
  200. with mock.patch(
  201. "desecapi.models.timezone.now",
  202. return_value=self.token.created + 0.75 * hour,
  203. ):
  204. self.assertAuthenticationStatus(HTTP_200_OK)
  205. with mock.patch(
  206. "desecapi.models.timezone.now", return_value=self.token.created + 1.5 * hour
  207. ):
  208. self.assertAuthenticationStatus(HTTP_200_OK)
  209. # max_unused_period wins again if tighter than max_age
  210. with mock.patch(
  211. "desecapi.models.timezone.now",
  212. return_value=self.token.created + 2.75 * hour,
  213. ):
  214. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)
  215. # Can use continuously within max_unused_period
  216. with mock.patch(
  217. "desecapi.models.timezone.now",
  218. return_value=self.token.created + 2.25 * hour,
  219. ):
  220. self.assertAuthenticationStatus(HTTP_200_OK)
  221. with mock.patch(
  222. "desecapi.models.timezone.now",
  223. return_value=self.token.created + 2.75 * hour,
  224. ):
  225. self.assertAuthenticationStatus(HTTP_200_OK)
  226. # max_age wins again if tighter than max_unused_period
  227. with mock.patch(
  228. "desecapi.models.timezone.now",
  229. return_value=self.token.created + 3.25 * hour,
  230. ):
  231. self.assertAuthenticationStatus(HTTP_401_UNAUTHORIZED, expired=True)