use crate::handler::error::LibrageError;
use age::{ssh::Identity as SshIdentity, x25519};
use std::io::{BufRead, BufReader};
use std::path::Path;
pub fn read_recipients_from_file(path: impl AsRef<Path>) -> Result<Vec<String>, LibrageError> {
let file = std::fs::File::open(path).map_err(LibrageError::Io)?;
let reader = BufReader::new(file);
let mut recipients = Vec::new();
for line in reader.lines() {
let line = line.map_err(LibrageError::Io)?;
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
recipients.push(trimmed.to_string());
}
Ok(recipients)
}
pub fn read_identities_from_file(
path: impl AsRef<Path>,
) -> Result<Vec<Box<dyn age::Identity>>, LibrageError> {
let file = std::fs::File::open(path).map_err(LibrageError::Io)?;
let reader = BufReader::new(file);
let mut identities = Vec::new();
let mut current_ssh_block = String::new();
let mut in_ssh_block = false;
for line in reader.lines() {
let line = line.map_err(LibrageError::Io)?;
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
if trimmed.starts_with("-----BEGIN") {
in_ssh_block = true;
current_ssh_block.clear();
current_ssh_block.push_str(&line);
continue;
}
if in_ssh_block {
current_ssh_block.push('\n');
current_ssh_block.push_str(&line);
if trimmed.starts_with("-----END") {
in_ssh_block = false;
match SshIdentity::from_buffer(BufReader::new(current_ssh_block.as_bytes()), None) {
Ok(ssh_id) => match ssh_id {
SshIdentity::Unencrypted(unenc) => {
identities
.push(Box::new(SshIdentity::Unencrypted(unenc))
as Box<dyn age::Identity>);
}
SshIdentity::Encrypted(_) => {
return Err(LibrageError::InvalidSshKey(
"SSH key is encrypted, passphrase required. Use read_identities_from_file_with_passphrase instead."
.into(),
));
}
SshIdentity::Unsupported(_) => {
return Err(LibrageError::InvalidSshKey(
"Unsupported SSH key type".into(),
));
}
},
Err(_) => {
return Err(LibrageError::InvalidSshKey(
"Invalid SSH identity block".into(),
));
}
}
current_ssh_block.clear();
}
continue;
}
if let Ok(id) = trimmed.parse::<x25519::Identity>() {
identities.push(Box::new(id) as Box<dyn age::Identity>);
continue;
}
return Err(LibrageError::InvalidPublicKey(format!(
"Invalid identity: {}",
trimmed
)));
}
if in_ssh_block {
return Err(LibrageError::InvalidSshKey(
"Incomplete SSH identity block".into(),
));
}
Ok(identities)
}
pub fn read_identities_from_file_with_passphrase(
path: impl AsRef<Path>,
passphrase: Option<&str>,
) -> Result<Vec<Box<dyn age::Identity>>, LibrageError> {
let file = std::fs::File::open(path).map_err(LibrageError::Io)?;
let reader = BufReader::new(file);
let mut identities = Vec::new();
let mut current_ssh_block = String::new();
let mut in_ssh_block = false;
for line in reader.lines() {
let line = line.map_err(LibrageError::Io)?;
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
if trimmed.starts_with("-----BEGIN") {
in_ssh_block = true;
current_ssh_block.clear();
current_ssh_block.push_str(&line);
continue;
}
if in_ssh_block {
current_ssh_block.push('\n');
current_ssh_block.push_str(&line);
if trimmed.starts_with("-----END") {
in_ssh_block = false;
let ssh_result =
crate::core::parsers::parse_ssh_identity(¤t_ssh_block, passphrase)?;
identities.push(ssh_result);
current_ssh_block.clear();
}
continue;
}
if let Ok(id) = trimmed.parse::<x25519::Identity>() {
identities.push(Box::new(id) as Box<dyn age::Identity>);
continue;
}
return Err(LibrageError::InvalidPublicKey(format!(
"Invalid identity: {}",
trimmed
)));
}
if in_ssh_block {
return Err(LibrageError::InvalidSshKey(
"Incomplete SSH identity block".into(),
));
}
Ok(identities)
}