Sepparated the code out into 3 more library crates: cryptography, entity and pass_manager
This commit is contained in:
11
entity/Cargo.toml
Normal file
11
entity/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "entity"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3.28"
|
||||
sea-orm = "0.11.3"
|
||||
|
96
entity/src/account.rs
Normal file
96
entity/src/account.rs
Normal file
@ -0,0 +1,96 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
use futures::Stream;
|
||||
use sea_orm::{entity::prelude::*, QueryOrder, QuerySelect};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "account")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub user_id: u64,
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub name: String,
|
||||
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")]
|
||||
pub salt: Vec<u8>,
|
||||
#[sea_orm(column_type = "VarBinary(256)")]
|
||||
pub enc_login: Vec<u8>,
|
||||
#[sea_orm(column_type = "VarBinary(256)")]
|
||||
pub enc_password: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl Entity {
|
||||
/// Gets all user's account from DB
|
||||
#[inline]
|
||||
pub async fn get_all(
|
||||
user_id: u64,
|
||||
db: &DatabaseConnection,
|
||||
) -> crate::Result<impl Stream<Item = crate::Result<Model>> + '_> {
|
||||
let result = Self::find()
|
||||
.filter(Column::UserId.eq(user_id))
|
||||
.stream(db)
|
||||
.await?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Gets a list of account names of a user
|
||||
#[inline]
|
||||
pub async fn get_names(
|
||||
user_id: u64,
|
||||
db: &DatabaseConnection,
|
||||
ordered: bool,
|
||||
) -> crate::Result<impl Stream<Item = crate::Result<String>> + '_> {
|
||||
let mut select = Self::find()
|
||||
.select_only()
|
||||
.column(Column::Name)
|
||||
.filter(Column::UserId.eq(user_id));
|
||||
if ordered {
|
||||
select = select.order_by_asc(Column::Name);
|
||||
}
|
||||
let result = select.into_tuple().stream(db).await?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Checks if the account exists
|
||||
#[inline]
|
||||
pub async fn exists(
|
||||
user_id: u64,
|
||||
account_name: impl Into<String>,
|
||||
db: &DatabaseConnection,
|
||||
) -> crate::Result<bool> {
|
||||
let result = Self::find_by_id((user_id, account_name.into()))
|
||||
.select_only()
|
||||
.column(Column::UserId)
|
||||
.into_tuple::<u64>()
|
||||
.one(db)
|
||||
.await?;
|
||||
Ok(result.is_some())
|
||||
}
|
||||
|
||||
/// Gets the account from the DB
|
||||
#[inline]
|
||||
pub async fn get(
|
||||
user_id: u64,
|
||||
account_name: impl Into<String>,
|
||||
db: &DatabaseConnection,
|
||||
) -> crate::Result<Option<Model>> {
|
||||
Self::find_by_id((user_id, account_name.into()))
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Deletes all the user's accounts from DB
|
||||
#[inline]
|
||||
pub async fn delete_all(user_id: u64, db: &DatabaseConnection) -> crate::Result<()> {
|
||||
Self::delete_many()
|
||||
.filter(Column::UserId.eq(user_id))
|
||||
.exec(db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
7
entity/src/lib.rs
Normal file
7
entity/src/lib.rs
Normal file
@ -0,0 +1,7 @@
|
||||
pub mod account;
|
||||
pub mod master_pass;
|
||||
pub mod prelude;
|
||||
|
||||
use sea_orm::DbErr;
|
||||
|
||||
type Result<T> = std::result::Result<T, DbErr>;
|
45
entity/src/master_pass.rs
Normal file
45
entity/src/master_pass.rs
Normal file
@ -0,0 +1,45 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
use sea_orm::{entity::prelude::*, QuerySelect};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "master_pass")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub user_id: u64,
|
||||
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")]
|
||||
pub salt: Vec<u8>,
|
||||
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")]
|
||||
pub password_hash: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl Entity {
|
||||
/// Gets the master password from the database
|
||||
#[inline]
|
||||
pub async fn get(user_id: u64, db: &DatabaseConnection) -> crate::Result<Option<Model>> {
|
||||
Self::find_by_id(user_id).one(db).await
|
||||
}
|
||||
|
||||
/// Checks if the master password for the user exists
|
||||
#[inline]
|
||||
pub async fn exists(user_id: u64, db: &DatabaseConnection) -> Result<bool, DbErr> {
|
||||
let id = Self::find_by_id(user_id)
|
||||
.select_only()
|
||||
.column(Column::UserId)
|
||||
.into_tuple::<u64>()
|
||||
.one(db)
|
||||
.await?;
|
||||
Ok(id.is_some())
|
||||
}
|
||||
|
||||
/// Removes a master password of the user from the database
|
||||
pub async fn remove(user_id: u64, db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
Self::delete_by_id(user_id).exec(db).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
4
entity/src/prelude.rs
Normal file
4
entity/src/prelude.rs
Normal file
@ -0,0 +1,4 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
pub use super::account::{self, Entity as Account};
|
||||
pub use super::master_pass::{self, Entity as MasterPass};
|
Reference in New Issue
Block a user