syn2mas/synapse_reader/config/
oidc.rs

1// Copyright 2025 New Vector Ltd.
2//
3// SPDX-License-Identifier: AGPL-3.0-only
4// Please see LICENSE in the repository root for full details.
5
6use std::{collections::BTreeMap, str::FromStr as _};
7
8use chrono::{DateTime, Utc};
9use mas_config::{
10    UpstreamOAuth2ClaimsImports, UpstreamOAuth2DiscoveryMode, UpstreamOAuth2ImportAction,
11    UpstreamOAuth2PkceMethod, UpstreamOAuth2ResponseMode, UpstreamOAuth2TokenAuthMethod,
12};
13use mas_iana::jose::JsonWebSignatureAlg;
14use oauth2_types::scope::{OPENID, Scope, ScopeToken};
15use rand::Rng;
16use serde::Deserialize;
17use tracing::warn;
18use ulid::Ulid;
19use url::Url;
20
21#[derive(Clone, Deserialize, Default)]
22enum UserMappingProviderModule {
23    #[default]
24    #[serde(rename = "synapse.handlers.oidc.JinjaOidcMappingProvider")]
25    Jinja,
26
27    #[serde(rename = "synapse.handlers.oidc_handler.JinjaOidcMappingProvider")]
28    JinjaLegacy,
29
30    #[serde(other)]
31    Other,
32}
33
34#[derive(Clone, Deserialize, Default)]
35struct UserMappingProviderConfig {
36    subject_template: Option<String>,
37    subject_claim: Option<String>,
38    localpart_template: Option<String>,
39    display_name_template: Option<String>,
40    email_template: Option<String>,
41
42    #[serde(default)]
43    confirm_localpart: bool,
44}
45
46impl UserMappingProviderConfig {
47    fn into_mas_config(self) -> UpstreamOAuth2ClaimsImports {
48        let mut config = UpstreamOAuth2ClaimsImports::default();
49
50        match (self.subject_claim, self.subject_template) {
51            (Some(_), Some(subject_template)) => {
52                warn!(
53                    "Both `subject_claim` and `subject_template` options are set, using `subject_template`."
54                );
55                config.subject.template = Some(subject_template);
56            }
57            (None, Some(subject_template)) => {
58                config.subject.template = Some(subject_template);
59            }
60            (Some(subject_claim), None) => {
61                config.subject.template = Some(format!("{{{{ user.{subject_claim} }}}}"));
62            }
63            (None, None) => {}
64        }
65
66        if let Some(localpart_template) = self.localpart_template {
67            config.localpart.template = Some(localpart_template);
68            config.localpart.action = if self.confirm_localpart {
69                UpstreamOAuth2ImportAction::Suggest
70            } else {
71                UpstreamOAuth2ImportAction::Require
72            };
73        }
74
75        if let Some(displayname_template) = self.display_name_template {
76            config.displayname.template = Some(displayname_template);
77            config.displayname.action = if self.confirm_localpart {
78                UpstreamOAuth2ImportAction::Suggest
79            } else {
80                UpstreamOAuth2ImportAction::Force
81            };
82        }
83
84        if let Some(email_template) = self.email_template {
85            config.email.template = Some(email_template);
86            config.email.action = if self.confirm_localpart {
87                UpstreamOAuth2ImportAction::Suggest
88            } else {
89                UpstreamOAuth2ImportAction::Force
90            };
91        }
92
93        config
94    }
95}
96
97#[derive(Clone, Deserialize, Default)]
98struct UserMappingProvider {
99    #[serde(default)]
100    module: UserMappingProviderModule,
101    #[serde(default)]
102    config: UserMappingProviderConfig,
103}
104
105#[derive(Clone, Deserialize, Default)]
106#[serde(rename_all = "lowercase")]
107enum PkceMethod {
108    #[default]
109    Auto,
110    Always,
111    Never,
112    #[serde(other)]
113    Other,
114}
115
116#[derive(Clone, Deserialize, Default)]
117#[serde(rename_all = "snake_case")]
118enum UserProfileMethod {
119    #[default]
120    Auto,
121    UserinfoEndpoint,
122    #[serde(other)]
123    Other,
124}
125
126#[derive(Clone, Deserialize)]
127#[expect(clippy::struct_excessive_bools)]
128pub struct OidcProvider {
129    pub issuer: Option<String>,
130
131    /// Required, except for the old `oidc_config` where this is implied to be
132    /// "oidc".
133    pub idp_id: Option<String>,
134
135    idp_name: Option<String>,
136    idp_brand: Option<String>,
137
138    #[serde(default = "default_true")]
139    discover: bool,
140
141    client_id: Option<String>,
142    client_secret: Option<String>,
143
144    // Unsupported, we want to shout about it
145    client_secret_path: Option<String>,
146
147    // Unsupported, we want to shout about it
148    client_secret_jwt_key: Option<serde_json::Value>,
149    client_auth_method: Option<UpstreamOAuth2TokenAuthMethod>,
150    #[serde(default)]
151    pkce_method: PkceMethod,
152    // Unsupported, we want to shout about it
153    id_token_signing_alg_values_supported: Option<Vec<String>>,
154    scopes: Option<Vec<String>>,
155    authorization_endpoint: Option<Url>,
156    token_endpoint: Option<Url>,
157    userinfo_endpoint: Option<Url>,
158    jwks_uri: Option<Url>,
159    #[serde(default)]
160    skip_verification: bool,
161
162    // Unsupported, we want to shout about it
163    #[serde(default)]
164    backchannel_logout_enabled: bool,
165
166    #[serde(default)]
167    user_profile_method: UserProfileMethod,
168
169    // Unsupported, we want to shout about it
170    attribute_requirements: Option<serde_json::Value>,
171
172    // Unsupported, we want to shout about it
173    #[serde(default = "default_true")]
174    enable_registration: bool,
175    #[serde(default)]
176    additional_authorization_parameters: BTreeMap<String, String>,
177    #[serde(default)]
178    user_mapping_provider: UserMappingProvider,
179}
180
181fn default_true() -> bool {
182    true
183}
184
185impl OidcProvider {
186    /// Returns true if the two 'required' fields are set. This is used to
187    /// ignore an empty dict on the `oidc_config` section.
188    #[must_use]
189    pub(crate) fn has_required_fields(&self) -> bool {
190        self.issuer.is_some() && self.client_id.is_some()
191    }
192
193    /// Map this Synapse OIDC provider config to a MAS upstream provider config.
194    #[expect(clippy::too_many_lines)]
195    pub(crate) fn into_mas_config(
196        self,
197        rng: &mut impl Rng,
198        now: DateTime<Utc>,
199    ) -> Option<mas_config::UpstreamOAuth2Provider> {
200        let client_id = self.client_id?;
201
202        if self.client_secret_path.is_some() {
203            warn!(
204                "The `client_secret_path` option is not supported, ignoring. You *will* need to include the secret in the `client_secret` field."
205            );
206        }
207
208        if self.client_secret_jwt_key.is_some() {
209            warn!("The `client_secret_jwt_key` option is not supported, ignoring.");
210        }
211
212        if self.attribute_requirements.is_some() {
213            warn!("The `attribute_requirements` option is not supported, ignoring.");
214        }
215
216        if self.id_token_signing_alg_values_supported.is_some() {
217            warn!("The `id_token_signing_alg_values_supported` option is not supported, ignoring.");
218        }
219
220        if self.backchannel_logout_enabled {
221            warn!("The `backchannel_logout_enabled` option is not supported, ignoring.");
222        }
223
224        if !self.enable_registration {
225            warn!(
226                "Setting the `enable_registration` option to `false` is not supported, ignoring."
227            );
228        }
229
230        let scope: Scope = match self.scopes {
231            None => [OPENID].into_iter().collect(), // Synapse defaults to the 'openid' scope
232            Some(scopes) => scopes
233                .into_iter()
234                .filter_map(|scope| match ScopeToken::from_str(&scope) {
235                    Ok(scope) => Some(scope),
236                    Err(err) => {
237                        warn!("OIDC provider scope '{scope}' is invalid: {err}");
238                        None
239                    }
240                })
241                .collect(),
242        };
243
244        let id = Ulid::from_datetime_with_source(now.into(), rng);
245
246        let token_endpoint_auth_method = self.client_auth_method.unwrap_or_else(|| {
247            // The token auth method defaults to 'none' if no client_secret is set and
248            // 'client_secret_basic' otherwise
249            if self.client_secret.is_some() {
250                UpstreamOAuth2TokenAuthMethod::ClientSecretBasic
251            } else {
252                UpstreamOAuth2TokenAuthMethod::None
253            }
254        });
255
256        let discovery_mode = match (self.discover, self.skip_verification) {
257            (true, false) => UpstreamOAuth2DiscoveryMode::Oidc,
258            (true, true) => UpstreamOAuth2DiscoveryMode::Insecure,
259            (false, _) => UpstreamOAuth2DiscoveryMode::Disabled,
260        };
261
262        let pkce_method = match self.pkce_method {
263            PkceMethod::Auto => UpstreamOAuth2PkceMethod::Auto,
264            PkceMethod::Always => UpstreamOAuth2PkceMethod::Always,
265            PkceMethod::Never => UpstreamOAuth2PkceMethod::Never,
266            PkceMethod::Other => {
267                warn!(
268                    "The `pkce_method` option is not supported, expected 'auto', 'always', or 'never'; assuming 'auto'."
269                );
270                UpstreamOAuth2PkceMethod::default()
271            }
272        };
273
274        // "auto" doesn't mean the same thing depending on whether we request the openid
275        // scope or not
276        let has_openid_scope = scope.contains(&OPENID);
277        let fetch_userinfo = match self.user_profile_method {
278            UserProfileMethod::Auto => has_openid_scope,
279            UserProfileMethod::UserinfoEndpoint => true,
280            UserProfileMethod::Other => {
281                warn!(
282                    "The `user_profile_method` option is not supported, expected 'auto' or 'userinfo_endpoint'; assuming 'auto'."
283                );
284                has_openid_scope
285            }
286        };
287
288        // Check if there is a `response_mode` set in the additional authorization
289        // parameters
290        let mut additional_authorization_parameters = self.additional_authorization_parameters;
291        let response_mode = if let Some(response_mode) =
292            additional_authorization_parameters.remove("response_mode")
293        {
294            match response_mode.to_ascii_lowercase().as_str() {
295                "query" => Some(UpstreamOAuth2ResponseMode::Query),
296                "form_post" => Some(UpstreamOAuth2ResponseMode::FormPost),
297                _ => {
298                    warn!(
299                        "Invalid `response_mode` in the `additional_authorization_parameters` option, expected 'query' or 'form_post'; ignoring."
300                    );
301                    None
302                }
303            }
304        } else {
305            None
306        };
307
308        let claims_imports = if matches!(
309            self.user_mapping_provider.module,
310            UserMappingProviderModule::Other
311        ) {
312            warn!(
313                "The `user_mapping_provider` module specified is not supported, ignoring. Please adjust the `claims_imports` to match the mapping provider behaviour."
314            );
315            UpstreamOAuth2ClaimsImports::default()
316        } else {
317            self.user_mapping_provider.config.into_mas_config()
318        };
319
320        Some(mas_config::UpstreamOAuth2Provider {
321            enabled: true,
322            id,
323            synapse_idp_id: self.idp_id,
324            issuer: self.issuer,
325            human_name: self.idp_name,
326            brand_name: self.idp_brand,
327            client_id,
328            client_secret: self.client_secret,
329            token_endpoint_auth_method,
330            sign_in_with_apple: None,
331            token_endpoint_auth_signing_alg: None,
332            id_token_signed_response_alg: JsonWebSignatureAlg::Rs256,
333            scope: scope.to_string(),
334            discovery_mode,
335            pkce_method,
336            fetch_userinfo,
337            userinfo_signed_response_alg: None,
338            authorization_endpoint: self.authorization_endpoint,
339            userinfo_endpoint: self.userinfo_endpoint,
340            token_endpoint: self.token_endpoint,
341            jwks_uri: self.jwks_uri,
342            response_mode,
343            claims_imports,
344            additional_authorization_parameters,
345        })
346    }
347}