| 1 |
//! Common password-related functionality such as prompting. |
| 2 |
|
| 3 |
use openpgp::crypto::Password; |
| 4 |
use openpgp::Result; |
| 5 |
use sequoia_openpgp as openpgp; |
| 6 |
|
| 7 |
use crate::Sq; |
| 8 |
|
| 9 |
/// Prompt to repeat a password. |
| 10 |
const REPEAT_PROMPT: &str = "Please repeat the password"; |
| 11 |
|
| 12 |
|
| 13 |
/// Wraps `rpassword::prompt_password`. |
| 14 |
fn prompt_password(sq: &Sq, prompt: impl ToString) -> Result<String> { |
| 15 |
if sq.batch { |
| 16 |
Err(anyhow::anyhow!("cannot prompt for password in batch mode")) |
| 17 |
} else { |
| 18 |
Ok(rpassword::prompt_password(prompt)?) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
/// Prompts the user to enter a new password. |
| 24 |
/// |
| 25 |
/// This function is intended for creating artifacts. For example, if |
| 26 |
/// a new key or subkey is generated, or a message should be encrypted |
| 27 |
/// using a password. The cost of mistyping is high, so we prompt |
| 28 |
/// twice. |
| 29 |
/// |
| 30 |
/// If the two entered passwords match, the result is returned. |
| 31 |
/// |
| 32 |
/// If the passwords differ or no password was entered, an error |
| 33 |
/// message is printed and the process is repeated. |
| 34 |
pub fn prompt_for_new(sq: &Sq, reason: &str) -> Result<Password> { |
| 35 |
prompt_for_new_internal(sq, reason, false) |
| 36 |
.map(|p| p.expect("is not None")) |
| 37 |
} |
| 38 |
|
| 39 |
/// Prompts the user to enter an optional new password. |
| 40 |
/// |
| 41 |
/// This function is intended for creating artifacts. For example, if |
| 42 |
/// a new key or subkey is generated, or a message should be encrypted |
| 43 |
/// using a password. The cost of mistyping is high, so we prompt |
| 44 |
/// twice. |
| 45 |
/// |
| 46 |
/// If the two entered passwords match, the result is returned. If no |
| 47 |
/// password was entered, `Ok(None)` is returned. |
| 48 |
/// |
| 49 |
/// If the passwords differ, an error message is printed and the |
| 50 |
/// process is repeated. |
| 51 |
pub fn prompt_for_new_or_none(sq: &Sq, reason: &str) |
| 52 |
-> Result<Option<Password>> |
| 53 |
{ |
| 54 |
prompt_for_new_internal(sq, reason, true) |
| 55 |
} |
| 56 |
|
| 57 |
/// Prompts the user to enter a new password. |
| 58 |
/// |
| 59 |
/// This function is intended for creating artifacts. For example, if |
| 60 |
/// a new key or subkey is generated, or a message should be encrypted |
| 61 |
/// using a password. The cost of mistyping is high, so we prompt |
| 62 |
/// twice. |
| 63 |
/// |
| 64 |
/// If the two entered passwords match, the result is returned. If |
| 65 |
/// `allow_none` is `true`, and no password was entered, `Ok(None)` is |
| 66 |
/// returned. |
| 67 |
/// |
| 68 |
/// If the passwords differ or `allow_none` is `false` and no password |
| 69 |
/// was entered, an error message is printed and the process is |
| 70 |
/// repeated. |
| 71 |
fn prompt_for_new_internal( |
| 72 |
sq: &Sq, |
| 73 |
reason: &str, |
| 74 |
allow_none: bool, |
| 75 |
) -> Result<Option<Password>> { |
| 76 |
let mut prompt = format!("Please enter the password to protect {}", reason); |
| 77 |
if allow_none { |
| 78 |
prompt.push_str(" (press enter to not use a password)"); |
| 79 |
} |
| 80 |
|
| 81 |
let width = prompt.len().max(REPEAT_PROMPT.len()); |
| 82 |
let p0 = format!("{:>1$}: ", prompt, width); |
| 83 |
let p1 = format!("{:>1$}: ", REPEAT_PROMPT, width); |
| 84 |
|
| 85 |
loop { |
| 86 |
let password = prompt_password(sq, &p0)?; |
| 87 |
|
| 88 |
if password.is_empty() && ! allow_none { |
| 89 |
weprintln!("Password required. Please try again."); |
| 90 |
weprintln!(); |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
let password_repeat = prompt_password(sq, &p1)?; |
| 95 |
|
| 96 |
if password != password_repeat { |
| 97 |
weprintln!("The passwords do not match. Please try again."); |
| 98 |
weprintln!(); |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
return if password.is_empty() { |
| 103 |
Ok(None) |
| 104 |
} else { |
| 105 |
Ok(Some(password.into())) |
| 106 |
}; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/// Prompts once for a password to unlock an existing object. |
| 111 |
/// |
| 112 |
/// This function is intended for consuming artifacts. For example, |
| 113 |
/// if a key or subkey is locked and must be unlocked, or a message |
| 114 |
/// should be decrypted using a password. |
| 115 |
pub fn prompt_to_unlock(sq: &Sq, reason: &str) -> Result<Password> { |
| 116 |
let prompt = |
| 117 |
format!("Please enter the password to decrypt {}: ", reason); |
| 118 |
let password = prompt_password(sq, &prompt)?; |
| 119 |
Ok(password.into()) |
| 120 |
} |
| 121 |
|
| 122 |
/// Prompts once for a password to unlock an existing object. |
| 123 |
/// |
| 124 |
/// This function is intended for consuming artifacts. For example, |
| 125 |
/// if a key or subkey is locked and must be unlocked, or a message |
| 126 |
/// should be decrypted using a password. |
| 127 |
pub fn prompt_to_unlock_or_cancel(sq: &Sq, reason: &str) |
| 128 |
-> Result<Option<Password>> |
| 129 |
{ |
| 130 |
let password = |
| 131 |
prompt_to_unlock(sq, &format!("{} (blank to skip)", reason))?; |
| 132 |
Ok(if password.map(|p| p.is_empty()) { None } else { Some(password) }) |
| 133 |
} |