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

@@ -0,0 +1,82 @@
use axum::{
extract::State,
http::{HeaderMap, StatusCode},
};
use serde::Serialize;
use uuid::Uuid;
use crate::{
domain::warren::{
models::{
auth_session::{
AuthSessionId,
requests::{FetchAuthSessionRequest, FetchAuthSessionResponse},
},
user::User,
},
ports::{AuthService, WarrenService},
},
inbound::http::{
AppState,
responses::{ApiError, ApiSuccess},
},
};
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct SessionUser {
id: Uuid,
name: String,
email: String,
admin: bool,
}
impl From<User> for SessionUser {
fn from(value: User) -> Self {
Self {
id: *value.id(),
name: value.name().to_string(),
email: value.email().to_string(),
admin: value.admin(),
}
}
}
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FetchSessionResponseBody {
user: SessionUser,
expires_at: i64,
}
impl From<FetchAuthSessionResponse> for FetchSessionResponseBody {
fn from(value: FetchAuthSessionResponse) -> Self {
Self {
user: value.user().clone().into(),
expires_at: value.session().expires_at().and_utc().timestamp_millis(),
}
}
}
pub async fn fetch_session<WS: WarrenService, AS: AuthService>(
State(state): State<AppState<WS, AS>>,
headers: HeaderMap,
) -> Result<ApiSuccess<FetchSessionResponseBody>, ApiError> {
let Some(Ok(Ok(session_id))) = headers.get("authorization").map(|h| {
h.to_str()
.map(|h| AuthSessionId::new(&h["WarrenAuth ".len()..]))
}) else {
return Err(ApiError::BadRequest(
"No authorization header set".to_string(),
));
};
let domain_request = FetchAuthSessionRequest::new(session_id);
state
.auth_service
.fetch_auth_session(domain_request)
.await
.map(|response| ApiSuccess::new(StatusCode::OK, response.into()))
.map_err(ApiError::from)
}