use std::{error::Error, fmt::Display, str::FromStr};
use serde::{ser::SerializeSeq, Deserialize, Serialize, Serializer};
use crate::hidden::Hidden;
#[derive(Clone, Debug, Deserialize)]
#[serde(transparent)]
pub struct SafePassword {
passphrase: Hidden<Vec<u8>>,
}
impl SafePassword {
pub fn reveal(&self) -> &Vec<u8> {
self.passphrase.reveal()
}
pub fn reveal_mut(&mut self) -> &mut Vec<u8> {
self.passphrase.reveal_mut()
}
}
#[derive(Debug)]
pub struct PasswordError;
impl Error for PasswordError {}
impl Display for PasswordError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PasswordError")
}
}
impl FromStr for SafePassword {
type Err = PasswordError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self {
passphrase: Hidden::<Vec<u8>>::hide(String::from(s).into_bytes()),
})
}
}
impl<S: Into<String>> From<S> for SafePassword {
fn from(s: S) -> Self {
Self {
passphrase: Hidden::<Vec<u8>>::hide(s.into().into_bytes()),
}
}
}
impl Serialize for SafePassword {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
let mut seq = serializer.serialize_seq(Some(self.passphrase.reveal().len()))?;
for e in self.passphrase.reveal() {
seq.serialize_element(e)?;
}
seq.end()
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::SafePassword;
#[test]
fn from_strings() {
let password = "password";
let from_str = SafePassword::from_str(password).unwrap();
let from_string = SafePassword::from(password.to_string());
let from_string_ref = SafePassword::from(password);
assert_eq!(from_str.reveal(), from_string.reveal());
assert_eq!(from_string.reveal(), from_string_ref.reveal());
}
#[test]
fn serialization() {
let safe_password = SafePassword::from("password");
let ser = serde_json::to_string(&safe_password).unwrap();
let deser: SafePassword = serde_json::from_str(&ser).unwrap();
assert_eq!(safe_password.reveal(), deser.reveal());
}
}