1
0

test_login.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import json
  2. import urllib.parse
  3. from mock import Mock
  4. import synapse.rest.admin
  5. from synapse.rest.client.v1 import login, logout
  6. from synapse.rest.client.v2_alpha import devices
  7. from synapse.rest.client.v2_alpha.account import WhoamiRestServlet
  8. from tests import unittest
  9. from tests.unittest import override_config
  10. LOGIN_URL = b"/_matrix/client/r0/login"
  11. TEST_URL = b"/_matrix/client/r0/account/whoami"
  12. class LoginRestServletTestCase(unittest.HomeserverTestCase):
  13. servlets = [
  14. synapse.rest.admin.register_servlets_for_client_rest_resource,
  15. login.register_servlets,
  16. logout.register_servlets,
  17. devices.register_servlets,
  18. lambda hs, http_server: WhoamiRestServlet(hs).register(http_server),
  19. ]
  20. def make_homeserver(self, reactor, clock):
  21. self.hs = self.setup_test_homeserver()
  22. self.hs.config.enable_registration = True
  23. self.hs.config.registrations_require_3pid = []
  24. self.hs.config.auto_join_rooms = []
  25. self.hs.config.enable_registration_captcha = False
  26. return self.hs
  27. def test_POST_ratelimiting_per_address(self):
  28. self.hs.config.rc_login_address.burst_count = 5
  29. self.hs.config.rc_login_address.per_second = 0.17
  30. # Create different users so we're sure not to be bothered by the per-user
  31. # ratelimiter.
  32. for i in range(0, 6):
  33. self.register_user("kermit" + str(i), "monkey")
  34. for i in range(0, 6):
  35. params = {
  36. "type": "m.login.password",
  37. "identifier": {"type": "m.id.user", "user": "kermit" + str(i)},
  38. "password": "monkey",
  39. }
  40. request_data = json.dumps(params)
  41. request, channel = self.make_request(b"POST", LOGIN_URL, request_data)
  42. self.render(request)
  43. if i == 5:
  44. self.assertEquals(channel.result["code"], b"429", channel.result)
  45. retry_after_ms = int(channel.json_body["retry_after_ms"])
  46. else:
  47. self.assertEquals(channel.result["code"], b"200", channel.result)
  48. # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
  49. # than 1min.
  50. self.assertTrue(retry_after_ms < 6000)
  51. self.reactor.advance(retry_after_ms / 1000.0)
  52. params = {
  53. "type": "m.login.password",
  54. "identifier": {"type": "m.id.user", "user": "kermit" + str(i)},
  55. "password": "monkey",
  56. }
  57. request_data = json.dumps(params)
  58. request, channel = self.make_request(b"POST", LOGIN_URL, params)
  59. self.render(request)
  60. self.assertEquals(channel.result["code"], b"200", channel.result)
  61. def test_POST_ratelimiting_per_account(self):
  62. self.hs.config.rc_login_account.burst_count = 5
  63. self.hs.config.rc_login_account.per_second = 0.17
  64. self.register_user("kermit", "monkey")
  65. for i in range(0, 6):
  66. params = {
  67. "type": "m.login.password",
  68. "identifier": {"type": "m.id.user", "user": "kermit"},
  69. "password": "monkey",
  70. }
  71. request_data = json.dumps(params)
  72. request, channel = self.make_request(b"POST", LOGIN_URL, request_data)
  73. self.render(request)
  74. if i == 5:
  75. self.assertEquals(channel.result["code"], b"429", channel.result)
  76. retry_after_ms = int(channel.json_body["retry_after_ms"])
  77. else:
  78. self.assertEquals(channel.result["code"], b"200", channel.result)
  79. # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
  80. # than 1min.
  81. self.assertTrue(retry_after_ms < 6000)
  82. self.reactor.advance(retry_after_ms / 1000.0)
  83. params = {
  84. "type": "m.login.password",
  85. "identifier": {"type": "m.id.user", "user": "kermit"},
  86. "password": "monkey",
  87. }
  88. request_data = json.dumps(params)
  89. request, channel = self.make_request(b"POST", LOGIN_URL, params)
  90. self.render(request)
  91. self.assertEquals(channel.result["code"], b"200", channel.result)
  92. def test_POST_ratelimiting_per_account_failed_attempts(self):
  93. self.hs.config.rc_login_failed_attempts.burst_count = 5
  94. self.hs.config.rc_login_failed_attempts.per_second = 0.17
  95. self.register_user("kermit", "monkey")
  96. for i in range(0, 6):
  97. params = {
  98. "type": "m.login.password",
  99. "identifier": {"type": "m.id.user", "user": "kermit"},
  100. "password": "notamonkey",
  101. }
  102. request_data = json.dumps(params)
  103. request, channel = self.make_request(b"POST", LOGIN_URL, request_data)
  104. self.render(request)
  105. if i == 5:
  106. self.assertEquals(channel.result["code"], b"429", channel.result)
  107. retry_after_ms = int(channel.json_body["retry_after_ms"])
  108. else:
  109. self.assertEquals(channel.result["code"], b"403", channel.result)
  110. # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
  111. # than 1min.
  112. self.assertTrue(retry_after_ms < 6000)
  113. self.reactor.advance(retry_after_ms / 1000.0)
  114. params = {
  115. "type": "m.login.password",
  116. "identifier": {"type": "m.id.user", "user": "kermit"},
  117. "password": "notamonkey",
  118. }
  119. request_data = json.dumps(params)
  120. request, channel = self.make_request(b"POST", LOGIN_URL, params)
  121. self.render(request)
  122. self.assertEquals(channel.result["code"], b"403", channel.result)
  123. @override_config({"session_lifetime": "24h"})
  124. def test_soft_logout(self):
  125. self.register_user("kermit", "monkey")
  126. # we shouldn't be able to make requests without an access token
  127. request, channel = self.make_request(b"GET", TEST_URL)
  128. self.render(request)
  129. self.assertEquals(channel.result["code"], b"401", channel.result)
  130. self.assertEquals(channel.json_body["errcode"], "M_MISSING_TOKEN")
  131. # log in as normal
  132. params = {
  133. "type": "m.login.password",
  134. "identifier": {"type": "m.id.user", "user": "kermit"},
  135. "password": "monkey",
  136. }
  137. request, channel = self.make_request(b"POST", LOGIN_URL, params)
  138. self.render(request)
  139. self.assertEquals(channel.code, 200, channel.result)
  140. access_token = channel.json_body["access_token"]
  141. device_id = channel.json_body["device_id"]
  142. # we should now be able to make requests with the access token
  143. request, channel = self.make_request(
  144. b"GET", TEST_URL, access_token=access_token
  145. )
  146. self.render(request)
  147. self.assertEquals(channel.code, 200, channel.result)
  148. # time passes
  149. self.reactor.advance(24 * 3600)
  150. # ... and we should be soft-logouted
  151. request, channel = self.make_request(
  152. b"GET", TEST_URL, access_token=access_token
  153. )
  154. self.render(request)
  155. self.assertEquals(channel.code, 401, channel.result)
  156. self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  157. self.assertEquals(channel.json_body["soft_logout"], True)
  158. #
  159. # test behaviour after deleting the expired device
  160. #
  161. # we now log in as a different device
  162. access_token_2 = self.login("kermit", "monkey")
  163. # more requests with the expired token should still return a soft-logout
  164. self.reactor.advance(3600)
  165. request, channel = self.make_request(
  166. b"GET", TEST_URL, access_token=access_token
  167. )
  168. self.render(request)
  169. self.assertEquals(channel.code, 401, channel.result)
  170. self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  171. self.assertEquals(channel.json_body["soft_logout"], True)
  172. # ... but if we delete that device, it will be a proper logout
  173. self._delete_device(access_token_2, "kermit", "monkey", device_id)
  174. request, channel = self.make_request(
  175. b"GET", TEST_URL, access_token=access_token
  176. )
  177. self.render(request)
  178. self.assertEquals(channel.code, 401, channel.result)
  179. self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  180. self.assertEquals(channel.json_body["soft_logout"], False)
  181. def _delete_device(self, access_token, user_id, password, device_id):
  182. """Perform the UI-Auth to delete a device"""
  183. request, channel = self.make_request(
  184. b"DELETE", "devices/" + device_id, access_token=access_token
  185. )
  186. self.render(request)
  187. self.assertEquals(channel.code, 401, channel.result)
  188. # check it's a UI-Auth fail
  189. self.assertEqual(
  190. set(channel.json_body.keys()),
  191. {"flows", "params", "session"},
  192. channel.result,
  193. )
  194. auth = {
  195. "type": "m.login.password",
  196. # https://github.com/matrix-org/synapse/issues/5665
  197. # "identifier": {"type": "m.id.user", "user": user_id},
  198. "user": user_id,
  199. "password": password,
  200. "session": channel.json_body["session"],
  201. }
  202. request, channel = self.make_request(
  203. b"DELETE",
  204. "devices/" + device_id,
  205. access_token=access_token,
  206. content={"auth": auth},
  207. )
  208. self.render(request)
  209. self.assertEquals(channel.code, 200, channel.result)
  210. @override_config({"session_lifetime": "24h"})
  211. def test_session_can_hard_logout_after_being_soft_logged_out(self):
  212. self.register_user("kermit", "monkey")
  213. # log in as normal
  214. access_token = self.login("kermit", "monkey")
  215. # we should now be able to make requests with the access token
  216. request, channel = self.make_request(
  217. b"GET", TEST_URL, access_token=access_token
  218. )
  219. self.render(request)
  220. self.assertEquals(channel.code, 200, channel.result)
  221. # time passes
  222. self.reactor.advance(24 * 3600)
  223. # ... and we should be soft-logouted
  224. request, channel = self.make_request(
  225. b"GET", TEST_URL, access_token=access_token
  226. )
  227. self.render(request)
  228. self.assertEquals(channel.code, 401, channel.result)
  229. self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  230. self.assertEquals(channel.json_body["soft_logout"], True)
  231. # Now try to hard logout this session
  232. request, channel = self.make_request(
  233. b"POST", "/logout", access_token=access_token
  234. )
  235. self.render(request)
  236. self.assertEquals(channel.result["code"], b"200", channel.result)
  237. @override_config({"session_lifetime": "24h"})
  238. def test_session_can_hard_logout_all_sessions_after_being_soft_logged_out(self):
  239. self.register_user("kermit", "monkey")
  240. # log in as normal
  241. access_token = self.login("kermit", "monkey")
  242. # we should now be able to make requests with the access token
  243. request, channel = self.make_request(
  244. b"GET", TEST_URL, access_token=access_token
  245. )
  246. self.render(request)
  247. self.assertEquals(channel.code, 200, channel.result)
  248. # time passes
  249. self.reactor.advance(24 * 3600)
  250. # ... and we should be soft-logouted
  251. request, channel = self.make_request(
  252. b"GET", TEST_URL, access_token=access_token
  253. )
  254. self.render(request)
  255. self.assertEquals(channel.code, 401, channel.result)
  256. self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  257. self.assertEquals(channel.json_body["soft_logout"], True)
  258. # Now try to hard log out all of the user's sessions
  259. request, channel = self.make_request(
  260. b"POST", "/logout/all", access_token=access_token
  261. )
  262. self.render(request)
  263. self.assertEquals(channel.result["code"], b"200", channel.result)
  264. class CASTestCase(unittest.HomeserverTestCase):
  265. servlets = [
  266. login.register_servlets,
  267. ]
  268. def make_homeserver(self, reactor, clock):
  269. self.base_url = "https://matrix.goodserver.com/"
  270. self.redirect_path = "_synapse/client/login/sso/redirect/confirm"
  271. config = self.default_config()
  272. config["cas_config"] = {
  273. "enabled": True,
  274. "server_url": "https://fake.test",
  275. "service_url": "https://matrix.goodserver.com:8448",
  276. }
  277. cas_user_id = "username"
  278. self.user_id = "@%s:test" % cas_user_id
  279. async def get_raw(uri, args):
  280. """Return an example response payload from a call to the `/proxyValidate`
  281. endpoint of a CAS server, copied from
  282. https://apereo.github.io/cas/5.0.x/protocol/CAS-Protocol-V2-Specification.html#26-proxyvalidate-cas-20
  283. This needs to be returned by an async function (as opposed to set as the
  284. mock's return value) because the corresponding Synapse code awaits on it.
  285. """
  286. return (
  287. """
  288. <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
  289. <cas:authenticationSuccess>
  290. <cas:user>%s</cas:user>
  291. <cas:proxyGrantingTicket>PGTIOU-84678-8a9d...</cas:proxyGrantingTicket>
  292. <cas:proxies>
  293. <cas:proxy>https://proxy2/pgtUrl</cas:proxy>
  294. <cas:proxy>https://proxy1/pgtUrl</cas:proxy>
  295. </cas:proxies>
  296. </cas:authenticationSuccess>
  297. </cas:serviceResponse>
  298. """
  299. % cas_user_id
  300. )
  301. mocked_http_client = Mock(spec=["get_raw"])
  302. mocked_http_client.get_raw.side_effect = get_raw
  303. self.hs = self.setup_test_homeserver(
  304. config=config, proxied_http_client=mocked_http_client,
  305. )
  306. return self.hs
  307. def prepare(self, reactor, clock, hs):
  308. self.deactivate_account_handler = hs.get_deactivate_account_handler()
  309. def test_cas_redirect_confirm(self):
  310. """Tests that the SSO login flow serves a confirmation page before redirecting a
  311. user to the redirect URL.
  312. """
  313. base_url = "/_matrix/client/r0/login/cas/ticket?redirectUrl"
  314. redirect_url = "https://dodgy-site.com/"
  315. url_parts = list(urllib.parse.urlparse(base_url))
  316. query = dict(urllib.parse.parse_qsl(url_parts[4]))
  317. query.update({"redirectUrl": redirect_url})
  318. query.update({"ticket": "ticket"})
  319. url_parts[4] = urllib.parse.urlencode(query)
  320. cas_ticket_url = urllib.parse.urlunparse(url_parts)
  321. # Get Synapse to call the fake CAS and serve the template.
  322. request, channel = self.make_request("GET", cas_ticket_url)
  323. self.render(request)
  324. # Test that the response is HTML.
  325. self.assertEqual(channel.code, 200)
  326. content_type_header_value = ""
  327. for header in channel.result.get("headers", []):
  328. if header[0] == b"Content-Type":
  329. content_type_header_value = header[1].decode("utf8")
  330. self.assertTrue(content_type_header_value.startswith("text/html"))
  331. # Test that the body isn't empty.
  332. self.assertTrue(len(channel.result["body"]) > 0)
  333. # And that it contains our redirect link
  334. self.assertIn(redirect_url, channel.result["body"].decode("UTF-8"))
  335. @override_config(
  336. {
  337. "sso": {
  338. "client_whitelist": [
  339. "https://legit-site.com/",
  340. "https://other-site.com/",
  341. ]
  342. }
  343. }
  344. )
  345. def test_cas_redirect_whitelisted(self):
  346. """Tests that the SSO login flow serves a redirect to a whitelisted url
  347. """
  348. self._test_redirect("https://legit-site.com/")
  349. @override_config({"public_baseurl": "https://example.com"})
  350. def test_cas_redirect_login_fallback(self):
  351. self._test_redirect("https://example.com/_matrix/static/client/login")
  352. def _test_redirect(self, redirect_url):
  353. """Tests that the SSO login flow serves a redirect for the given redirect URL."""
  354. cas_ticket_url = (
  355. "/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket"
  356. % (urllib.parse.quote(redirect_url))
  357. )
  358. # Get Synapse to call the fake CAS and serve the template.
  359. request, channel = self.make_request("GET", cas_ticket_url)
  360. self.render(request)
  361. self.assertEqual(channel.code, 302)
  362. location_headers = channel.headers.getRawHeaders("Location")
  363. self.assertEqual(location_headers[0][: len(redirect_url)], redirect_url)
  364. @override_config({"sso": {"client_whitelist": ["https://legit-site.com/"]}})
  365. def test_deactivated_user(self):
  366. """Logging in as a deactivated account should error."""
  367. redirect_url = "https://legit-site.com/"
  368. # First login (to create the user).
  369. self._test_redirect(redirect_url)
  370. # Deactivate the account.
  371. self.get_success(
  372. self.deactivate_account_handler.deactivate_account(self.user_id, False)
  373. )
  374. # Request the CAS ticket.
  375. cas_ticket_url = (
  376. "/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket"
  377. % (urllib.parse.quote(redirect_url))
  378. )
  379. # Get Synapse to call the fake CAS and serve the template.
  380. request, channel = self.make_request("GET", cas_ticket_url)
  381. self.render(request)
  382. # Because the user is deactivated they are served an error template.
  383. self.assertEqual(channel.code, 403)
  384. self.assertIn(b"SSO account deactivated", channel.result["body"])