Skip to content

Commit dd714ba

Browse files
authored
Use the stable MAS integration of Synapse in the documentation and in mas-cli doctor (#4902)
2 parents af1c083 + 0a5f05f commit dd714ba

File tree

2 files changed

+66
-103
lines changed

2 files changed

+66
-103
lines changed

crates/cli/src/commands/doctor.rs

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::process::ExitCode;
1414
use anyhow::Context;
1515
use clap::Parser;
1616
use figment::Figment;
17+
use hyper::StatusCode;
1718
use mas_config::{ConfigurationSection, RootConfig};
1819
use mas_http::RequestBuilderExt;
1920
use tracing::{error, info, info_span, warn};
@@ -43,7 +44,7 @@ impl Options {
4344
r"The homeserver host in the config (`matrix.homeserver`) is not a valid domain.
4445
See {DOCS_BASE}/setup/homeserver.html",
4546
)?;
46-
let admin_token = config.matrix.secret().await?;
47+
let secret = config.matrix.secret().await?;
4748
let hs_api = config.matrix.endpoint;
4849

4950
if !issuer.starts_with("https://") {
@@ -99,17 +100,14 @@ Make sure that the MAS config contains:
99100
100101
http:
101102
public_base: {issuer:?}
102-
# Or, if the issuer is different from the public base:
103-
issuer: {issuer:?}
104103
105104
And in the Synapse config:
106105
107-
experimental_features:
108-
msc3861:
109-
enabled: true
110-
# This must exactly match:
111-
issuer: {issuer:?}
112-
# ...
106+
matrix_authentication_service:
107+
enabled: true
108+
# This must point to where MAS is reachable by Synapse
109+
endpoint: {issuer:?}
110+
# ...
113111
114112
See {DOCS_BASE}/setup/homeserver.html
115113
"#
@@ -128,11 +126,10 @@ Check the well-known document at "{well_known_uri}"
128126
Check the well-known document at "{well_known_uri}"
129127
Make sure Synapse has delegated auth enabled:
130128
131-
experimental_features:
132-
msc3861:
133-
enabled: true
134-
issuer: {issuer:?}
135-
# ...
129+
matrix_authentication_service:
130+
enabled: true
131+
endpoint: {issuer:?}
132+
# ...
136133
137134
If it is not Synapse handling the well-known document, update it to include the following:
138135
@@ -283,70 +280,50 @@ Error details: {e}
283280
),
284281
}
285282

286-
// Try to reach the admin API on an unauthorized endpoint
287-
let server_version = hs_api.join("/_synapse/admin/v1/server_version")?;
288-
let result = http_client.get(server_version.as_str()).send_traced().await;
289-
match result {
290-
Ok(response) => {
291-
let status = response.status();
292-
if status.is_success() {
293-
info!(r#"✅ The Synapse admin API is reachable at "{server_version}"."#);
294-
} else {
295-
error!(
296-
r#"❌ A Synapse admin API endpoint at "{server_version}" replied with {status}.
297-
Make sure MAS can reach the admin API, and that the homeserver is running.
298-
"#
299-
);
300-
}
301-
}
302-
Err(e) => error!(
303-
r#"❌ Can't reach the Synapse admin API at "{server_version}".
304-
Make sure MAS can reach the admin API, and that the homeserver is running.
305-
306-
Error details: {e}
307-
"#
308-
),
309-
}
310-
311-
// Try to reach an authenticated admin API endpoint
312-
let background_updates = hs_api.join("/_synapse/admin/v1/background_updates/status")?;
283+
// Try to reach an authenticated MAS API endpoint
284+
let mas_api = hs_api.join("/_synapse/mas/is_localpart_available")?;
313285
let result = http_client
314-
.get(background_updates.as_str())
315-
.bearer_auth(&admin_token)
286+
.get(mas_api.as_str())
287+
.bearer_auth(&secret)
316288
.send_traced()
317289
.await;
318290
match result {
319291
Ok(response) => {
320292
let status = response.status();
321-
if status.is_success() {
293+
// We intentionally omit the required 'localpart' parameter
294+
// in this request. If authentication is successful, Synapse
295+
// returns a 400 Bad Request because of the missing
296+
// parameter. If authentication fails, Synapse will return a
297+
// 403 Forbidden. If the MAS integration isn't enabled,
298+
// Synapse will return a 404 Not found.
299+
if status == StatusCode::BAD_REQUEST {
322300
info!(
323-
r#"✅ The Synapse admin API is reachable with authentication at "{background_updates}"."#
301+
r#"✅ The Synapse MAS API is reachable with authentication at "{mas_api}"."#
324302
);
325303
} else {
326304
error!(
327-
r#"❌ A Synapse admin API endpoint at "{background_updates}" replied with {status}.
305+
r#"❌ A Synapse MAS API endpoint at "{mas_api}" replied with {status}.
328306
Make sure the homeserver is running, and that the MAS config has the correct `matrix.secret`.
329-
It should match the `admin_token` set in the Synapse config.
307+
It should match the `secret` set in the Synapse config.
330308
331-
experimental_features:
332-
msc3861:
333-
enabled: true
334-
issuer: {issuer}
335-
# This must exactly match the secret in the MAS config:
336-
admin_token: {admin_token:?}
309+
matrix_authentication_service:
310+
enabled: true
311+
endpoint: {issuer:?}
312+
# This must exactly match the secret in the MAS config:
313+
secret: {secret:?}
337314
338315
And in the MAS config:
339316
340317
matrix:
341318
homeserver: "{matrix_domain}"
342319
endpoint: "{hs_api}"
343-
secret: {admin_token:?}
320+
secret: {secret:?}
344321
"#
345322
);
346323
}
347324
}
348325
Err(e) => error!(
349-
r#"❌ Can't reach the Synapse admin API at "{background_updates}".
326+
r#"❌ Can't reach the Synapse MAS API at "{mas_api}".
350327
Make sure the homeserver is running, and that the MAS config has the correct `matrix.secret`.
351328
352329
Error details: {e}

docs/setup/homeserver.md

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,46 @@
11
# Homeserver configuration
22

33
The `matrix-authentication-service` is designed to be run alongside a Matrix homeserver.
4-
It currently only supports [Synapse](https://github.com/element-hq/synapse) through the experimental OAuth delegation feature.
4+
It currently only supports [Synapse](https://github.com/element-hq/synapse) version 1.136.0 or later.
55
The authentication service needs to be able to call the Synapse admin API to provision users through a shared secret, and Synapse needs to be able to call the service to verify access tokens using the OAuth 2.0 token introspection endpoint.
66

7-
## Provision a client for the Homeserver to use
8-
9-
In the [`clients`](../reference/configuration.md#clients) section of the configuration file, add a new client with the following properties:
10-
11-
- `client_id`: a unique identifier for the client. It must be a valid [ULID](https://github.com/ulid/spec), and it happens that `0000000000000000000SYNAPSE` is a valid ULID.
12-
- `client_auth_method`: set to `client_secret_basic`. Other methods are possible, but this is the easiest to set up.
13-
- `client_secret`: a shared secret used for the homeserver to authenticate
14-
15-
```yaml
16-
clients:
17-
- client_id: 0000000000000000000SYNAPSE
18-
client_auth_method: client_secret_basic
19-
client_secret: "SomeRandomSecret"
20-
```
21-
22-
**Don't forget to sync the configuration file** with the database after adding the client, using the [`config sync`](../reference/cli/config.md#config-sync---prune---dry-run) command.
23-
247
## Configure the connection to the homeserver
258

269
In the [`matrix`](../reference/configuration.md#matrix) section of the configuration file, add the following properties:
2710

11+
- `kind`: the type of homeserver to connect to, currently only `synapse` is supported
2812
- `homeserver`: corresponds to the `server_name` in the Synapse configuration file
29-
- `secret`: a shared secret the service will use to call the homeserver admin API
13+
- `secret`: a shared secret the service will use to call the homeserver MAS API
3014
- `endpoint`: the URL to which the homeserver is accessible from the service
3115

3216
```yaml
3317
matrix:
34-
homeserver: localhost:8008
35-
secret: "AnotherRandomSecret"
18+
kind: synapse
19+
homeserver: example.com
3620
endpoint: "http://localhost:8008"
21+
secret: "AVeryRandomSecretPleaseUseSomethingSecure"
22+
# Alternatively, using a file:
23+
#secret_path: /path/to/secret.txt
3724
```
3825

3926
## Configure the homeserver to delegate authentication to the service
4027

41-
Set up the delegated authentication feature in the Synapse configuration in the `experimental_features` section:
28+
Set up the delegated authentication feature **in the Synapse configuration** in the `matrix_authentication_service` section:
4229

4330
```yaml
44-
experimental_features:
45-
msc3861:
46-
enabled: true
47-
48-
# Synapse will call `{issuer}/.well-known/openid-configuration` to get the OIDC configuration
49-
issuer: http://localhost:8080/
50-
51-
# Matches the `client_id` in the auth service config
52-
client_id: 0000000000000000000SYNAPSE
53-
# Matches the `client_auth_method` in the auth service config
54-
client_auth_method: client_secret_basic
55-
# Matches the `client_secret` in the auth service config
56-
client_secret: "SomeRandomSecret"
57-
58-
# Matches the `matrix.secret` in the auth service config
59-
admin_token: "AnotherRandomSecret"
60-
61-
# URL to advertise to clients where users can self-manage their account
62-
# Defaults to the URL advertised by MAS, e.g. `https://{public_mas_domain}/account/`
63-
#account_management_url: "http://localhost:8080/account/"
64-
65-
# URL which Synapse will use to introspect access tokens
66-
# Defaults to the URL advertised by MAS, e.g. `https://{public_mas_domain}/oauth2/introspect`
67-
# This is useful to override if Synapse has a way to call the auth service's
68-
# introspection endpoint directly, skipping intermediate reverse proxies
69-
#introspection_endpoint: "http://localhost:8080/oauth2/introspect"
31+
matrix_authentication_service:
32+
enabled: true
33+
endpoint: http://localhost:8080/
34+
secret: "AVeryRandomSecretPleaseUseSomethingSecure"
35+
# Alternatively, using a file:
36+
#secret_file: /path/to/secret.txt
7037
```
7138

39+
The `endpoint` property should be set to the URL of the authentication service.
40+
This can be an internal URL, to avoid unnecessary round-trips.
41+
42+
The `secret` property must match in both the Synapse configuration and the Matrix Authentication Service configuration.
43+
7244
## Set up the compatibility layer
7345

7446
The service exposes a compatibility layer to allow legacy clients to authenticate using the service.
@@ -81,3 +53,17 @@ The following Matrix Client-Server API endpoints need to be handled by the authe
8153
- [`/_matrix/client/*/refresh`](https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3refresh)
8254

8355
See the [reverse proxy configuration](./reverse-proxy.md) guide for more information.
56+
57+
58+
## Migrating from the experimental MSC3861 feature
59+
60+
If you are migrating from the experimental MSC3861 feature in Synapse, you will need to migrate the `experimental_features.msc3861` section of the Synapse configuration to the `matrix_authentication_service` section.
61+
62+
To do so, you need to:
63+
64+
- Remove the `experimental_features.msc3861` section from the Synapse configuration
65+
- Add the `matrix_authentication_service` section to the Synapse configuration with:
66+
- `enabled: true`
67+
- `endpoint` set to the URL of the authentication service
68+
- `secret` set to the same secret as the `admin_token` that was set in the `msc3861` section
69+
- Optionally, remove the client provisioned for Synapse in the `clients` section of the MAS configuration

0 commit comments

Comments
 (0)