use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::auth::Principal;
#[derive(Clone, Default, Debug)]
pub struct SessionOwners(Arc<Mutex<HashMap<String, Principal>>>);
impl SessionOwners {
pub fn claim(&self, id: String, principal: Principal) {
if let Ok(mut owners) = self.0.lock() {
owners.insert(id, principal);
}
}
#[must_use]
pub fn check(&self, id: &str, principal: Principal) -> bool {
self.0
.lock()
.is_ok_and(|owners| owners.get(id).is_none_or(|owner| *owner == principal))
}
pub fn release(&self, id: &str) {
if let Ok(mut owners) = self.0.lock() {
owners.remove(id);
}
}
}
pub async fn enforce(
axum::extract::State(owners): axum::extract::State<SessionOwners>,
request: axum::http::Request<axum::body::Body>,
next: axum::middleware::Next,
) -> axum::response::Response {
use axum::response::IntoResponse as _;
const SESSION_HEADER: &str = "mcp-session-id";
const NOT_FOUND_BODY: &str = "Not Found: Session not found";
let Some(principal) = crate::auth::principal(request.extensions()) else {
return next.run(request).await;
};
if let Some(id) = request
.headers()
.get(SESSION_HEADER)
.and_then(|value| value.to_str().ok())
&& !owners.check(id, principal)
{
return (axum::http::StatusCode::NOT_FOUND, NOT_FOUND_BODY).into_response();
}
if request.headers().contains_key(SESSION_HEADER)
&& request
.headers()
.get(SESSION_HEADER)
.and_then(|value| value.to_str().ok())
.is_none()
{
return (axum::http::StatusCode::NOT_FOUND, NOT_FOUND_BODY).into_response();
}
let method = request.method().clone();
let incoming = request
.headers()
.get(SESSION_HEADER)
.and_then(|value| value.to_str().ok())
.map(str::to_owned);
let response = next.run(request).await;
if let Some(new_id) = response
.headers()
.get(SESSION_HEADER)
.and_then(|value| value.to_str().ok())
.map(str::to_owned)
&& incoming.is_none()
{
owners.claim(new_id, principal);
}
if let Some(id) = incoming
&& ((method == axum::http::Method::DELETE && response.status().is_success())
|| response.status() == axum::http::StatusCode::NOT_FOUND)
{
owners.release(&id);
}
response
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unknown_session_is_allowed() {
let owners = SessionOwners::default();
assert!(owners.check("new-id", Principal(0)));
}
#[test]
fn owner_may_reuse_its_session() {
let owners = SessionOwners::default();
owners.claim("sess".to_string(), Principal(1));
assert!(owners.check("sess", Principal(1)));
}
#[test]
fn another_key_is_denied() {
let owners = SessionOwners::default();
owners.claim("sess".to_string(), Principal(0));
assert!(!owners.check("sess", Principal(1)));
}
#[test]
fn release_forgets_the_owner() {
let owners = SessionOwners::default();
owners.claim("sess".to_string(), Principal(0));
owners.release("sess");
assert!(owners.check("sess", Principal(1)));
}
}