Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(admin): map SSO groups to community roles, set button label

Hampusbb0394ecad8e
diff --git a/fluxer_admin/src/routes/system_actions/sso.rs b/fluxer_admin/src/routes/system_actions/sso.rs
new file mode 100644
index 000000000..b8f51f6b9
--- /dev/null
+++ b/fluxer_admin/src/routes/system_actions/sso.rs
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+use crate::{
+ api::types::{InstanceConfigUpdateRequest, SsoConfigUpdateRequest, SsoGroupRoleMapping},
+ templates::pages::instance_config::SSO_ALLOWED_DOMAINS_FIELD,
+ utils::forms::MultiValueForm,
+};
+
+fn parse_sso_group_role_mappings(raw: &str) -> Result<Vec<SsoGroupRoleMapping>, String> {
+ raw.lines()
+ .map(str::trim)
+ .filter(|line| !line.is_empty())
+ .map(|line| {
+ let invalid = || {
+ format!(
+ "Write each group mapping as a group name, \"=\" and a role ID, not \"{line}\""
+ )
+ };
+ let (group, role_id) = line.rsplit_once('=').ok_or_else(invalid)?;
+ let (group, role_id) = (group.trim(), role_id.trim());
+ if group.is_empty()
+ || role_id.is_empty()
+ || !role_id.chars().all(|c| c.is_ascii_digit())
+ {
+ return Err(invalid());
+ }
+ Ok(SsoGroupRoleMapping {
+ group: group.to_string(),
+ role_id: role_id.to_string(),
+ })
+ })
+ .collect()
+}
+
+pub(super) fn build_sso_update(
+ form: &MultiValueForm,
+) -> Result<InstanceConfigUpdateRequest, String> {
+ let flag = |key: &str| form.bool_value(key);
+ let get = |key: &str| Some(form.clean(key));
+ let new_secret = form.clean("sso_client_secret");
+ let clear_secret = flag("sso_clear_client_secret");
+ let allowed = form.list_values_any(&["sso_allowed_domains[]", SSO_ALLOWED_DOMAINS_FIELD]);
+ let client_secret = if new_secret.is_some() {
+ Some(new_secret)
+ } else if clear_secret {
+ Some(None)
+ } else {
+ None
+ };
+ Ok(InstanceConfigUpdateRequest {
+ sso: Some(SsoConfigUpdateRequest {
+ enabled: Some(flag("sso_enabled")),
+ enforced: Some(flag("sso_enforced")),
+ auto_provision: Some(flag("sso_auto_provision")),
+ display_name: get("sso_display_name"),
+ button_label: get("sso_button_label"),
+ issuer: get("sso_issuer"),
+ authorization_url: get("sso_authorization_url"),
+ token_url: get("sso_token_url"),
+ userinfo_url: get("sso_userinfo_url"),
+ jwks_url: get("sso_jwks_url"),
+ client_id: get("sso_client_id"),
+ client_secret,
+ scope: get("sso_scope"),
+ allowed_domains: Some(allowed),
+ redirect_uri: None,
+ groups_claim: form
+ .first("sso_groups_claim")
+ .map(|_| form.clean("sso_groups_claim")),
+ group_role_mappings: form
+ .first("sso_group_role_mappings")
+ .map(parse_sso_group_role_mappings)
+ .transpose()?,
+ }),
+ ..Default::default()
+ })
+}
diff --git a/fluxer_admin/src/templates/pages/instance_config/sso.rs b/fluxer_admin/src/templates/pages/instance_config/sso.rs
new file mode 100644
index 000000000..d3540d562
--- /dev/null
+++ b/fluxer_admin/src/templates/pages/instance_config/sso.rs
@@ -0,0 +1,157 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+use crate::templates::pages::instance_config::ConfigForm;
+use crate::{
+ api::types::SsoConfigResponse,
+ templates::components::{
+ form::{
+ Checkbox, FORM_INPUT_CLASS, form_actions, form_field_group, submit_button, text_input,
+ textarea_input,
+ },
+ section_card::section_card_with_description,
+ },
+};
+use maud::{Markup, html};
+
+use super::{SSO_ALLOWED_DOMAINS_FIELD, allowed_domains_input};
+
+const SSO_BUTTON_LABEL_MAX_CHARS: usize = 64;
+
+pub(super) fn sso_config_section(
+ base: &str,
+ config_form: ConfigForm<'_>,
+ sso: &SsoConfigResponse,
+) -> Markup {
+ let allowed_domains = sso.allowed_domains.join("\n");
+ let group_role_mappings = sso
+ .group_role_mappings
+ .iter()
+ .map(|mapping| format!("{} = {}", mapping.group, mapping.role_id))
+ .collect::<Vec<_>>()
+ .join("\n");
+ let button_label_placeholder = match sso.display_name.as_deref().map(str::trim) {
+ Some(name) if !name.is_empty() => format!("Continue with {name}"),
+ _ => "Continue with SSO".to_string(),
+ };
+ let secret_caption = if sso.client_secret_set {
+ "A secret is set. Check to clear, or enter a new value to rotate."
+ } else {
+ "No secret set yet."
+ };
+ section_card_with_description(
+ "Single Sign-On (SSO)",
+ "Configure OIDC-style SSO for the admin and client apps. When enabled, \
+ users can sign in through your SSO provider. Require SSO to block local \
+ password registration and login.",
+ html! {
+ form method="post" action={(base) "/instance-config?action=update_sso"} {
+ (config_form.fields())
+ div class="space-y-6" {
+ div class="flex flex-col gap-2" {
+ (Checkbox {
+ name: "sso_enabled",
+ value: "true",
+ label: "Enable SSO",
+ checked: sso.enabled,
+ enabled: true,
+ }.render())
+ (Checkbox {
+ name: "sso_enforced",
+ value: "true",
+ label: "Require SSO (disables local password login and registration)",
+ checked: sso.enforced,
+ enabled: true,
+ }.render())
+ (Checkbox {
+ name: "sso_auto_provision",
+ value: "true",
+ label: "Automatically provision users on first SSO login",
+ checked: sso.auto_provision,
+ enabled: true,
+ }.render())
+ }
+ div class="grid grid-cols-1 gap-4 sm:grid-cols-2" {
+ (text_input("sso_display_name", "Display Name", sso.display_name.as_deref().unwrap_or(""), "Example Identity Provider"))
+ (text_input("sso_issuer", "Issuer", sso.issuer.as_deref().unwrap_or(""), "https://idp.example.com"))
+ }
+ div class="grid grid-cols-1 gap-4 sm:grid-cols-2" {
+ (form_field_group("Button Text", "sso_button_label", false, None,
+ Some("Shown on the sign-in button. Leave empty to use \"Continue with\" and the display name, or \"Continue with SSO\" if there's no display name."),
+ html! {
+ input type="text" id="sso_button_label" name="sso_button_label"
+ value=(sso.button_label.as_deref().unwrap_or(""))
+ placeholder=(button_label_placeholder)
+ maxlength=(SSO_BUTTON_LABEL_MAX_CHARS)
+ class=(FORM_INPUT_CLASS);
+ },
+ ))
+ }
+ div class="grid grid-cols-1 gap-4 sm:grid-cols-2" {
+ (text_input("sso_authorization_url", "Authorization URL", sso.authorization_url.as_deref().unwrap_or(""), "https://idp.example.com/oauth/authorize"))
+ (text_input("sso_token_url", "Token URL", sso.token_url.as_deref().unwrap_or(""), "https://idp.example.com/oauth/token"))
+ }
+ div class="grid grid-cols-1 gap-4 sm:grid-cols-2" {
+ (text_input("sso_userinfo_url", "User Info URL", sso.userinfo_url.as_deref().unwrap_or(""), "https://idp.example.com/oauth/userinfo"))
+ (text_input("sso_jwks_url", "JWKS URL", sso.jwks_url.as_deref().unwrap_or(""), "https://idp.example.com/.well-known/jwks.json"))
+ }
+ div class="grid grid-cols-1 gap-4 sm:grid-cols-2" {
+ (text_input("sso_client_id", "Client ID", sso.client_id.as_deref().unwrap_or(""), "client-id"))
+ div class="space-y-2" {
+ (form_field_group("Client Secret", "sso_client_secret", false, None, None, html! {
+ input type="password" id="sso_client_secret" name="sso_client_secret"
+ placeholder="Leave blank to keep existing"
+ class=(FORM_INPUT_CLASS);
+ }))
+ (Checkbox {
+ name: "sso_clear_client_secret",
+ value: "true",
+ label: "Clear secret",
+ checked: false,
+ enabled: true,
+ }.render())
+ p class="text-xs text-neutral-500" {
+ (secret_caption)
+ }
+ }
+ }
+ div class="grid grid-cols-1 gap-4 sm:grid-cols-2" {
+ (text_input("sso_scope", "Scope", sso.scope.as_deref().unwrap_or(""), "openid email profile"))
+ (form_field_group("Redirect URI", "sso_redirect_uri", false, None,
+ Some("Register this exact URI with your identity provider. It is the web app address followed by /auth/sso/callback. The desktop and mobile apps sign in through the same URI."),
+ html! {
+ input type="url" id="sso_redirect_uri" name="sso_redirect_uri"
+ value=(sso.redirect_uri.as_deref().unwrap_or(""))
+ disabled class=(FORM_INPUT_CLASS);
+ },
+ ))
+ }
+ (allowed_domains_input(
+ SSO_ALLOWED_DOMAINS_FIELD,
+ &allowed_domains,
+ "Limit SSO logins to these domains (one per line). Leave empty to allow any verified email.",
+ ))
+ div class="space-y-2" {
+ (text_input("sso_groups_claim", "Groups Claim", sso.groups_claim.as_deref().unwrap_or(""), "groups"))
+ (textarea_input(
+ "sso_group_role_mappings",
+ "Community Roles From Groups",
+ "staff = 1400000000000000001\n/engineering/devs = 1400000000000000002",
+ &group_role_mappings,
+ 4,
+ false,
+ ))
+ p class="text-xs text-neutral-500" {
+ "Only used in single-community mode. Write one mapping per line, as the group name the provider sends, \"=\" and the ID of a role in the single community. \
+ At every SSO sign-in, members get each listed role whose group they are in and lose each listed role whose group they left. \
+ Roles not listed here are never changed. If the provider sends no groups claim, roles stay as they are. \
+ Leave the claim empty to use \"groups\"."
+ }
+ }
+ (form_actions(html! {
+ (submit_button("Save SSO Settings"))
+ }))
+ }
+ }
+ },
+ )
+}