@@ -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),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user