pass_manager/src/models.rs

52 lines
1.4 KiB
Rust
Raw Normal View History

use crate::entity::account;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct DecryptedAccount {
pub name: String,
pub login: String,
pub password: String,
}
impl DecryptedAccount {
2023-05-09 17:27:58 +00:00
/// Constructs DecryptedAccount by decrypting the provided account
pub fn from_account(account: account::Model, master_pass: &str) -> crate::Result<Self> {
let name = account.name.clone();
let (login, password) = account.decrypt(master_pass)?;
Ok(Self {
name,
login,
password,
})
}
2023-05-09 17:27:58 +00:00
/// Constructs ActiveModel with eath field Set by encrypting `self`
pub fn into_account(
self,
user_id: u64,
master_pass: &str,
) -> crate::Result<account::ActiveModel> {
let (name, login, password) = (self.name, self.login, self.password);
account::ActiveModel::from_unencrypted(user_id, name, &login, &password, master_pass)
}
2023-05-09 17:27:58 +00:00
/// Returns true if the account's fields are valid
#[inline]
pub fn validate(&self) -> bool {
for string in [&self.name, &self.login, &self.password] {
let is_invalid = string
.chars()
.any(|char| char == '`' || char == '\\' || char == '\n');
if is_invalid {
return false;
}
}
true
}
}
#[derive(Serialize, Deserialize)]
pub struct User {
pub accounts: Vec<DecryptedAccount>,
}