auths-cli 0.0.1-rc.8

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
489
490
491
#![allow(
    clippy::print_stdout,
    clippy::print_stderr,
    clippy::disallowed_methods,
    clippy::exit,
    clippy::unwrap_used,
    clippy::expect_used
)]
//! auths-sign: Git SSH signing program compatible with `gpg.ssh.program`
//!
//! Git calls this binary with ssh-keygen compatible arguments:
//! ```
//! auths-sign -Y sign -n git -f <key_file> <buffer_file>
//! ```
//!
//! For Auths keys, the key_file is in the format `auths:<alias>`.
//!
//! ## Passphrase-Free Signing
//!
//! This program implements a three-tier signing strategy via
//! [`CommitSigningWorkflow`]:
//!
//! 1. **Tier 1: Agent signing** - If the agent is running with keys loaded,
//!    sign via the agent without any passphrase prompt.
//!
//! 2. **Tier 2: Auto-start + load key** - If the agent is not running or has
//!    no keys, auto-start it, prompt for passphrase once, load the key, and sign.
//!
//! 3. **Tier 3: Direct signing** - If agent approach fails, fall back to
//!    direct passphrase-based signing via SDK pipeline.

use std::fs;
use std::path::PathBuf;
use std::sync::Arc;

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

use auths_cli::core::pubkey_cache::get_cached_pubkey;
use auths_cli::factories::build_agent_provider;
use auths_core::config::{EnvironmentConfig, load_config};
use auths_core::signing::{KeychainPassphraseProvider, PassphraseProvider};
use auths_core::storage::keychain::get_platform_keychain;
use auths_core::storage::passphrase_cache::{get_passphrase_cache, parse_duration_str};
use auths_sdk::workflows::signing::{
    CommitSigningContext, CommitSigningParams, CommitSigningWorkflow,
};

/// Auths SSH signing program for Git integration.
///
/// Compatible with `gpg.ssh.program` interface - mimics ssh-keygen signing behavior.
/// Supports both signing and verification operations.
///
/// Git calls this binary with ssh-keygen compatible arguments:
/// - Signing:           `auths-sign -Y sign -n git -f auths:<alias> <buffer_file>`
/// - Verification:      `auths-sign -Y verify -f <allowed_signers> -I <email> -n git -O verify-time=<ts> -s <sig_file>`
/// - Find principals:   `auths-sign -Y find-principals -f <allowed_signers> -s <sig_file> -Overify-time=<ts>`
/// - Check (no-validate):`auths-sign -Y check-novalidate -n git -s <sig_file> -Overify-time=<ts>`
///
/// Note: `-n` and `-f` are not required by every operation; for example,
/// `find-principals` omits `-n` and `check-novalidate` omits `-f`.
#[derive(Parser, Debug)]
#[command(name = "auths-sign")]
#[command(version)]
struct Args {
    /// Operation type: "sign", "verify", "find-principals", or "check-novalidate"
    #[arg(short = 'Y')]
    operation: String,

    /// Namespace for the signature (e.g., "git"). Not required for all operations.
    #[arg(short = 'n')]
    namespace: Option<String>,

    /// For sign: Key identifier (auths:<alias>)
    /// For verify/find-principals: Allowed signers file
    /// Not required for all operations.
    #[arg(short = 'f')]
    file_arg: Option<String>,

    /// For verify: Identity/principal to check
    #[arg(short = 'I')]
    identity: Option<String>,

    /// For verify/find-principals/check-novalidate: Signature file path
    #[arg(short = 's')]
    signature_file: Option<PathBuf>,

    /// Options forwarded by git (e.g. -O verify-time=<timestamp> or -Overify-time=<ts>).
    #[arg(short = 'O', action = clap::ArgAction::Append, required = false)]
    verify_options: Vec<String>,

    /// For sign: Buffer file containing data to sign
    buffer_file: Option<PathBuf>,
}

fn parse_key_identifier(key_file: &str) -> Result<String> {
    if let Some(alias) = key_file.strip_prefix("auths:") {
        if alias.is_empty() {
            bail!("Invalid Auths key format: alias cannot be empty. Use 'auths:<alias>'");
        }
        Ok(alias.to_string())
    } else {
        bail!(
            "Unsupported key format: '{}'. \
             Auths keys should be specified as 'auths:<alias>' \
             (e.g., 'auths:default' or 'auths:my-signing-key')",
            key_file
        );
    }
}

fn build_signing_context(alias: &str) -> Result<CommitSigningContext> {
    let env_config = EnvironmentConfig::from_env();

    let keychain =
        get_platform_keychain().map_err(|e| anyhow!("Failed to access keychain: {e}"))?;

    let passphrase_provider: Arc<dyn PassphraseProvider + Send + Sync> =
        if let Some(passphrase) = env_config.keychain.passphrase.clone() {
            Arc::new(auths_core::PrefilledPassphraseProvider::new(&passphrase))
        } else {
            let config = load_config();
            let cache = get_passphrase_cache(config.passphrase.biometric);
            let ttl_secs = config
                .passphrase
                .duration
                .as_deref()
                .and_then(parse_duration_str);
            let inner = Arc::new(auths_cli::core::provider::CliPassphraseProvider::new());
            Arc::new(KeychainPassphraseProvider::new(
                inner,
                cache,
                alias.to_string(),
                config.passphrase.cache,
                ttl_secs,
            ))
        };

    Ok(CommitSigningContext {
        key_storage: Arc::from(keychain),
        passphrase_provider,
        agent_signing: build_agent_provider(),
    })
}

fn main() {
    if let Err(e) = run() {
        auths_cli::errors::renderer::render_error(&e, false);
        std::process::exit(1);
    }
}

fn run() -> Result<()> {
    let args = Args::parse();

    match args.operation.as_str() {
        "sign" => run_sign(&args),
        "verify" => run_verify(&args),
        "find-principals" | "check-novalidate" => run_delegate_to_ssh_keygen(&args),
        other => bail!(
            "Unsupported operation: '{}'. Use 'sign', 'verify', 'find-principals', or 'check-novalidate'.",
            other
        ),
    }
}

fn run_verify(args: &Args) -> Result<()> {
    let allowed_signers = args
        .file_arg
        .as_deref()
        .ok_or_else(|| anyhow!("-f <allowed_signers> required for verify"))?;
    let namespace = args
        .namespace
        .as_deref()
        .ok_or_else(|| anyhow!("-n <namespace> required for verify"))?;
    let identity = args.identity.as_deref().unwrap_or("*");
    let sig_file = args
        .signature_file
        .as_ref()
        .ok_or_else(|| anyhow!("-s <signature_file> required for verify"))?;

    let mut cmd = std::process::Command::new("ssh-keygen");
    cmd.args([
        "-Y",
        "verify",
        "-f",
        allowed_signers,
        "-I",
        identity,
        "-n",
        namespace,
        "-s",
        sig_file.to_str().unwrap(),
    ]);
    for opt in &args.verify_options {
        cmd.arg("-O").arg(opt);
    }
    let status = cmd
        .stdin(std::process::Stdio::inherit())
        .stdout(std::process::Stdio::inherit())
        .stderr(std::process::Stdio::inherit())
        .status()
        .context("Failed to run ssh-keygen")?;

    if status.success() {
        Ok(())
    } else {
        std::process::exit(status.code().unwrap_or(1));
    }
}

fn run_delegate_to_ssh_keygen(args: &Args) -> Result<()> {
    let mut cmd = std::process::Command::new("ssh-keygen");
    cmd.arg("-Y").arg(&args.operation);
    if let Some(ns) = &args.namespace {
        cmd.arg("-n").arg(ns);
    }
    if let Some(f) = &args.file_arg {
        cmd.arg("-f").arg(f);
    }
    if let Some(id) = &args.identity {
        cmd.arg("-I").arg(id);
    }
    if let Some(sig) = &args.signature_file {
        cmd.arg("-s").arg(sig);
    }
    for opt in &args.verify_options {
        cmd.arg("-O").arg(opt);
    }
    let status = cmd
        .stdin(std::process::Stdio::inherit())
        .stdout(std::process::Stdio::inherit())
        .stderr(std::process::Stdio::inherit())
        .status()
        .context("Failed to run ssh-keygen")?;

    if status.success() {
        Ok(())
    } else {
        std::process::exit(status.code().unwrap_or(1));
    }
}

fn run_sign(args: &Args) -> Result<()> {
    let file_arg = args
        .file_arg
        .as_deref()
        .ok_or_else(|| anyhow!("-f <key_identifier> required for sign (e.g. auths:main)"))?;
    let alias = parse_key_identifier(file_arg)?;
    let namespace = args.namespace.as_deref().unwrap_or("git");

    let buffer_file = args
        .buffer_file
        .as_ref()
        .ok_or_else(|| anyhow!("Buffer file required for signing"))?;

    let data = fs::read(buffer_file)
        .with_context(|| format!("Failed to read input file: {}", buffer_file.display()))?;

    let pubkey = get_cached_pubkey(&alias).ok().flatten().unwrap_or_default();

    let repo_path = auths_id::storage::layout::resolve_repo_path(None).ok();

    let ctx = build_signing_context(&alias)?;
    let mut params = CommitSigningParams::new(&alias, namespace, data).with_pubkey(pubkey);
    if let Some(path) = repo_path {
        params = params.with_repo_path(path);
    }

    let signature_pem = CommitSigningWorkflow::execute(&ctx, params, chrono::Utc::now())
        .map_err(anyhow::Error::new)?;

    let sig_path = format!("{}.sig", buffer_file.display());
    fs::write(&sig_path, &signature_pem)
        .with_context(|| format!("Failed to write signature to: {}", sig_path))?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use auths_core::crypto::ssh::construct_sshsig_signed_data;
    use auths_crypto::Pkcs8Der;

    #[test]
    fn test_args_accepts_o_flag() {
        let args = Args::try_parse_from([
            "auths-sign",
            "-Y",
            "verify",
            "-n",
            "git",
            "-f",
            "/tmp/allowed_signers",
            "-I",
            "user@example.com",
            "-s",
            "/tmp/file.sig",
            "-O",
            "verify-time=1700000000",
        ])
        .expect("Args must accept -O verify-time=<ts>");
        assert_eq!(args.verify_options, vec!["verify-time=1700000000"]);
    }

    #[test]
    fn test_args_accepts_multiple_o_flags() {
        let args = Args::try_parse_from([
            "auths-sign",
            "-Y",
            "verify",
            "-n",
            "git",
            "-f",
            "/tmp/allowed_signers",
            "-I",
            "user@example.com",
            "-s",
            "/tmp/file.sig",
            "-O",
            "verify-time=1700000000",
            "-O",
            "print-pubkey",
        ])
        .expect("Args must accept multiple -O flags");
        assert_eq!(
            args.verify_options,
            vec!["verify-time=1700000000", "print-pubkey"]
        );
    }

    #[test]
    fn test_args_o_flag_absent_defaults_to_empty() {
        let args = Args::try_parse_from(["auths-sign", "-Y", "sign", "-f", "auths:main"])
            .expect("Args without -O should parse fine");
        assert!(args.verify_options.is_empty());
    }

    #[test]
    fn test_args_find_principals_no_namespace() {
        let args = Args::try_parse_from([
            "auths-sign",
            "-Y",
            "find-principals",
            "-f",
            "/tmp/allowed_signers",
            "-s",
            "/tmp/file.sig",
            "-Overify-time=20260218012319",
        ])
        .expect("find-principals without -n must parse");
        assert_eq!(args.operation, "find-principals");
        assert!(args.namespace.is_none());
        assert_eq!(args.verify_options, vec!["verify-time=20260218012319"]);
    }

    #[test]
    fn test_args_check_novalidate_no_file() {
        let args = Args::try_parse_from([
            "auths-sign",
            "-Y",
            "check-novalidate",
            "-n",
            "git",
            "-s",
            "/tmp/file.sig",
            "-Overify-time=20260218012319",
        ])
        .expect("check-novalidate without -f must parse");
        assert_eq!(args.operation, "check-novalidate");
        assert!(args.file_arg.is_none());
        assert_eq!(args.verify_options, vec!["verify-time=20260218012319"]);
    }

    #[test]
    fn test_parse_key_identifier_valid() {
        assert_eq!(parse_key_identifier("auths:default").unwrap(), "default");
        assert_eq!(
            parse_key_identifier("auths:my-key-alias").unwrap(),
            "my-key-alias"
        );
    }

    #[test]
    fn test_parse_key_identifier_invalid() {
        assert!(parse_key_identifier("/path/to/key").is_err());
        assert!(parse_key_identifier("ssh-ed25519 AAAA...").is_err());
        assert!(parse_key_identifier("auths:").is_err());
    }

    #[test]
    fn test_sshsig_format() {
        use auths_core::crypto::ssh::SecureSeed;

        let seed = SecureSeed::new([
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
            0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
            0x1d, 0x1e, 0x1f, 0x20,
        ]);

        let data = b"test data to sign";
        let result = auths_core::crypto::ssh::create_sshsig(&seed, data, "git");

        assert!(result.is_ok(), "SSHSIG creation failed: {:?}", result.err());

        let pem = result.unwrap();
        assert!(pem.starts_with("-----BEGIN SSH SIGNATURE-----"));
        assert!(pem.contains("-----END SSH SIGNATURE-----"));
    }

    #[test]
    fn test_encode_ssh_pubkey() {
        use auths_core::crypto::ssh::encode_ssh_pubkey;

        let pubkey = [0x42u8; 32];
        let blob = encode_ssh_pubkey(&pubkey);

        assert_eq!(&blob[0..4], &11u32.to_be_bytes());
        assert_eq!(&blob[4..15], b"ssh-ed25519");
        assert_eq!(&blob[15..19], &32u32.to_be_bytes());
        assert_eq!(&blob[19..51], &[0x42; 32]);
    }

    #[test]
    fn test_construct_sshsig_signed_data() {
        let data = b"test";
        let result = construct_sshsig_signed_data(data, "git");
        assert!(result.is_ok());

        let blob = result.unwrap();

        assert_ne!(
            &blob[0..4],
            &6u32.to_be_bytes(),
            "SSHSIG magic must not have a uint32 length prefix"
        );
        assert_eq!(
            &blob[0..6],
            b"SSHSIG",
            "First 6 bytes must be literal SSHSIG"
        );
        assert_eq!(&blob[6..10], &3u32.to_be_bytes());
        assert_eq!(&blob[10..13], b"git");
        assert_eq!(&blob[13..17], &0u32.to_be_bytes());
        assert_eq!(&blob[17..21], &6u32.to_be_bytes());
        assert_eq!(&blob[21..27], b"sha512");
        assert_eq!(&blob[27..31], &64u32.to_be_bytes());
        assert_eq!(blob.len(), 31 + 64);
    }

    #[test]
    fn test_extract_seed_from_pkcs8_ring_generated_key() {
        use auths_core::crypto::ssh::extract_seed_from_pkcs8;
        use ring::rand::SystemRandom;
        use ring::signature::{Ed25519KeyPair, KeyPair};

        let rng = SystemRandom::new();
        let pkcs8_doc = Ed25519KeyPair::generate_pkcs8(&rng)
            .expect("ring must generate a valid PKCS#8 document");
        let pkcs8 = Pkcs8Der::new(pkcs8_doc.as_ref());

        let result = extract_seed_from_pkcs8(&pkcs8);
        assert!(
            result.is_ok(),
            "extract_seed_from_pkcs8 must succeed on a ring-generated key, got: {:?}",
            result.err()
        );

        let seed = result.unwrap();
        assert_eq!(seed.as_bytes().len(), 32, "seed must be exactly 32 bytes");

        let derived = Ed25519KeyPair::from_seed_unchecked(seed.as_bytes())
            .expect("extracted seed must be valid");
        let original = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).expect("original key must parse");
        assert_eq!(
            derived.public_key().as_ref(),
            original.public_key().as_ref(),
            "seed must reproduce the original public key"
        );
    }

    #[test]
    fn test_extract_seed_from_pkcs8_rejects_invalid_input() {
        use auths_core::crypto::ssh::extract_seed_from_pkcs8;

        let bad_input = Pkcs8Der::new(vec![0u8; 50]);
        let result = extract_seed_from_pkcs8(&bad_input);
        assert!(result.is_err(), "must reject non-PKCS#8 input");
    }
}