Added apis
This commit is contained in:
@@ -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");
|
||||
|
||||
|
@@ -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!()
|
||||
}
|
||||
}
|
||||
|
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"query": "\n SELECT * from users\n where username=$1\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "username",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "password_hash",
|
||||
"type_info": "Varchar"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "d3f222cf6c3d9816705426fdbed3b13cb575bb432eb1f33676c0b414e67aecaf"
|
||||
}
|
Reference in New Issue
Block a user