1
0
Fork 0
mirror of https://codeberg.org/tyy/aspm synced 2024-12-23 01:19:28 -07:00

Add success text to import gpg

This commit is contained in:
Tyler Beckman 2023-09-28 13:13:57 -06:00
parent 91a4a42fe6
commit 4f83bb9a3f
Signed by: Ty
GPG key ID: 2813440C772555A4

View file

@ -9,6 +9,7 @@ use data_encoding::{BASE64URL_NOPAD, BASE64_NOPAD};
use dialoguer::{theme::ColorfulTheme, Password}; use dialoguer::{theme::ColorfulTheme, Password};
use elliptic_curve::sec1::{Coordinates, ToEncodedPoint}; use elliptic_curve::sec1::{Coordinates, ToEncodedPoint};
use gpgme::{Context as GpgContext, PassphraseRequest}; use gpgme::{Context as GpgContext, PassphraseRequest};
use indoc::printdoc;
use pgp::{ use pgp::{
crypto::ecc_curve::ECCCurve, crypto::ecc_curve::ECCCurve,
types::{EcdsaPublicParams, KeyTrait, PlainSecretParams, PublicParams, SecretParams}, types::{EcdsaPublicParams, KeyTrait, PlainSecretParams, PublicParams, SecretParams},
@ -38,9 +39,7 @@ impl AspmSubcommand for KeysImportGpgCommand {
.secret_keys() .secret_keys()
.context("Unable to fetch GPG secret keys")? .context("Unable to fetch GPG secret keys")?
{ {
let Ok(key) = key else { let Ok(key) = key else { continue };
continue
};
if key.fingerprint().unwrap_or("") != self.fingerprint if key.fingerprint().unwrap_or("") != self.fingerprint
&& key && key
@ -54,9 +53,9 @@ impl AspmSubcommand for KeysImportGpgCommand {
break; break;
} }
let Some(found_key) = found_key else { let Some(found_key) = found_key else {
eprintln!("No key was found matching the provided fingerprint"); eprintln!("No key was found matching the provided fingerprint");
std::process::exit(1); std::process::exit(1);
}; };
// For some reason, when exporting secret keys, GPG will prompt for a password but then still // For some reason, when exporting secret keys, GPG will prompt for a password but then still
// export the key in an encrypted form. In order to prevent a password needing to be entered // export the key in an encrypted form. In order to prevent a password needing to be entered
@ -104,8 +103,8 @@ impl AspmSubcommand for KeysImportGpgCommand {
let parsed = parsed.next().context("Invalid GPG data")?; let parsed = parsed.next().context("Invalid GPG data")?;
let Ok(key) = parsed else { let Ok(key) = parsed else {
bail!("GPG data was unable to be parsed"); bail!("GPG data was unable to be parsed");
}; };
let key = key.into_secret(); let key = key.into_secret();
let Some(uid) = key let Some(uid) = key
@ -113,16 +112,17 @@ impl AspmSubcommand for KeysImportGpgCommand {
.users .users
.iter() .iter()
.find(|uid| uid.is_primary()) .find(|uid| uid.is_primary())
.or_else(|| { .or_else(|| {
if key.details.users.len() == 1 { if key.details.users.len() == 1 {
Some(&key.details.users[0]) Some(&key.details.users[0])
} else { } else {
None None
} }
}) else { })
eprintln!("Key being imported has no primary uid. This must be set, as it is used for the key alias."); else {
std::process::exit(1); eprintln!("Key being imported has no primary uid. This must be set, as it is used for the key alias.");
}; std::process::exit(1);
};
let (algorithm, public_params, secret_params) = { let (algorithm, public_params, secret_params) = {
if key if key
.primary_key .primary_key
@ -193,8 +193,8 @@ impl AspmSubcommand for KeysImportGpgCommand {
let encoded = public_key.to_encoded_point(false); let encoded = public_key.to_encoded_point(false);
let Coordinates::Uncompressed { x, y } = encoded.coordinates() else { let Coordinates::Uncompressed { x, y } = encoded.coordinates() else {
bail!("EC key coordinates were not uncompressed") bail!("EC key coordinates were not uncompressed")
}; };
map.insert("x".to_string(), BASE64URL_NOPAD.encode(x.as_slice()).into()); map.insert("x".to_string(), BASE64URL_NOPAD.encode(x.as_slice()).into());
map.insert("y".to_string(), BASE64URL_NOPAD.encode(y.as_slice()).into()); map.insert("y".to_string(), BASE64URL_NOPAD.encode(y.as_slice()).into());
map map
@ -255,7 +255,7 @@ impl AspmSubcommand for KeysImportGpgCommand {
let entry = keys::ActiveModel { let entry = keys::ActiveModel {
fingerprint: ActiveValue::Set(asp_key.fingerprint.clone()), fingerprint: ActiveValue::Set(asp_key.fingerprint.clone()),
key_type: ActiveValue::Set(asp_key.key_type.into()), key_type: ActiveValue::Set(asp_key.key_type.clone().into()),
alias: ActiveValue::Set(format!("{uid}", uid = uid.id.id())), alias: ActiveValue::Set(format!("{uid}", uid = uid.id.id())),
encrypted: ActiveValue::Set(encrypted), encrypted: ActiveValue::Set(encrypted),
}; };
@ -267,6 +267,16 @@ impl AspmSubcommand for KeysImportGpgCommand {
bail!("The key was unable to be saved to the database") bail!("The key was unable to be saved to the database")
} }
printdoc! {
"
Successfully imported key!
ASP Fingerprint: {fpr}
Type: {type:?}
",
fpr = asp_key.fingerprint,
r#type = asp_key.key_type
};
Ok(()) Ok(())
} }
} }