2023-05-15 16:09:15 +00:00
|
|
|
//! Models to export and import the accounts
|
|
|
|
|
2023-06-01 11:42:35 +00:00
|
|
|
use cryptography::prelude::*;
|
|
|
|
use entity::prelude::*;
|
2023-05-05 13:45:56 +00:00
|
|
|
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]
|
2023-05-05 13:45:56 +00:00
|
|
|
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,
|
2023-05-05 13:45:56 +00:00
|
|
|
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]
|
2023-05-05 13:45:56 +00:00
|
|
|
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-06-01 11:42:35 +00:00
|
|
|
.map_err(Into::into)
|
2023-05-05 13:45:56 +00:00
|
|
|
}
|
2023-05-07 16:04:48 +00:00
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Returns true if the account's fields are valid
|
2023-05-07 16:04:48 +00:00
|
|
|
#[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
|
|
|
|
}
|
2023-05-05 13:45:56 +00:00
|
|
|
}
|
2023-05-05 18:29:58 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct User {
|
|
|
|
pub accounts: Vec<DecryptedAccount>,
|
|
|
|
}
|