Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrganizationSsoPortalSessionResponse, FlooApiError> {
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)
Expand Down
17 changes: 17 additions & 0 deletions src/api_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,20 @@ pub struct OrganizationSsoStatusResponse {
pub enforcement_policy: OrganizationSsoEnforcementPolicy,
pub recovery_ready: bool,
pub observed_at: Option<String>,
pub directory: Option<OrganizationDirectoryStatus>,
}

/// 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<String>,
pub issue: Option<String>,
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)]
Expand All @@ -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,
}
Expand Down
16 changes: 15 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// 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.
Expand Down Expand Up @@ -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)
Expand Down
64 changes: 56 additions & 8 deletions src/commands/organization_sso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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)));
Expand Down
24 changes: 24 additions & 0 deletions src/redact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const SECRET_FIELD_NAMES: &[&str] = &[
"connection_url",
"webhook_url",
"invite_url",
"portal_url",
"generated_password",
"encrypted_value",
];
Expand Down Expand Up @@ -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.
Expand Down
63 changes: 61 additions & 2 deletions tests/mock_api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading