test_auth.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2016 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale tests with simple requests and authentication.
  20. """
  21. import base64
  22. import os
  23. import shutil
  24. import tempfile
  25. import pytest
  26. from radicale import Application, config, xmlutils
  27. from radicale.tests import BaseTest
  28. class TestBaseAuthRequests(BaseTest):
  29. """Tests basic requests with auth.
  30. We should setup auth for each type before creating the Application object.
  31. """
  32. def setup(self):
  33. self.configuration = config.load()
  34. self.colpath = tempfile.mkdtemp()
  35. self.configuration.update({
  36. "storage": {"filesystem_folder": self.colpath},
  37. # Disable syncing to disk for better performance
  38. "internal": {"filesystem_fsync": "False"},
  39. # Set incorrect authentication delay to a very low value
  40. "auth": {"delay": "0.002"}}, "test", internal=True)
  41. def teardown(self):
  42. shutil.rmtree(self.colpath)
  43. def _test_htpasswd(self, htpasswd_encryption, htpasswd_content,
  44. test_matrix="ascii"):
  45. """Test htpasswd authentication with user "tmp" and password "bepo" for
  46. ``test_matrix`` "ascii" or user "😀" and password "🔑" for
  47. ``test_matrix`` "unicode"."""
  48. if htpasswd_encryption == "bcrypt":
  49. try:
  50. from passlib.hash import bcrypt
  51. from passlib.exc import MissingBackendError
  52. except ImportError:
  53. pytest.skip("passlib[bcrypt] is not installed")
  54. try:
  55. bcrypt.hash("test-bcrypt-backend")
  56. except MissingBackendError:
  57. pytest.skip("bcrypt backend for passlib is not installed")
  58. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  59. encoding = self.configuration.get("encoding", "stock")
  60. with open(htpasswd_file_path, "w", encoding=encoding) as f:
  61. f.write(htpasswd_content)
  62. self.configuration.update({
  63. "auth": {"type": "htpasswd",
  64. "htpasswd_filename": htpasswd_file_path,
  65. "htpasswd_encryption": htpasswd_encryption}}, "test")
  66. self.application = Application(self.configuration)
  67. if test_matrix == "ascii":
  68. test_matrix = (("tmp", "bepo", True), ("tmp", "tmp", False),
  69. ("tmp", "", False), ("unk", "unk", False),
  70. ("unk", "", False), ("", "", False))
  71. elif test_matrix == "unicode":
  72. test_matrix = (("😀", "🔑", True), ("😀", "🌹", False),
  73. ("😁", "🔑", False), ("😀", "", False),
  74. ("", "🔑", False), ("", "", False))
  75. for user, password, valid in test_matrix:
  76. self.propfind("/", check=207 if valid else 401,
  77. HTTP_AUTHORIZATION=("Basic %s" % base64.b64encode(
  78. ("%s:%s" % (user, password)).encode()).decode()))
  79. def test_htpasswd_plain(self):
  80. self._test_htpasswd("plain", "tmp:bepo")
  81. def test_htpasswd_plain_password_split(self):
  82. self._test_htpasswd("plain", "tmp:be:po", (
  83. ("tmp", "be:po", True), ("tmp", "bepo", False)))
  84. def test_htpasswd_plain_unicode(self):
  85. self._test_htpasswd("plain", "😀:🔑", "unicode")
  86. def test_htpasswd_md5(self):
  87. self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  88. def test_htpasswd_md5_unicode(self):
  89. self._test_htpasswd(
  90. "md5", "😀:$apr1$w4ev89r1$29xO8EvJmS2HEAadQ5qy11", "unicode")
  91. def test_htpasswd_bcrypt(self):
  92. self._test_htpasswd("bcrypt", "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3V"
  93. "NTRI3w5KDnj8NTUKJNWfVpvRq")
  94. def test_htpasswd_bcrypt_unicode(self):
  95. self._test_htpasswd("bcrypt", "😀:$2y$10$Oyz5aHV4MD9eQJbk6GPemOs4T6edK"
  96. "6U9Sqlzr.W1mMVCS8wJUftnW", "unicode")
  97. def test_htpasswd_multi(self):
  98. self._test_htpasswd("plain", "ign:ign\ntmp:bepo")
  99. @pytest.mark.skipif(os.name == "nt", reason="leading and trailing "
  100. "whitespaces not allowed in file names")
  101. def test_htpasswd_whitespace_user(self):
  102. for user in (" tmp", "tmp ", " tmp "):
  103. self._test_htpasswd("plain", "%s:bepo" % user, (
  104. (user, "bepo", True), ("tmp", "bepo", False)))
  105. def test_htpasswd_whitespace_password(self):
  106. for password in (" bepo", "bepo ", " bepo "):
  107. self._test_htpasswd("plain", "tmp:%s" % password, (
  108. ("tmp", password, True), ("tmp", "bepo", False)))
  109. def test_htpasswd_comment(self):
  110. self._test_htpasswd("plain", "#comment\n #comment\n \ntmp:bepo\n\n")
  111. def test_remote_user(self):
  112. self.configuration.update({"auth": {"type": "remote_user"}}, "test")
  113. self.application = Application(self.configuration)
  114. _, responses = self.propfind("/", """\
  115. <?xml version="1.0" encoding="utf-8"?>
  116. <propfind xmlns="DAV:">
  117. <prop>
  118. <current-user-principal />
  119. </prop>
  120. </propfind>""", REMOTE_USER="test")
  121. status, prop = responses["/"]["D:current-user-principal"]
  122. assert status == 200
  123. assert prop.find(xmlutils.make_clark("D:href")).text == "/test/"
  124. def test_http_x_remote_user(self):
  125. self.configuration.update(
  126. {"auth": {"type": "http_x_remote_user"}}, "test")
  127. self.application = Application(self.configuration)
  128. _, responses = self.propfind("/", """\
  129. <?xml version="1.0" encoding="utf-8"?>
  130. <propfind xmlns="DAV:">
  131. <prop>
  132. <current-user-principal />
  133. </prop>
  134. </propfind>""", HTTP_X_REMOTE_USER="test")
  135. status, prop = responses["/"]["D:current-user-principal"]
  136. assert status == 200
  137. assert prop.find(xmlutils.make_clark("D:href")).text == "/test/"
  138. def test_custom(self):
  139. """Custom authentication."""
  140. self.configuration.update(
  141. {"auth": {"type": "radicale.tests.custom.auth"}}, "test")
  142. self.application = Application(self.configuration)
  143. self.propfind("/tmp/", HTTP_AUTHORIZATION="Basic %s" %
  144. base64.b64encode(("tmp:").encode()).decode())