1
0

test_login.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. # Copyright 2019-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import time
  15. import urllib.parse
  16. from typing import Any, Collection, Dict, List, Optional, Tuple, Union
  17. from unittest.mock import Mock
  18. from urllib.parse import urlencode
  19. import pymacaroons
  20. from typing_extensions import Literal
  21. from twisted.test.proto_helpers import MemoryReactor
  22. from twisted.web.resource import Resource
  23. import synapse.rest.admin
  24. from synapse.api.constants import ApprovalNoticeMedium, LoginType
  25. from synapse.api.errors import Codes
  26. from synapse.appservice import ApplicationService
  27. from synapse.module_api import ModuleApi
  28. from synapse.rest.client import devices, login, logout, register
  29. from synapse.rest.client.account import WhoamiRestServlet
  30. from synapse.rest.synapse.client import build_synapse_client_resource_tree
  31. from synapse.server import HomeServer
  32. from synapse.types import JsonDict, create_requester
  33. from synapse.util import Clock
  34. from tests import unittest
  35. from tests.handlers.test_oidc import HAS_OIDC
  36. from tests.handlers.test_saml import has_saml2
  37. from tests.rest.client.utils import TEST_OIDC_CONFIG
  38. from tests.server import FakeChannel
  39. from tests.test_utils.html_parsers import TestHtmlParser
  40. from tests.unittest import HomeserverTestCase, override_config, skip_unless
  41. try:
  42. from authlib.jose import JsonWebKey, jwt
  43. HAS_JWT = True
  44. except ImportError:
  45. HAS_JWT = False
  46. # synapse server name: used to populate public_baseurl in some tests
  47. SYNAPSE_SERVER_PUBLIC_HOSTNAME = "synapse"
  48. # public_baseurl for some tests. It uses an http:// scheme because
  49. # FakeChannel.isSecure() returns False, so synapse will see the requested uri as
  50. # http://..., so using http in the public_baseurl stops Synapse trying to redirect to
  51. # https://....
  52. BASE_URL = "http://%s/" % (SYNAPSE_SERVER_PUBLIC_HOSTNAME,)
  53. # CAS server used in some tests
  54. CAS_SERVER = "https://fake.test"
  55. # just enough to tell pysaml2 where to redirect to
  56. SAML_SERVER = "https://test.saml.server/idp/sso"
  57. TEST_SAML_METADATA = """
  58. <md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
  59. <md:IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
  60. <md:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="%(SAML_SERVER)s"/>
  61. </md:IDPSSODescriptor>
  62. </md:EntityDescriptor>
  63. """ % {
  64. "SAML_SERVER": SAML_SERVER,
  65. }
  66. LOGIN_URL = b"/_matrix/client/r0/login"
  67. TEST_URL = b"/_matrix/client/r0/account/whoami"
  68. # a (valid) url with some annoying characters in. %3D is =, %26 is &, %2B is +
  69. TEST_CLIENT_REDIRECT_URL = 'https://x?<ab c>&q"+%3D%2B"="fö%26=o"'
  70. # the query params in TEST_CLIENT_REDIRECT_URL
  71. EXPECTED_CLIENT_REDIRECT_URL_PARAMS = [("<ab c>", ""), ('q" =+"', '"fö&=o"')]
  72. # Login flows we expect to appear in the list after the normal ones.
  73. ADDITIONAL_LOGIN_FLOWS = [
  74. {"type": "m.login.application_service"},
  75. ]
  76. class TestSpamChecker:
  77. def __init__(self, config: None, api: ModuleApi):
  78. api.register_spam_checker_callbacks(
  79. check_login_for_spam=self.check_login_for_spam,
  80. )
  81. @staticmethod
  82. def parse_config(config: JsonDict) -> None:
  83. return None
  84. async def check_login_for_spam(
  85. self,
  86. user_id: str,
  87. device_id: Optional[str],
  88. initial_display_name: Optional[str],
  89. request_info: Collection[Tuple[Optional[str], str]],
  90. auth_provider_id: Optional[str] = None,
  91. ) -> Union[
  92. Literal["NOT_SPAM"],
  93. Tuple["synapse.module_api.errors.Codes", JsonDict],
  94. ]:
  95. return "NOT_SPAM"
  96. class DenyAllSpamChecker:
  97. def __init__(self, config: None, api: ModuleApi):
  98. api.register_spam_checker_callbacks(
  99. check_login_for_spam=self.check_login_for_spam,
  100. )
  101. @staticmethod
  102. def parse_config(config: JsonDict) -> None:
  103. return None
  104. async def check_login_for_spam(
  105. self,
  106. user_id: str,
  107. device_id: Optional[str],
  108. initial_display_name: Optional[str],
  109. request_info: Collection[Tuple[Optional[str], str]],
  110. auth_provider_id: Optional[str] = None,
  111. ) -> Union[
  112. Literal["NOT_SPAM"],
  113. Tuple["synapse.module_api.errors.Codes", JsonDict],
  114. ]:
  115. # Return an odd set of values to ensure that they get correctly passed
  116. # to the client.
  117. return Codes.LIMIT_EXCEEDED, {"extra": "value"}
  118. class LoginRestServletTestCase(unittest.HomeserverTestCase):
  119. servlets = [
  120. synapse.rest.admin.register_servlets_for_client_rest_resource,
  121. login.register_servlets,
  122. logout.register_servlets,
  123. devices.register_servlets,
  124. lambda hs, http_server: WhoamiRestServlet(hs).register(http_server),
  125. register.register_servlets,
  126. ]
  127. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  128. self.hs = self.setup_test_homeserver()
  129. self.hs.config.registration.enable_registration = True
  130. self.hs.config.registration.registrations_require_3pid = []
  131. self.hs.config.registration.auto_join_rooms = []
  132. self.hs.config.captcha.enable_registration_captcha = False
  133. return self.hs
  134. @override_config(
  135. {
  136. "rc_login": {
  137. "address": {"per_second": 0.17, "burst_count": 5},
  138. # Prevent the account login ratelimiter from raising first
  139. #
  140. # This is normally covered by the default test homeserver config
  141. # which sets these values to 10000, but as we're overriding the entire
  142. # rc_login dict here, we need to set this manually as well
  143. "account": {"per_second": 10000, "burst_count": 10000},
  144. }
  145. }
  146. )
  147. def test_POST_ratelimiting_per_address(self) -> None:
  148. # Create different users so we're sure not to be bothered by the per-user
  149. # ratelimiter.
  150. for i in range(0, 6):
  151. self.register_user("kermit" + str(i), "monkey")
  152. for i in range(0, 6):
  153. params = {
  154. "type": "m.login.password",
  155. "identifier": {"type": "m.id.user", "user": "kermit" + str(i)},
  156. "password": "monkey",
  157. }
  158. channel = self.make_request(b"POST", LOGIN_URL, params)
  159. if i == 5:
  160. self.assertEqual(channel.code, 429, msg=channel.result)
  161. retry_after_ms = int(channel.json_body["retry_after_ms"])
  162. else:
  163. self.assertEqual(channel.code, 200, msg=channel.result)
  164. # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
  165. # than 1min.
  166. self.assertTrue(retry_after_ms < 6000)
  167. self.reactor.advance(retry_after_ms / 1000.0 + 1.0)
  168. params = {
  169. "type": "m.login.password",
  170. "identifier": {"type": "m.id.user", "user": "kermit" + str(i)},
  171. "password": "monkey",
  172. }
  173. channel = self.make_request(b"POST", LOGIN_URL, params)
  174. self.assertEqual(channel.code, 200, msg=channel.result)
  175. @override_config(
  176. {
  177. "rc_login": {
  178. "account": {"per_second": 0.17, "burst_count": 5},
  179. # Prevent the address login ratelimiter from raising first
  180. #
  181. # This is normally covered by the default test homeserver config
  182. # which sets these values to 10000, but as we're overriding the entire
  183. # rc_login dict here, we need to set this manually as well
  184. "address": {"per_second": 10000, "burst_count": 10000},
  185. }
  186. }
  187. )
  188. def test_POST_ratelimiting_per_account(self) -> None:
  189. self.register_user("kermit", "monkey")
  190. for i in range(0, 6):
  191. params = {
  192. "type": "m.login.password",
  193. "identifier": {"type": "m.id.user", "user": "kermit"},
  194. "password": "monkey",
  195. }
  196. channel = self.make_request(b"POST", LOGIN_URL, params)
  197. if i == 5:
  198. self.assertEqual(channel.code, 429, msg=channel.result)
  199. retry_after_ms = int(channel.json_body["retry_after_ms"])
  200. else:
  201. self.assertEqual(channel.code, 200, msg=channel.result)
  202. # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
  203. # than 1min.
  204. self.assertTrue(retry_after_ms < 6000)
  205. self.reactor.advance(retry_after_ms / 1000.0)
  206. params = {
  207. "type": "m.login.password",
  208. "identifier": {"type": "m.id.user", "user": "kermit"},
  209. "password": "monkey",
  210. }
  211. channel = self.make_request(b"POST", LOGIN_URL, params)
  212. self.assertEqual(channel.code, 200, msg=channel.result)
  213. @override_config(
  214. {
  215. "rc_login": {
  216. # Prevent the address login ratelimiter from raising first
  217. #
  218. # This is normally covered by the default test homeserver config
  219. # which sets these values to 10000, but as we're overriding the entire
  220. # rc_login dict here, we need to set this manually as well
  221. "address": {"per_second": 10000, "burst_count": 10000},
  222. "failed_attempts": {"per_second": 0.17, "burst_count": 5},
  223. }
  224. }
  225. )
  226. def test_POST_ratelimiting_per_account_failed_attempts(self) -> None:
  227. self.register_user("kermit", "monkey")
  228. for i in range(0, 6):
  229. params = {
  230. "type": "m.login.password",
  231. "identifier": {"type": "m.id.user", "user": "kermit"},
  232. "password": "notamonkey",
  233. }
  234. channel = self.make_request(b"POST", LOGIN_URL, params)
  235. if i == 5:
  236. self.assertEqual(channel.code, 429, msg=channel.result)
  237. retry_after_ms = int(channel.json_body["retry_after_ms"])
  238. else:
  239. self.assertEqual(channel.code, 403, msg=channel.result)
  240. # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
  241. # than 1min.
  242. self.assertTrue(retry_after_ms < 6000)
  243. self.reactor.advance(retry_after_ms / 1000.0 + 1.0)
  244. params = {
  245. "type": "m.login.password",
  246. "identifier": {"type": "m.id.user", "user": "kermit"},
  247. "password": "notamonkey",
  248. }
  249. channel = self.make_request(b"POST", LOGIN_URL, params)
  250. self.assertEqual(channel.code, 403, msg=channel.result)
  251. @override_config({"session_lifetime": "24h"})
  252. def test_soft_logout(self) -> None:
  253. self.register_user("kermit", "monkey")
  254. # we shouldn't be able to make requests without an access token
  255. channel = self.make_request(b"GET", TEST_URL)
  256. self.assertEqual(channel.code, 401, msg=channel.result)
  257. self.assertEqual(channel.json_body["errcode"], "M_MISSING_TOKEN")
  258. # log in as normal
  259. params = {
  260. "type": "m.login.password",
  261. "identifier": {"type": "m.id.user", "user": "kermit"},
  262. "password": "monkey",
  263. }
  264. channel = self.make_request(b"POST", LOGIN_URL, params)
  265. self.assertEqual(channel.code, 200, channel.result)
  266. access_token = channel.json_body["access_token"]
  267. device_id = channel.json_body["device_id"]
  268. # we should now be able to make requests with the access token
  269. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  270. self.assertEqual(channel.code, 200, channel.result)
  271. # time passes
  272. self.reactor.advance(24 * 3600)
  273. # ... and we should be soft-logouted
  274. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  275. self.assertEqual(channel.code, 401, channel.result)
  276. self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  277. self.assertEqual(channel.json_body["soft_logout"], True)
  278. #
  279. # test behaviour after deleting the expired device
  280. #
  281. # we now log in as a different device
  282. access_token_2 = self.login("kermit", "monkey")
  283. # more requests with the expired token should still return a soft-logout
  284. self.reactor.advance(3600)
  285. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  286. self.assertEqual(channel.code, 401, channel.result)
  287. self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  288. self.assertEqual(channel.json_body["soft_logout"], True)
  289. # ... but if we delete that device, it will be a proper logout
  290. self._delete_device(access_token_2, "kermit", "monkey", device_id)
  291. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  292. self.assertEqual(channel.code, 401, channel.result)
  293. self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  294. self.assertEqual(channel.json_body["soft_logout"], False)
  295. def _delete_device(
  296. self, access_token: str, user_id: str, password: str, device_id: str
  297. ) -> None:
  298. """Perform the UI-Auth to delete a device"""
  299. channel = self.make_request(
  300. b"DELETE", "devices/" + device_id, access_token=access_token
  301. )
  302. self.assertEqual(channel.code, 401, channel.result)
  303. # check it's a UI-Auth fail
  304. self.assertEqual(
  305. set(channel.json_body.keys()),
  306. {"flows", "params", "session"},
  307. channel.result,
  308. )
  309. auth = {
  310. "type": "m.login.password",
  311. # https://github.com/matrix-org/synapse/issues/5665
  312. # "identifier": {"type": "m.id.user", "user": user_id},
  313. "user": user_id,
  314. "password": password,
  315. "session": channel.json_body["session"],
  316. }
  317. channel = self.make_request(
  318. b"DELETE",
  319. "devices/" + device_id,
  320. access_token=access_token,
  321. content={"auth": auth},
  322. )
  323. self.assertEqual(channel.code, 200, channel.result)
  324. @override_config({"session_lifetime": "24h"})
  325. def test_session_can_hard_logout_after_being_soft_logged_out(self) -> None:
  326. self.register_user("kermit", "monkey")
  327. # log in as normal
  328. access_token = self.login("kermit", "monkey")
  329. # we should now be able to make requests with the access token
  330. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  331. self.assertEqual(channel.code, 200, channel.result)
  332. # time passes
  333. self.reactor.advance(24 * 3600)
  334. # ... and we should be soft-logouted
  335. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  336. self.assertEqual(channel.code, 401, channel.result)
  337. self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  338. self.assertEqual(channel.json_body["soft_logout"], True)
  339. # Now try to hard logout this session
  340. channel = self.make_request(b"POST", "/logout", access_token=access_token)
  341. self.assertEqual(channel.code, 200, msg=channel.result)
  342. @override_config({"session_lifetime": "24h"})
  343. def test_session_can_hard_logout_all_sessions_after_being_soft_logged_out(
  344. self,
  345. ) -> None:
  346. self.register_user("kermit", "monkey")
  347. # log in as normal
  348. access_token = self.login("kermit", "monkey")
  349. # we should now be able to make requests with the access token
  350. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  351. self.assertEqual(channel.code, 200, channel.result)
  352. # time passes
  353. self.reactor.advance(24 * 3600)
  354. # ... and we should be soft-logouted
  355. channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
  356. self.assertEqual(channel.code, 401, channel.result)
  357. self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
  358. self.assertEqual(channel.json_body["soft_logout"], True)
  359. # Now try to hard log out all of the user's sessions
  360. channel = self.make_request(b"POST", "/logout/all", access_token=access_token)
  361. self.assertEqual(channel.code, 200, msg=channel.result)
  362. def test_login_with_overly_long_device_id_fails(self) -> None:
  363. self.register_user("mickey", "cheese")
  364. # create a device_id longer than 512 characters
  365. device_id = "yolo" * 512
  366. body = {
  367. "type": "m.login.password",
  368. "user": "mickey",
  369. "password": "cheese",
  370. "device_id": device_id,
  371. }
  372. # make a login request with the bad device_id
  373. channel = self.make_request(
  374. "POST",
  375. "/_matrix/client/v3/login",
  376. body,
  377. custom_headers=None,
  378. )
  379. # test that the login fails with the correct error code
  380. self.assertEqual(channel.code, 400)
  381. self.assertEqual(channel.json_body["errcode"], "M_INVALID_PARAM")
  382. @override_config(
  383. {
  384. "experimental_features": {
  385. "msc3866": {
  386. "enabled": True,
  387. "require_approval_for_new_accounts": True,
  388. }
  389. }
  390. }
  391. )
  392. def test_require_approval(self) -> None:
  393. channel = self.make_request(
  394. "POST",
  395. "register",
  396. {
  397. "username": "kermit",
  398. "password": "monkey",
  399. "auth": {"type": LoginType.DUMMY},
  400. },
  401. )
  402. self.assertEqual(403, channel.code, channel.result)
  403. self.assertEqual(Codes.USER_AWAITING_APPROVAL, channel.json_body["errcode"])
  404. self.assertEqual(
  405. ApprovalNoticeMedium.NONE, channel.json_body["approval_notice_medium"]
  406. )
  407. params = {
  408. "type": LoginType.PASSWORD,
  409. "identifier": {"type": "m.id.user", "user": "kermit"},
  410. "password": "monkey",
  411. }
  412. channel = self.make_request("POST", LOGIN_URL, params)
  413. self.assertEqual(403, channel.code, channel.result)
  414. self.assertEqual(Codes.USER_AWAITING_APPROVAL, channel.json_body["errcode"])
  415. self.assertEqual(
  416. ApprovalNoticeMedium.NONE, channel.json_body["approval_notice_medium"]
  417. )
  418. def test_get_login_flows_with_login_via_existing_disabled(self) -> None:
  419. """GET /login should return m.login.token without get_login_token"""
  420. channel = self.make_request("GET", "/_matrix/client/r0/login")
  421. self.assertEqual(channel.code, 200, channel.result)
  422. flows = {flow["type"]: flow for flow in channel.json_body["flows"]}
  423. self.assertNotIn("m.login.token", flows)
  424. @override_config({"login_via_existing_session": {"enabled": True}})
  425. def test_get_login_flows_with_login_via_existing_enabled(self) -> None:
  426. """GET /login should return m.login.token with get_login_token true"""
  427. channel = self.make_request("GET", "/_matrix/client/r0/login")
  428. self.assertEqual(channel.code, 200, channel.result)
  429. self.assertCountEqual(
  430. channel.json_body["flows"],
  431. [
  432. {"type": "m.login.token", "get_login_token": True},
  433. {"type": "m.login.password"},
  434. {"type": "m.login.application_service"},
  435. ],
  436. )
  437. @override_config(
  438. {
  439. "modules": [
  440. {
  441. "module": TestSpamChecker.__module__
  442. + "."
  443. + TestSpamChecker.__qualname__
  444. }
  445. ]
  446. }
  447. )
  448. def test_spam_checker_allow(self) -> None:
  449. """Check that that adding a spam checker doesn't break login."""
  450. self.register_user("kermit", "monkey")
  451. body = {"type": "m.login.password", "user": "kermit", "password": "monkey"}
  452. channel = self.make_request(
  453. "POST",
  454. "/_matrix/client/r0/login",
  455. body,
  456. )
  457. self.assertEqual(channel.code, 200, channel.result)
  458. @override_config(
  459. {
  460. "modules": [
  461. {
  462. "module": DenyAllSpamChecker.__module__
  463. + "."
  464. + DenyAllSpamChecker.__qualname__
  465. }
  466. ]
  467. }
  468. )
  469. def test_spam_checker_deny(self) -> None:
  470. """Check that login"""
  471. self.register_user("kermit", "monkey")
  472. body = {"type": "m.login.password", "user": "kermit", "password": "monkey"}
  473. channel = self.make_request(
  474. "POST",
  475. "/_matrix/client/r0/login",
  476. body,
  477. )
  478. self.assertEqual(channel.code, 403, channel.result)
  479. self.assertDictContainsSubset(
  480. {"errcode": Codes.LIMIT_EXCEEDED, "extra": "value"}, channel.json_body
  481. )
  482. @skip_unless(has_saml2 and HAS_OIDC, "Requires SAML2 and OIDC")
  483. class MultiSSOTestCase(unittest.HomeserverTestCase):
  484. """Tests for homeservers with multiple SSO providers enabled"""
  485. servlets = [
  486. login.register_servlets,
  487. ]
  488. def default_config(self) -> Dict[str, Any]:
  489. config = super().default_config()
  490. config["public_baseurl"] = BASE_URL
  491. config["cas_config"] = {
  492. "enabled": True,
  493. "server_url": CAS_SERVER,
  494. "service_url": "https://matrix.goodserver.com:8448",
  495. }
  496. config["saml2_config"] = {
  497. "sp_config": {
  498. "metadata": {"inline": [TEST_SAML_METADATA]},
  499. # use the XMLSecurity backend to avoid relying on xmlsec1
  500. "crypto_backend": "XMLSecurity",
  501. },
  502. }
  503. # default OIDC provider
  504. config["oidc_config"] = TEST_OIDC_CONFIG
  505. # additional OIDC providers
  506. config["oidc_providers"] = [
  507. {
  508. "idp_id": "idp1",
  509. "idp_name": "IDP1",
  510. "discover": False,
  511. "issuer": "https://issuer1",
  512. "client_id": "test-client-id",
  513. "client_secret": "test-client-secret",
  514. "scopes": ["profile"],
  515. "authorization_endpoint": "https://issuer1/auth",
  516. "token_endpoint": "https://issuer1/token",
  517. "userinfo_endpoint": "https://issuer1/userinfo",
  518. "user_mapping_provider": {
  519. "config": {"localpart_template": "{{ user.sub }}"}
  520. },
  521. }
  522. ]
  523. return config
  524. def create_resource_dict(self) -> Dict[str, Resource]:
  525. d = super().create_resource_dict()
  526. d.update(build_synapse_client_resource_tree(self.hs))
  527. return d
  528. def test_get_login_flows(self) -> None:
  529. """GET /login should return password and SSO flows"""
  530. channel = self.make_request("GET", "/_matrix/client/r0/login")
  531. self.assertEqual(channel.code, 200, channel.result)
  532. expected_flow_types = [
  533. "m.login.cas",
  534. "m.login.sso",
  535. "m.login.token",
  536. "m.login.password",
  537. ] + [f["type"] for f in ADDITIONAL_LOGIN_FLOWS]
  538. self.assertCountEqual(
  539. [f["type"] for f in channel.json_body["flows"]], expected_flow_types
  540. )
  541. flows = {flow["type"]: flow for flow in channel.json_body["flows"]}
  542. self.assertCountEqual(
  543. flows["m.login.sso"]["identity_providers"],
  544. [
  545. {"id": "cas", "name": "CAS"},
  546. {"id": "saml", "name": "SAML"},
  547. {"id": "oidc-idp1", "name": "IDP1"},
  548. {"id": "oidc", "name": "OIDC"},
  549. ],
  550. )
  551. def test_multi_sso_redirect(self) -> None:
  552. """/login/sso/redirect should redirect to an identity picker"""
  553. # first hit the redirect url, which should redirect to our idp picker
  554. channel = self._make_sso_redirect_request(None)
  555. self.assertEqual(channel.code, 302, channel.result)
  556. location_headers = channel.headers.getRawHeaders("Location")
  557. assert location_headers
  558. uri = location_headers[0]
  559. # hitting that picker should give us some HTML
  560. channel = self.make_request("GET", uri)
  561. self.assertEqual(channel.code, 200, channel.result)
  562. # parse the form to check it has fields assumed elsewhere in this class
  563. html = channel.result["body"].decode("utf-8")
  564. p = TestHtmlParser()
  565. p.feed(html)
  566. p.close()
  567. # there should be a link for each href
  568. returned_idps: List[str] = []
  569. for link in p.links:
  570. path, query = link.split("?", 1)
  571. self.assertEqual(path, "pick_idp")
  572. params = urllib.parse.parse_qs(query)
  573. self.assertEqual(params["redirectUrl"], [TEST_CLIENT_REDIRECT_URL])
  574. returned_idps.append(params["idp"][0])
  575. self.assertCountEqual(returned_idps, ["cas", "oidc", "oidc-idp1", "saml"])
  576. def test_multi_sso_redirect_to_cas(self) -> None:
  577. """If CAS is chosen, should redirect to the CAS server"""
  578. channel = self.make_request(
  579. "GET",
  580. "/_synapse/client/pick_idp?redirectUrl="
  581. + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL)
  582. + "&idp=cas",
  583. shorthand=False,
  584. )
  585. self.assertEqual(channel.code, 302, channel.result)
  586. location_headers = channel.headers.getRawHeaders("Location")
  587. assert location_headers
  588. cas_uri = location_headers[0]
  589. cas_uri_path, cas_uri_query = cas_uri.split("?", 1)
  590. # it should redirect us to the login page of the cas server
  591. self.assertEqual(cas_uri_path, CAS_SERVER + "/login")
  592. # check that the redirectUrl is correctly encoded in the service param - ie, the
  593. # place that CAS will redirect to
  594. cas_uri_params = urllib.parse.parse_qs(cas_uri_query)
  595. service_uri = cas_uri_params["service"][0]
  596. _, service_uri_query = service_uri.split("?", 1)
  597. service_uri_params = urllib.parse.parse_qs(service_uri_query)
  598. self.assertEqual(service_uri_params["redirectUrl"][0], TEST_CLIENT_REDIRECT_URL)
  599. def test_multi_sso_redirect_to_saml(self) -> None:
  600. """If SAML is chosen, should redirect to the SAML server"""
  601. channel = self.make_request(
  602. "GET",
  603. "/_synapse/client/pick_idp?redirectUrl="
  604. + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL)
  605. + "&idp=saml",
  606. )
  607. self.assertEqual(channel.code, 302, channel.result)
  608. location_headers = channel.headers.getRawHeaders("Location")
  609. assert location_headers
  610. saml_uri = location_headers[0]
  611. saml_uri_path, saml_uri_query = saml_uri.split("?", 1)
  612. # it should redirect us to the login page of the SAML server
  613. self.assertEqual(saml_uri_path, SAML_SERVER)
  614. # the RelayState is used to carry the client redirect url
  615. saml_uri_params = urllib.parse.parse_qs(saml_uri_query)
  616. relay_state_param = saml_uri_params["RelayState"][0]
  617. self.assertEqual(relay_state_param, TEST_CLIENT_REDIRECT_URL)
  618. def test_login_via_oidc(self) -> None:
  619. """If OIDC is chosen, should redirect to the OIDC auth endpoint"""
  620. fake_oidc_server = self.helper.fake_oidc_server()
  621. with fake_oidc_server.patch_homeserver(hs=self.hs):
  622. # pick the default OIDC provider
  623. channel = self.make_request(
  624. "GET",
  625. "/_synapse/client/pick_idp?redirectUrl="
  626. + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL)
  627. + "&idp=oidc",
  628. )
  629. self.assertEqual(channel.code, 302, channel.result)
  630. location_headers = channel.headers.getRawHeaders("Location")
  631. assert location_headers
  632. oidc_uri = location_headers[0]
  633. oidc_uri_path, oidc_uri_query = oidc_uri.split("?", 1)
  634. # it should redirect us to the auth page of the OIDC server
  635. self.assertEqual(oidc_uri_path, fake_oidc_server.authorization_endpoint)
  636. # ... and should have set a cookie including the redirect url
  637. cookie_headers = channel.headers.getRawHeaders("Set-Cookie")
  638. assert cookie_headers
  639. cookies: Dict[str, str] = {}
  640. for h in cookie_headers:
  641. key, value = h.split(";")[0].split("=", maxsplit=1)
  642. cookies[key] = value
  643. oidc_session_cookie = cookies["oidc_session"]
  644. macaroon = pymacaroons.Macaroon.deserialize(oidc_session_cookie)
  645. self.assertEqual(
  646. self._get_value_from_macaroon(macaroon, "client_redirect_url"),
  647. TEST_CLIENT_REDIRECT_URL,
  648. )
  649. channel, _ = self.helper.complete_oidc_auth(
  650. fake_oidc_server, oidc_uri, cookies, {"sub": "user1"}
  651. )
  652. # that should serve a confirmation page
  653. self.assertEqual(channel.code, 200, channel.result)
  654. content_type_headers = channel.headers.getRawHeaders("Content-Type")
  655. assert content_type_headers
  656. self.assertTrue(content_type_headers[-1].startswith("text/html"))
  657. p = TestHtmlParser()
  658. p.feed(channel.text_body)
  659. p.close()
  660. # ... which should contain our redirect link
  661. self.assertEqual(len(p.links), 1)
  662. path, query = p.links[0].split("?", 1)
  663. self.assertEqual(path, "https://x")
  664. # it will have url-encoded the params properly, so we'll have to parse them
  665. params = urllib.parse.parse_qsl(
  666. query, keep_blank_values=True, strict_parsing=True, errors="strict"
  667. )
  668. self.assertEqual(params[0:2], EXPECTED_CLIENT_REDIRECT_URL_PARAMS)
  669. self.assertEqual(params[2][0], "loginToken")
  670. # finally, submit the matrix login token to the login API, which gives us our
  671. # matrix access token, mxid, and device id.
  672. login_token = params[2][1]
  673. chan = self.make_request(
  674. "POST",
  675. "/login",
  676. content={"type": "m.login.token", "token": login_token},
  677. )
  678. self.assertEqual(chan.code, 200, chan.result)
  679. self.assertEqual(chan.json_body["user_id"], "@user1:test")
  680. def test_multi_sso_redirect_to_unknown(self) -> None:
  681. """An unknown IdP should cause a 400"""
  682. channel = self.make_request(
  683. "GET",
  684. "/_synapse/client/pick_idp?redirectUrl=http://x&idp=xyz",
  685. )
  686. self.assertEqual(channel.code, 400, channel.result)
  687. def test_client_idp_redirect_to_unknown(self) -> None:
  688. """If the client tries to pick an unknown IdP, return a 404"""
  689. channel = self._make_sso_redirect_request("xxx")
  690. self.assertEqual(channel.code, 404, channel.result)
  691. self.assertEqual(channel.json_body["errcode"], "M_NOT_FOUND")
  692. def test_client_idp_redirect_to_oidc(self) -> None:
  693. """If the client pick a known IdP, redirect to it"""
  694. fake_oidc_server = self.helper.fake_oidc_server()
  695. with fake_oidc_server.patch_homeserver(hs=self.hs):
  696. channel = self._make_sso_redirect_request("oidc")
  697. self.assertEqual(channel.code, 302, channel.result)
  698. location_headers = channel.headers.getRawHeaders("Location")
  699. assert location_headers
  700. oidc_uri = location_headers[0]
  701. oidc_uri_path, oidc_uri_query = oidc_uri.split("?", 1)
  702. # it should redirect us to the auth page of the OIDC server
  703. self.assertEqual(oidc_uri_path, fake_oidc_server.authorization_endpoint)
  704. def _make_sso_redirect_request(self, idp_prov: Optional[str] = None) -> FakeChannel:
  705. """Send a request to /_matrix/client/r0/login/sso/redirect
  706. ... possibly specifying an IDP provider
  707. """
  708. endpoint = "/_matrix/client/r0/login/sso/redirect"
  709. if idp_prov is not None:
  710. endpoint += "/" + idp_prov
  711. endpoint += "?redirectUrl=" + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL)
  712. return self.make_request(
  713. "GET",
  714. endpoint,
  715. custom_headers=[("Host", SYNAPSE_SERVER_PUBLIC_HOSTNAME)],
  716. )
  717. @staticmethod
  718. def _get_value_from_macaroon(macaroon: pymacaroons.Macaroon, key: str) -> str:
  719. prefix = key + " = "
  720. for caveat in macaroon.caveats:
  721. if caveat.caveat_id.startswith(prefix):
  722. return caveat.caveat_id[len(prefix) :]
  723. raise ValueError("No %s caveat in macaroon" % (key,))
  724. class CASTestCase(unittest.HomeserverTestCase):
  725. servlets = [
  726. login.register_servlets,
  727. ]
  728. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  729. self.base_url = "https://matrix.goodserver.com/"
  730. self.redirect_path = "_synapse/client/login/sso/redirect/confirm"
  731. config = self.default_config()
  732. config["public_baseurl"] = (
  733. config.get("public_baseurl") or "https://matrix.goodserver.com:8448"
  734. )
  735. config["cas_config"] = {
  736. "enabled": True,
  737. "server_url": CAS_SERVER,
  738. }
  739. cas_user_id = "username"
  740. self.user_id = "@%s:test" % cas_user_id
  741. async def get_raw(uri: str, args: Any) -> bytes:
  742. """Return an example response payload from a call to the `/proxyValidate`
  743. endpoint of a CAS server, copied from
  744. https://apereo.github.io/cas/5.0.x/protocol/CAS-Protocol-V2-Specification.html#26-proxyvalidate-cas-20
  745. This needs to be returned by an async function (as opposed to set as the
  746. mock's return value) because the corresponding Synapse code awaits on it.
  747. """
  748. return (
  749. """
  750. <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
  751. <cas:authenticationSuccess>
  752. <cas:user>%s</cas:user>
  753. <cas:proxyGrantingTicket>PGTIOU-84678-8a9d...</cas:proxyGrantingTicket>
  754. <cas:proxies>
  755. <cas:proxy>https://proxy2/pgtUrl</cas:proxy>
  756. <cas:proxy>https://proxy1/pgtUrl</cas:proxy>
  757. </cas:proxies>
  758. </cas:authenticationSuccess>
  759. </cas:serviceResponse>
  760. """
  761. % cas_user_id
  762. ).encode("utf-8")
  763. mocked_http_client = Mock(spec=["get_raw"])
  764. mocked_http_client.get_raw.side_effect = get_raw
  765. self.hs = self.setup_test_homeserver(
  766. config=config,
  767. proxied_http_client=mocked_http_client,
  768. )
  769. return self.hs
  770. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  771. self.deactivate_account_handler = hs.get_deactivate_account_handler()
  772. def test_cas_redirect_confirm(self) -> None:
  773. """Tests that the SSO login flow serves a confirmation page before redirecting a
  774. user to the redirect URL.
  775. """
  776. base_url = "/_matrix/client/r0/login/cas/ticket?redirectUrl"
  777. redirect_url = "https://dodgy-site.com/"
  778. url_parts = list(urllib.parse.urlparse(base_url))
  779. query = dict(urllib.parse.parse_qsl(url_parts[4]))
  780. query.update({"redirectUrl": redirect_url})
  781. query.update({"ticket": "ticket"})
  782. url_parts[4] = urllib.parse.urlencode(query)
  783. cas_ticket_url = urllib.parse.urlunparse(url_parts)
  784. # Get Synapse to call the fake CAS and serve the template.
  785. channel = self.make_request("GET", cas_ticket_url)
  786. # Test that the response is HTML.
  787. self.assertEqual(channel.code, 200, channel.result)
  788. content_type_header_value = ""
  789. for header in channel.result.get("headers", []):
  790. if header[0] == b"Content-Type":
  791. content_type_header_value = header[1].decode("utf8")
  792. self.assertTrue(content_type_header_value.startswith("text/html"))
  793. # Test that the body isn't empty.
  794. self.assertTrue(len(channel.result["body"]) > 0)
  795. # And that it contains our redirect link
  796. self.assertIn(redirect_url, channel.result["body"].decode("UTF-8"))
  797. @override_config(
  798. {
  799. "sso": {
  800. "client_whitelist": [
  801. "https://legit-site.com/",
  802. "https://other-site.com/",
  803. ]
  804. }
  805. }
  806. )
  807. def test_cas_redirect_whitelisted(self) -> None:
  808. """Tests that the SSO login flow serves a redirect to a whitelisted url"""
  809. self._test_redirect("https://legit-site.com/")
  810. @override_config({"public_baseurl": "https://example.com"})
  811. def test_cas_redirect_login_fallback(self) -> None:
  812. self._test_redirect("https://example.com/_matrix/static/client/login")
  813. def _test_redirect(self, redirect_url: str) -> None:
  814. """Tests that the SSO login flow serves a redirect for the given redirect URL."""
  815. cas_ticket_url = (
  816. "/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket"
  817. % (urllib.parse.quote(redirect_url))
  818. )
  819. # Get Synapse to call the fake CAS and serve the template.
  820. channel = self.make_request("GET", cas_ticket_url)
  821. self.assertEqual(channel.code, 302)
  822. location_headers = channel.headers.getRawHeaders("Location")
  823. assert location_headers
  824. self.assertEqual(location_headers[0][: len(redirect_url)], redirect_url)
  825. @override_config({"sso": {"client_whitelist": ["https://legit-site.com/"]}})
  826. def test_deactivated_user(self) -> None:
  827. """Logging in as a deactivated account should error."""
  828. redirect_url = "https://legit-site.com/"
  829. # First login (to create the user).
  830. self._test_redirect(redirect_url)
  831. # Deactivate the account.
  832. self.get_success(
  833. self.deactivate_account_handler.deactivate_account(
  834. self.user_id, False, create_requester(self.user_id)
  835. )
  836. )
  837. # Request the CAS ticket.
  838. cas_ticket_url = (
  839. "/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket"
  840. % (urllib.parse.quote(redirect_url))
  841. )
  842. # Get Synapse to call the fake CAS and serve the template.
  843. channel = self.make_request("GET", cas_ticket_url)
  844. # Because the user is deactivated they are served an error template.
  845. self.assertEqual(channel.code, 403)
  846. self.assertIn(b"SSO account deactivated", channel.result["body"])
  847. @skip_unless(HAS_JWT, "requires authlib")
  848. class JWTTestCase(unittest.HomeserverTestCase):
  849. servlets = [
  850. synapse.rest.admin.register_servlets_for_client_rest_resource,
  851. login.register_servlets,
  852. ]
  853. jwt_secret = "secret"
  854. jwt_algorithm = "HS256"
  855. base_config = {
  856. "enabled": True,
  857. "secret": jwt_secret,
  858. "algorithm": jwt_algorithm,
  859. }
  860. def default_config(self) -> Dict[str, Any]:
  861. config = super().default_config()
  862. # If jwt_config has been defined (eg via @override_config), don't replace it.
  863. if config.get("jwt_config") is None:
  864. config["jwt_config"] = self.base_config
  865. return config
  866. def jwt_encode(self, payload: Dict[str, Any], secret: str = jwt_secret) -> str:
  867. header = {"alg": self.jwt_algorithm}
  868. result: bytes = jwt.encode(header, payload, secret)
  869. return result.decode("ascii")
  870. def jwt_login(self, *args: Any) -> FakeChannel:
  871. params = {"type": "org.matrix.login.jwt", "token": self.jwt_encode(*args)}
  872. channel = self.make_request(b"POST", LOGIN_URL, params)
  873. return channel
  874. def test_login_jwt_valid_registered(self) -> None:
  875. self.register_user("kermit", "monkey")
  876. channel = self.jwt_login({"sub": "kermit"})
  877. self.assertEqual(channel.code, 200, msg=channel.result)
  878. self.assertEqual(channel.json_body["user_id"], "@kermit:test")
  879. def test_login_jwt_valid_unregistered(self) -> None:
  880. channel = self.jwt_login({"sub": "frog"})
  881. self.assertEqual(channel.code, 200, msg=channel.result)
  882. self.assertEqual(channel.json_body["user_id"], "@frog:test")
  883. def test_login_jwt_invalid_signature(self) -> None:
  884. channel = self.jwt_login({"sub": "frog"}, "notsecret")
  885. self.assertEqual(channel.code, 403, msg=channel.result)
  886. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  887. self.assertEqual(
  888. channel.json_body["error"],
  889. "JWT validation failed: Signature verification failed",
  890. )
  891. def test_login_jwt_expired(self) -> None:
  892. channel = self.jwt_login({"sub": "frog", "exp": 864000})
  893. self.assertEqual(channel.code, 403, msg=channel.result)
  894. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  895. self.assertEqual(
  896. channel.json_body["error"],
  897. "JWT validation failed: expired_token: The token is expired",
  898. )
  899. def test_login_jwt_not_before(self) -> None:
  900. now = int(time.time())
  901. channel = self.jwt_login({"sub": "frog", "nbf": now + 3600})
  902. self.assertEqual(channel.code, 403, msg=channel.result)
  903. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  904. self.assertEqual(
  905. channel.json_body["error"],
  906. "JWT validation failed: invalid_token: The token is not valid yet",
  907. )
  908. def test_login_no_sub(self) -> None:
  909. channel = self.jwt_login({"username": "root"})
  910. self.assertEqual(channel.code, 403, msg=channel.result)
  911. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  912. self.assertEqual(channel.json_body["error"], "Invalid JWT")
  913. @override_config({"jwt_config": {**base_config, "issuer": "test-issuer"}})
  914. def test_login_iss(self) -> None:
  915. """Test validating the issuer claim."""
  916. # A valid issuer.
  917. channel = self.jwt_login({"sub": "kermit", "iss": "test-issuer"})
  918. self.assertEqual(channel.code, 200, msg=channel.result)
  919. self.assertEqual(channel.json_body["user_id"], "@kermit:test")
  920. # An invalid issuer.
  921. channel = self.jwt_login({"sub": "kermit", "iss": "invalid"})
  922. self.assertEqual(channel.code, 403, msg=channel.result)
  923. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  924. self.assertEqual(
  925. channel.json_body["error"],
  926. 'JWT validation failed: invalid_claim: Invalid claim "iss"',
  927. )
  928. # Not providing an issuer.
  929. channel = self.jwt_login({"sub": "kermit"})
  930. self.assertEqual(channel.code, 403, msg=channel.result)
  931. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  932. self.assertEqual(
  933. channel.json_body["error"],
  934. 'JWT validation failed: missing_claim: Missing "iss" claim',
  935. )
  936. def test_login_iss_no_config(self) -> None:
  937. """Test providing an issuer claim without requiring it in the configuration."""
  938. channel = self.jwt_login({"sub": "kermit", "iss": "invalid"})
  939. self.assertEqual(channel.code, 200, msg=channel.result)
  940. self.assertEqual(channel.json_body["user_id"], "@kermit:test")
  941. @override_config({"jwt_config": {**base_config, "audiences": ["test-audience"]}})
  942. def test_login_aud(self) -> None:
  943. """Test validating the audience claim."""
  944. # A valid audience.
  945. channel = self.jwt_login({"sub": "kermit", "aud": "test-audience"})
  946. self.assertEqual(channel.code, 200, msg=channel.result)
  947. self.assertEqual(channel.json_body["user_id"], "@kermit:test")
  948. # An invalid audience.
  949. channel = self.jwt_login({"sub": "kermit", "aud": "invalid"})
  950. self.assertEqual(channel.code, 403, msg=channel.result)
  951. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  952. self.assertEqual(
  953. channel.json_body["error"],
  954. 'JWT validation failed: invalid_claim: Invalid claim "aud"',
  955. )
  956. # Not providing an audience.
  957. channel = self.jwt_login({"sub": "kermit"})
  958. self.assertEqual(channel.code, 403, msg=channel.result)
  959. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  960. self.assertEqual(
  961. channel.json_body["error"],
  962. 'JWT validation failed: missing_claim: Missing "aud" claim',
  963. )
  964. def test_login_aud_no_config(self) -> None:
  965. """Test providing an audience without requiring it in the configuration."""
  966. channel = self.jwt_login({"sub": "kermit", "aud": "invalid"})
  967. self.assertEqual(channel.code, 403, msg=channel.result)
  968. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  969. self.assertEqual(
  970. channel.json_body["error"],
  971. 'JWT validation failed: invalid_claim: Invalid claim "aud"',
  972. )
  973. def test_login_default_sub(self) -> None:
  974. """Test reading user ID from the default subject claim."""
  975. channel = self.jwt_login({"sub": "kermit"})
  976. self.assertEqual(channel.code, 200, msg=channel.result)
  977. self.assertEqual(channel.json_body["user_id"], "@kermit:test")
  978. @override_config({"jwt_config": {**base_config, "subject_claim": "username"}})
  979. def test_login_custom_sub(self) -> None:
  980. """Test reading user ID from a custom subject claim."""
  981. channel = self.jwt_login({"username": "frog"})
  982. self.assertEqual(channel.code, 200, msg=channel.result)
  983. self.assertEqual(channel.json_body["user_id"], "@frog:test")
  984. def test_login_no_token(self) -> None:
  985. params = {"type": "org.matrix.login.jwt"}
  986. channel = self.make_request(b"POST", LOGIN_URL, params)
  987. self.assertEqual(channel.code, 403, msg=channel.result)
  988. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  989. self.assertEqual(channel.json_body["error"], "Token field for JWT is missing")
  990. def test_deactivated_user(self) -> None:
  991. """Logging in as a deactivated account should error."""
  992. user_id = self.register_user("kermit", "monkey")
  993. self.get_success(
  994. self.hs.get_deactivate_account_handler().deactivate_account(
  995. user_id, erase_data=False, requester=create_requester(user_id)
  996. )
  997. )
  998. channel = self.jwt_login({"sub": "kermit"})
  999. self.assertEqual(channel.code, 403, msg=channel.result)
  1000. self.assertEqual(channel.json_body["errcode"], "M_USER_DEACTIVATED")
  1001. self.assertEqual(
  1002. channel.json_body["error"], "This account has been deactivated"
  1003. )
  1004. # The JWTPubKeyTestCase is a complement to JWTTestCase where we instead use
  1005. # RSS256, with a public key configured in synapse as "jwt_secret", and tokens
  1006. # signed by the private key.
  1007. @skip_unless(HAS_JWT, "requires authlib")
  1008. class JWTPubKeyTestCase(unittest.HomeserverTestCase):
  1009. servlets = [
  1010. login.register_servlets,
  1011. ]
  1012. # This key's pubkey is used as the jwt_secret setting of synapse. Valid
  1013. # tokens are signed by this and validated using the pubkey. It is generated
  1014. # with `openssl genrsa 512` (not a secure way to generate real keys, but
  1015. # good enough for tests!)
  1016. jwt_privatekey = "\n".join(
  1017. [
  1018. "-----BEGIN RSA PRIVATE KEY-----",
  1019. "MIIBPAIBAAJBAM50f1Q5gsdmzifLstzLHb5NhfajiOt7TKO1vSEWdq7u9x8SMFiB",
  1020. "492RM9W/XFoh8WUfL9uL6Now6tPRDsWv3xsCAwEAAQJAUv7OOSOtiU+wzJq82rnk",
  1021. "yR4NHqt7XX8BvkZPM7/+EjBRanmZNSp5kYZzKVaZ/gTOM9+9MwlmhidrUOweKfB/",
  1022. "kQIhAPZwHazbjo7dYlJs7wPQz1vd+aHSEH+3uQKIysebkmm3AiEA1nc6mDdmgiUq",
  1023. "TpIN8A4MBKmfZMWTLq6z05y/qjKyxb0CIQDYJxCwTEenIaEa4PdoJl+qmXFasVDN",
  1024. "ZU0+XtNV7yul0wIhAMI9IhiStIjS2EppBa6RSlk+t1oxh2gUWlIh+YVQfZGRAiEA",
  1025. "tqBR7qLZGJ5CVKxWmNhJZGt1QHoUtOch8t9C4IdOZ2g=",
  1026. "-----END RSA PRIVATE KEY-----",
  1027. ]
  1028. )
  1029. # Generated with `openssl rsa -in foo.key -pubout`, with the the above
  1030. # private key placed in foo.key (jwt_privatekey).
  1031. jwt_pubkey = "\n".join(
  1032. [
  1033. "-----BEGIN PUBLIC KEY-----",
  1034. "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM50f1Q5gsdmzifLstzLHb5NhfajiOt7",
  1035. "TKO1vSEWdq7u9x8SMFiB492RM9W/XFoh8WUfL9uL6Now6tPRDsWv3xsCAwEAAQ==",
  1036. "-----END PUBLIC KEY-----",
  1037. ]
  1038. )
  1039. # This key is used to sign tokens that shouldn't be accepted by synapse.
  1040. # Generated just like jwt_privatekey.
  1041. bad_privatekey = "\n".join(
  1042. [
  1043. "-----BEGIN RSA PRIVATE KEY-----",
  1044. "MIIBOgIBAAJBAL//SQrKpKbjCCnv/FlasJCv+t3k/MPsZfniJe4DVFhsktF2lwQv",
  1045. "gLjmQD3jBUTz+/FndLSBvr3F4OHtGL9O/osCAwEAAQJAJqH0jZJW7Smzo9ShP02L",
  1046. "R6HRZcLExZuUrWI+5ZSP7TaZ1uwJzGFspDrunqaVoPobndw/8VsP8HFyKtceC7vY",
  1047. "uQIhAPdYInDDSJ8rFKGiy3Ajv5KWISBicjevWHF9dbotmNO9AiEAxrdRJVU+EI9I",
  1048. "eB4qRZpY6n4pnwyP0p8f/A3NBaQPG+cCIFlj08aW/PbxNdqYoBdeBA0xDrXKfmbb",
  1049. "iwYxBkwL0JCtAiBYmsi94sJn09u2Y4zpuCbJeDPKzWkbuwQh+W1fhIWQJQIhAKR0",
  1050. "KydN6cRLvphNQ9c/vBTdlzWxzcSxREpguC7F1J1m",
  1051. "-----END RSA PRIVATE KEY-----",
  1052. ]
  1053. )
  1054. def default_config(self) -> Dict[str, Any]:
  1055. config = super().default_config()
  1056. config["jwt_config"] = {
  1057. "enabled": True,
  1058. "secret": self.jwt_pubkey,
  1059. "algorithm": "RS256",
  1060. }
  1061. return config
  1062. def jwt_encode(self, payload: Dict[str, Any], secret: str = jwt_privatekey) -> str:
  1063. header = {"alg": "RS256"}
  1064. if secret.startswith("-----BEGIN RSA PRIVATE KEY-----"):
  1065. secret = JsonWebKey.import_key(secret, {"kty": "RSA"})
  1066. result: bytes = jwt.encode(header, payload, secret)
  1067. return result.decode("ascii")
  1068. def jwt_login(self, *args: Any) -> FakeChannel:
  1069. params = {"type": "org.matrix.login.jwt", "token": self.jwt_encode(*args)}
  1070. channel = self.make_request(b"POST", LOGIN_URL, params)
  1071. return channel
  1072. def test_login_jwt_valid(self) -> None:
  1073. channel = self.jwt_login({"sub": "kermit"})
  1074. self.assertEqual(channel.code, 200, msg=channel.result)
  1075. self.assertEqual(channel.json_body["user_id"], "@kermit:test")
  1076. def test_login_jwt_invalid_signature(self) -> None:
  1077. channel = self.jwt_login({"sub": "frog"}, self.bad_privatekey)
  1078. self.assertEqual(channel.code, 403, msg=channel.result)
  1079. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  1080. self.assertEqual(
  1081. channel.json_body["error"],
  1082. "JWT validation failed: Signature verification failed",
  1083. )
  1084. AS_USER = "as_user_alice"
  1085. class AppserviceLoginRestServletTestCase(unittest.HomeserverTestCase):
  1086. servlets = [
  1087. login.register_servlets,
  1088. register.register_servlets,
  1089. ]
  1090. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  1091. self.hs = self.setup_test_homeserver()
  1092. self.service = ApplicationService(
  1093. id="unique_identifier",
  1094. token="some_token",
  1095. sender="@asbot:example.com",
  1096. namespaces={
  1097. ApplicationService.NS_USERS: [
  1098. {"regex": r"@as_user.*", "exclusive": False}
  1099. ],
  1100. ApplicationService.NS_ROOMS: [],
  1101. ApplicationService.NS_ALIASES: [],
  1102. },
  1103. )
  1104. self.another_service = ApplicationService(
  1105. id="another__identifier",
  1106. token="another_token",
  1107. sender="@as2bot:example.com",
  1108. namespaces={
  1109. ApplicationService.NS_USERS: [
  1110. {"regex": r"@as2_user.*", "exclusive": False}
  1111. ],
  1112. ApplicationService.NS_ROOMS: [],
  1113. ApplicationService.NS_ALIASES: [],
  1114. },
  1115. )
  1116. self.hs.get_datastores().main.services_cache.append(self.service)
  1117. self.hs.get_datastores().main.services_cache.append(self.another_service)
  1118. return self.hs
  1119. def test_login_appservice_user(self) -> None:
  1120. """Test that an appservice user can use /login"""
  1121. self.register_appservice_user(AS_USER, self.service.token)
  1122. params = {
  1123. "type": login.LoginRestServlet.APPSERVICE_TYPE,
  1124. "identifier": {"type": "m.id.user", "user": AS_USER},
  1125. }
  1126. channel = self.make_request(
  1127. b"POST", LOGIN_URL, params, access_token=self.service.token
  1128. )
  1129. self.assertEqual(channel.code, 200, msg=channel.result)
  1130. def test_login_appservice_user_bot(self) -> None:
  1131. """Test that the appservice bot can use /login"""
  1132. self.register_appservice_user(AS_USER, self.service.token)
  1133. params = {
  1134. "type": login.LoginRestServlet.APPSERVICE_TYPE,
  1135. "identifier": {"type": "m.id.user", "user": self.service.sender},
  1136. }
  1137. channel = self.make_request(
  1138. b"POST", LOGIN_URL, params, access_token=self.service.token
  1139. )
  1140. self.assertEqual(channel.code, 200, msg=channel.result)
  1141. def test_login_appservice_wrong_user(self) -> None:
  1142. """Test that non-as users cannot login with the as token"""
  1143. self.register_appservice_user(AS_USER, self.service.token)
  1144. params = {
  1145. "type": login.LoginRestServlet.APPSERVICE_TYPE,
  1146. "identifier": {"type": "m.id.user", "user": "fibble_wibble"},
  1147. }
  1148. channel = self.make_request(
  1149. b"POST", LOGIN_URL, params, access_token=self.service.token
  1150. )
  1151. self.assertEqual(channel.code, 403, msg=channel.result)
  1152. def test_login_appservice_wrong_as(self) -> None:
  1153. """Test that as users cannot login with wrong as token"""
  1154. self.register_appservice_user(AS_USER, self.service.token)
  1155. params = {
  1156. "type": login.LoginRestServlet.APPSERVICE_TYPE,
  1157. "identifier": {"type": "m.id.user", "user": AS_USER},
  1158. }
  1159. channel = self.make_request(
  1160. b"POST", LOGIN_URL, params, access_token=self.another_service.token
  1161. )
  1162. self.assertEqual(channel.code, 403, msg=channel.result)
  1163. def test_login_appservice_no_token(self) -> None:
  1164. """Test that users must provide a token when using the appservice
  1165. login method
  1166. """
  1167. self.register_appservice_user(AS_USER, self.service.token)
  1168. params = {
  1169. "type": login.LoginRestServlet.APPSERVICE_TYPE,
  1170. "identifier": {"type": "m.id.user", "user": AS_USER},
  1171. }
  1172. channel = self.make_request(b"POST", LOGIN_URL, params)
  1173. self.assertEqual(channel.code, 401, msg=channel.result)
  1174. @skip_unless(HAS_OIDC, "requires OIDC")
  1175. class UsernamePickerTestCase(HomeserverTestCase):
  1176. """Tests for the username picker flow of SSO login"""
  1177. servlets = [login.register_servlets]
  1178. def default_config(self) -> Dict[str, Any]:
  1179. config = super().default_config()
  1180. config["public_baseurl"] = BASE_URL
  1181. config["oidc_config"] = {}
  1182. config["oidc_config"].update(TEST_OIDC_CONFIG)
  1183. config["oidc_config"]["user_mapping_provider"] = {
  1184. "config": {"display_name_template": "{{ user.displayname }}"}
  1185. }
  1186. # whitelist this client URI so we redirect straight to it rather than
  1187. # serving a confirmation page
  1188. config["sso"] = {"client_whitelist": ["https://x"]}
  1189. return config
  1190. def create_resource_dict(self) -> Dict[str, Resource]:
  1191. d = super().create_resource_dict()
  1192. d.update(build_synapse_client_resource_tree(self.hs))
  1193. return d
  1194. def test_username_picker(self) -> None:
  1195. """Test the happy path of a username picker flow."""
  1196. fake_oidc_server = self.helper.fake_oidc_server()
  1197. # do the start of the login flow
  1198. channel, _ = self.helper.auth_via_oidc(
  1199. fake_oidc_server,
  1200. {"sub": "tester", "displayname": "Jonny"},
  1201. TEST_CLIENT_REDIRECT_URL,
  1202. )
  1203. # that should redirect to the username picker
  1204. self.assertEqual(channel.code, 302, channel.result)
  1205. location_headers = channel.headers.getRawHeaders("Location")
  1206. assert location_headers
  1207. picker_url = location_headers[0]
  1208. self.assertEqual(picker_url, "/_synapse/client/pick_username/account_details")
  1209. # ... with a username_mapping_session cookie
  1210. cookies: Dict[str, str] = {}
  1211. channel.extract_cookies(cookies)
  1212. self.assertIn("username_mapping_session", cookies)
  1213. session_id = cookies["username_mapping_session"]
  1214. # introspect the sso handler a bit to check that the username mapping session
  1215. # looks ok.
  1216. username_mapping_sessions = self.hs.get_sso_handler()._username_mapping_sessions
  1217. self.assertIn(
  1218. session_id,
  1219. username_mapping_sessions,
  1220. "session id not found in map",
  1221. )
  1222. session = username_mapping_sessions[session_id]
  1223. self.assertEqual(session.remote_user_id, "tester")
  1224. self.assertEqual(session.display_name, "Jonny")
  1225. self.assertEqual(session.client_redirect_url, TEST_CLIENT_REDIRECT_URL)
  1226. # the expiry time should be about 15 minutes away
  1227. expected_expiry = self.clock.time_msec() + (15 * 60 * 1000)
  1228. self.assertApproximates(session.expiry_time_ms, expected_expiry, tolerance=1000)
  1229. # Now, submit a username to the username picker, which should serve a redirect
  1230. # to the completion page
  1231. content = urlencode({b"username": b"bobby"}).encode("utf8")
  1232. chan = self.make_request(
  1233. "POST",
  1234. path=picker_url,
  1235. content=content,
  1236. content_is_form=True,
  1237. custom_headers=[
  1238. ("Cookie", "username_mapping_session=" + session_id),
  1239. # old versions of twisted don't do form-parsing without a valid
  1240. # content-length header.
  1241. ("Content-Length", str(len(content))),
  1242. ],
  1243. )
  1244. self.assertEqual(chan.code, 302, chan.result)
  1245. location_headers = chan.headers.getRawHeaders("Location")
  1246. assert location_headers
  1247. # send a request to the completion page, which should 302 to the client redirectUrl
  1248. chan = self.make_request(
  1249. "GET",
  1250. path=location_headers[0],
  1251. custom_headers=[("Cookie", "username_mapping_session=" + session_id)],
  1252. )
  1253. self.assertEqual(chan.code, 302, chan.result)
  1254. location_headers = chan.headers.getRawHeaders("Location")
  1255. assert location_headers
  1256. # ensure that the returned location matches the requested redirect URL
  1257. path, query = location_headers[0].split("?", 1)
  1258. self.assertEqual(path, "https://x")
  1259. # it will have url-encoded the params properly, so we'll have to parse them
  1260. params = urllib.parse.parse_qsl(
  1261. query, keep_blank_values=True, strict_parsing=True, errors="strict"
  1262. )
  1263. self.assertEqual(params[0:2], EXPECTED_CLIENT_REDIRECT_URL_PARAMS)
  1264. self.assertEqual(params[2][0], "loginToken")
  1265. # fish the login token out of the returned redirect uri
  1266. login_token = params[2][1]
  1267. # finally, submit the matrix login token to the login API, which gives us our
  1268. # matrix access token, mxid, and device id.
  1269. chan = self.make_request(
  1270. "POST",
  1271. "/login",
  1272. content={"type": "m.login.token", "token": login_token},
  1273. )
  1274. self.assertEqual(chan.code, 200, chan.result)
  1275. self.assertEqual(chan.json_body["user_id"], "@bobby:test")