refactor mutibility
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use std::marker::PhantomData;
|
||||
use tokio::sync::Mutex;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
use opaque_ke::{CredentialFinalization, CredentialRequest, RegistrationRequest};
|
||||
use crate::shared::models::opaque::{NKodeCipherSuite, NKodeServerSetup, OpaqueLoginSession, OpaqueRegisterSession, PasswordFile};
|
||||
@@ -14,23 +12,20 @@ use crate::server::repository::in_memory::in_memory_opaque_session::InMemoryOpaq
|
||||
use crate::server::repository::in_memory::in_memory_user_db::InMemoryUserDB;
|
||||
use crate::shared::models::app::LoggedInSession;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct InMemoryServer<K: CredKind> {
|
||||
auth_db: Arc<Mutex<ServerApp<InMemoryOpaqueDB, InMemoryOpaqueSession, InMemoryUserDB>>>,
|
||||
auth_db: ServerApp<InMemoryOpaqueDB, InMemoryOpaqueSession, InMemoryUserDB>,
|
||||
_kind: PhantomData<K>,
|
||||
}
|
||||
|
||||
impl<K: CredKind> InMemoryServer<K> {
|
||||
pub fn new(server_setup: NKodeServerSetup) -> Self {
|
||||
Self {
|
||||
auth_db: Arc::new(Mutex::new(
|
||||
ServerApp::new(
|
||||
server_setup,
|
||||
InMemoryOpaqueDB::new(),
|
||||
InMemoryOpaqueSession::new(),
|
||||
InMemoryUserDB::new()
|
||||
)
|
||||
)),
|
||||
auth_db: ServerApp::new(
|
||||
server_setup,
|
||||
InMemoryOpaqueDB::new(),
|
||||
InMemoryOpaqueSession::new(),
|
||||
InMemoryUserDB::new()
|
||||
),
|
||||
_kind: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -51,7 +46,7 @@ where
|
||||
) -> Result<OpaqueRegisterSession, ClientAuthError> {
|
||||
// Server API takes ownership; client trait gives us a reference.
|
||||
// opaque-ke request types are typically Clone; if not, you'll need to adjust signatures.
|
||||
self.auth_db.lock().await
|
||||
self.auth_db
|
||||
.reg_start::<K>(identifier, message.clone())
|
||||
.await
|
||||
.map_err(|e| ClientAuthError::Transport(e))
|
||||
@@ -62,8 +57,7 @@ where
|
||||
session_id: &Uuid,
|
||||
password_file: PasswordFile,
|
||||
) -> Result<(), ClientAuthError> {
|
||||
self.auth_db.lock().await
|
||||
.reg_finish::<K>(session_id, password_file)
|
||||
self.auth_db.reg_finish::<K>(session_id, password_file)
|
||||
.await
|
||||
.map_err(|e| ClientAuthError::Transport(e))
|
||||
}
|
||||
@@ -79,7 +73,7 @@ where
|
||||
identifier: &[u8],
|
||||
request: &CredentialRequest<NKodeCipherSuite>,
|
||||
) -> Result<OpaqueLoginSession, ClientAuthError> {
|
||||
self.auth_db.lock().await
|
||||
self.auth_db
|
||||
.login_start::<K>(identifier, request.clone())
|
||||
.await
|
||||
.map_err(|e| ClientAuthError::Transport(e))
|
||||
@@ -91,7 +85,7 @@ where
|
||||
message: &CredentialFinalization<NKodeCipherSuite>,
|
||||
) -> Result<LoggedInSession, ClientAuthError> {
|
||||
Ok(self
|
||||
.auth_db.lock().await
|
||||
.auth_db
|
||||
.login_finish(session_id, message.clone())
|
||||
.await
|
||||
.map_err(|e| ClientAuthError::Transport(e))?
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use async_trait::async_trait;
|
||||
use tokio::sync::Mutex;
|
||||
use uuid::Uuid;
|
||||
use crate::server::repository::user_repo::UserRepo;
|
||||
use crate::shared::models::app::{CodeLoggedInSession, CodeLoginData, KeyLoggedInSession};
|
||||
use crate::shared::models::email::Email;
|
||||
|
||||
pub struct InMemoryUserDB {
|
||||
key_session: HashMap<Uuid, KeyLoggedInSession>,
|
||||
code_session: HashMap<Uuid, CodeLoggedInSession>,
|
||||
code_data: HashMap<Email, CodeLoginData>,
|
||||
key_session: Arc<Mutex<HashMap<Uuid, KeyLoggedInSession>>>,
|
||||
code_session: Arc<Mutex<HashMap<Uuid, CodeLoggedInSession>>>,
|
||||
code_data: Arc<Mutex<HashMap<Email, CodeLoginData>>>,
|
||||
}
|
||||
|
||||
impl InMemoryUserDB {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
key_session: HashMap::new(),
|
||||
code_session: HashMap::new(),
|
||||
code_data: HashMap::new(),
|
||||
key_session: Arc::new(Mutex::new(HashMap::new())),
|
||||
code_session: Arc::new(Mutex::new(HashMap::new())),
|
||||
code_data: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,40 +29,39 @@ impl Default for InMemoryUserDB {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl UserRepo for InMemoryUserDB {
|
||||
fn get_key_session(&mut self, session_id: &Uuid) -> Result<KeyLoggedInSession, String> {
|
||||
self.key_session
|
||||
async fn get_key_session(&self, session_id: &Uuid) -> Result<KeyLoggedInSession, String> {
|
||||
self.key_session.lock().await
|
||||
.get(&session_id)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("key session not found for session_id={}", session_id))
|
||||
}
|
||||
|
||||
fn get_code_session(&mut self, session_id: &Uuid) -> Result<CodeLoggedInSession, String> {
|
||||
self.code_session
|
||||
async fn get_code_session(&self, session_id: &Uuid) -> Result<CodeLoggedInSession, String> {
|
||||
self.code_session.lock().await
|
||||
.get(&session_id)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("code session not found for session_id={}", session_id))
|
||||
}
|
||||
|
||||
fn set_key_session(&mut self, session: KeyLoggedInSession) -> Result<(), String> {
|
||||
// Assumes KeyLoggedInSession has a session_id: Uuid field (common pattern)
|
||||
self.key_session.insert(session.0.session_id, session);
|
||||
async fn set_key_session(&self, session: KeyLoggedInSession) -> Result<(), String> {
|
||||
self.key_session.lock().await.insert(session.0.session_id, session);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_code_session(&mut self, session: CodeLoggedInSession) -> Result<(), String> {
|
||||
// Assumes CodeLoggedInSession has a session_id: Uuid field (common pattern)
|
||||
self.code_session.insert(session.0.session_id, session);
|
||||
async fn set_code_session(&self, session: CodeLoggedInSession) -> Result<(), String> {
|
||||
self.code_session.lock().await.insert(session.0.session_id, session);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_code_login_data(&mut self, email: Email, data: CodeLoginData) -> Result<(), String> {
|
||||
self.code_data.insert(email, data);
|
||||
async fn set_code_login_data(&self, email: Email, data: CodeLoginData) -> Result<(), String> {
|
||||
self.code_data.lock().await.insert(email, data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_code_login_data(&mut self, email: &Email) -> Result<CodeLoginData, String> {
|
||||
self.code_data
|
||||
async fn get_code_login_data(&self, email: &Email) -> Result<CodeLoginData, String> {
|
||||
self.code_data.lock().await
|
||||
.get(email)
|
||||
.cloned()
|
||||
.ok_or_else(|| "code login data not found for email".to_string())
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
use async_trait::async_trait;
|
||||
use uuid::Uuid;
|
||||
use crate::shared::models::app::{CodeLoggedInSession, CodeLoginData, KeyLoggedInSession};
|
||||
use crate::shared::models::email::Email;
|
||||
|
||||
#[async_trait]
|
||||
pub trait UserRepo {
|
||||
fn get_key_session(&mut self, session_id: &Uuid) -> Result<KeyLoggedInSession, String>;
|
||||
fn get_code_session(&mut self, session_id: &Uuid) -> Result<CodeLoggedInSession, String>;
|
||||
async fn get_key_session(&self, session_id: &Uuid) -> Result<KeyLoggedInSession, String>;
|
||||
async fn get_code_session(&self, session_id: &Uuid) -> Result<CodeLoggedInSession, String>;
|
||||
|
||||
fn set_key_session(&mut self, session: KeyLoggedInSession) -> Result<(), String>;
|
||||
fn set_code_session(&mut self, session: CodeLoggedInSession) -> Result<(), String>;
|
||||
async fn set_key_session(&self, session: KeyLoggedInSession) -> Result<(), String>;
|
||||
async fn set_code_session(&self, session: CodeLoggedInSession) -> Result<(), String>;
|
||||
|
||||
fn set_code_login_data(&mut self, email: Email, data: CodeLoginData) -> Result<(), String>;
|
||||
fn get_code_login_data(&mut self, email: &Email) -> Result<CodeLoginData, String>;
|
||||
async fn set_code_login_data(&self, email: Email, data: CodeLoginData) -> Result<(), String>;
|
||||
async fn get_code_login_data(&self, email: &Email) -> Result<CodeLoginData, String>;
|
||||
}
|
||||
Reference in New Issue
Block a user