romm-cli 0.30.0

Rust-based CLI and TUI for the ROMM API
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Authentication command group (`romm-cli auth ...`).
//!
//! This is intentionally layered on top of the existing config/keyring logic in
//! `src/config.rs` so users can rotate credentials without re-entering the ROM
//! path / base URL.

use anyhow::{anyhow, Context, Result};
use clap::{Args, Subcommand};
use dialoguer::{Input, Password, Select};
use serde_json::json;
use std::fs;
use std::io::Read;

use crate::client::RommClient;
use crate::commands::OutputFormat;
use crate::config::{
    disk_has_unresolved_keyring_sentinel, is_keyring_placeholder, load_config, persist_user_config,
    read_user_config_json_from_disk, user_config_json_path, AuthConfig, Config,
    KEYRING_SECRET_PLACEHOLDER,
};
use crate::endpoints::client_tokens::ExchangeClientToken;

/// Top-level `romm-cli auth` command group.
#[derive(Args, Debug, Clone)]
pub struct AuthCommand {
    #[command(subcommand)]
    pub action: AuthAction,
}

/// Specific action within `romm-cli auth`.
#[derive(Subcommand, Debug, Clone)]
pub enum AuthAction {
    /// Set/rotate authentication credentials (Bearer, Basic, API key, or pairing code).
    Login(AuthLoginCommand),
    /// Remove stored authentication (leaves non-auth config untouched).
    Logout,
    /// Show current authentication mode and where it comes from (env/config/keyring).
    Status,
}

/// Options for `romm-cli auth login`.
///
/// If no auth flags are provided, the command runs interactively.
#[derive(Args, Debug, Clone)]
pub struct AuthLoginCommand {
    /// API token (Bearer). Skips interactive prompts.
    #[arg(long)]
    pub token: Option<String>,

    /// Read API token (Bearer) from UTF-8 file. Use '-' for stdin.
    #[arg(long)]
    pub token_file: Option<String>,

    /// Basic auth username.
    #[arg(long)]
    pub username: Option<String>,

    /// Basic auth password (discouraged: visible in process list).
    #[arg(long)]
    pub password: Option<String>,

    /// Read Basic auth password from a UTF-8 file. Use '-' for stdin.
    #[arg(long)]
    pub password_file: Option<String>,

    /// API key header name (e.g. X-API-Key).
    #[arg(long)]
    pub api_key_header: Option<String>,

    /// API key value.
    #[arg(long)]
    pub api_key: Option<String>,

    /// Web UI pairing code (8 characters).
    #[arg(long)]
    pub pairing_code: Option<String>,
}

fn env_nonempty(key: &str) -> Option<String> {
    std::env::var(key)
        .ok()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
}

fn read_secret_from_path_or_stdin(path: &str) -> Result<String> {
    let mut content = String::new();
    if path == "-" {
        std::io::stdin()
            .read_to_string(&mut content)
            .context("read secret from stdin")?;
    } else {
        content =
            fs::read_to_string(path).with_context(|| format!("read secret from file {}", path))?;
    }
    let trimmed = content.trim();
    if trimmed.is_empty() {
        return Err(anyhow!("secret read from {} is empty", path));
    }
    Ok(trimmed.to_string())
}

fn disk_config_or_die() -> Result<Config> {
    read_user_config_json_from_disk().ok_or_else(|| {
        anyhow!(
            "Could not read user config.json. Run `romm-cli init` first (or ensure your config exists)."
        )
    })
}

fn preserve_non_auth_fields_for_persist() -> Result<(String, String, bool, std::path::PathBuf)> {
    let disk = disk_config_or_die()?;
    let config_path =
        user_config_json_path().ok_or_else(|| anyhow!("Could not resolve config path"))?;
    Ok((
        disk.base_url,
        disk.download_dir,
        disk.use_https,
        config_path,
    ))
}

async fn persist_auth_from_login(auth: Option<AuthConfig>, client: &RommClient) -> Result<()> {
    let (base_url, download_dir, use_https, config_path) = preserve_non_auth_fields_for_persist()?;

    // Compute the human-readable auth mode before persisting, since `auth` is moved.
    let mode = match &auth {
        None => "none",
        Some(AuthConfig::Basic { .. }) => "basic",
        Some(AuthConfig::Bearer { .. }) => "bearer",
        Some(AuthConfig::ApiKey { .. }) => "api-key",
    };

    persist_user_config(&base_url, &download_dir, use_https, auth)?;

    if config_path.exists() {
        println!("Auth updated: {mode} (wrote {})", config_path.display());
    } else {
        // Should not happen (persist_user_config creates the directory), but keep it safe.
        println!("Auth updated: {mode}");
    }

    // Keep the client reference "used" in this helper for future extensions
    // (e.g. verification) without changing function signature.
    let _ = client.verbose();
    Ok(())
}

async fn login_interactive(cmd: &AuthLoginCommand, client: &RommClient) -> Result<AuthConfig> {
    // If any login flags are present, we do not consider it "interactive".
    let has_flags = cmd.token.is_some()
        || cmd.token_file.is_some()
        || cmd.username.is_some()
        || cmd.password.is_some()
        || cmd.password_file.is_some()
        || cmd.api_key_header.is_some()
        || cmd.api_key.is_some()
        || cmd.pairing_code.is_some();
    if has_flags {
        return Err(anyhow!(
            "internal error: interactive auth called with flags present"
        ));
    }

    let items = vec![
        "Basic (username + password)",
        "API Token (Bearer)",
        "API key in custom header",
        "Pair with Web UI (8-character code)",
    ];
    let idx = Select::new()
        .with_prompt("Authentication")
        .items(&items)
        .default(1)
        .interact()?;

    match idx {
        0 => {
            let username: String = Input::new().with_prompt("Username").interact_text()?;
            let password = Password::new().with_prompt("Password").interact()?;
            Ok(AuthConfig::Basic {
                username: username.trim().to_string(),
                password,
            })
        }
        1 => {
            let token = Password::new().with_prompt("API Token").interact()?;
            Ok(AuthConfig::Bearer { token })
        }
        2 => {
            let header: String = Input::new()
                .with_prompt("Header name (e.g. X-API-Key)")
                .interact_text()?;
            let key = Password::new().with_prompt("API key value").interact()?;
            Ok(AuthConfig::ApiKey {
                header: header.trim().to_string(),
                key,
            })
        }
        3 => {
            let code: String = Input::new()
                .with_prompt("8-character pairing code")
                .interact_text()?;

            // Pairing-code exchange should not depend on the current auth mode
            // (because we are rotating it). Use an unauthenticated client.
            let disk = disk_config_or_die()?;
            let temp_config = Config {
                base_url: disk.base_url,
                download_dir: disk.download_dir,
                use_https: disk.use_https,
                auth: None,
            };
            let unauth_client = RommClient::new(&temp_config, client.verbose())?;

            let endpoint = ExchangeClientToken { code };
            let response = unauth_client
                .call(&endpoint)
                .await
                .context("failed to exchange pairing code")?;

            Ok(AuthConfig::Bearer {
                token: response.raw_token,
            })
        }
        _ => Err(anyhow!("unreachable login auth choice")),
    }
}

fn env_hint_auth_mode() -> Option<&'static str> {
    // Mirrors `load_config` auth precedence order at a high level.
    if env_nonempty("API_USERNAME").is_some() || env_nonempty("API_PASSWORD").is_some() {
        return Some("basic");
    }
    if env_nonempty("API_KEY").is_some() || env_nonempty("API_KEY_HEADER").is_some() {
        return Some("api-key");
    }
    if env_nonempty("API_TOKEN").is_some()
        || env_nonempty("ROMM_TOKEN_FILE").is_some()
        || env_nonempty("API_TOKEN_FILE").is_some()
    {
        return Some("bearer");
    }
    None
}

fn disk_secret_unresolved_placeholder(auth: &Option<AuthConfig>) -> bool {
    match auth {
        None => false,
        Some(AuthConfig::Basic { password, .. }) => is_keyring_placeholder(password),
        Some(AuthConfig::Bearer { token }) => is_keyring_placeholder(token),
        Some(AuthConfig::ApiKey { key, .. }) => is_keyring_placeholder(key),
    }
}

fn auth_mode_string(auth: &Option<AuthConfig>) -> &'static str {
    match auth {
        None => "none",
        Some(AuthConfig::Basic { .. }) => "basic",
        Some(AuthConfig::Bearer { .. }) => "bearer",
        Some(AuthConfig::ApiKey { .. }) => "api-key",
    }
}

/// Execute `romm-cli auth ...`.
pub async fn handle(cmd: AuthCommand, client: &RommClient, format: OutputFormat) -> Result<()> {
    match cmd.action {
        AuthAction::Login(login) => {
            // Non-interactive fast path: infer auth from provided flags.
            let has_flags = login.token.is_some()
                || login.token_file.is_some()
                || login.username.is_some()
                || login.password.is_some()
                || login.password_file.is_some()
                || login.api_key_header.is_some()
                || login.api_key.is_some()
                || login.pairing_code.is_some();

            let auth = if has_flags {
                // Enforce "single auth mode" for predictable behavior.
                let mut modes = Vec::new();
                if login.pairing_code.is_some() {
                    modes.push("pairing-code");
                }
                if login.token.is_some() || login.token_file.is_some() {
                    modes.push("bearer");
                }
                if login.username.is_some()
                    || login.password.is_some()
                    || login.password_file.is_some()
                {
                    modes.push("basic");
                }
                if login.api_key_header.is_some() || login.api_key.is_some() {
                    modes.push("api-key");
                }

                if modes.is_empty() {
                    return Err(anyhow!("no authentication fields found"));
                }
                if modes.len() != 1 {
                    return Err(anyhow!(
                        "Specify exactly one authentication mode, got: {}",
                        modes.join(", ")
                    ));
                }

                if let Some(code) = login.pairing_code {
                    let disk = disk_config_or_die()?;
                    let temp_config = Config {
                        base_url: disk.base_url,
                        download_dir: disk.download_dir,
                        use_https: disk.use_https,
                        auth: None,
                    };
                    let unauth_client = RommClient::new(&temp_config, client.verbose())?;
                    let endpoint = ExchangeClientToken { code };
                    let response = unauth_client
                        .call(&endpoint)
                        .await
                        .context("failed to exchange pairing code")?;
                    AuthConfig::Bearer {
                        token: response.raw_token,
                    }
                } else if login.token.is_some() || login.token_file.is_some() {
                    let token = match (login.token, login.token_file) {
                        (Some(_), Some(_)) => {
                            return Err(anyhow!(
                                "Provide either --token or --token-file, not both"
                            ));
                        }
                        (Some(t), None) => t,
                        (None, Some(f)) => read_secret_from_path_or_stdin(&f)?,
                        (None, None) => unreachable!("checked by flags"),
                    };
                    AuthConfig::Bearer { token }
                } else if login.api_key_header.is_some() || login.api_key.is_some() {
                    let header = login.api_key_header.ok_or_else(|| {
                        anyhow!("--api-key-header is required when using --api-key")
                    })?;
                    let key = login.api_key.ok_or_else(|| {
                        anyhow!("--api-key is required when using --api-key-header")
                    })?;
                    AuthConfig::ApiKey {
                        header: header.trim().to_string(),
                        key,
                    }
                } else {
                    // Basic
                    let username = login
                        .username
                        .ok_or_else(|| anyhow!("--username is required for basic auth"))?;
                    let password = match (login.password, login.password_file) {
                        (Some(p), None) => p,
                        (None, Some(f)) => read_secret_from_path_or_stdin(&f)?,
                        (None, None) => {
                            return Err(anyhow!(
                                "--password or --password-file is required for basic auth"
                            ))
                        }
                        (Some(_), Some(_)) => {
                            return Err(anyhow!(
                                "Provide either --password or --password-file, not both"
                            ))
                        }
                    };
                    AuthConfig::Basic {
                        username: username.trim().to_string(),
                        password,
                    }
                }
            } else {
                login_interactive(&login, client).await?
            };

            persist_auth_from_login(Some(auth), client).await?;
            Ok(())
        }

        AuthAction::Logout => {
            persist_auth_from_login(None, client).await?;
            Ok(())
        }

        AuthAction::Status => {
            let effective = load_config()?;
            let disk = read_user_config_json_from_disk();

            let effective_mode = auth_mode_string(&effective.auth);
            let disk_auth = disk.as_ref().and_then(|c| c.auth.clone());
            let disk_mode = auth_mode_string(&disk_auth);
            let disk_unresolved = disk_secret_unresolved_placeholder(&disk_auth);
            let unresolved_keyring_sentinel = disk_has_unresolved_keyring_sentinel(&effective);

            let env_mode = env_hint_auth_mode();
            let env_hints = json!({
                "API_USERNAME_set": std::env::var("API_USERNAME").ok().map(|v| !v.trim().is_empty()).unwrap_or(false),
                "API_PASSWORD_set": std::env::var("API_PASSWORD").ok().map(|v| !v.trim().is_empty()).unwrap_or(false),
                "API_TOKEN_set": std::env::var("API_TOKEN").ok().map(|v| !v.trim().is_empty()).unwrap_or(false),
                "ROMM_TOKEN_FILE_set": std::env::var("ROMM_TOKEN_FILE").ok().map(|v| !v.trim().is_empty()).unwrap_or(false),
                "API_TOKEN_FILE_set": std::env::var("API_TOKEN_FILE").ok().map(|v| !v.trim().is_empty()).unwrap_or(false),
                "API_KEY_HEADER_set": std::env::var("API_KEY_HEADER").ok().map(|v| !v.trim().is_empty()).unwrap_or(false),
                "API_KEY_set": std::env::var("API_KEY").ok().map(|v| !v.trim().is_empty()).unwrap_or(false),
            });

            match format {
                OutputFormat::Json => {
                    let out = json!({
                        "effective": { "mode": effective_mode },
                        "disk": {
                            "mode": disk_mode,
                            "secret_unresolved_in_keyring": disk_unresolved,
                        },
                        "env_hint_mode": env_mode,
                        "env": env_hints,
                        "keyring_resolution_status": {
                            "unresolved_keyring_sentinel": unresolved_keyring_sentinel,
                        }
                    });
                    println!("{}", serde_json::to_string_pretty(&out)?);
                }
                OutputFormat::Text => {
                    println!("Auth (effective): {effective_mode}");
                    println!("Auth (disk): {disk_mode}");
                    if disk_auth.is_some() {
                        println!(
                            "Disk secret unresolved sentinel: {}",
                            if disk_unresolved { "yes" } else { "no" }
                        );
                    } else {
                        println!("Disk config: not found");
                    }
                    if unresolved_keyring_sentinel {
                        println!(
                            "Keyring lookup failed: config contains `{}` but effective auth is missing.",
                            KEYRING_SECRET_PLACEHOLDER
                        );
                    }
                    if let Some(m) = env_mode {
                        println!("Env auth hint (not showing secrets): {m}");
                    } else {
                        println!("Env auth hint: none");
                    }
                }
            }
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::{Cli, Commands};
    use clap::Parser;

    struct TestEnv {
        dir: std::path::PathBuf,
        _guard: std::sync::MutexGuard<'static, ()>,
    }

    impl TestEnv {
        fn new() -> Self {
            let guard = crate::config::test_env_lock()
                .lock()
                .unwrap_or_else(|e| e.into_inner());

            let mut unique = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
                .to_string();
            unique.push_str("-auth");

            let dir = std::env::temp_dir().join(format!("romm-cli-auth-test-{}", unique));
            let _ = std::fs::remove_dir_all(&dir);
            std::fs::create_dir_all(&dir).expect("create test config dir");

            clear_env();
            std::env::set_var("ROMM_TEST_CONFIG_DIR", &dir);
            Self { dir, _guard: guard }
        }
    }

    impl Drop for TestEnv {
        fn drop(&mut self) {
            clear_env();
            let _ = std::fs::remove_dir_all(&self.dir);
            std::env::remove_var("ROMM_TEST_CONFIG_DIR");
        }
    }

    fn clear_env() {
        for key in [
            "ROMM_TEST_CONFIG_DIR",
            "API_BASE_URL",
            "ROMM_ROMS_DIR",
            "ROMM_DOWNLOAD_DIR",
            "API_USE_HTTPS",
            "API_USERNAME",
            "API_PASSWORD",
            "API_TOKEN",
            "ROMM_TOKEN_FILE",
            "API_TOKEN_FILE",
            "API_KEY",
            "API_KEY_HEADER",
        ] {
            std::env::remove_var(key);
        }
    }

    fn write_disk_config(path: &std::path::Path, disk_auth: Option<AuthConfig>) {
        fs::create_dir_all(path).unwrap();
        let cfg = Config {
            base_url: "https://disk.example".to_string(),
            download_dir: "/disk/dl".to_string(),
            use_https: true,
            auth: disk_auth,
        };
        let content = serde_json::to_string_pretty(&cfg).unwrap();
        fs::write(path.join("config.json"), content).unwrap();
    }

    #[test]
    fn parse_auth_logout() {
        let cli = Cli::parse_from(["romm-cli", "auth", "logout"]);
        let Commands::Auth(cmd) = cli.command else {
            panic!("expected auth command");
        };
        assert!(matches!(cmd.action, AuthAction::Logout));
    }

    #[test]
    fn auth_status_unresolved_sentinel_detected_from_disk() {
        let env = TestEnv::new();
        write_disk_config(
            &env.dir,
            Some(AuthConfig::Bearer {
                token: KEYRING_SECRET_PLACEHOLDER.to_string(),
            }),
        );

        let effective = Config {
            base_url: String::new(),
            download_dir: String::new(),
            use_https: true,
            auth: None,
        };

        assert!(disk_has_unresolved_keyring_sentinel(&effective));
    }

    #[test]
    fn auth_login_preserves_disk_non_auth_fields_even_with_env_overrides() {
        let env = TestEnv::new();
        write_disk_config(&env.dir, None);

        std::env::set_var("API_BASE_URL", "https://env.example");
        std::env::set_var("ROMM_ROMS_DIR", "/env/dl");
        std::env::set_var("API_USE_HTTPS", "false");

        // Use the sentinel value to avoid keyring interaction in tests.
        let disk_auth = Some(AuthConfig::Bearer {
            token: KEYRING_SECRET_PLACEHOLDER.to_string(),
        });

        let tmp_client = RommClient::new(
            &Config {
                base_url: "https://dummy.example".to_string(),
                download_dir: "/tmp".to_string(),
                use_https: true,
                auth: None,
            },
            false,
        )
        .unwrap();

        // This helper is async; execute with a runtime.
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(persist_auth_from_login(disk_auth, &tmp_client))
            .unwrap();

        let saved = read_user_config_json_from_disk().unwrap();
        assert_eq!(saved.base_url, "https://disk.example");
        assert_eq!(saved.download_dir, "/disk/dl");
        assert!(saved.use_https);
        match saved.auth {
            Some(AuthConfig::Bearer { token }) => {
                assert!(is_keyring_placeholder(&token));
            }
            _ => panic!("expected bearer auth on disk"),
        }
    }

    #[test]
    fn auth_logout_clears_auth_but_preserves_non_auth_fields() {
        let env = TestEnv::new();
        write_disk_config(
            &env.dir,
            Some(AuthConfig::Bearer {
                token: "some-token".to_string(),
            }),
        );

        let tmp_client = RommClient::new(
            &Config {
                base_url: "https://dummy.example".to_string(),
                download_dir: "/tmp".to_string(),
                use_https: true,
                auth: None,
            },
            false,
        )
        .unwrap();

        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(persist_auth_from_login(None, &tmp_client))
            .unwrap();

        let saved = read_user_config_json_from_disk().unwrap();
        assert_eq!(saved.base_url, "https://disk.example");
        assert_eq!(saved.download_dir, "/disk/dl");
        assert!(saved.use_https);
        assert!(saved.auth.is_none());
    }
}