Switched from sea-orm to sqlx

This commit is contained in:
2023-11-25 19:29:06 +03:00
parent e4e33f52b1
commit 9f967e82d5
40 changed files with 434 additions and 1110 deletions

View File

@ -19,7 +19,6 @@ rand = { version = "0.8", default-features = false, features = [
"std_rng",
"std",
] }
sea-orm = "0.12"
bitflags = "2"
arrayvec = "0.7"
subtle = "2"

View File

@ -1,8 +1,7 @@
use chacha20poly1305::{AeadCore, AeadInPlace, ChaCha20Poly1305, KeyInit};
use entity::account::{self, ActiveModel};
use entity::account::Account;
use pbkdf2::pbkdf2_hmac_array;
use rand::{rngs::OsRng, RngCore};
use sea_orm::ActiveValue::Set;
use sha2::Sha256;
pub struct Cipher {
@ -62,7 +61,7 @@ impl Decrypted {
///
/// Returns an error if the tag doesn't match the ciphertext or if the decrypted data isn't valid UTF-8
#[inline]
pub fn from_account(mut account: account::Model, master_pass: &str) -> crate::Result<Self> {
pub fn from_account(mut account: Account, master_pass: &str) -> crate::Result<Self> {
let cipher = Cipher::new(master_pass.as_bytes(), &account.salt);
cipher.decrypt(&mut account.enc_login)?;
cipher.decrypt(&mut account.enc_password)?;
@ -77,22 +76,22 @@ impl Decrypted {
/// Constructs `ActiveModel` with eath field Set by encrypting `self`
#[inline]
#[must_use]
pub fn into_account(self, user_id: u64, master_pass: &str) -> account::ActiveModel {
let mut login = self.login.into_bytes();
let mut password = self.password.into_bytes();
pub fn into_account(self, user_id: u64, master_pass: &str) -> Account {
let mut enc_login = self.login.into_bytes();
let mut enc_password = self.password.into_bytes();
let mut salt = vec![0; 64];
OsRng.fill_bytes(&mut salt);
let cipher = Cipher::new(master_pass.as_bytes(), &salt);
cipher.encrypt(&mut login);
cipher.encrypt(&mut password);
cipher.encrypt(&mut enc_login);
cipher.encrypt(&mut enc_password);
ActiveModel {
user_id: Set(user_id),
name: Set(self.name),
salt: Set(salt),
enc_login: Set(login),
enc_password: Set(password),
Account {
user_id,
name: self.name,
salt,
enc_login,
enc_password,
}
}

View File

@ -1,4 +1,4 @@
use entity::master_pass;
use entity::master_pass::MasterPass;
use once_cell::sync::Lazy;
use rand::{rngs::OsRng, RngCore};
use scrypt::{scrypt, Params};
@ -55,9 +55,9 @@ where
}
}
impl<'a> From<&'a master_pass::Model> for HashedBytes<&'a [u8], &'a [u8]> {
impl<'a> From<&'a MasterPass> for HashedBytes<&'a [u8], &'a [u8]> {
#[inline]
fn from(value: &'a master_pass::Model) -> Self {
fn from(value: &'a MasterPass) -> Self {
HashedBytes {
hash: &value.password_hash,
salt: &value.salt,
@ -65,8 +65,8 @@ impl<'a> From<&'a master_pass::Model> for HashedBytes<&'a [u8], &'a [u8]> {
}
}
impl From<master_pass::Model> for HashedBytes<Vec<u8>, Vec<u8>> {
fn from(value: master_pass::Model) -> Self {
impl From<MasterPass> for HashedBytes<Vec<u8>, Vec<u8>> {
fn from(value: MasterPass) -> Self {
Self {
hash: value.password_hash,
salt: value.salt,

View File

@ -1,20 +1,19 @@
use super::hashing::HashedBytes;
use entity::master_pass;
use sea_orm::ActiveValue::Set;
pub trait FromUnencryptedExt {
fn from_unencrypted(user_id: u64, password: &str) -> master_pass::ActiveModel;
fn from_unencrypted(user_id: u64, password: &str) -> master_pass::MasterPass;
}
impl FromUnencryptedExt for master_pass::ActiveModel {
impl FromUnencryptedExt for master_pass::MasterPass {
/// Hashes the password and creates an `ActiveModel` with all fields set to Set variant
#[inline]
fn from_unencrypted(user_id: u64, password: &str) -> Self {
let hash = HashedBytes::new(password.as_bytes());
Self {
user_id: Set(user_id),
password_hash: Set(hash.hash.to_vec()),
salt: Set(hash.salt.to_vec()),
user_id,
password_hash: hash.hash.to_vec(),
salt: hash.salt.to_vec(),
}
}
}