fix(pin): keep pin if unchanged
Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
1 parent
e1bdb06565
commit
f1b2da51b7
1 file changed
+33
-3
+33
-3
@@ -521,8 +521,9 @@ impl Config2 {
|
|||||||
encrypt_str_or_original(&socks.password, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
encrypt_str_or_original(&socks.password, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
||||||
config.socks = Some(socks);
|
config.socks = Some(socks);
|
||||||
}
|
}
|
||||||
|
let stored = Config::load_::<Config2>("2");
|
||||||
config.unlock_pin =
|
config.unlock_pin =
|
||||||
encrypt_str_or_original(&config.unlock_pin, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
keep_encrypted_storage_if_plaintext_unchanged(&config.unlock_pin, &stored.unlock_pin);
|
||||||
Config::store_(&config, "2");
|
Config::store_(&config, "2");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -541,6 +542,14 @@ impl Config2 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn keep_encrypted_storage_if_plaintext_unchanged(plain: &str, stored: &str) -> String {
|
||||||
|
let (stored_plain, encrypted, _) = decrypt_str_or_original(stored, PASSWORD_ENC_VERSION);
|
||||||
|
if encrypted && stored_plain == plain {
|
||||||
|
return stored.to_owned();
|
||||||
|
}
|
||||||
|
encrypt_str_or_original(plain, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_path<T: serde::Serialize + serde::de::DeserializeOwned + Default + std::fmt::Debug>(
|
pub fn load_path<T: serde::Serialize + serde::de::DeserializeOwned + Default + std::fmt::Debug>(
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
) -> T {
|
) -> T {
|
||||||
@@ -3503,8 +3512,7 @@ mod tests {
|
|||||||
assert!(Config::set(cfg));
|
assert!(Config::set(cfg));
|
||||||
|
|
||||||
let stored = Config::load().enc_id;
|
let stored = Config::load().enc_id;
|
||||||
let (stored_id, encrypted, _) =
|
let (stored_id, encrypted, _) = decrypt_str_or_original(&stored, PASSWORD_ENC_VERSION);
|
||||||
decrypt_str_or_original(&stored, PASSWORD_ENC_VERSION);
|
|
||||||
assert_ne!(stored, original_enc_id);
|
assert_ne!(stored, original_enc_id);
|
||||||
assert!(encrypted);
|
assert!(encrypted);
|
||||||
assert_eq!(stored_id, updated_id);
|
assert_eq!(stored_id, updated_id);
|
||||||
@@ -3512,6 +3520,28 @@ mod tests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_config2_store_keeps_existing_unlock_pin_when_pin_is_unchanged() {
|
||||||
|
let pin = "123456";
|
||||||
|
let original_unlock_pin =
|
||||||
|
encrypt_str_or_original(pin, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
||||||
|
let mut cfg = Config2 {
|
||||||
|
unlock_pin: original_unlock_pin.clone(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
Config::store_(&cfg, "2");
|
||||||
|
let (unlock_pin, decrypted, _) =
|
||||||
|
decrypt_str_or_original(&cfg.unlock_pin, PASSWORD_ENC_VERSION);
|
||||||
|
assert!(decrypted);
|
||||||
|
cfg.unlock_pin = unlock_pin;
|
||||||
|
cfg.nat_type = 1;
|
||||||
|
|
||||||
|
cfg.store();
|
||||||
|
|
||||||
|
let stored = Config::load_::<Config2>("2");
|
||||||
|
assert_eq!(stored.unlock_pin, original_unlock_pin);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_does_not_convert_plaintext_permanent_password_to_storage_format_in_memory() {
|
fn test_set_does_not_convert_plaintext_permanent_password_to_storage_format_in_memory() {
|
||||||
let mut cfg = Config::default();
|
let mut cfg = Config::default();
|
||||||
|
|||||||
Reference in new issue
Block a user