diff --git a/conduwuit-example.toml b/conduwuit-example.toml index 637a5f705..39e3cb4bf 100644 --- a/conduwuit-example.toml +++ b/conduwuit-example.toml @@ -2579,9 +2579,10 @@ # Additional scopes Continuwuity should request from the IDP. This may be # necessary to access certain claims. Continuwuity always requests the -# `openid` scope, regardless of the contents of this option, -# and will also request the `profile` scope by default if this option is -# not set. +# `openid` scope, regardless of the value of this option. +# +# Defaults to only the `profile` scope, as recommended by the OIDC +# specification. # # env: CONTINUWUITY_OAUTH__OIDC__ADDITIONAL_SCOPES #additional_scopes = ["profile"] @@ -2597,6 +2598,19 @@ # env: CONTINUWUITY_OAUTH__OIDC__ADDITIONAL_TRUSTED_AUDIENCES #additional_trusted_audiences = [] +# If `true`, Continuwuity will pass on the Matrix ID of the user trying to +# log in to the identity provider where applicable. +# +# If Continuwuity knows the Matrix ID of the user trying to log in, it +# will set the authorization URL's `login_hint` query parameter to the +# _localpart_ of that ID (everything between the @ sign and the colon). +# +# Defaults to `true`. Set this to `false` if supplying the `login_hint` +# causes your identity provider to behave undesirably. +# +# env: CONTINUWUITY_OAUTH__OIDC__SUPPLY_LOGIN_HINT +#supply_login_hint = true + # The name of the identity provider. If this is set, the login page's # button reads "Continue with " instead of just "Continue". # diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index 43a471d49..c45f36593 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -2820,9 +2820,10 @@ pub struct OidcConfig { /// Additional scopes Continuwuity should request from the IDP. This may be /// necessary to access certain claims. Continuwuity always requests the - /// `openid` scope, regardless of the contents of this option, - /// and will also request the `profile` scope by default if this option is - /// not set. + /// `openid` scope, regardless of the value of this option. + /// + /// Defaults to only the `profile` scope, as recommended by the OIDC + /// specification. /// /// default: ["profile"] #[serde(default = "default_additional_scopes")] @@ -2840,6 +2841,20 @@ pub struct OidcConfig { #[serde(default)] pub additional_trusted_audiences: BTreeSet, + /// If `true`, Continuwuity will pass on the Matrix ID of the user trying to + /// log in to the identity provider where applicable. + /// + /// If Continuwuity knows the Matrix ID of the user trying to log in, it + /// will set the authorization URL's `login_hint` query parameter to the + /// _localpart_ of that ID (everything between the @ sign and the colon). + /// + /// Defaults to `true`. Set this to `false` if supplying the `login_hint` + /// causes your identity provider to behave undesirably. + /// + /// default: true + #[serde(default = "true_fn")] + pub supply_login_hint: bool, + /// The name of the identity provider. If this is set, the login page's /// button reads "Continue with " instead of just "Continue". /// diff --git a/src/service/oidc/mod.rs b/src/service/oidc/mod.rs index a1632baf9..57c46aa52 100644 --- a/src/service/oidc/mod.rs +++ b/src/service/oidc/mod.rs @@ -256,7 +256,11 @@ impl Service { } } - pub async fn begin_session(&self, prompt: Option) -> (PendingSession, Url) { + pub async fn begin_session( + &self, + prompt: Option, + login_hint: Option<&UserId>, + ) -> (PendingSession, Url) { let OidcClient { machine, config, .. } = self.client.as_ref().expect("oidc should be configured"); let machine = machine.wait().await.read().clone(); @@ -276,6 +280,13 @@ impl Service { auth_url = auth_url.add_prompt(prompt); } + if let Some(user_id) = login_hint + && config.supply_login_hint + { + auth_url = auth_url + .set_login_hint(openidconnect::LoginHint::new(user_id.localpart().to_owned())); + } + let (auth_url, csrf_token, nonce) = auth_url.url(); (PendingSession { pkce_verifier, nonce, csrf_token }, auth_url) diff --git a/src/web/pages/account/login.rs b/src/web/pages/account/login.rs index cde147087..18f79406b 100644 --- a/src/web/pages/account/login.rs +++ b/src/web/pages/account/login.rs @@ -93,14 +93,19 @@ async fn route_login( } let login_type = if services.oidc.enabled() { - let (session, redirect_url) = services - .oidc - .begin_session(match intent { - | Some(LoginIntent::SwitchAccounts) => Some(CoreAuthPrompt::SelectAccount), - | _ if reauthenticate => Some(CoreAuthPrompt::Consent), - | _ => None, - }) - .await; + let prompt = match intent { + | Some(LoginIntent::SwitchAccounts) => Some(CoreAuthPrompt::SelectAccount), + | _ if reauthenticate => Some(CoreAuthPrompt::Consent), + | _ => None, + }; + + let login_hint = if let Some(LoginHint::Mxid(user_id)) = &login_hint { + Some(user_id.as_ref()) + } else { + current_user.as_deref() + }; + + let (session, redirect_url) = services.oidc.begin_session(prompt, login_hint).await; session_store .insert(OIDC_SESSION_ID_KEY, OidcSession {