use std::net::SocketAddr;
use axum::extract::{ConnectInfo, State};
use axum::http::HeaderMap;
use axum::response::IntoResponse;
use crate::control::security::session_handle::ClientFingerprint;
use super::super::auth::{ApiError, AppState, resolve_auth};
use super::super::types::{HttpSessionResponse, HttpStatusOk};
pub async fn create_session(
ConnectInfo(peer): ConnectInfo<SocketAddr>,
headers: HeaderMap,
State(state): State<AppState>,
) -> Result<impl IntoResponse, ApiError> {
let (identity, auth_ctx) = resolve_auth(&headers, &state, "http")?;
let fingerprint = ClientFingerprint::from_peer(identity.tenant_id, &peer);
let handle = state.shared.session_handles.create(auth_ctx, fingerprint);
Ok(axum::Json(HttpSessionResponse {
session_id: handle,
expires_in: 3600,
}))
}
pub async fn delete_session(
ConnectInfo(peer): ConnectInfo<SocketAddr>,
headers: HeaderMap,
State(state): State<AppState>,
) -> Result<impl IntoResponse, ApiError> {
let _identity = {
let peer_str = peer.to_string();
crate::control::server::http::auth::resolve_identity(&headers, &state, &peer_str)?
};
let handle = headers
.get("x-session-id")
.and_then(|v| v.to_str().ok())
.ok_or_else(|| ApiError::BadRequest("missing X-Session-Id header".into()))?;
let found = state.shared.session_handles.invalidate(handle);
if !found {
return Err(ApiError::BadRequest("session handle not found".into()));
}
Ok(axum::Json(HttpStatusOk::ok()))
}