diff --git a/auth-service/Cargo.lock b/auth-service/Cargo.lock index 6eaafbbee..33d3597cd 100644 --- a/auth-service/Cargo.lock +++ b/auth-service/Cargo.lock @@ -17,22 +17,10 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "async-trait" -version = "0.1.89" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "auth-service" version = "0.1.0" dependencies = [ - "async-trait", "axum", "reqwest", "serde", diff --git a/auth-service/Cargo.toml b/auth-service/Cargo.toml index d4bac832a..f23b7f5a9 100644 --- a/auth-service/Cargo.toml +++ b/auth-service/Cargo.toml @@ -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 @@ -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" + diff --git a/auth-service/src/domain/data_stores.rs b/auth-service/src/domain/data_stores.rs index e6323f76b..ede90b580 100644 --- a/auth-service/src/domain/data_stores.rs +++ b/auth-service/src/domain/data_stores.rs @@ -1,5 +1,5 @@ use crate::domain::User; -use async_trait::async_trait; +use std::pin::Pin; #[derive(Debug, PartialEq)] pub enum UserStoreError { @@ -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; - async fn validate_user(&self, email: &str, password: &str) -> Result<(), UserStoreError>; + fn add_user<'a>( + &'a mut self, + user: User, + ) -> Pin> + Send + 'a>>; + fn get_user<'a>( + &'a self, + email: &'a str, + ) -> Pin> + Send + 'a>>; + fn validate_user<'a>( + &'a self, + email: &'a str, + password: &'a str, + ) -> Pin> + Send + 'a>>; } diff --git a/auth-service/src/lib.rs b/auth-service/src/lib.rs index b9c41d5ca..df2b0d3f0 100644 --- a/auth-service/src/lib.rs +++ b/auth-service/src/lib.rs @@ -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; diff --git a/auth-service/src/main.rs b/auth-service/src/main.rs index 41749ee85..da63838ea 100644 --- a/auth-service/src/main.rs +++ b/auth-service/src/main.rs @@ -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; diff --git a/auth-service/src/routes/signup.rs b/auth-service/src/routes/signup.rs index d654bf28e..853486fe4 100644 --- a/auth-service/src/routes/signup.rs +++ b/auth-service/src/routes/signup.rs @@ -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)] diff --git a/auth-service/src/services/hashmap_user_store.rs b/auth-service/src/services/hashmap_user_store.rs index 2a54d9c5a..dbc19b652 100644 --- a/auth-service/src/services/hashmap_user_store.rs +++ b/auth-service/src/services/hashmap_user_store.rs @@ -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] @@ -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> + 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 { - match self.users.get(email) { - Some(user) => Ok(user.clone()), - None => Err(UserStoreError::UserNotFound), - } + fn get_user<'a>( + &'a self, + email: &'a str, + ) -> Pin> + 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> + 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), - } + }) } } diff --git a/auth-service/tests/api/helpers.rs b/auth-service/tests/api/helpers.rs index e516653fd..02fd04ec0 100644 --- a/auth-service/tests/api/helpers.rs +++ b/auth-service/tests/api/helpers.rs @@ -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; diff --git a/auth-service/tests/api/signup.rs b/auth-service/tests/api/signup.rs index 374e79121..6306b2a8d 100644 --- a/auth-service/tests/api/signup.rs +++ b/auth-service/tests/api/signup.rs @@ -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() {