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

Expose OIDC discovery information under the CSAPI #16726

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16726.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update the implementation of [MSC2965](https://github.com/matrix-org/matrix-spec-proposals/pull/2965) (OIDC Provider discovery).
11 changes: 11 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,14 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
self.msc4069_profile_inhibit_propagation = experimental.get(
"msc4069_profile_inhibit_propagation", False
)

def get_msc2965_discovery_data(self) -> Optional[JsonDict]:
# We use the MSC3861 values as they are used by multiple MSCs
if not self.msc3861.enabled:
return None

result = {"issuer": self.msc3861.issuer}
if self.msc3861.account_management_url is not None:
result["account"] = self.msc3861.account_management_url

return result
2 changes: 2 additions & 0 deletions synapse/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
account_validity,
appservice_ping,
auth,
auth_issuer,
capabilities,
devices,
directory,
Expand Down Expand Up @@ -148,3 +149,4 @@ def register_servlets(client_resource: HttpServer, hs: "HomeServer") -> None:
mutual_rooms.register_servlets(hs, client_resource)
login_token_request.register_servlets(hs, client_resource)
rendezvous.register_servlets(hs, client_resource)
auth_issuer.register_servlets(hs, client_resource)
65 changes: 65 additions & 0 deletions synapse/rest/client/auth_issuer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2023 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import typing
from typing import Tuple

from synapse.api.errors import Codes, SynapseError
from synapse.http.server import HttpServer
from synapse.http.servlet import RestServlet
from synapse.http.site import SynapseRequest
from synapse.rest.client._base import client_patterns
from synapse.types import JsonDict

if typing.TYPE_CHECKING:
from synapse.server import HomeServer


logger = logging.getLogger(__name__)


class AuthIssuerServlet(RestServlet):
"""
Handles Client / Server API authentication in any situations where it
cannot be handled in the normal flow (with requests to the same endpoint).
Current use is for web fallback auth.
"""
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

PATTERNS = client_patterns(
"/org.matrix.msc2965/auth_issuer$",
unstable=True,
releases=(),
)

def __init__(self, hs: "HomeServer"):
super().__init__()
self._config = hs.config

async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
discovery_data = self._config.experimental.get_msc2965_discovery_data()
if discovery_data is None:
# Wouldn't expect this to be reached: the servelet shouldn't have been
# registered. Still, fail gracefully if we are registered for some reason.
raise SynapseError(
404,
"OIDC discovery has not been configured on this homeserver",
Codes.NOT_FOUND,
)
return 200, discovery_data


def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
# We use the MSC3861 values as they are used by multiple MSCs
if hs.config.experimental.msc3861.enabled:
AuthIssuerServlet(hs).register(http_server)
12 changes: 3 additions & 9 deletions synapse/rest/well_known.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@ def get_well_known(self) -> Optional[JsonDict]:
"base_url": self._config.registration.default_identity_server
}

# We use the MSC3861 values as they are used by multiple MSCs
if self._config.experimental.msc3861.enabled:
result["org.matrix.msc2965.authentication"] = {
"issuer": self._config.experimental.msc3861.issuer
}
if self._config.experimental.msc3861.account_management_url is not None:
result["org.matrix.msc2965.authentication"][
"account"
] = self._config.experimental.msc3861.account_management_url
discovery_data = self._config.experimental.get_msc2965_discovery_data()
if discovery_data is not None:
result["org.matrix.msc2965.authentication"] = discovery_data

if self._config.server.extra_well_known_client_content:
for (
Expand Down
61 changes: 61 additions & 0 deletions tests/rest/client/test_auth_issuer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2023 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from http import HTTPStatus

from synapse.rest.client import auth_issuer

from tests.unittest import HomeserverTestCase, override_config

ISSUER = "https://account.example.com/"
ACCOUNT_MANAGEMENT_URL = "https://account.example.com/myaccount/"


class AccountDataTestCase(HomeserverTestCase):
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
servlets = [
auth_issuer.register_servlets,
]

def test_returns_404_when_msc3861_disabled(self) -> None:
# Make an unauthenticated request for the discovery info.
channel = self.make_request(
"GET",
"/_matrix/client/unstable/org.matrix.msc2965/auth_issuer",
)
self.assertEqual(channel.code, HTTPStatus.NOT_FOUND)

@override_config(
{
"disable_registration": True,
"experimental_features": {
"msc3861": {
"enabled": True,
"issuer": ISSUER,
"account_management_url": ACCOUNT_MANAGEMENT_URL,
"client_id": "David Lister",
"client_auth_method": "client_secret_post",
"client_secret": "Who shot Mister Burns?",
}
},
}
)
def test_returns_discovery_data_when_oidc_enabled(self) -> None:
# Make an unauthenticated request for the discovery info.
channel = self.make_request(
"GET",
"/_matrix/client/unstable/org.matrix.msc2965/auth_issuer",
)
self.assertEqual(channel.code, HTTPStatus.OK)
self.assertEqual(
channel.json_body, {"issuer": ISSUER, "account": ACCOUNT_MANAGEMENT_URL}
sandhose marked this conversation as resolved.
Show resolved Hide resolved
)