pass_manager/src/models.rs

63 lines
1.5 KiB
Rust

//! Models to export and import the accounts
use cryptography::prelude::*;
use entity::prelude::*;
use serde::{Deserialize, Serialize};
use crate::utils::validate_field;
#[derive(Serialize, Deserialize)]
pub struct DecryptedAccount {
pub name: String,
pub login: String,
pub password: String,
}
impl DecryptedAccount {
/// Constructs DecryptedAccount by decrypting the provided account
#[inline]
pub fn from_account(account: account::Model, master_pass: &str) -> crate::Result<Self> {
let (login, password) = account.decrypt(master_pass)?;
Ok(Self {
name: account.name,
login,
password,
})
}
/// Constructs ActiveModel with eath field Set by encrypting `self`
#[inline]
pub fn into_account(
self,
user_id: u64,
master_pass: &str,
) -> crate::Result<account::ActiveModel> {
account::ActiveModel::from_unencrypted(
user_id,
self.name,
&self.login,
&self.password,
master_pass,
)
.map_err(Into::into)
}
/// Returns true if the account's fields are valid
#[inline]
pub fn validate(&self) -> bool {
[
self.name.as_str(),
self.login.as_str(),
self.password.as_str(),
]
.into_iter()
.all(validate_field)
}
}
#[derive(Serialize, Deserialize)]
#[repr(transparent)]
pub struct User {
pub accounts: Vec<DecryptedAccount>,
}