Skip to content

Commit

Permalink
feat: add function to add a new trusted key
Browse files Browse the repository at this point in the history
  • Loading branch information
gotlougit committed Oct 26, 2023
1 parent b6575b7 commit 8d5507e
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ fn get_config_path() -> String {
fn create_config_folder(config_path: &str, public_key: &[u8], private_key: &[u8]) -> Result<()> {
eprintln!("Creating the config folder and default config");
fs::create_dir(config_path)?;
let mut config = Table::new();
let encoded_public_key = general_purpose::STANDARD.encode(public_key);
let encoded_private_key = general_purpose::STANDARD.encode(private_key);
let trusted_keys: Vec<String> = Vec::new();
config.insert("public_key".to_string(), encoded_public_key.into());
config.insert("private_key".to_string(), encoded_private_key.into());
write_config(
config_path,
&encoded_public_key,
&encoded_private_key,
trusted_keys,
)
}

fn write_config(
config_path: &str,
public_key: &str,
private_key: &str,
trusted_keys: Vec<String>,
) -> Result<()> {
let mut config = Table::new();
config.insert("public_key".to_string(), public_key.into());
config.insert("private_key".to_string(), private_key.into());
config.insert("trusted_keys".to_string(), trusted_keys.into());
let rawconf = config.to_string();
let conffilename = config_path.to_string() + "/filetransfer.toml";
Expand All @@ -43,6 +57,25 @@ fn create_config_folder(config_path: &str, public_key: &[u8], private_key: &[u8]
Ok(())
}

pub fn add_trusted_key(config_path: &str, key: Vec<u8>) -> Result<()> {
let mut conf = get_all_vars().unwrap();
conf.trusted_keys.push(key);
let encoded_public_key = general_purpose::STANDARD.encode(conf.public_key);
let encoded_private_key = general_purpose::STANDARD.encode(conf.private_key);
let encoded_trusted_keys: Vec<_> = conf
.trusted_keys
.iter()
.map(|val| general_purpose::STANDARD.encode(val))
.collect();

write_config(
config_path,
&encoded_public_key,
&encoded_private_key,
encoded_trusted_keys,
)
}

pub fn get_all_vars() -> Result<Config> {
let config_path = get_config_path();
if !Path::new(&config_path).exists() {
Expand Down

0 comments on commit 8d5507e

Please sign in to comment.