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

Commit 907c6b5

Browse files
committed
Split out a separate endpoint to complete SSO registration
There are going to be a couple of paths to get to the final step of SSO reg, and I want the URL in the browser to conistent. So, let's move the final step onto a separate path, which we redirect to.
1 parent 4d01a33 commit 907c6b5

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

synapse/app/homeserver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from synapse.rest.key.v2 import KeyApiV2Resource
6363
from synapse.rest.synapse.client.pick_idp import PickIdpResource
6464
from synapse.rest.synapse.client.pick_username import pick_username_resource
65+
from synapse.rest.synapse.client.sso_register import SsoRegisterResource
6566
from synapse.rest.well_known import WellKnownResource
6667
from synapse.server import HomeServer
6768
from synapse.storage import DataStore
@@ -192,6 +193,7 @@ def _configure_named_resource(self, name, compress=False):
192193
"/_synapse/admin": AdminRestResource(self),
193194
"/_synapse/client/pick_username": pick_username_resource(self),
194195
"/_synapse/client/pick_idp": PickIdpResource(self),
196+
"/_synapse/client/sso_register": SsoRegisterResource(self),
195197
}
196198
)
197199

synapse/handlers/sso.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from synapse.api.errors import Codes, NotFoundError, RedirectException, SynapseError
2828
from synapse.handlers.ui_auth import UIAuthSessionDataConstants
2929
from synapse.http import get_request_user_agent
30-
from synapse.http.server import respond_with_html
30+
from synapse.http.server import respond_with_html, respond_with_redirect
3131
from synapse.http.site import SynapseRequest
3232
from synapse.types import JsonDict, UserID, contains_invalid_mxid_characters
3333
from synapse.util.async_helpers import Linearizer
@@ -719,6 +719,20 @@ async def handle_submit_username_request(
719719
# update the session with the user's choices
720720
session.chosen_localpart = localpart
721721

722+
# we're done; now we can register the user
723+
respond_with_redirect(request, b"/_synapse/client/sso_register")
724+
725+
async def register_sso_user(self, request: Request, session_id: str) -> None:
726+
"""Called once we have all the info we need to register a new user.
727+
728+
Does so and serves an HTTP response
729+
730+
Args:
731+
request: HTTP request
732+
session_id: ID of the username mapping session, extracted from a cookie
733+
"""
734+
session = self.get_mapping_session(session_id)
735+
722736
logger.info(
723737
"[session %s] Registering localpart %s",
724738
session_id,

synapse/http/server.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,13 @@ def set_clickjacking_protection_headers(request: Request):
761761
request.setHeader(b"Content-Security-Policy", b"frame-ancestors 'none';")
762762

763763

764+
def respond_with_redirect(request: Request, url: bytes) -> None:
765+
"""Write a 302 response to the request, if it is still alive."""
766+
logger.debug("Redirect to %s", url.decode("utf-8"))
767+
request.redirect(url)
768+
finish_request(request)
769+
770+
764771
def finish_request(request: Request):
765772
""" Finish writing the response to the request.
766773
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2021 The Matrix.org Foundation C.I.C.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import logging
17+
from typing import TYPE_CHECKING
18+
19+
from twisted.web.http import Request
20+
21+
from synapse.api.errors import SynapseError
22+
from synapse.handlers.sso import get_username_mapping_session_cookie_from_request
23+
from synapse.http.server import DirectServeHtmlResource
24+
25+
if TYPE_CHECKING:
26+
from synapse.server import HomeServer
27+
28+
logger = logging.getLogger(__name__)
29+
30+
31+
class SsoRegisterResource(DirectServeHtmlResource):
32+
"""A resource which completes SSO registration
33+
34+
This resource gets mounted at /_synapse/client/sso_register, and is shown
35+
after we collect username and/or consent for a new SSO user. It (finally) registers
36+
the user, and confirms redirect to the client
37+
"""
38+
39+
def __init__(self, hs: "HomeServer"):
40+
super().__init__()
41+
self._sso_handler = hs.get_sso_handler()
42+
43+
async def _async_render_GET(self, request: Request) -> None:
44+
try:
45+
session_id = get_username_mapping_session_cookie_from_request(request)
46+
except SynapseError as e:
47+
logger.warning("Error fetching session cookie: %s", e)
48+
self._sso_handler.render_error(request, "bad_session", e.msg, code=e.code)
49+
return
50+
await self._sso_handler.register_sso_user(request, session_id)

0 commit comments

Comments
 (0)