83 lines
2.1 KiB
Rust
83 lines
2.1 KiB
Rust
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)
|
|
}
|