Added apis

This commit is contained in:
2022-10-04 11:06:48 +02:00
parent 6234cf18e8
commit ae74f66c3a
28 changed files with 392 additions and 300 deletions

View File

@@ -20,12 +20,12 @@ pub struct ServiceRegister {
}
impl ServiceRegister {
pub fn new(_pool: ConnectionPool, _config: AppConfig) -> Self {
pub fn new(pool: ConnectionPool, _config: Arc<AppConfig>) -> Self {
info!("creating services");
let item_service = Arc::new(DefaultItemService::new()) as DynItemService;
let project_service = Arc::new(DefaultProjectService::new()) as DynProjectService;
let user_service = Arc::new(DefaultUserService::new()) as DynUserService;
let user_service = Arc::new(DefaultUserService::new(pool.clone())) as DynUserService;
info!("services created succesfully");

View File

@@ -1,4 +1,6 @@
use axum::async_trait;
use como_core::items::ItemService;
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto};
pub struct DefaultItemService {}
@@ -8,4 +10,9 @@ impl DefaultItemService {
}
}
impl ItemService for DefaultItemService {}
#[async_trait]
impl ItemService for DefaultItemService {
async fn add_item(&self, _item: CreateItemDto) -> anyhow::Result<CreatedItemDto> {
todo!()
}
}

View File

@@ -1,11 +1,83 @@
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use axum::async_trait;
use como_core::users::UserService;
use rand_core::OsRng;
pub struct DefaultUserService {}
use crate::database::ConnectionPool;
pub struct DefaultUserService {
pool: ConnectionPool,
}
impl DefaultUserService {
pub fn new() -> Self {
Self {}
pub fn new(pool: ConnectionPool) -> Self {
Self { pool }
}
fn hash_password(&self, password: String) -> anyhow::Result<String> {
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
let password_hash = argon2
.hash_password(password.as_bytes(), &salt)
.map_err(|e| anyhow::anyhow!(e))?
.to_string();
Ok(password_hash)
}
fn validate_password(&self, password: String, hashed_password: String) -> anyhow::Result<bool> {
let argon2 = Argon2::default();
let parsed_hash = PasswordHash::new(&hashed_password).map_err(|e| anyhow::anyhow!(e))?;
match argon2.verify_password(password.as_bytes(), &parsed_hash) {
Ok(..) => Ok(true),
Err(..) => Ok(false),
}
}
}
impl UserService for DefaultUserService {}
#[async_trait]
impl UserService for DefaultUserService {
async fn add_user(&self, username: String, password: String) -> anyhow::Result<String> {
let hashed_password = self.hash_password(password)?;
let rec = sqlx::query!(
r#"
INSERT INTO users (username, password_hash)
VALUES ( $1, $2 )
RETURNING id
"#,
username,
hashed_password
)
.fetch_one(&self.pool)
.await?;
Ok(rec.id.to_string())
}
async fn validate_user(
&self,
username: String,
password: String,
) -> anyhow::Result<Option<String>> {
let rec = sqlx::query!(
r#"
SELECT * from users
where username=$1
"#,
username,
)
.fetch_optional(&self.pool)
.await?;
match rec {
Some(user) => match self.validate_password(password, user.password_hash)? {
true => Ok(Some(user.id.to_string())),
false => Ok(None),
},
None => Ok(None),
}
}
}