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

Add custom well-known #13035

Merged
merged 12 commits into from
Jun 16, 2022
1 change: 1 addition & 0 deletions changelog.d/13035.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow server admins to customise the response of the `/.well-known/matrix/client` endpoint.
16 changes: 16 additions & 0 deletions docs/usage/configuration/config_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ Example configuration:
serve_server_wellknown: true
```
---
Config option: `extra_well_known_content`

This option allows server runners to add arbitrary key-value pairs to the [client-facing `.well-known` response](https://spec.matrix.org/latest/client-server-api/#well-known-uri).

If this option is provided, it parses the given yaml to json and
serves it on `/.well-known/matrix/client` endpoint
alongside the standard properties.

Example configuration:
```yaml
extra_well_known_content:
option1: value1
option2: value2
```
---
Vetchu marked this conversation as resolved.
Show resolved Hide resolved
Config option: `soft_file_limit`

Set the soft limit on the number of file descriptors synapse can use.
Expand Down Expand Up @@ -3578,3 +3593,4 @@ background_updates:
min_batch_size: 10
default_batch_size: 50
```

18 changes: 18 additions & 0 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,24 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
"public_baseurl cannot contain query parameters or a #-fragment"
)

self.extra_well_known_content = config.get("extra_well_known_content")
Vetchu marked this conversation as resolved.
Show resolved Hide resolved

if type(self.extra_well_known_content) is not dict:
Vetchu marked this conversation as resolved.
Show resolved Hide resolved
raise ConfigError(
"extra_well_known_content must be a dictionary of key-value pairs"
)

if "m.homeserver" in self.extra_well_known_content.keys():
raise ConfigError(
"m.homeserver is not supported in extra_well_known_content, "
"use public_baseurl in base config instead."
)
if "m.identity_server" in self.extra_well_known_content.keys():
raise ConfigError(
"m.identity_server is not supported in extra_well_known_content, "
"use default_identity_server in base config instead."
)
Vetchu marked this conversation as resolved.
Show resolved Hide resolved

# Whether to enable user presence.
presence_config = config.get("presence") or {}
self.use_presence = presence_config.get("enabled")
Expand Down
6 changes: 5 additions & 1 deletion synapse/rest/well_known.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# 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
from typing import TYPE_CHECKING, Optional

Expand Down Expand Up @@ -44,6 +43,11 @@ def get_well_known(self) -> Optional[JsonDict]:
"base_url": self._config.registration.default_identity_server
}

if self._config.server.extra_well_known_content:
for key, value in self._config.server.extra_well_known_content.items():
if key not in result:
result[key] = value

return result


Expand Down
22 changes: 22 additions & 0 deletions tests/rest/test_well_known.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ def test_client_well_known_no_public_baseurl(self) -> None:

self.assertEqual(channel.code, HTTPStatus.NOT_FOUND)

@unittest.override_config(
{
"public_baseurl": "https://tesths",
"default_identity_server": "https://testis",
"extra_well_known_content": {"custom": False},
}
)
def test_client_well_known_custom(self) -> None:
channel = self.make_request(
"GET", "/.well-known/matrix/client", shorthand=False
)

self.assertEqual(channel.code, HTTPStatus.OK)
self.assertEqual(
channel.json_body,
{
"m.homeserver": {"base_url": "https://tesths/"},
"m.identity_server": {"base_url": "https://testis"},
"custom": False,
},
)

@unittest.override_config({"serve_server_wellknown": True})
def test_server_well_known(self) -> None:
channel = self.make_request(
Expand Down