keyfork-pinentry: use multiline prompts at least for passphrase

This commit is contained in:
Ryan Heywood 2023-11-05 16:21:16 -06:00
parent 2c9d09ea61
commit baa289ce62
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 21 additions and 2 deletions

View File

@ -11,7 +11,8 @@ authors = ["Ryan Heywood <ryan@distrust.co>"]
# categories = ["api-bindings", "command-line-interface"]
# license = "MIT OR Apache-2.0"
license = "MIT"
edition = "2018"
# edition = "2018"
edition = "2021"
[dependencies]
nom = { version = "7", default-features = false }

View File

@ -69,6 +69,24 @@ pub fn default_binary() -> Result<PathBuf> {
which::which("pinentry-curses").map_err(Into::into)
}
fn convert_multiline(line: &str) -> String {
// convert into multiline
let mut converted_line = String::new();
let mut last_end = 0;
for (start, part) in line.match_indices(&['\n', '\r', '%']) {
converted_line.push_str(line.get(last_end..start).unwrap());
converted_line.push_str(match part {
"\n" => "%0A",
"\r" => "%0D",
"%" => "%25",
fb => panic!("expected index given to match_indices, got: {fb}"),
});
last_end = start + part.len();
}
converted_line.push_str(line.get(last_end..line.len()).unwrap());
converted_line
}
/// A dialog for requesting a passphrase from the user.
pub struct PassphraseInput<'a> {
binary: PathBuf,
@ -211,7 +229,7 @@ impl<'a> PassphraseInput<'a> {
pinentry.send_request("SETTITLE", Some(title))?;
}
if let Some(desc) = &self.description {
pinentry.send_request("SETDESC", Some(desc))?;
pinentry.send_request("SETDESC", Some(convert_multiline(desc).as_ref()))?;
}
if let Some(error) = &self.error {
pinentry.send_request("SETERROR", Some(error))?;