login.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. # Copyright 2014-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 logging
  15. import re
  16. from typing import (
  17. TYPE_CHECKING,
  18. Any,
  19. Awaitable,
  20. Callable,
  21. Dict,
  22. List,
  23. Optional,
  24. Tuple,
  25. Union,
  26. )
  27. from typing_extensions import TypedDict
  28. from synapse.api.errors import Codes, LoginError, SynapseError
  29. from synapse.api.ratelimiting import Ratelimiter
  30. from synapse.api.urls import CLIENT_API_PREFIX
  31. from synapse.appservice import ApplicationService
  32. from synapse.handlers.sso import SsoIdentityProvider
  33. from synapse.http import get_request_uri
  34. from synapse.http.server import HttpServer, finish_request
  35. from synapse.http.servlet import (
  36. RestServlet,
  37. assert_params_in_dict,
  38. parse_bytes_from_args,
  39. parse_json_object_from_request,
  40. parse_string,
  41. )
  42. from synapse.http.site import SynapseRequest
  43. from synapse.rest.client._base import client_patterns
  44. from synapse.rest.well_known import WellKnownBuilder
  45. from synapse.types import JsonDict, UserID
  46. if TYPE_CHECKING:
  47. from synapse.server import HomeServer
  48. logger = logging.getLogger(__name__)
  49. class LoginResponse(TypedDict, total=False):
  50. user_id: str
  51. access_token: str
  52. home_server: str
  53. expires_in_ms: Optional[int]
  54. refresh_token: Optional[str]
  55. device_id: str
  56. well_known: Optional[Dict[str, Any]]
  57. class LoginRestServlet(RestServlet):
  58. PATTERNS = client_patterns("/login$", v1=True)
  59. CAS_TYPE = "m.login.cas"
  60. SSO_TYPE = "m.login.sso"
  61. TOKEN_TYPE = "m.login.token"
  62. JWT_TYPE = "org.matrix.login.jwt"
  63. APPSERVICE_TYPE = "m.login.application_service"
  64. REFRESH_TOKEN_PARAM = "refresh_token"
  65. def __init__(self, hs: "HomeServer"):
  66. super().__init__()
  67. self.hs = hs
  68. # JWT configuration variables.
  69. self.jwt_enabled = hs.config.jwt.jwt_enabled
  70. self.jwt_secret = hs.config.jwt.jwt_secret
  71. self.jwt_subject_claim = hs.config.jwt.jwt_subject_claim
  72. self.jwt_algorithm = hs.config.jwt.jwt_algorithm
  73. self.jwt_issuer = hs.config.jwt.jwt_issuer
  74. self.jwt_audiences = hs.config.jwt.jwt_audiences
  75. # SSO configuration.
  76. self.saml2_enabled = hs.config.saml2.saml2_enabled
  77. self.cas_enabled = hs.config.cas.cas_enabled
  78. self.oidc_enabled = hs.config.oidc.oidc_enabled
  79. self._refresh_tokens_enabled = (
  80. hs.config.registration.refreshable_access_token_lifetime is not None
  81. )
  82. self.auth = hs.get_auth()
  83. self.clock = hs.get_clock()
  84. self.auth_handler = self.hs.get_auth_handler()
  85. self.registration_handler = hs.get_registration_handler()
  86. self._sso_handler = hs.get_sso_handler()
  87. self._well_known_builder = WellKnownBuilder(hs)
  88. self._address_ratelimiter = Ratelimiter(
  89. store=hs.get_datastores().main,
  90. clock=hs.get_clock(),
  91. rate_hz=self.hs.config.ratelimiting.rc_login_address.per_second,
  92. burst_count=self.hs.config.ratelimiting.rc_login_address.burst_count,
  93. )
  94. self._account_ratelimiter = Ratelimiter(
  95. store=hs.get_datastores().main,
  96. clock=hs.get_clock(),
  97. rate_hz=self.hs.config.ratelimiting.rc_login_account.per_second,
  98. burst_count=self.hs.config.ratelimiting.rc_login_account.burst_count,
  99. )
  100. # ensure the CAS/SAML/OIDC handlers are loaded on this worker instance.
  101. # The reason for this is to ensure that the auth_provider_ids are registered
  102. # with SsoHandler, which in turn ensures that the login/registration prometheus
  103. # counters are initialised for the auth_provider_ids.
  104. _load_sso_handlers(hs)
  105. def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
  106. flows: List[JsonDict] = []
  107. if self.jwt_enabled:
  108. flows.append({"type": LoginRestServlet.JWT_TYPE})
  109. if self.cas_enabled:
  110. # we advertise CAS for backwards compat, though MSC1721 renamed it
  111. # to SSO.
  112. flows.append({"type": LoginRestServlet.CAS_TYPE})
  113. if self.cas_enabled or self.saml2_enabled or self.oidc_enabled:
  114. flows.append(
  115. {
  116. "type": LoginRestServlet.SSO_TYPE,
  117. "identity_providers": [
  118. _get_auth_flow_dict_for_idp(idp)
  119. for idp in self._sso_handler.get_identity_providers().values()
  120. ],
  121. }
  122. )
  123. # While it's valid for us to advertise this login type generally,
  124. # synapse currently only gives out these tokens as part of the
  125. # SSO login flow.
  126. # Generally we don't want to advertise login flows that clients
  127. # don't know how to implement, since they (currently) will always
  128. # fall back to the fallback API if they don't understand one of the
  129. # login flow types returned.
  130. flows.append({"type": LoginRestServlet.TOKEN_TYPE})
  131. flows.extend({"type": t} for t in self.auth_handler.get_supported_login_types())
  132. flows.append({"type": LoginRestServlet.APPSERVICE_TYPE})
  133. return 200, {"flows": flows}
  134. async def on_POST(self, request: SynapseRequest) -> Tuple[int, LoginResponse]:
  135. login_submission = parse_json_object_from_request(request)
  136. # Check to see if the client requested a refresh token.
  137. client_requested_refresh_token = login_submission.get(
  138. LoginRestServlet.REFRESH_TOKEN_PARAM, False
  139. )
  140. if not isinstance(client_requested_refresh_token, bool):
  141. raise SynapseError(400, "`refresh_token` should be true or false.")
  142. should_issue_refresh_token = (
  143. self._refresh_tokens_enabled and client_requested_refresh_token
  144. )
  145. try:
  146. if login_submission["type"] == LoginRestServlet.APPSERVICE_TYPE:
  147. appservice = self.auth.get_appservice_by_req(request)
  148. if appservice.is_rate_limited():
  149. await self._address_ratelimiter.ratelimit(
  150. None, request.getClientAddress().host
  151. )
  152. result = await self._do_appservice_login(
  153. login_submission,
  154. appservice,
  155. should_issue_refresh_token=should_issue_refresh_token,
  156. )
  157. elif (
  158. self.jwt_enabled
  159. and login_submission["type"] == LoginRestServlet.JWT_TYPE
  160. ):
  161. await self._address_ratelimiter.ratelimit(
  162. None, request.getClientAddress().host
  163. )
  164. result = await self._do_jwt_login(
  165. login_submission,
  166. should_issue_refresh_token=should_issue_refresh_token,
  167. )
  168. elif login_submission["type"] == LoginRestServlet.TOKEN_TYPE:
  169. await self._address_ratelimiter.ratelimit(
  170. None, request.getClientAddress().host
  171. )
  172. result = await self._do_token_login(
  173. login_submission,
  174. should_issue_refresh_token=should_issue_refresh_token,
  175. )
  176. else:
  177. await self._address_ratelimiter.ratelimit(
  178. None, request.getClientAddress().host
  179. )
  180. result = await self._do_other_login(
  181. login_submission,
  182. should_issue_refresh_token=should_issue_refresh_token,
  183. )
  184. except KeyError:
  185. raise SynapseError(400, "Missing JSON keys.")
  186. well_known_data = self._well_known_builder.get_well_known()
  187. if well_known_data:
  188. result["well_known"] = well_known_data
  189. return 200, result
  190. async def _do_appservice_login(
  191. self,
  192. login_submission: JsonDict,
  193. appservice: ApplicationService,
  194. should_issue_refresh_token: bool = False,
  195. ) -> LoginResponse:
  196. identifier = login_submission.get("identifier")
  197. logger.info("Got appservice login request with identifier: %r", identifier)
  198. if not isinstance(identifier, dict):
  199. raise SynapseError(
  200. 400, "Invalid identifier in login submission", Codes.INVALID_PARAM
  201. )
  202. # this login flow only supports identifiers of type "m.id.user".
  203. if identifier.get("type") != "m.id.user":
  204. raise SynapseError(
  205. 400, "Unknown login identifier type", Codes.INVALID_PARAM
  206. )
  207. user = identifier.get("user")
  208. if not isinstance(user, str):
  209. raise SynapseError(400, "Invalid user in identifier", Codes.INVALID_PARAM)
  210. if user.startswith("@"):
  211. qualified_user_id = user
  212. else:
  213. qualified_user_id = UserID(user, self.hs.hostname).to_string()
  214. if not appservice.is_interested_in_user(qualified_user_id):
  215. raise LoginError(403, "Invalid access_token", errcode=Codes.FORBIDDEN)
  216. return await self._complete_login(
  217. qualified_user_id,
  218. login_submission,
  219. ratelimit=appservice.is_rate_limited(),
  220. should_issue_refresh_token=should_issue_refresh_token,
  221. )
  222. async def _do_other_login(
  223. self, login_submission: JsonDict, should_issue_refresh_token: bool = False
  224. ) -> LoginResponse:
  225. """Handle non-token/saml/jwt logins
  226. Args:
  227. login_submission:
  228. should_issue_refresh_token: True if this login should issue
  229. a refresh token alongside the access token.
  230. Returns:
  231. HTTP response
  232. """
  233. # Log the request we got, but only certain fields to minimise the chance of
  234. # logging someone's password (even if they accidentally put it in the wrong
  235. # field)
  236. logger.info(
  237. "Got login request with identifier: %r, medium: %r, address: %r, user: %r",
  238. login_submission.get("identifier"),
  239. login_submission.get("medium"),
  240. login_submission.get("address"),
  241. login_submission.get("user"),
  242. )
  243. canonical_user_id, callback = await self.auth_handler.validate_login(
  244. login_submission, ratelimit=True
  245. )
  246. result = await self._complete_login(
  247. canonical_user_id,
  248. login_submission,
  249. callback,
  250. should_issue_refresh_token=should_issue_refresh_token,
  251. )
  252. return result
  253. async def _complete_login(
  254. self,
  255. user_id: str,
  256. login_submission: JsonDict,
  257. callback: Optional[Callable[[LoginResponse], Awaitable[None]]] = None,
  258. create_non_existent_users: bool = False,
  259. ratelimit: bool = True,
  260. auth_provider_id: Optional[str] = None,
  261. should_issue_refresh_token: bool = False,
  262. auth_provider_session_id: Optional[str] = None,
  263. ) -> LoginResponse:
  264. """Called when we've successfully authed the user and now need to
  265. actually login them in (e.g. create devices). This gets called on
  266. all successful logins.
  267. Applies the ratelimiting for successful login attempts against an
  268. account.
  269. Args:
  270. user_id: ID of the user to register.
  271. login_submission: Dictionary of login information.
  272. callback: Callback function to run after login.
  273. create_non_existent_users: Whether to create the user if they don't
  274. exist. Defaults to False.
  275. ratelimit: Whether to ratelimit the login request.
  276. auth_provider_id: The SSO IdP the user used, if any.
  277. should_issue_refresh_token: True if this login should issue
  278. a refresh token alongside the access token.
  279. auth_provider_session_id: The session ID got during login from the SSO IdP.
  280. Returns:
  281. result: Dictionary of account information after successful login.
  282. """
  283. # Before we actually log them in we check if they've already logged in
  284. # too often. This happens here rather than before as we don't
  285. # necessarily know the user before now.
  286. if ratelimit:
  287. await self._account_ratelimiter.ratelimit(None, user_id.lower())
  288. if create_non_existent_users:
  289. canonical_uid = await self.auth_handler.check_user_exists(user_id)
  290. if not canonical_uid:
  291. canonical_uid = await self.registration_handler.register_user(
  292. localpart=UserID.from_string(user_id).localpart
  293. )
  294. user_id = canonical_uid
  295. device_id = login_submission.get("device_id")
  296. # If device_id is present, check that device_id is not longer than a reasonable 512 characters
  297. if device_id and len(device_id) > 512:
  298. raise LoginError(
  299. 400,
  300. "device_id cannot be longer than 512 characters.",
  301. errcode=Codes.INVALID_PARAM,
  302. )
  303. initial_display_name = login_submission.get("initial_device_display_name")
  304. (
  305. device_id,
  306. access_token,
  307. valid_until_ms,
  308. refresh_token,
  309. ) = await self.registration_handler.register_device(
  310. user_id,
  311. device_id,
  312. initial_display_name,
  313. auth_provider_id=auth_provider_id,
  314. should_issue_refresh_token=should_issue_refresh_token,
  315. auth_provider_session_id=auth_provider_session_id,
  316. )
  317. result = LoginResponse(
  318. user_id=user_id,
  319. access_token=access_token,
  320. home_server=self.hs.hostname,
  321. device_id=device_id,
  322. )
  323. if valid_until_ms is not None:
  324. expires_in_ms = valid_until_ms - self.clock.time_msec()
  325. result["expires_in_ms"] = expires_in_ms
  326. if refresh_token is not None:
  327. result["refresh_token"] = refresh_token
  328. if callback is not None:
  329. await callback(result)
  330. return result
  331. async def _do_token_login(
  332. self, login_submission: JsonDict, should_issue_refresh_token: bool = False
  333. ) -> LoginResponse:
  334. """
  335. Handle the final stage of SSO login.
  336. Args:
  337. login_submission: The JSON request body.
  338. should_issue_refresh_token: True if this login should issue
  339. a refresh token alongside the access token.
  340. Returns:
  341. The body of the JSON response.
  342. """
  343. token = login_submission["token"]
  344. auth_handler = self.auth_handler
  345. res = await auth_handler.validate_short_term_login_token(token)
  346. return await self._complete_login(
  347. res.user_id,
  348. login_submission,
  349. self.auth_handler._sso_login_callback,
  350. auth_provider_id=res.auth_provider_id,
  351. should_issue_refresh_token=should_issue_refresh_token,
  352. auth_provider_session_id=res.auth_provider_session_id,
  353. )
  354. async def _do_jwt_login(
  355. self, login_submission: JsonDict, should_issue_refresh_token: bool = False
  356. ) -> LoginResponse:
  357. token = login_submission.get("token", None)
  358. if token is None:
  359. raise LoginError(
  360. 403, "Token field for JWT is missing", errcode=Codes.FORBIDDEN
  361. )
  362. import jwt
  363. try:
  364. payload = jwt.decode(
  365. token,
  366. self.jwt_secret,
  367. algorithms=[self.jwt_algorithm],
  368. issuer=self.jwt_issuer,
  369. audience=self.jwt_audiences,
  370. )
  371. except jwt.PyJWTError as e:
  372. # A JWT error occurred, return some info back to the client.
  373. raise LoginError(
  374. 403,
  375. "JWT validation failed: %s" % (str(e),),
  376. errcode=Codes.FORBIDDEN,
  377. )
  378. user = payload.get(self.jwt_subject_claim, None)
  379. if user is None:
  380. raise LoginError(403, "Invalid JWT", errcode=Codes.FORBIDDEN)
  381. user_id = UserID(user, self.hs.hostname).to_string()
  382. result = await self._complete_login(
  383. user_id,
  384. login_submission,
  385. create_non_existent_users=True,
  386. should_issue_refresh_token=should_issue_refresh_token,
  387. )
  388. return result
  389. def _get_auth_flow_dict_for_idp(idp: SsoIdentityProvider) -> JsonDict:
  390. """Return an entry for the login flow dict
  391. Returns an entry suitable for inclusion in "identity_providers" in the
  392. response to GET /_matrix/client/r0/login
  393. Args:
  394. idp: the identity provider to describe
  395. """
  396. e: JsonDict = {"id": idp.idp_id, "name": idp.idp_name}
  397. if idp.idp_icon:
  398. e["icon"] = idp.idp_icon
  399. if idp.idp_brand:
  400. e["brand"] = idp.idp_brand
  401. return e
  402. class RefreshTokenServlet(RestServlet):
  403. PATTERNS = (re.compile("^/_matrix/client/v1/refresh$"),)
  404. def __init__(self, hs: "HomeServer"):
  405. self._auth_handler = hs.get_auth_handler()
  406. self._clock = hs.get_clock()
  407. self.refreshable_access_token_lifetime = (
  408. hs.config.registration.refreshable_access_token_lifetime
  409. )
  410. self.refresh_token_lifetime = hs.config.registration.refresh_token_lifetime
  411. async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
  412. refresh_submission = parse_json_object_from_request(request)
  413. assert_params_in_dict(refresh_submission, ["refresh_token"])
  414. token = refresh_submission["refresh_token"]
  415. if not isinstance(token, str):
  416. raise SynapseError(400, "Invalid param: refresh_token", Codes.INVALID_PARAM)
  417. now = self._clock.time_msec()
  418. access_valid_until_ms = None
  419. if self.refreshable_access_token_lifetime is not None:
  420. access_valid_until_ms = now + self.refreshable_access_token_lifetime
  421. refresh_valid_until_ms = None
  422. if self.refresh_token_lifetime is not None:
  423. refresh_valid_until_ms = now + self.refresh_token_lifetime
  424. (
  425. access_token,
  426. refresh_token,
  427. actual_access_token_expiry,
  428. ) = await self._auth_handler.refresh_token(
  429. token, access_valid_until_ms, refresh_valid_until_ms
  430. )
  431. response: Dict[str, Union[str, int]] = {
  432. "access_token": access_token,
  433. "refresh_token": refresh_token,
  434. }
  435. # expires_in_ms is only present if the token expires
  436. if actual_access_token_expiry is not None:
  437. response["expires_in_ms"] = actual_access_token_expiry - now
  438. return 200, response
  439. class SsoRedirectServlet(RestServlet):
  440. PATTERNS = list(client_patterns("/login/(cas|sso)/redirect$", v1=True)) + [
  441. re.compile(
  442. "^"
  443. + CLIENT_API_PREFIX
  444. + "/(r0|v3)/login/sso/redirect/(?P<idp_id>[A-Za-z0-9_.~-]+)$"
  445. )
  446. ]
  447. def __init__(self, hs: "HomeServer"):
  448. # make sure that the relevant handlers are instantiated, so that they
  449. # register themselves with the main SSOHandler.
  450. _load_sso_handlers(hs)
  451. self._sso_handler = hs.get_sso_handler()
  452. self._public_baseurl = hs.config.server.public_baseurl
  453. async def on_GET(
  454. self, request: SynapseRequest, idp_id: Optional[str] = None
  455. ) -> None:
  456. if not self._public_baseurl:
  457. raise SynapseError(400, "SSO requires a valid public_baseurl")
  458. # if this isn't the expected hostname, redirect to the right one, so that we
  459. # get our cookies back.
  460. requested_uri = get_request_uri(request)
  461. baseurl_bytes = self._public_baseurl.encode("utf-8")
  462. if not requested_uri.startswith(baseurl_bytes):
  463. # swap out the incorrect base URL for the right one.
  464. #
  465. # The idea here is to redirect from
  466. # https://foo.bar/whatever/_matrix/...
  467. # to
  468. # https://public.baseurl/_matrix/...
  469. #
  470. i = requested_uri.index(b"/_matrix")
  471. new_uri = baseurl_bytes[:-1] + requested_uri[i:]
  472. logger.info(
  473. "Requested URI %s is not canonical: redirecting to %s",
  474. requested_uri.decode("utf-8", errors="replace"),
  475. new_uri.decode("utf-8", errors="replace"),
  476. )
  477. request.redirect(new_uri)
  478. finish_request(request)
  479. return
  480. args: Dict[bytes, List[bytes]] = request.args # type: ignore
  481. client_redirect_url = parse_bytes_from_args(args, "redirectUrl", required=True)
  482. sso_url = await self._sso_handler.handle_redirect_request(
  483. request,
  484. client_redirect_url,
  485. idp_id,
  486. )
  487. logger.info("Redirecting to %s", sso_url)
  488. request.redirect(sso_url)
  489. finish_request(request)
  490. class CasTicketServlet(RestServlet):
  491. PATTERNS = client_patterns("/login/cas/ticket", v1=True)
  492. def __init__(self, hs: "HomeServer"):
  493. super().__init__()
  494. self._cas_handler = hs.get_cas_handler()
  495. async def on_GET(self, request: SynapseRequest) -> None:
  496. client_redirect_url = parse_string(request, "redirectUrl")
  497. ticket = parse_string(request, "ticket", required=True)
  498. # Maybe get a session ID (if this ticket is from user interactive
  499. # authentication).
  500. session = parse_string(request, "session")
  501. # Either client_redirect_url or session must be provided.
  502. if not client_redirect_url and not session:
  503. message = "Missing string query parameter redirectUrl or session"
  504. raise SynapseError(400, message, errcode=Codes.MISSING_PARAM)
  505. await self._cas_handler.handle_ticket(
  506. request, ticket, client_redirect_url, session
  507. )
  508. def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
  509. LoginRestServlet(hs).register(http_server)
  510. if hs.config.registration.refreshable_access_token_lifetime is not None:
  511. RefreshTokenServlet(hs).register(http_server)
  512. SsoRedirectServlet(hs).register(http_server)
  513. if hs.config.cas.cas_enabled:
  514. CasTicketServlet(hs).register(http_server)
  515. def _load_sso_handlers(hs: "HomeServer") -> None:
  516. """Ensure that the SSO handlers are loaded, if they are enabled by configuration.
  517. This is mostly useful to ensure that the CAS/SAML/OIDC handlers register themselves
  518. with the main SsoHandler.
  519. It's safe to call this multiple times.
  520. """
  521. if hs.config.cas.cas_enabled:
  522. hs.get_cas_handler()
  523. if hs.config.saml2.saml2_enabled:
  524. hs.get_saml_handler()
  525. if hs.config.oidc.oidc_enabled:
  526. hs.get_oidc_handler()