auths-cli 0.1.2

Command-line interface for Auths decentralized identity system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! SCIM provisioning server management commands.
//!
//! `serve`/`quickstart` run the in-workspace `auths-scim-server` library in-process
//! (KERI/registry is authoritative — there is no provisioning database). Tenants are
//! process-configured via flags, so `add-tenant`/`rotate-token` mint channel bearer
//! tokens, `status` probes a running server, and `tenants` is an honest "no tenant
//! registry in this model" error rather than a fake database stub.

use std::net::SocketAddr;
use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};

use auths_scim_server::{ServeConfig, TenantBootstrap, run};

use crate::commands::executable::ExecutableCommand;
use crate::config::CliConfig;

/// Manage the SCIM provisioning server.
#[derive(Parser, Debug, Clone)]
#[command(name = "scim", about = "SCIM 2.0 provisioning for agent identities")]
pub struct ScimCommand {
    #[command(subcommand)]
    pub command: ScimSubcommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum ScimSubcommand {
    /// Start the SCIM provisioning server (in-process, KERI-authoritative).
    Serve(ScimServeCommand),
    /// Zero-config quickstart: generate a token and run a single-tenant server.
    Quickstart(ScimQuickstartCommand),
    /// Validate the full SCIM pipeline: create -> get -> patch -> delete.
    TestConnection(ScimTestConnectionCommand),
    /// List SCIM tenants (process-configured in this model).
    Tenants(ScimTenantsCommand),
    /// Mint a bearer token for an IdP provisioning channel.
    AddTenant(ScimAddTenantCommand),
    /// Mint a replacement bearer token to rotate a tenant's channel.
    RotateToken(ScimRotateTokenCommand),
    /// Probe a running SCIM server's health and discovery surface.
    Status(ScimStatusCommand),
}

/// Start the SCIM provisioning server.
#[derive(Parser, Debug, Clone)]
pub struct ScimServeCommand {
    /// Listen address.
    #[arg(long, default_value = "0.0.0.0:8787")]
    pub bind: SocketAddr,
    /// Tenant id for the single-tenant bootstrap (matches the IdP's tenant).
    #[arg(long)]
    pub tenant: Option<String>,
    /// Auths org prefix this tenant provisions into.
    #[arg(long)]
    pub org_prefix: Option<String>,
    /// Bearer token authenticating the provisioning channel.
    #[arg(long)]
    pub token: Option<String>,
    /// Org signing-key alias (default: derived `org-<slug>`).
    #[arg(long)]
    pub org_key: Option<String>,
    /// Base URL used for SCIM `meta.location`.
    #[arg(long)]
    pub base_url: Option<String>,
    /// Passphrase for the org signing key (single-host custody).
    #[arg(long)]
    pub passphrase: Option<String>,
    /// Path to the Auths registry Git repository (default: `~/.auths`).
    #[arg(long)]
    pub registry_path: Option<PathBuf>,
}

/// Zero-config quickstart with copy-paste curl examples.
#[derive(Parser, Debug, Clone)]
pub struct ScimQuickstartCommand {
    /// Listen address.
    #[arg(long, default_value = "0.0.0.0:8787")]
    pub bind: SocketAddr,
    /// Auths org prefix to provision into (must already exist on this host).
    #[arg(long)]
    pub org_prefix: String,
    /// Tenant id (defaults to `quickstart`).
    #[arg(long, default_value = "quickstart")]
    pub tenant: String,
    /// Passphrase for the org signing key (single-host custody).
    #[arg(long)]
    pub passphrase: Option<String>,
    /// Path to the Auths registry Git repository (default: `~/.auths`).
    #[arg(long)]
    pub registry_path: Option<PathBuf>,
}

/// Validate the full SCIM pipeline against a running server.
#[derive(Parser, Debug, Clone)]
pub struct ScimTestConnectionCommand {
    /// Server URL.
    #[arg(long, default_value = "http://localhost:8787")]
    pub url: String,
    /// Bearer token.
    #[arg(long)]
    pub token: String,
}

/// List SCIM tenants.
#[derive(Parser, Debug, Clone)]
pub struct ScimTenantsCommand {
    /// Output as JSON.
    #[arg(long)]
    pub json: bool,
}

/// Mint a bearer token for an IdP provisioning channel.
#[derive(Parser, Debug, Clone)]
pub struct ScimAddTenantCommand {
    /// Tenant name.
    #[arg(long)]
    pub name: String,
    /// Auths org prefix this tenant provisions into.
    #[arg(long)]
    pub org_prefix: String,
}

/// Mint a replacement bearer token to rotate a tenant's channel.
#[derive(Parser, Debug, Clone)]
pub struct ScimRotateTokenCommand {
    /// Tenant name.
    #[arg(long)]
    pub name: String,
}

/// Probe a running SCIM server.
#[derive(Parser, Debug, Clone)]
pub struct ScimStatusCommand {
    /// Server URL.
    #[arg(long, default_value = "http://localhost:8787")]
    pub url: String,
    /// Output as JSON.
    #[arg(long)]
    pub json: bool,
}

fn handle_scim(cmd: ScimCommand, ctx: &CliConfig) -> Result<()> {
    match cmd.command {
        ScimSubcommand::Serve(serve) => handle_serve(serve, ctx),
        ScimSubcommand::Quickstart(qs) => handle_quickstart(qs, ctx),
        ScimSubcommand::TestConnection(tc) => handle_test_connection(tc),
        ScimSubcommand::Tenants(_) => handle_tenants(),
        ScimSubcommand::AddTenant(at) => handle_add_tenant(&at.name, &at.org_prefix),
        ScimSubcommand::RotateToken(rt) => handle_rotate_token(&rt.name),
        ScimSubcommand::Status(st) => handle_status(st),
    }
}

/// Resolve the registry home from the command flag, the global CLI config, or the
/// server's `~/.auths` default (`None`).
fn resolve_home(registry_path: Option<PathBuf>, ctx: &CliConfig) -> Option<PathBuf> {
    registry_path.or_else(|| ctx.repo_path.clone())
}

fn handle_serve(cmd: ScimServeCommand, ctx: &CliConfig) -> Result<()> {
    let tenant = match (cmd.tenant, cmd.org_prefix, cmd.token) {
        (Some(tenant_id), Some(org_prefix), Some(bearer_token)) => Some(TenantBootstrap {
            tenant_id,
            org_prefix,
            bearer_token,
            org_key_alias: cmd.org_key,
            base_url: cmd.base_url,
        }),
        (None, None, None) => {
            println!("No tenant configured — running discovery-only; /Users rejects all callers.");
            println!("Configure with --tenant <id> --org-prefix <E…> --token <bearer>.");
            None
        }
        _ => anyhow::bail!(
            "--tenant, --org-prefix, and --token must be set together (or all omitted for discovery-only)"
        ),
    };

    println!("Starting SCIM server on {}...", cmd.bind);
    serve(ServeConfig {
        bind: cmd.bind.to_string(),
        tenant,
        home: resolve_home(cmd.registry_path, ctx),
        passphrase: cmd.passphrase.unwrap_or_default(),
    })
}

fn handle_quickstart(cmd: ScimQuickstartCommand, ctx: &CliConfig) -> Result<()> {
    let token = format!("scim_test_{}", generate_token_b64());

    println!();
    println!("  Auths SCIM Quickstart");
    println!();
    println!("  Server:   http://{}", cmd.bind);
    println!("  Tenant:   {}", cmd.tenant);
    println!("  Org:      {}", cmd.org_prefix);
    println!("  Token:    {}", token);
    println!();
    println!("  Try it (in another shell):");
    println!(
        "    curl -s -H \"Authorization: Bearer {token}\" http://{}/scim/v2/Users | jq",
        cmd.bind
    );
    println!();
    println!("    curl -s -X POST -H \"Authorization: Bearer {token}\" \\");
    println!("      -H \"Content-Type: application/scim+json\" \\");
    println!(
        "      -d '{{\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:User\"],\"userName\":\"deploy-bot\",\"externalId\":\"okta-1\"}}' \\"
    );
    println!("      http://{}/scim/v2/Users | jq", cmd.bind);
    println!();
    println!("  Press Ctrl+C to stop.");
    println!();

    serve(ServeConfig {
        bind: cmd.bind.to_string(),
        tenant: Some(TenantBootstrap {
            tenant_id: cmd.tenant,
            org_prefix: cmd.org_prefix,
            bearer_token: token,
            org_key_alias: None,
            base_url: Some(format!("http://{}/scim/v2", cmd.bind)),
        }),
        home: resolve_home(cmd.registry_path, ctx),
        passphrase: cmd.passphrase.unwrap_or_default(),
    })
}

fn handle_tenants() -> Result<()> {
    anyhow::bail!(
        "SCIM tenants are process-configured (via `auths scim serve --tenant/--org-prefix/--token` \
         or the SCIM_* env), not stored in a tenant database — the KEL is the source of truth, so \
         there is no tenant registry to list. Use `auths scim status --url <server>` to probe a \
         running server."
    )
}

fn handle_add_tenant(name: &str, org_prefix: &str) -> Result<()> {
    let token = format!("scim_{}", generate_token_b64());
    println!("Minted a SCIM channel bearer token for tenant '{name}':");
    println!();
    println!("  {token}");
    println!();
    println!("Run the server with it:");
    println!("  auths scim serve --tenant {name} --org-prefix {org_prefix} --token {token}");
    println!();
    println!("Configure your IdP (Okta/Entra) with this token as the SCIM bearer secret.");
    println!("The token is hashed at rest by the server; store this plaintext securely now.");
    Ok(())
}

fn handle_rotate_token(name: &str) -> Result<()> {
    let token = format!("scim_{}", generate_token_b64());
    println!("Minted a replacement SCIM bearer token for tenant '{name}':");
    println!();
    println!("  {token}");
    println!();
    println!("Swap it in by restarting the server with --token {token} and updating the IdP.");
    println!("The previous token stops authenticating as soon as the server restarts.");
    Ok(())
}

fn handle_status(cmd: ScimStatusCommand) -> Result<()> {
    let result = block_on(probe_status(&cmd.url));
    match result {
        Ok(report) => {
            if cmd.json {
                println!("{}", serde_json::to_string_pretty(&report)?);
            } else {
                println!("SCIM server: {}", cmd.url);
                println!(
                    "  health:  {}",
                    if report.healthy { "ok" } else { "unreachable" }
                );
                println!("  patch:   {}", report.patch_supported);
                println!("  filter:  {}", report.filter_supported);
            }
            Ok(())
        }
        Err(e) => anyhow::bail!("could not reach SCIM server at {}: {e}", cmd.url),
    }
}

/// A minimal status snapshot probed from discovery.
#[derive(Debug, serde::Serialize)]
struct StatusReport {
    healthy: bool,
    patch_supported: bool,
    filter_supported: bool,
}

async fn probe_status(base_url: &str) -> Result<StatusReport> {
    let client = build_http_client()?;
    let healthy = client
        .get(format!("{base_url}/health"))
        .send()
        .await
        .map(|r| r.status().is_success())
        .unwrap_or(false);

    let spc: serde_json::Value = client
        .get(format!("{base_url}/scim/v2/ServiceProviderConfig"))
        .send()
        .await
        .context("ServiceProviderConfig request failed")?
        .json()
        .await
        .context("ServiceProviderConfig was not valid JSON")?;

    Ok(StatusReport {
        healthy,
        patch_supported: spc["patch"]["supported"].as_bool().unwrap_or(false),
        filter_supported: spc["filter"]["supported"].as_bool().unwrap_or(false),
    })
}

fn handle_test_connection(cmd: ScimTestConnectionCommand) -> Result<()> {
    println!();
    println!("  Testing SCIM connection to {}...", cmd.url);
    println!();

    match block_on(run_test_connection(&cmd.url, &cmd.token)) {
        Ok(()) => {
            println!("  All checks passed. Your SCIM server is ready.");
            println!();
        }
        Err(e) => {
            println!("  Connection test failed: {e}");
            println!();
        }
    }
    Ok(())
}

#[allow(clippy::disallowed_methods)] // CLI boundary: Utc::now() for test user naming
async fn run_test_connection(base_url: &str, token: &str) -> Result<()> {
    let client = build_http_client()?;
    let auth = format!("Bearer {token}");

    let start = std::time::Instant::now();
    let resp = client
        .post(format!("{base_url}/scim/v2/Users"))
        .header("Authorization", &auth)
        .header("Content-Type", "application/scim+json")
        .json(&serde_json::json!({
            "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
            "userName": format!("test-agent-{}", chrono::Utc::now().timestamp()),
            "externalId": format!("scim-test-{}", chrono::Utc::now().timestamp()),
            "displayName": "SCIM Test Agent"
        }))
        .send()
        .await
        .context("POST /Users failed")?;
    let elapsed = start.elapsed();

    if resp.status().as_u16() == 201 {
        println!("  [PASS] POST /Users -> 201 Created ({elapsed:.0?})");
    } else {
        println!("  [FAIL] POST /Users -> {} ({elapsed:.0?})", resp.status());
        return Ok(());
    }

    let body: serde_json::Value = resp.json().await?;
    let id = body["id"].as_str().unwrap_or("unknown").to_string();
    let did = body
        .get("urn:ietf:params:scim:schemas:extension:auths:2.0:Agent")
        .and_then(|ext| ext["identityDid"].as_str())
        .unwrap_or("unknown");
    println!("         Agent: {} (userName: {})", did, body["userName"]);

    probe_step(&client, &auth, "GET /Users/{id}", |c| {
        c.get(format!("{base_url}/scim/v2/Users/{id}"))
    })
    .await;

    // PATCH active=false (soft-disable)
    let start = std::time::Instant::now();
    let resp = client
        .patch(format!("{base_url}/scim/v2/Users/{id}"))
        .header("Authorization", &auth)
        .header("Content-Type", "application/scim+json")
        .json(&serde_json::json!({
            "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
            "Operations": [{"op": "replace", "path": "active", "value": false}]
        }))
        .send()
        .await?;
    println!(
        "  [{}] PATCH active=false -> {} ({:.0?})",
        pass_fail(resp.status().is_success()),
        resp.status(),
        start.elapsed()
    );

    // DELETE (soft leaver) -> 204
    let start = std::time::Instant::now();
    let resp = client
        .delete(format!("{base_url}/scim/v2/Users/{id}"))
        .header("Authorization", &auth)
        .send()
        .await?;
    println!(
        "  [{}] DELETE /Users/{{id}} -> {} ({:.0?})",
        pass_fail(resp.status().as_u16() == 204),
        resp.status(),
        start.elapsed()
    );

    // GET should now be 404
    let start = std::time::Instant::now();
    let resp = client
        .get(format!("{base_url}/scim/v2/Users/{id}"))
        .header("Authorization", &auth)
        .send()
        .await?;
    println!(
        "  [{}] GET /Users/{{id}} -> {} ({:.0?})",
        pass_fail(resp.status().as_u16() == 404),
        resp.status(),
        start.elapsed()
    );

    println!();
    Ok(())
}

async fn probe_step<F>(client: &reqwest::Client, auth: &str, label: &str, build: F)
where
    F: FnOnce(&reqwest::Client) -> reqwest::RequestBuilder,
{
    let start = std::time::Instant::now();
    let outcome = build(client).header("Authorization", auth).send().await;
    match outcome {
        Ok(resp) => println!(
            "  [{}] {label} -> {} ({:.0?})",
            pass_fail(resp.status().is_success()),
            resp.status(),
            start.elapsed()
        ),
        Err(e) => println!("  [FAIL] {label} -> {e}"),
    }
}

fn pass_fail(ok: bool) -> &'static str {
    if ok { "PASS" } else { "FAIL" }
}

fn build_http_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .connect_timeout(std::time::Duration::from_secs(10))
        .timeout(std::time::Duration::from_secs(30))
        .user_agent(concat!("auths/", env!("CARGO_PKG_VERSION")))
        .min_tls_version(reqwest::tls::Version::TLS_1_2)
        .build()
        .context("failed to build HTTP client")
}

/// Run a future to completion on a fresh runtime (the CLI's `main` is synchronous).
fn block_on<F: std::future::Future>(fut: F) -> F::Output {
    #[allow(clippy::expect_used)] // INVARIANT: tokio runtime creation failing is unrecoverable
    let rt = tokio::runtime::Runtime::new().expect("tokio runtime");
    rt.block_on(fut)
}

/// Serve the in-process SCIM server until stopped.
fn serve(config: ServeConfig) -> Result<()> {
    block_on(run(config))
}

fn generate_token_b64() -> String {
    use base64::Engine;
    let mut bytes = [0u8; 32];
    #[allow(clippy::expect_used)] // INVARIANT: system RNG failure is unrecoverable
    ring::rand::SystemRandom::new()
        .fill(&mut bytes)
        .expect("random bytes");
    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
}

impl ExecutableCommand for ScimCommand {
    fn execute(&self, ctx: &CliConfig) -> Result<()> {
        handle_scim(self.clone(), ctx)
    }
}

use ring::rand::SecureRandom;