-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.rs
More file actions
61 lines (50 loc) · 1.71 KB
/
Copy pathcommon.rs
File metadata and controls
61 lines (50 loc) · 1.71 KB
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
//! Common utilities and structures for command handling
use anyhow::Result;
use std::env;
use std::io::{self, BufRead};
use crate::config::Config;
/// Shared context for all command handlers
pub struct CommandContext {
pub config: Config,
pub module_path: String,
}
impl CommandContext {
pub fn new(config: Config) -> Result<Self> {
let module_path =
env::var("PKCS11_MODULE").unwrap_or_else(|_| config.get_pkcs11_module().to_string());
tracing::info!("Using PKCS#11 module: {}", module_path);
Ok(CommandContext {
config,
module_path,
})
}
/// Get token label, using CLI value or config default
pub fn token_label(&self, cli_value: Option<String>) -> Result<String> {
self.config
.token_label(cli_value.as_deref())
.ok_or_else(|| anyhow::anyhow!("Token label required"))
}
}
/// Read a PIN from stdin, trimming whitespace
pub fn read_pin_from_stdin() -> Result<String> {
let stdin = io::stdin();
let mut line = String::new();
stdin.lock().read_line(&mut line)?;
Ok(line.trim().to_string())
}
/// Common PIN handling logic
pub fn get_pin(pin: Option<String>, use_stdin: bool, error_msg: &str) -> Result<String> {
if use_stdin {
read_pin_from_stdin()
} else {
pin.ok_or_else(|| anyhow::anyhow!("{}", error_msg))
}
}
/// Get user PIN with consistent error handling
pub fn get_user_pin(pin: Option<String>, use_stdin: bool) -> Result<String> {
get_pin(pin, use_stdin, "User PIN required")
}
/// Get SO PIN with consistent error handling
pub fn get_so_pin(pin: Option<String>, use_stdin: bool) -> Result<String> {
get_pin(pin, use_stdin, "SO PIN required")
}