test_admin.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import hashlib
  16. import hmac
  17. import json
  18. from mock import Mock
  19. from synapse.api.constants import UserTypes
  20. from synapse.rest.client.v1.admin import register_servlets
  21. from tests import unittest
  22. class UserRegisterTestCase(unittest.HomeserverTestCase):
  23. servlets = [register_servlets]
  24. def make_homeserver(self, reactor, clock):
  25. self.url = "/_matrix/client/r0/admin/register"
  26. self.registration_handler = Mock()
  27. self.identity_handler = Mock()
  28. self.login_handler = Mock()
  29. self.device_handler = Mock()
  30. self.device_handler.check_device_registered = Mock(return_value="FAKE")
  31. self.datastore = Mock(return_value=Mock())
  32. self.datastore.get_current_state_deltas = Mock(return_value=[])
  33. self.secrets = Mock()
  34. self.hs = self.setup_test_homeserver()
  35. self.hs.config.registration_shared_secret = u"shared"
  36. self.hs.get_media_repository = Mock()
  37. self.hs.get_deactivate_account_handler = Mock()
  38. return self.hs
  39. def test_disabled(self):
  40. """
  41. If there is no shared secret, registration through this method will be
  42. prevented.
  43. """
  44. self.hs.config.registration_shared_secret = None
  45. request, channel = self.make_request("POST", self.url, b'{}')
  46. self.render(request)
  47. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  48. self.assertEqual(
  49. 'Shared secret registration is not enabled', channel.json_body["error"]
  50. )
  51. def test_get_nonce(self):
  52. """
  53. Calling GET on the endpoint will return a randomised nonce, using the
  54. homeserver's secrets provider.
  55. """
  56. secrets = Mock()
  57. secrets.token_hex = Mock(return_value="abcd")
  58. self.hs.get_secrets = Mock(return_value=secrets)
  59. request, channel = self.make_request("GET", self.url)
  60. self.render(request)
  61. self.assertEqual(channel.json_body, {"nonce": "abcd"})
  62. def test_expired_nonce(self):
  63. """
  64. Calling GET on the endpoint will return a randomised nonce, which will
  65. only last for SALT_TIMEOUT (60s).
  66. """
  67. request, channel = self.make_request("GET", self.url)
  68. self.render(request)
  69. nonce = channel.json_body["nonce"]
  70. # 59 seconds
  71. self.reactor.advance(59)
  72. body = json.dumps({"nonce": nonce})
  73. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  74. self.render(request)
  75. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  76. self.assertEqual('username must be specified', channel.json_body["error"])
  77. # 61 seconds
  78. self.reactor.advance(2)
  79. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  80. self.render(request)
  81. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  82. self.assertEqual('unrecognised nonce', channel.json_body["error"])
  83. def test_register_incorrect_nonce(self):
  84. """
  85. Only the provided nonce can be used, as it's checked in the MAC.
  86. """
  87. request, channel = self.make_request("GET", self.url)
  88. self.render(request)
  89. nonce = channel.json_body["nonce"]
  90. want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
  91. want_mac.update(b"notthenonce\x00bob\x00abc123\x00admin")
  92. want_mac = want_mac.hexdigest()
  93. body = json.dumps(
  94. {
  95. "nonce": nonce,
  96. "username": "bob",
  97. "password": "abc123",
  98. "admin": True,
  99. "mac": want_mac,
  100. }
  101. )
  102. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  103. self.render(request)
  104. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  105. self.assertEqual("HMAC incorrect", channel.json_body["error"])
  106. def test_register_correct_nonce(self):
  107. """
  108. When the correct nonce is provided, and the right key is provided, the
  109. user is registered.
  110. """
  111. request, channel = self.make_request("GET", self.url)
  112. self.render(request)
  113. nonce = channel.json_body["nonce"]
  114. want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
  115. want_mac.update(
  116. nonce.encode('ascii') + b"\x00bob\x00abc123\x00admin\x00support"
  117. )
  118. want_mac = want_mac.hexdigest()
  119. body = json.dumps(
  120. {
  121. "nonce": nonce,
  122. "username": "bob",
  123. "password": "abc123",
  124. "admin": True,
  125. "user_type": UserTypes.SUPPORT,
  126. "mac": want_mac,
  127. }
  128. )
  129. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  130. self.render(request)
  131. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  132. self.assertEqual("@bob:test", channel.json_body["user_id"])
  133. def test_nonce_reuse(self):
  134. """
  135. A valid unrecognised nonce.
  136. """
  137. request, channel = self.make_request("GET", self.url)
  138. self.render(request)
  139. nonce = channel.json_body["nonce"]
  140. want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
  141. want_mac.update(
  142. nonce.encode('ascii') + b"\x00bob\x00abc123\x00admin"
  143. )
  144. want_mac = want_mac.hexdigest()
  145. body = json.dumps(
  146. {
  147. "nonce": nonce,
  148. "username": "bob",
  149. "password": "abc123",
  150. "admin": True,
  151. "mac": want_mac,
  152. }
  153. )
  154. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  155. self.render(request)
  156. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  157. self.assertEqual("@bob:test", channel.json_body["user_id"])
  158. # Now, try and reuse it
  159. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  160. self.render(request)
  161. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  162. self.assertEqual('unrecognised nonce', channel.json_body["error"])
  163. def test_missing_parts(self):
  164. """
  165. Synapse will complain if you don't give nonce, username, password, and
  166. mac. Admin and user_types are optional. Additional checks are done for length
  167. and type.
  168. """
  169. def nonce():
  170. request, channel = self.make_request("GET", self.url)
  171. self.render(request)
  172. return channel.json_body["nonce"]
  173. #
  174. # Nonce check
  175. #
  176. # Must be present
  177. body = json.dumps({})
  178. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  179. self.render(request)
  180. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  181. self.assertEqual('nonce must be specified', channel.json_body["error"])
  182. #
  183. # Username checks
  184. #
  185. # Must be present
  186. body = json.dumps({"nonce": nonce()})
  187. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  188. self.render(request)
  189. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  190. self.assertEqual('username must be specified', channel.json_body["error"])
  191. # Must be a string
  192. body = json.dumps({"nonce": nonce(), "username": 1234})
  193. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  194. self.render(request)
  195. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  196. self.assertEqual('Invalid username', channel.json_body["error"])
  197. # Must not have null bytes
  198. body = json.dumps({"nonce": nonce(), "username": u"abcd\u0000"})
  199. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  200. self.render(request)
  201. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  202. self.assertEqual('Invalid username', channel.json_body["error"])
  203. # Must not have null bytes
  204. body = json.dumps({"nonce": nonce(), "username": "a" * 1000})
  205. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  206. self.render(request)
  207. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  208. self.assertEqual('Invalid username', channel.json_body["error"])
  209. #
  210. # Password checks
  211. #
  212. # Must be present
  213. body = json.dumps({"nonce": nonce(), "username": "a"})
  214. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  215. self.render(request)
  216. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  217. self.assertEqual('password must be specified', channel.json_body["error"])
  218. # Must be a string
  219. body = json.dumps({"nonce": nonce(), "username": "a", "password": 1234})
  220. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  221. self.render(request)
  222. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  223. self.assertEqual('Invalid password', channel.json_body["error"])
  224. # Must not have null bytes
  225. body = json.dumps(
  226. {"nonce": nonce(), "username": "a", "password": u"abcd\u0000"}
  227. )
  228. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  229. self.render(request)
  230. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  231. self.assertEqual('Invalid password', channel.json_body["error"])
  232. # Super long
  233. body = json.dumps({"nonce": nonce(), "username": "a", "password": "A" * 1000})
  234. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  235. self.render(request)
  236. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  237. self.assertEqual('Invalid password', channel.json_body["error"])
  238. #
  239. # user_type check
  240. #
  241. # Invalid user_type
  242. body = json.dumps({
  243. "nonce": nonce(),
  244. "username": "a",
  245. "password": "1234",
  246. "user_type": "invalid"}
  247. )
  248. request, channel = self.make_request("POST", self.url, body.encode('utf8'))
  249. self.render(request)
  250. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  251. self.assertEqual('Invalid user type', channel.json_body["error"])