Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions auth-service/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions auth-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "auth-service"
version = "0.1.0"
edition = "2021"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -13,4 +13,4 @@ reqwest = { version = "0.12.23", default-features = false, features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "1.7.0", features = ["v4", "serde"] }
async-trait = "0.1.89"

19 changes: 14 additions & 5 deletions auth-service/src/domain/data_stores.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::domain::User;
use async_trait::async_trait;
use std::pin::Pin;

#[derive(Debug, PartialEq)]
pub enum UserStoreError {
Expand All @@ -9,9 +9,18 @@ pub enum UserStoreError {
UnexpectedError,
}

#[async_trait]
pub trait UserStore: Send + Sync {
async fn add_user(&mut self, user: User) -> Result<(), UserStoreError>;
async fn get_user(&self, email: &str) -> Result<User, UserStoreError>;
async fn validate_user(&self, email: &str, password: &str) -> Result<(), UserStoreError>;
fn add_user<'a>(
&'a mut self,
user: User,
) -> Pin<Box<dyn Future<Output = Result<(), UserStoreError>> + Send + 'a>>;
fn get_user<'a>(
&'a self,
email: &'a str,
) -> Pin<Box<dyn Future<Output = Result<User, UserStoreError>> + Send + 'a>>;
fn validate_user<'a>(
&'a self,
email: &'a str,
password: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(), UserStoreError>> + Send + 'a>>;
}
2 changes: 1 addition & 1 deletion auth-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::domain::AuthAPIError;
use crate::routes::{login, logout, signup, verify_2fa, verify_token};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{serve::Serve, Json, Router};
use axum::{Json, Router, serve::Serve};
use serde::{Deserialize, Serialize};
use tokio::net::TcpListener;
use tower_http::services::ServeDir;
Expand Down
2 changes: 1 addition & 1 deletion auth-service/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use auth_service::services::hashmap_user_store::HashmapUserStore;
use auth_service::Application;
use auth_service::services::hashmap_user_store::HashmapUserStore;
use std::sync::Arc;
use tokio::sync::RwLock;

Expand Down
4 changes: 2 additions & 2 deletions auth-service/src/routes/signup.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::domain::{AuthAPIError, Email, Password};
use crate::{domain, AppState};
use crate::{AppState, domain};
use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
Expand Down
62 changes: 39 additions & 23 deletions auth-service/src/services/hashmap_user_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::domain::{User, UserStore, UserStoreError};
use async_trait::async_trait;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;

#[derive(Debug, Default)]
#[non_exhaustive]
Expand All @@ -18,39 +19,54 @@ impl HashmapUserStore {
}
}

#[async_trait]
impl UserStore for HashmapUserStore {
/// Adds a new user to the store.
async fn add_user(&mut self, user: User) -> Result<(), UserStoreError> {
if Some(&user) == self.users.get(&user.email.value) {
return Err(UserStoreError::UserAlreadyExists);
} else {
self.users.insert(user.email.value.clone(), user);
}
fn add_user<'a>(
&'a mut self,
user: User,
) -> Pin<Box<dyn Future<Output = Result<(), UserStoreError>> + Send + 'a>> {
Box::pin(async move {
if Some(&user) == self.users.get(&user.email.value) {
return Err(UserStoreError::UserAlreadyExists);
} else {
self.users.insert(user.email.value.clone(), user);
}

Ok(())
Ok(())
})
}

/// Retrieves a user by email.
async fn get_user(&self, email: &str) -> Result<User, UserStoreError> {
match self.users.get(email) {
Some(user) => Ok(user.clone()),
None => Err(UserStoreError::UserNotFound),
}
fn get_user<'a>(
&'a self,
email: &'a str,
) -> Pin<Box<dyn Future<Output = Result<User, UserStoreError>> + Send + 'a>> {
Box::pin(async move {
match self.users.get(email) {
Some(user) => Ok(user.clone()),
None => Err(UserStoreError::UserNotFound),
}
})
}

/// Validates user credentials.
async fn validate_user(&self, email: &str, password: &str) -> Result<(), UserStoreError> {
match self.users.get(email) {
Some(user) => {
if user.password.hash == password {
Ok(())
} else {
Err(UserStoreError::InvalidCredentials)
fn validate_user<'a>(
&'a self,
email: &'a str,
password: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(), UserStoreError>> + Send + 'a>> {
Box::pin(async move {
match self.users.get(email) {
Some(user) => {
if user.password.hash == password {
Ok(())
} else {
Err(UserStoreError::InvalidCredentials)
}
}
None => Err(UserStoreError::UserNotFound),
}
None => Err(UserStoreError::UserNotFound),
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion auth-service/tests/api/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use auth_service::services::HashmapUserStore;
use auth_service::Application;
use auth_service::services::HashmapUserStore;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
Expand Down
4 changes: 2 additions & 2 deletions auth-service/tests/api/signup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::helpers::{get_random_email, TestApp};
use auth_service::routes::SignupResponse;
use crate::helpers::{TestApp, get_random_email};
use auth_service::ErrorResponse;
use auth_service::routes::SignupResponse;

#[tokio::test]
async fn should_return_422_if_malformed_input() {
Expand Down