Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit fa4e504

Browse files
committed
Split handle_redirect_request in two
1 parent ef41023 commit fa4e504

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

synapse/handlers/oidc_handler.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,10 @@ async def handle_oidc_callback(self, request: SynapseRequest) -> None:
566566
- then we fetch the session cookie, decode and verify it
567567
- the ``state`` query parameter should match with the one stored in the
568568
session cookie
569-
- once we known this session is legit, exchange the code with the
570-
provider using the ``token_endpoint`` (see ``_exchange_code``)
571-
- once we have the token, use it to either extract the UserInfo from
572-
the ``id_token`` (``_parse_id_token``), or use the ``access_token``
573-
to fetch UserInfo from the ``userinfo_endpoint``
574-
(``_fetch_userinfo``)
575-
- map those UserInfo to a Matrix user (``_map_userinfo_to_user``) and
576-
finish the login
569+
570+
Once we know the session is legit, we then then ddelegate to
571+
_handle_oidc_callback_for_provider, which will exchange the code with the
572+
provider and complete the login/authentication.
577573
578574
Args:
579575
request: the incoming request from the browser.
@@ -646,17 +642,42 @@ async def handle_oidc_callback(self, request: SynapseRequest) -> None:
646642
self._sso_handler.render_error(request, "mismatching_session", str(e))
647643
return
648644

649-
# Exchange the code with the provider
650645
if b"code" not in request.args:
651646
logger.info("Code parameter is missing")
652647
self._sso_handler.render_error(
653648
request, "invalid_request", "Code parameter is missing"
654649
)
655650
return
656651

657-
logger.debug("Exchanging code")
658652
code = request.args[b"code"][0].decode()
653+
654+
await self._handle_oidc_callback_for_provider(request, session_data, code)
655+
656+
async def _handle_oidc_callback_for_provider(
657+
self, request: SynapseRequest, session_data: "OidcSessionData", code: str
658+
) -> None:
659+
"""Handle an incoming request to /_synapse/oidc/callback
660+
661+
By this time we have already validated the session on the synapse side, and
662+
now need to do the provider-specific operations. This includes:
663+
664+
- exchange the code with the provider using the ``token_endpoint`` (see
665+
``_exchange_code``)
666+
- once we have the token, use it to either extract the UserInfo from
667+
the ``id_token`` (``_parse_id_token``), or use the ``access_token``
668+
to fetch UserInfo from the ``userinfo_endpoint``
669+
(``_fetch_userinfo``)
670+
- map those UserInfo to a Matrix user (``_map_userinfo_to_user``) and
671+
finish the login
672+
673+
Args:
674+
request: the incoming request from the browser.
675+
session_data: the session data, extracted from our cookie
676+
code: The authorization code we got from the callback.
677+
"""
678+
# Exchange the code with the provider
659679
try:
680+
logger.debug("Exchanging code")
660681
token = await self._exchange_code(code)
661682
except OidcError as e:
662683
logger.exception("Could not exchange code")

0 commit comments

Comments
 (0)