From 362bd1c9d302b6ef18e0fb81531d92a0facc43df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 12 Aug 2026 16:58:11 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20feat:=20accept=20an=20extension-?= =?UTF-8?q?injected=20principal=20in=20auth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/web/extractor.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/web/extractor.rs b/src/web/extractor.rs index 62d8d33..0e6e655 100644 --- a/src/web/extractor.rs +++ b/src/web/extractor.rs @@ -8,14 +8,25 @@ use crate::domain::user::User; use crate::error::AppError; use crate::state::AppState; -/// Resolve the user behind the `session` cookie, or `401`. The role is read -/// fresh from the row on every request, so a role change takes effect on the -/// user's next call — no re-login required. +/// A principal injected as a request extension by the AI bridge, for the +/// synthetic requests it dispatches into the router. Extensions cannot be set +/// from outside the process, so this is unforgeable over HTTP. +#[derive(Clone)] +pub(crate) struct AiPrincipal(pub User); + +/// Resolve the user behind the `session` cookie, or `401`. An injected +/// [`AiPrincipal`] extension wins over the cookie. The role is read fresh from +/// the row on every request, so a role change takes effect on the user's next +/// call — no re-login required. async fn authed_user(parts: &mut Parts, state: &S) -> Result where S: Send + Sync, AppState: FromRef, { + if let Some(principal) = parts.extensions.get::() { + return Ok(principal.0.clone()); + } + let jar = CookieJar::from_request_parts(parts, state) .await .map_err(|_| AppError::Unauthorized)?;