fetch auth session data from token

This commit is contained in:
2025-07-18 12:11:29 +02:00
parent 026f84b870
commit 1a6c60ff03
19 changed files with 368 additions and 20 deletions

View File

@@ -144,4 +144,11 @@ impl AuthMetrics for MetricsDebugLogger {
async fn record_auth_session_creation_failure(&self) {
tracing::debug!("[Metrics] Auth session creation failed");
}
async fn record_auth_session_fetch_success(&self) {
tracing::debug!("[Metrics] Auth session fetch succeeded");
}
async fn record_auth_session_fetch_failure(&self) {
tracing::debug!("[Metrics] Auth session fetch failed");
}
}

View File

@@ -2,6 +2,7 @@ use uuid::Uuid;
use crate::domain::warren::{
models::{
auth_session::requests::FetchAuthSessionResponse,
file::{File, FilePath},
user::User,
warren::Warren,
@@ -128,4 +129,11 @@ impl AuthNotifier for NotifierDebugLogger {
async fn auth_session_created(&self, user_id: &Uuid) {
tracing::debug!("[Notifier] Created auth session for user {}", user_id);
}
async fn auth_session_fetched(&self, response: &FetchAuthSessionResponse) {
tracing::debug!(
"[Notifier] Fetched auth session for user {}",
response.user().id()
);
}
}

View File

@@ -18,8 +18,11 @@ use uuid::Uuid;
use crate::domain::warren::{
models::{
auth_session::{
AuthSession,
requests::{CreateAuthSessionError, CreateAuthSessionRequest, SessionExpirationTime},
AuthSession, AuthSessionId,
requests::{
CreateAuthSessionError, CreateAuthSessionRequest, FetchAuthSessionError,
FetchAuthSessionRequest, FetchAuthSessionResponse, SessionExpirationTime,
},
},
user::{
RegisterUserError, RegisterUserRequest, User, UserEmail, UserName, UserPassword,
@@ -163,6 +166,28 @@ impl Postgres {
Ok(user)
}
async fn get_user_from_id(
&self,
connection: &mut PgConnection,
id: &Uuid,
) -> Result<User, sqlx::Error> {
let user: User = sqlx::query_as(
"
SELECT
*
FROM
users
WHERE
id = $1
",
)
.bind(id)
.fetch_one(connection)
.await?;
Ok(user)
}
async fn get_user_from_email(
&self,
connection: &mut PgConnection,
@@ -245,6 +270,28 @@ impl Postgres {
Ok(session)
}
async fn get_auth_session(
&self,
connection: &mut PgConnection,
session_id: &AuthSessionId,
) -> anyhow::Result<AuthSession> {
let session: AuthSession = sqlx::query_as(
"
SELECT
*
FROM
auth_sessions
WHERE
session_id = $1
",
)
.bind(session_id)
.fetch_one(connection)
.await?;
Ok(session)
}
}
impl WarrenRepository for Postgres {
@@ -355,6 +402,28 @@ impl AuthRepository for Postgres {
Ok(session)
}
async fn fetch_auth_session(
&self,
request: FetchAuthSessionRequest,
) -> Result<FetchAuthSessionResponse, FetchAuthSessionError> {
let mut connection = self
.pool
.acquire()
.await
.context("Failed to get a PostgreSQL connection")?;
let session = self
.get_auth_session(&mut connection, request.session_id())
.await
.context("Failed to get auth session")?;
let user = self
.get_user_from_id(&mut connection, session.user_id())
.await
.context("Failed to get user")?;
Ok(FetchAuthSessionResponse::new(session, user))
}
}
fn is_not_found_error(err: &sqlx::Error) -> bool {