test_account.py 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. # Copyright 2015-2016 OpenMarket Ltd
  2. # Copyright 2017-2018 New Vector Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import json
  17. import os
  18. import re
  19. from email.parser import Parser
  20. from typing import Optional
  21. import pkg_resources
  22. import synapse.rest.admin
  23. from synapse.api.constants import LoginType, Membership
  24. from synapse.api.errors import Codes, HttpResponseException
  25. from synapse.appservice import ApplicationService
  26. from synapse.rest.client import account, login, register, room
  27. from synapse.rest.synapse.client.password_reset import PasswordResetSubmitTokenResource
  28. from tests import unittest
  29. from tests.server import FakeSite, make_request
  30. from tests.unittest import override_config
  31. class PasswordResetTestCase(unittest.HomeserverTestCase):
  32. servlets = [
  33. account.register_servlets,
  34. synapse.rest.admin.register_servlets_for_client_rest_resource,
  35. register.register_servlets,
  36. login.register_servlets,
  37. ]
  38. def make_homeserver(self, reactor, clock):
  39. config = self.default_config()
  40. # Email config.
  41. config["email"] = {
  42. "enable_notifs": False,
  43. "template_dir": os.path.abspath(
  44. pkg_resources.resource_filename("synapse", "res/templates")
  45. ),
  46. "smtp_host": "127.0.0.1",
  47. "smtp_port": 20,
  48. "require_transport_security": False,
  49. "smtp_user": None,
  50. "smtp_pass": None,
  51. "notif_from": "test@example.com",
  52. }
  53. config["public_baseurl"] = "https://example.com"
  54. hs = self.setup_test_homeserver(config=config)
  55. async def sendmail(
  56. reactor, smtphost, smtpport, from_addr, to_addrs, msg, **kwargs
  57. ):
  58. self.email_attempts.append(msg)
  59. self.email_attempts = []
  60. hs.get_send_email_handler()._sendmail = sendmail
  61. return hs
  62. def prepare(self, reactor, clock, hs):
  63. self.store = hs.get_datastore()
  64. self.submit_token_resource = PasswordResetSubmitTokenResource(hs)
  65. def test_basic_password_reset(self):
  66. """Test basic password reset flow"""
  67. old_password = "monkey"
  68. new_password = "kangeroo"
  69. user_id = self.register_user("kermit", old_password)
  70. self.login("kermit", old_password)
  71. email = "test@example.com"
  72. # Add a threepid
  73. self.get_success(
  74. self.store.user_add_threepid(
  75. user_id=user_id,
  76. medium="email",
  77. address=email,
  78. validated_at=0,
  79. added_at=0,
  80. )
  81. )
  82. client_secret = "foobar"
  83. session_id = self._request_token(email, client_secret)
  84. self.assertEquals(len(self.email_attempts), 1)
  85. link = self._get_link_from_email()
  86. self._validate_token(link)
  87. self._reset_password(new_password, session_id, client_secret)
  88. # Assert we can log in with the new password
  89. self.login("kermit", new_password)
  90. # Assert we can't log in with the old password
  91. self.attempt_wrong_password_login("kermit", old_password)
  92. @override_config({"rc_3pid_validation": {"burst_count": 3}})
  93. def test_ratelimit_by_email(self):
  94. """Test that we ratelimit /requestToken for the same email."""
  95. old_password = "monkey"
  96. new_password = "kangeroo"
  97. user_id = self.register_user("kermit", old_password)
  98. self.login("kermit", old_password)
  99. email = "test1@example.com"
  100. # Add a threepid
  101. self.get_success(
  102. self.store.user_add_threepid(
  103. user_id=user_id,
  104. medium="email",
  105. address=email,
  106. validated_at=0,
  107. added_at=0,
  108. )
  109. )
  110. def reset(ip):
  111. client_secret = "foobar"
  112. session_id = self._request_token(email, client_secret, ip)
  113. self.assertEquals(len(self.email_attempts), 1)
  114. link = self._get_link_from_email()
  115. self._validate_token(link)
  116. self._reset_password(new_password, session_id, client_secret)
  117. self.email_attempts.clear()
  118. # We expect to be able to make three requests before getting rate
  119. # limited.
  120. #
  121. # We change IPs to ensure that we're not being ratelimited due to the
  122. # same IP
  123. reset("127.0.0.1")
  124. reset("127.0.0.2")
  125. reset("127.0.0.3")
  126. with self.assertRaises(HttpResponseException) as cm:
  127. reset("127.0.0.4")
  128. self.assertEqual(cm.exception.code, 429)
  129. def test_basic_password_reset_canonicalise_email(self):
  130. """Test basic password reset flow
  131. Request password reset with different spelling
  132. """
  133. old_password = "monkey"
  134. new_password = "kangeroo"
  135. user_id = self.register_user("kermit", old_password)
  136. self.login("kermit", old_password)
  137. email_profile = "test@example.com"
  138. email_passwort_reset = "TEST@EXAMPLE.COM"
  139. # Add a threepid
  140. self.get_success(
  141. self.store.user_add_threepid(
  142. user_id=user_id,
  143. medium="email",
  144. address=email_profile,
  145. validated_at=0,
  146. added_at=0,
  147. )
  148. )
  149. client_secret = "foobar"
  150. session_id = self._request_token(email_passwort_reset, client_secret)
  151. self.assertEquals(len(self.email_attempts), 1)
  152. link = self._get_link_from_email()
  153. self._validate_token(link)
  154. self._reset_password(new_password, session_id, client_secret)
  155. # Assert we can log in with the new password
  156. self.login("kermit", new_password)
  157. # Assert we can't log in with the old password
  158. self.attempt_wrong_password_login("kermit", old_password)
  159. def test_cant_reset_password_without_clicking_link(self):
  160. """Test that we do actually need to click the link in the email"""
  161. old_password = "monkey"
  162. new_password = "kangeroo"
  163. user_id = self.register_user("kermit", old_password)
  164. self.login("kermit", old_password)
  165. email = "test@example.com"
  166. # Add a threepid
  167. self.get_success(
  168. self.store.user_add_threepid(
  169. user_id=user_id,
  170. medium="email",
  171. address=email,
  172. validated_at=0,
  173. added_at=0,
  174. )
  175. )
  176. client_secret = "foobar"
  177. session_id = self._request_token(email, client_secret)
  178. self.assertEquals(len(self.email_attempts), 1)
  179. # Attempt to reset password without clicking the link
  180. self._reset_password(new_password, session_id, client_secret, expected_code=401)
  181. # Assert we can log in with the old password
  182. self.login("kermit", old_password)
  183. # Assert we can't log in with the new password
  184. self.attempt_wrong_password_login("kermit", new_password)
  185. def test_no_valid_token(self):
  186. """Test that we do actually need to request a token and can't just
  187. make a session up.
  188. """
  189. old_password = "monkey"
  190. new_password = "kangeroo"
  191. user_id = self.register_user("kermit", old_password)
  192. self.login("kermit", old_password)
  193. email = "test@example.com"
  194. # Add a threepid
  195. self.get_success(
  196. self.store.user_add_threepid(
  197. user_id=user_id,
  198. medium="email",
  199. address=email,
  200. validated_at=0,
  201. added_at=0,
  202. )
  203. )
  204. client_secret = "foobar"
  205. session_id = "weasle"
  206. # Attempt to reset password without even requesting an email
  207. self._reset_password(new_password, session_id, client_secret, expected_code=401)
  208. # Assert we can log in with the old password
  209. self.login("kermit", old_password)
  210. # Assert we can't log in with the new password
  211. self.attempt_wrong_password_login("kermit", new_password)
  212. @unittest.override_config({"request_token_inhibit_3pid_errors": True})
  213. def test_password_reset_bad_email_inhibit_error(self):
  214. """Test that triggering a password reset with an email address that isn't bound
  215. to an account doesn't leak the lack of binding for that address if configured
  216. that way.
  217. """
  218. self.register_user("kermit", "monkey")
  219. self.login("kermit", "monkey")
  220. email = "test@example.com"
  221. client_secret = "foobar"
  222. session_id = self._request_token(email, client_secret)
  223. self.assertIsNotNone(session_id)
  224. def _request_token(self, email, client_secret, ip="127.0.0.1"):
  225. channel = self.make_request(
  226. "POST",
  227. b"account/password/email/requestToken",
  228. {"client_secret": client_secret, "email": email, "send_attempt": 1},
  229. client_ip=ip,
  230. )
  231. if channel.code != 200:
  232. raise HttpResponseException(
  233. channel.code,
  234. channel.result["reason"],
  235. channel.result["body"],
  236. )
  237. return channel.json_body["sid"]
  238. def _validate_token(self, link):
  239. # Remove the host
  240. path = link.replace("https://example.com", "")
  241. # Load the password reset confirmation page
  242. channel = make_request(
  243. self.reactor,
  244. FakeSite(self.submit_token_resource),
  245. "GET",
  246. path,
  247. shorthand=False,
  248. )
  249. self.assertEquals(200, channel.code, channel.result)
  250. # Now POST to the same endpoint, mimicking the same behaviour as clicking the
  251. # password reset confirm button
  252. # Confirm the password reset
  253. channel = make_request(
  254. self.reactor,
  255. FakeSite(self.submit_token_resource),
  256. "POST",
  257. path,
  258. content=b"",
  259. shorthand=False,
  260. content_is_form=True,
  261. )
  262. self.assertEquals(200, channel.code, channel.result)
  263. def _get_link_from_email(self):
  264. assert self.email_attempts, "No emails have been sent"
  265. raw_msg = self.email_attempts[-1].decode("UTF-8")
  266. mail = Parser().parsestr(raw_msg)
  267. text = None
  268. for part in mail.walk():
  269. if part.get_content_type() == "text/plain":
  270. text = part.get_payload(decode=True).decode("UTF-8")
  271. break
  272. if not text:
  273. self.fail("Could not find text portion of email to parse")
  274. match = re.search(r"https://example.com\S+", text)
  275. assert match, "Could not find link in email"
  276. return match.group(0)
  277. def _reset_password(
  278. self, new_password, session_id, client_secret, expected_code=200
  279. ):
  280. channel = self.make_request(
  281. "POST",
  282. b"account/password",
  283. {
  284. "new_password": new_password,
  285. "auth": {
  286. "type": LoginType.EMAIL_IDENTITY,
  287. "threepid_creds": {
  288. "client_secret": client_secret,
  289. "sid": session_id,
  290. },
  291. },
  292. },
  293. )
  294. self.assertEquals(expected_code, channel.code, channel.result)
  295. class DeactivateTestCase(unittest.HomeserverTestCase):
  296. servlets = [
  297. synapse.rest.admin.register_servlets_for_client_rest_resource,
  298. login.register_servlets,
  299. account.register_servlets,
  300. room.register_servlets,
  301. ]
  302. def make_homeserver(self, reactor, clock):
  303. self.hs = self.setup_test_homeserver()
  304. return self.hs
  305. def test_deactivate_account(self):
  306. user_id = self.register_user("kermit", "test")
  307. tok = self.login("kermit", "test")
  308. self.deactivate(user_id, tok)
  309. store = self.hs.get_datastore()
  310. # Check that the user has been marked as deactivated.
  311. self.assertTrue(self.get_success(store.get_user_deactivated_status(user_id)))
  312. # Check that this access token has been invalidated.
  313. channel = self.make_request("GET", "account/whoami", access_token=tok)
  314. self.assertEqual(channel.code, 401)
  315. def test_pending_invites(self):
  316. """Tests that deactivating a user rejects every pending invite for them."""
  317. store = self.hs.get_datastore()
  318. inviter_id = self.register_user("inviter", "test")
  319. inviter_tok = self.login("inviter", "test")
  320. invitee_id = self.register_user("invitee", "test")
  321. invitee_tok = self.login("invitee", "test")
  322. # Make @inviter:test invite @invitee:test in a new room.
  323. room_id = self.helper.create_room_as(inviter_id, tok=inviter_tok)
  324. self.helper.invite(
  325. room=room_id, src=inviter_id, targ=invitee_id, tok=inviter_tok
  326. )
  327. # Make sure the invite is here.
  328. pending_invites = self.get_success(
  329. store.get_invited_rooms_for_local_user(invitee_id)
  330. )
  331. self.assertEqual(len(pending_invites), 1, pending_invites)
  332. self.assertEqual(pending_invites[0].room_id, room_id, pending_invites)
  333. # Deactivate @invitee:test.
  334. self.deactivate(invitee_id, invitee_tok)
  335. # Check that the invite isn't there anymore.
  336. pending_invites = self.get_success(
  337. store.get_invited_rooms_for_local_user(invitee_id)
  338. )
  339. self.assertEqual(len(pending_invites), 0, pending_invites)
  340. # Check that the membership of @invitee:test in the room is now "leave".
  341. memberships = self.get_success(
  342. store.get_rooms_for_local_user_where_membership_is(
  343. invitee_id, [Membership.LEAVE]
  344. )
  345. )
  346. self.assertEqual(len(memberships), 1, memberships)
  347. self.assertEqual(memberships[0].room_id, room_id, memberships)
  348. def deactivate(self, user_id, tok):
  349. request_data = json.dumps(
  350. {
  351. "auth": {
  352. "type": "m.login.password",
  353. "user": user_id,
  354. "password": "test",
  355. },
  356. "erase": False,
  357. }
  358. )
  359. channel = self.make_request(
  360. "POST", "account/deactivate", request_data, access_token=tok
  361. )
  362. self.assertEqual(channel.code, 200)
  363. class WhoamiTestCase(unittest.HomeserverTestCase):
  364. servlets = [
  365. synapse.rest.admin.register_servlets_for_client_rest_resource,
  366. login.register_servlets,
  367. account.register_servlets,
  368. register.register_servlets,
  369. ]
  370. def test_GET_whoami(self):
  371. device_id = "wouldgohere"
  372. user_id = self.register_user("kermit", "test")
  373. tok = self.login("kermit", "test", device_id=device_id)
  374. whoami = self.whoami(tok)
  375. self.assertEqual(whoami, {"user_id": user_id, "device_id": device_id})
  376. def test_GET_whoami_appservices(self):
  377. user_id = "@as:test"
  378. as_token = "i_am_an_app_service"
  379. appservice = ApplicationService(
  380. as_token,
  381. self.hs.config.server_name,
  382. id="1234",
  383. namespaces={"users": [{"regex": user_id, "exclusive": True}]},
  384. sender=user_id,
  385. )
  386. self.hs.get_datastore().services_cache.append(appservice)
  387. whoami = self.whoami(as_token)
  388. self.assertEqual(whoami, {"user_id": user_id})
  389. self.assertFalse(hasattr(whoami, "device_id"))
  390. def whoami(self, tok):
  391. channel = self.make_request("GET", "account/whoami", {}, access_token=tok)
  392. self.assertEqual(channel.code, 200)
  393. return channel.json_body
  394. class ThreepidEmailRestTestCase(unittest.HomeserverTestCase):
  395. servlets = [
  396. account.register_servlets,
  397. login.register_servlets,
  398. synapse.rest.admin.register_servlets_for_client_rest_resource,
  399. ]
  400. def make_homeserver(self, reactor, clock):
  401. config = self.default_config()
  402. # Email config.
  403. config["email"] = {
  404. "enable_notifs": False,
  405. "template_dir": os.path.abspath(
  406. pkg_resources.resource_filename("synapse", "res/templates")
  407. ),
  408. "smtp_host": "127.0.0.1",
  409. "smtp_port": 20,
  410. "require_transport_security": False,
  411. "smtp_user": None,
  412. "smtp_pass": None,
  413. "notif_from": "test@example.com",
  414. }
  415. config["public_baseurl"] = "https://example.com"
  416. self.hs = self.setup_test_homeserver(config=config)
  417. async def sendmail(
  418. reactor, smtphost, smtpport, from_addr, to_addrs, msg, **kwargs
  419. ):
  420. self.email_attempts.append(msg)
  421. self.email_attempts = []
  422. self.hs.get_send_email_handler()._sendmail = sendmail
  423. return self.hs
  424. def prepare(self, reactor, clock, hs):
  425. self.store = hs.get_datastore()
  426. self.user_id = self.register_user("kermit", "test")
  427. self.user_id_tok = self.login("kermit", "test")
  428. self.email = "test@example.com"
  429. self.url_3pid = b"account/3pid"
  430. def test_add_valid_email(self):
  431. self.get_success(self._add_email(self.email, self.email))
  432. def test_add_valid_email_second_time(self):
  433. self.get_success(self._add_email(self.email, self.email))
  434. self.get_success(
  435. self._request_token_invalid_email(
  436. self.email,
  437. expected_errcode=Codes.THREEPID_IN_USE,
  438. expected_error="Email is already in use",
  439. )
  440. )
  441. def test_add_valid_email_second_time_canonicalise(self):
  442. self.get_success(self._add_email(self.email, self.email))
  443. self.get_success(
  444. self._request_token_invalid_email(
  445. "TEST@EXAMPLE.COM",
  446. expected_errcode=Codes.THREEPID_IN_USE,
  447. expected_error="Email is already in use",
  448. )
  449. )
  450. def test_add_email_no_at(self):
  451. self.get_success(
  452. self._request_token_invalid_email(
  453. "address-without-at.bar",
  454. expected_errcode=Codes.UNKNOWN,
  455. expected_error="Unable to parse email address",
  456. )
  457. )
  458. def test_add_email_two_at(self):
  459. self.get_success(
  460. self._request_token_invalid_email(
  461. "foo@foo@test.bar",
  462. expected_errcode=Codes.UNKNOWN,
  463. expected_error="Unable to parse email address",
  464. )
  465. )
  466. def test_add_email_bad_format(self):
  467. self.get_success(
  468. self._request_token_invalid_email(
  469. "user@bad.example.net@good.example.com",
  470. expected_errcode=Codes.UNKNOWN,
  471. expected_error="Unable to parse email address",
  472. )
  473. )
  474. def test_add_email_domain_to_lower(self):
  475. self.get_success(self._add_email("foo@TEST.BAR", "foo@test.bar"))
  476. def test_add_email_domain_with_umlaut(self):
  477. self.get_success(self._add_email("foo@Öumlaut.com", "foo@öumlaut.com"))
  478. def test_add_email_address_casefold(self):
  479. self.get_success(self._add_email("Strauß@Example.com", "strauss@example.com"))
  480. def test_address_trim(self):
  481. self.get_success(self._add_email(" foo@test.bar ", "foo@test.bar"))
  482. @override_config({"rc_3pid_validation": {"burst_count": 3}})
  483. def test_ratelimit_by_ip(self):
  484. """Tests that adding emails is ratelimited by IP"""
  485. # We expect to be able to set three emails before getting ratelimited.
  486. self.get_success(self._add_email("foo1@test.bar", "foo1@test.bar"))
  487. self.get_success(self._add_email("foo2@test.bar", "foo2@test.bar"))
  488. self.get_success(self._add_email("foo3@test.bar", "foo3@test.bar"))
  489. with self.assertRaises(HttpResponseException) as cm:
  490. self.get_success(self._add_email("foo4@test.bar", "foo4@test.bar"))
  491. self.assertEqual(cm.exception.code, 429)
  492. def test_add_email_if_disabled(self):
  493. """Test adding email to profile when doing so is disallowed"""
  494. self.hs.config.enable_3pid_changes = False
  495. client_secret = "foobar"
  496. session_id = self._request_token(self.email, client_secret)
  497. self.assertEquals(len(self.email_attempts), 1)
  498. link = self._get_link_from_email()
  499. self._validate_token(link)
  500. channel = self.make_request(
  501. "POST",
  502. b"/_matrix/client/unstable/account/3pid/add",
  503. {
  504. "client_secret": client_secret,
  505. "sid": session_id,
  506. "auth": {
  507. "type": "m.login.password",
  508. "user": self.user_id,
  509. "password": "test",
  510. },
  511. },
  512. access_token=self.user_id_tok,
  513. )
  514. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  515. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  516. # Get user
  517. channel = self.make_request(
  518. "GET",
  519. self.url_3pid,
  520. access_token=self.user_id_tok,
  521. )
  522. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  523. self.assertFalse(channel.json_body["threepids"])
  524. def test_delete_email(self):
  525. """Test deleting an email from profile"""
  526. # Add a threepid
  527. self.get_success(
  528. self.store.user_add_threepid(
  529. user_id=self.user_id,
  530. medium="email",
  531. address=self.email,
  532. validated_at=0,
  533. added_at=0,
  534. )
  535. )
  536. channel = self.make_request(
  537. "POST",
  538. b"account/3pid/delete",
  539. {"medium": "email", "address": self.email},
  540. access_token=self.user_id_tok,
  541. )
  542. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  543. # Get user
  544. channel = self.make_request(
  545. "GET",
  546. self.url_3pid,
  547. access_token=self.user_id_tok,
  548. )
  549. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  550. self.assertFalse(channel.json_body["threepids"])
  551. def test_delete_email_if_disabled(self):
  552. """Test deleting an email from profile when disallowed"""
  553. self.hs.config.enable_3pid_changes = False
  554. # Add a threepid
  555. self.get_success(
  556. self.store.user_add_threepid(
  557. user_id=self.user_id,
  558. medium="email",
  559. address=self.email,
  560. validated_at=0,
  561. added_at=0,
  562. )
  563. )
  564. channel = self.make_request(
  565. "POST",
  566. b"account/3pid/delete",
  567. {"medium": "email", "address": self.email},
  568. access_token=self.user_id_tok,
  569. )
  570. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  571. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  572. # Get user
  573. channel = self.make_request(
  574. "GET",
  575. self.url_3pid,
  576. access_token=self.user_id_tok,
  577. )
  578. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  579. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  580. self.assertEqual(self.email, channel.json_body["threepids"][0]["address"])
  581. def test_cant_add_email_without_clicking_link(self):
  582. """Test that we do actually need to click the link in the email"""
  583. client_secret = "foobar"
  584. session_id = self._request_token(self.email, client_secret)
  585. self.assertEquals(len(self.email_attempts), 1)
  586. # Attempt to add email without clicking the link
  587. channel = self.make_request(
  588. "POST",
  589. b"/_matrix/client/unstable/account/3pid/add",
  590. {
  591. "client_secret": client_secret,
  592. "sid": session_id,
  593. "auth": {
  594. "type": "m.login.password",
  595. "user": self.user_id,
  596. "password": "test",
  597. },
  598. },
  599. access_token=self.user_id_tok,
  600. )
  601. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  602. self.assertEqual(Codes.THREEPID_AUTH_FAILED, channel.json_body["errcode"])
  603. # Get user
  604. channel = self.make_request(
  605. "GET",
  606. self.url_3pid,
  607. access_token=self.user_id_tok,
  608. )
  609. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  610. self.assertFalse(channel.json_body["threepids"])
  611. def test_no_valid_token(self):
  612. """Test that we do actually need to request a token and can't just
  613. make a session up.
  614. """
  615. client_secret = "foobar"
  616. session_id = "weasle"
  617. # Attempt to add email without even requesting an email
  618. channel = self.make_request(
  619. "POST",
  620. b"/_matrix/client/unstable/account/3pid/add",
  621. {
  622. "client_secret": client_secret,
  623. "sid": session_id,
  624. "auth": {
  625. "type": "m.login.password",
  626. "user": self.user_id,
  627. "password": "test",
  628. },
  629. },
  630. access_token=self.user_id_tok,
  631. )
  632. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  633. self.assertEqual(Codes.THREEPID_AUTH_FAILED, channel.json_body["errcode"])
  634. # Get user
  635. channel = self.make_request(
  636. "GET",
  637. self.url_3pid,
  638. access_token=self.user_id_tok,
  639. )
  640. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  641. self.assertFalse(channel.json_body["threepids"])
  642. @override_config({"next_link_domain_whitelist": None})
  643. def test_next_link(self):
  644. """Tests a valid next_link parameter value with no whitelist (good case)"""
  645. self._request_token(
  646. "something@example.com",
  647. "some_secret",
  648. next_link="https://example.com/a/good/site",
  649. expect_code=200,
  650. )
  651. @override_config({"next_link_domain_whitelist": None})
  652. def test_next_link_exotic_protocol(self):
  653. """Tests using a esoteric protocol as a next_link parameter value.
  654. Someone may be hosting a client on IPFS etc.
  655. """
  656. self._request_token(
  657. "something@example.com",
  658. "some_secret",
  659. next_link="some-protocol://abcdefghijklmopqrstuvwxyz",
  660. expect_code=200,
  661. )
  662. @override_config({"next_link_domain_whitelist": None})
  663. def test_next_link_file_uri(self):
  664. """Tests next_link parameters cannot be file URI"""
  665. # Attempt to use a next_link value that points to the local disk
  666. self._request_token(
  667. "something@example.com",
  668. "some_secret",
  669. next_link="file:///host/path",
  670. expect_code=400,
  671. )
  672. @override_config({"next_link_domain_whitelist": ["example.com", "example.org"]})
  673. def test_next_link_domain_whitelist(self):
  674. """Tests next_link parameters must fit the whitelist if provided"""
  675. # Ensure not providing a next_link parameter still works
  676. self._request_token(
  677. "something@example.com",
  678. "some_secret",
  679. next_link=None,
  680. expect_code=200,
  681. )
  682. self._request_token(
  683. "something@example.com",
  684. "some_secret",
  685. next_link="https://example.com/some/good/page",
  686. expect_code=200,
  687. )
  688. self._request_token(
  689. "something@example.com",
  690. "some_secret",
  691. next_link="https://example.org/some/also/good/page",
  692. expect_code=200,
  693. )
  694. self._request_token(
  695. "something@example.com",
  696. "some_secret",
  697. next_link="https://bad.example.org/some/bad/page",
  698. expect_code=400,
  699. )
  700. @override_config({"next_link_domain_whitelist": []})
  701. def test_empty_next_link_domain_whitelist(self):
  702. """Tests an empty next_lint_domain_whitelist value, meaning next_link is essentially
  703. disallowed
  704. """
  705. self._request_token(
  706. "something@example.com",
  707. "some_secret",
  708. next_link="https://example.com/a/page",
  709. expect_code=400,
  710. )
  711. def _request_token(
  712. self,
  713. email: str,
  714. client_secret: str,
  715. next_link: Optional[str] = None,
  716. expect_code: int = 200,
  717. ) -> str:
  718. """Request a validation token to add an email address to a user's account
  719. Args:
  720. email: The email address to validate
  721. client_secret: A secret string
  722. next_link: A link to redirect the user to after validation
  723. expect_code: Expected return code of the call
  724. Returns:
  725. The ID of the new threepid validation session
  726. """
  727. body = {"client_secret": client_secret, "email": email, "send_attempt": 1}
  728. if next_link:
  729. body["next_link"] = next_link
  730. channel = self.make_request(
  731. "POST",
  732. b"account/3pid/email/requestToken",
  733. body,
  734. )
  735. if channel.code != expect_code:
  736. raise HttpResponseException(
  737. channel.code,
  738. channel.result["reason"],
  739. channel.result["body"],
  740. )
  741. return channel.json_body.get("sid")
  742. def _request_token_invalid_email(
  743. self,
  744. email,
  745. expected_errcode,
  746. expected_error,
  747. client_secret="foobar",
  748. ):
  749. channel = self.make_request(
  750. "POST",
  751. b"account/3pid/email/requestToken",
  752. {"client_secret": client_secret, "email": email, "send_attempt": 1},
  753. )
  754. self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
  755. self.assertEqual(expected_errcode, channel.json_body["errcode"])
  756. self.assertEqual(expected_error, channel.json_body["error"])
  757. def _validate_token(self, link):
  758. # Remove the host
  759. path = link.replace("https://example.com", "")
  760. channel = self.make_request("GET", path, shorthand=False)
  761. self.assertEquals(200, channel.code, channel.result)
  762. def _get_link_from_email(self):
  763. assert self.email_attempts, "No emails have been sent"
  764. raw_msg = self.email_attempts[-1].decode("UTF-8")
  765. mail = Parser().parsestr(raw_msg)
  766. text = None
  767. for part in mail.walk():
  768. if part.get_content_type() == "text/plain":
  769. text = part.get_payload(decode=True).decode("UTF-8")
  770. break
  771. if not text:
  772. self.fail("Could not find text portion of email to parse")
  773. match = re.search(r"https://example.com\S+", text)
  774. assert match, "Could not find link in email"
  775. return match.group(0)
  776. def _add_email(self, request_email, expected_email):
  777. """Test adding an email to profile"""
  778. previous_email_attempts = len(self.email_attempts)
  779. client_secret = "foobar"
  780. session_id = self._request_token(request_email, client_secret)
  781. self.assertEquals(len(self.email_attempts) - previous_email_attempts, 1)
  782. link = self._get_link_from_email()
  783. self._validate_token(link)
  784. channel = self.make_request(
  785. "POST",
  786. b"/_matrix/client/unstable/account/3pid/add",
  787. {
  788. "client_secret": client_secret,
  789. "sid": session_id,
  790. "auth": {
  791. "type": "m.login.password",
  792. "user": self.user_id,
  793. "password": "test",
  794. },
  795. },
  796. access_token=self.user_id_tok,
  797. )
  798. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  799. # Get user
  800. channel = self.make_request(
  801. "GET",
  802. self.url_3pid,
  803. access_token=self.user_id_tok,
  804. )
  805. self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
  806. self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
  807. threepids = {threepid["address"] for threepid in channel.json_body["threepids"]}
  808. self.assertIn(expected_email, threepids)