diff --git a/src/api_client.rs b/src/api_client.rs index dc597c5..a3c8860 100644 --- a/src/api_client.rs +++ b/src/api_client.rs @@ -332,12 +332,19 @@ impl FlooClient { self.handle_response(resp) } + /// Opens the provider's setup portal for SSO, or with `directory` for the SCIM directory. pub fn create_organization_sso_portal( &self, org_id: &str, + directory: bool, ) -> Result { + let path = if directory { + "sso/directory/portal-session" + } else { + "sso/portal-session" + }; let resp = self.post_json( - &format!("/v1/organizations/{org_id}/sso/portal-session"), + &format!("/v1/organizations/{org_id}/{path}"), &serde_json::json!({}), )?; self.handle_response(resp) diff --git a/src/api_types.rs b/src/api_types.rs index 1dbf1ab..1404f24 100644 --- a/src/api_types.rs +++ b/src/api_types.rs @@ -443,6 +443,20 @@ pub struct OrganizationSsoStatusResponse { pub enforcement_policy: OrganizationSsoEnforcementPolicy, pub recovery_ready: bool, pub observed_at: Option, + pub directory: Option, +} + +/// The org's SCIM directory as floo last synced it. `status`, `provider_type`, and `issue` +/// stay strings: they are the provider's and the API's words, shown rather than branched on. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct OrganizationDirectoryStatus { + pub status: String, + pub provider_type: String, + pub synced_at: Option, + pub issue: Option, + pub people: u64, + /// False while floo is not syncing it: the org is not on Enterprise or SSO is not enabled. + pub syncing: bool, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -458,6 +472,9 @@ pub struct OrganizationSsoDoctorResponse { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct OrganizationSsoPortalSessionResponse { pub id: String, + /// A live Admin Portal session for whoever holds it: emitted as `portal_url`, which the + /// `--json` redactor treats as a secret unless `--reveal-secrets` is set. + #[serde(rename(serialize = "portal_url"))] pub url: String, pub expires_in_seconds: u64, } diff --git a/src/cli.rs b/src/cli.rs index 0cfd2af..74db439 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -907,6 +907,17 @@ pub enum OrganizationSsoCommands { no_browser: bool, }, + /// Open a short-lived portal to connect or manage the SCIM directory. + Directory { + /// Organization slug or ID. Uses the current organization when omitted. + #[arg(long)] + org: Option, + + /// Print the portal URL without opening a browser. + #[arg(long)] + no_browser: bool, + }, + /// Verify with SSO in the dashboard and enable enforcement. Enforce { /// Organization slug or ID. Uses the current organization when omitted. @@ -2402,7 +2413,10 @@ pub fn run() { commands::organization_sso::status(org.as_deref()) } OrganizationSsoCommands::Portal { org, no_browser } => { - commands::organization_sso::portal(org.as_deref(), no_browser) + commands::organization_sso::portal(org.as_deref(), no_browser, false) + } + OrganizationSsoCommands::Directory { org, no_browser } => { + commands::organization_sso::portal(org.as_deref(), no_browser, true) } OrganizationSsoCommands::Enforce { org, no_browser } => { commands::organization_sso::enforce(org.as_deref(), no_browser) diff --git a/src/commands/organization_sso.rs b/src/commands/organization_sso.rs index 8031837..6da4489 100644 --- a/src/commands/organization_sso.rs +++ b/src/commands/organization_sso.rs @@ -2,11 +2,11 @@ use std::process; use crate::api_client::FlooClient; use crate::api_types::{ - OrgResponse, OrganizationSsoBrowserHandoff, OrganizationSsoCredentialStatus, - OrganizationSsoDoctorResponse, OrganizationSsoEnforcementAccess, - OrganizationSsoEnforcementPolicy, OrganizationSsoLifecycle, OrganizationSsoNextAction, - OrganizationSsoProviderReachability, OrganizationSsoProviderState, OrganizationSsoProviderType, - OrganizationSsoStatusResponse, + OrgResponse, OrganizationDirectoryStatus, OrganizationSsoBrowserHandoff, + OrganizationSsoCredentialStatus, OrganizationSsoDoctorResponse, + OrganizationSsoEnforcementAccess, OrganizationSsoEnforcementPolicy, OrganizationSsoLifecycle, + OrganizationSsoNextAction, OrganizationSsoProviderReachability, OrganizationSsoProviderState, + OrganizationSsoProviderType, OrganizationSsoStatusResponse, }; use crate::config::{load_config, FlooConfig}; use crate::errors::{ErrorCode, FlooApiError}; @@ -47,18 +47,22 @@ pub fn doctor(org_selector: Option<&str>) { } } -pub fn portal(org_selector: Option<&str>, no_browser: bool) { +pub fn portal(org_selector: Option<&str>, no_browser: bool, directory: bool) { let context = resolve_organization(org_selector); match context .client - .create_organization_sso_portal(&context.org.id) + .create_organization_sso_portal(&context.org.id, directory) { Ok(portal) => { if output::is_json_mode() { output::success("", Some(output::to_value(&portal))); return; } - output::success("Created a short-lived SSO setup session.", None); + let subject = if directory { "directory" } else { "SSO" }; + output::success( + &format!("Created a short-lived {subject} setup session."), + None, + ); output::info(&format!(" {}", portal.url), None); if !no_browser { open_browser(&portal.url); @@ -219,6 +223,17 @@ fn render_status(org: &OrgResponse, status: &OrganizationSsoStatusResponse) { .join(", "); output::info(&format!("Domains: {domains}"), None); } + output::info( + &format!( + "Directory: {}", + status + .directory + .as_ref() + .map(directory_label) + .unwrap_or_else(|| "Not connected".to_string()) + ), + None, + ); let selector = org.display_name().unwrap_or(org.id.as_str()); if status.lifecycle != OrganizationSsoLifecycle::Enabled || status.status != OrganizationSsoProviderState::Active @@ -235,6 +250,39 @@ fn render_status(org: &OrgResponse, status: &OrganizationSsoStatusResponse) { } } +fn directory_label(directory: &OrganizationDirectoryStatus) -> String { + let synced = directory.synced_at.as_deref().unwrap_or("never"); + let mut label = format!( + "{} ({}), {} people, synced {synced}", + string_label(&directory.provider_type), + string_label(&directory.status), + directory.people, + ); + if !directory.syncing { + label.push_str(", sync paused until SSO is enabled on an Enterprise plan"); + } + if let Some(issue) = directory.issue.as_deref() { + label.push_str(&format!( + ", needs attention: {}", + directory_issue_label(issue) + )); + } + label +} + +fn directory_issue_label(issue: &str) -> &str { + match issue { + "multiple_directories" => "more than one directory is connected; remove the extra one", + "provider_unavailable" => "the directory could not be read; floo retries every 5 minutes", + "not_linked" => "the directory is not linked yet, so nobody is added or removed", + "last_admin_kept" => { + "the directory removed the only admin; make someone else an admin to finish" + } + "invite_email_failed" => "an invite email failed; floo retries every hour", + other => string_label(other), + } +} + fn render_doctor(org: &OrgResponse, doctor: &OrganizationSsoDoctorResponse) { if output::is_json_mode() { output::success("", Some(output::to_value(doctor))); diff --git a/src/redact.rs b/src/redact.rs index 7784b61..bee5a06 100644 --- a/src/redact.rs +++ b/src/redact.rs @@ -79,6 +79,7 @@ const SECRET_FIELD_NAMES: &[&str] = &[ "connection_url", "webhook_url", "invite_url", + "portal_url", "generated_password", "encrypted_value", ]; @@ -978,6 +979,29 @@ mod snapshots { ); } + /// `floo orgs sso portal|directory --json`: the setup portal link opens a WorkOS + /// Admin Portal session for anyone who holds it, so it is a credential. + #[test] + fn sso_portal_session_url_redacted() { + let _g = lock_default_redact(); + let payload = json!({ + "success": true, + "data": { + "id": "sso-config-1", + "portal_url": "https://setup.example.test/launch/supersecretvaluethatshouldnotleak", + "expires_in_seconds": 300, + } + }); + let out = through_print_json(payload); + let leaks = collect_leaks(&out); + assert!( + leaks.is_empty(), + "portal link survived redaction: {leaks:?}" + ); + assert_eq!(out["contains_secrets"], true); + assert_eq!(out["data"]["expires_in_seconds"], 300); + } + /// Sweep test: kitchen-sink payload combining every leak vector. /// If any future change skips a branch in the redactor, this /// fails before it reaches users. diff --git a/tests/mock_api_tests.rs b/tests/mock_api_tests.rs index 075e791..f200b9c 100644 --- a/tests/mock_api_tests.rs +++ b/tests/mock_api_tests.rs @@ -805,10 +805,69 @@ fn test_orgs_sso_portal_json_returns_short_lived_url_once() { .env("HOME", home.path()) .assert() .success() + // The portal link opens a live setup session: redacted unless revealed. + .stdout(predicate::str::contains("launch/ephemeral").not()) + .stdout(predicate::str::contains("contains_secrets")) + .stdout(predicate::str::contains(r#""expires_in_seconds":300"#)); +} + +#[test] +fn test_orgs_sso_directory_json_reveals_the_portal_url_only_when_asked() { + let mut server = Server::new(); + let home = setup_config(&server); + let _org = mock_org_me(&mut server); + let _portal = server + .mock( + "POST", + format!("/v1/organizations/{TEST_ORG_ID}/sso/directory/portal-session").as_str(), + ) + .match_header("x-floo-org-id", TEST_ORG_ID) + .with_status(200) + .with_header("content-type", "application/json") + .with_body( + r#"{"id":"sso-config-1","url":"https://setup.example.test/launch/dsync","expires_in_seconds":300}"#, + ) + .create(); + + floo() + .args(["--json", "--reveal-secrets", "orgs", "sso", "directory"]) + .env("HOME", home.path()) + .assert() + .success() .stdout(predicate::str::contains( - "https://setup.example.test/launch/ephemeral", + r#""portal_url":"https://setup.example.test/launch/dsync""#, )) - .stdout(predicate::str::contains(r#""expires_in_seconds":300"#)); + .stdout(predicate::str::contains("contains_secrets")); +} + +#[test] +fn test_orgs_sso_status_shows_the_directory_and_its_issue() { + let mut server = Server::new(); + let home = setup_config(&server); + let _org = mock_org_me(&mut server); + let body = sso_status_json().replace( + r#""observed_at":"2026-08-28T12:00:00Z"}"#, + r#""observed_at":"2026-08-28T12:00:00Z","directory":{"status":"linked","provider_type":"okta scim v2.0","synced_at":"2026-11-18T12:00:00Z","issue":"last_admin_kept","people":42,"syncing":true}}"#, + ); + let _status = server + .mock( + "GET", + format!("/v1/organizations/{TEST_ORG_ID}/sso").as_str(), + ) + .with_status(200) + .with_header("content-type", "application/json") + .with_body(body) + .create(); + + floo() + .args(["orgs", "sso", "status"]) + .env("HOME", home.path()) + .assert() + .success() + .stderr(predicate::str::contains( + "okta scim v2.0 (linked), 42 people, synced 2026-11-18T12:00:00Z", + )) + .stderr(predicate::str::contains("make someone else an admin")); } #[test]