feat: with items

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-06-04 11:02:51 +02:00
parent 12c7c8f6ee
commit 534b2e4a23
25 changed files with 517 additions and 111 deletions

View File

@@ -1,6 +1,7 @@
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use axum::async_trait;
use como_core::users::UserService;
use como_domain::Context;
use rand_core::OsRng;
use crate::database::ConnectionPool;
@@ -14,7 +15,7 @@ impl DefaultUserService {
Self { pool }
}
fn hash_password(&self, password: String) -> anyhow::Result<String> {
fn hash_password(&self, _context: &Context, password: String) -> anyhow::Result<String> {
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
@@ -26,7 +27,12 @@ impl DefaultUserService {
Ok(password_hash)
}
fn validate_password(&self, password: String, hashed_password: String) -> anyhow::Result<bool> {
fn validate_password(
&self,
_context: &Context,
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))?;
@@ -39,8 +45,13 @@ impl 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)?;
async fn add_user(
&self,
context: &Context,
username: String,
password: String,
) -> anyhow::Result<String> {
let hashed_password = self.hash_password(context, password)?;
let rec = sqlx::query!(
r#"
@@ -59,6 +70,7 @@ impl UserService for DefaultUserService {
async fn validate_user(
&self,
context: &Context,
username: String,
password: String,
) -> anyhow::Result<Option<String>> {
@@ -73,7 +85,7 @@ impl UserService for DefaultUserService {
.await?;
match rec {
Some(user) => match self.validate_password(password, user.password_hash)? {
Some(user) => match self.validate_password(context, password, user.password_hash)? {
true => Ok(Some(user.id.to_string())),
false => Ok(None),
},