Skip to content

Commit d3d3a7b

Browse files
committed
chore: add private key metadata support
1 parent 3f9d1ce commit d3d3a7b

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/dotenvx.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl DotenvxKeyStore {
322322

323323
pub fn find_private_key(&self, public_key: &str) -> Option<String> {
324324
if let Some(key_pair) = self.keys.get(public_key) {
325-
return Some(key_pair.private_key.clone());
325+
return Some(trim_private_key(key_pair.private_key.clone()));
326326
}
327327
None
328328
}
@@ -335,7 +335,7 @@ pub fn get_private_key(
335335
if let Some(public_key_hex) = public_key {
336336
if let Ok(global_store) = DotenvxKeyStore::load_global() {
337337
if let Some(private_key) = global_store.find_private_key(public_key_hex) {
338-
return Ok(private_key);
338+
return Ok(trim_private_key(private_key));
339339
}
340340
}
341341
}
@@ -345,7 +345,7 @@ pub fn get_private_key(
345345
"DOTENV_PRIVATE_KEY".to_string()
346346
};
347347
if let Ok(private_key) = env::var(&env_key_name) {
348-
return Ok(private_key);
348+
return Ok(trim_private_key(private_key));
349349
}
350350
let env_key_prefix = format!("{env_key_name}=");
351351
let dotenv_keys_file = if let Some(profile) = profile_name
@@ -363,14 +363,23 @@ pub fn get_private_key(
363363
.lines()
364364
.find(|line| line.starts_with(&env_key_prefix))
365365
{
366-
return Ok(dotenv_vault[env_key_prefix.len()..]
366+
let private_key = dotenv_vault[env_key_prefix.len()..]
367367
.trim_matches('"')
368-
.to_owned());
368+
.to_owned();
369+
return Ok(trim_private_key(private_key));
369370
}
370371
}
371372
Err("Private key not found".into())
372373
}
373374

375+
fn trim_private_key(private_key_hex: String) -> String {
376+
if private_key_hex.contains("{") {
377+
private_key_hex[0..private_key_hex.find('{').unwrap()].to_string()
378+
} else {
379+
private_key_hex
380+
}
381+
}
382+
374383
// if the encrypted text starts with "encrypted:", it will decrypt it
375384
fn check_and_decrypt(
376385
private_key: &Option<String>,

0 commit comments

Comments
 (0)