pass_manager/src/models.rs

60 lines
1.5 KiB
Rust
Raw Normal View History

2023-05-15 16:09:15 +00:00
//! Models to export and import the accounts
use crate::entity::prelude::*;
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
2023-05-15 16:09:15 +00:00
#[inline]
pub fn from_account(account: account::Model, master_pass: &str) -> crate::Result<Self> {
let (login, password) = account.decrypt(master_pass)?;
Ok(Self {
2023-05-15 16:09:15 +00:00
name: account.name,
login,
password,
})
}
2023-05-09 17:27:58 +00:00
/// Constructs ActiveModel with eath field Set by encrypting `self`
2023-05-15 16:09:15 +00:00
#[inline]
pub fn into_account(
self,
user_id: u64,
master_pass: &str,
) -> crate::Result<account::ActiveModel> {
2023-05-15 16:09:15 +00:00
account::ActiveModel::from_unencrypted(
user_id,
self.name,
&self.login,
&self.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>,
}