forge
Explore
Log in

Don't you dare go Hollow

Accept an extension-injected principal in auth

Browse files at this commit

362bd1c9d302b6ef18e0fb81531d92a0facc43dfAhmet Kaan GümüşParent: b032b41
  • feat
    • accept an extension-injected principal in auth
Show full message
🤖 feat: accept an extension-injected principal in auth
src/web/extractor.rsmodified+14 -3
@@ -8,14 +8,25 @@ use crate::domain::user::User;
88 use crate::error::AppError;
99 use crate::state::AppState;
1010
11-/// Resolve the user behind the `session` cookie, or `401`. The role is read
12-/// fresh from the row on every request, so a role change takes effect on the
13-/// user's next call — no re-login required.
11+/// A principal injected as a request extension by the AI bridge, for the
12+/// synthetic requests it dispatches into the router. Extensions cannot be set
13+/// from outside the process, so this is unforgeable over HTTP.
14+#[derive(Clone)]
15+pub(crate) struct AiPrincipal(pub User);
16+
17+/// Resolve the user behind the `session` cookie, or `401`. An injected
18+/// [`AiPrincipal`] extension wins over the cookie. The role is read fresh from
19+/// the row on every request, so a role change takes effect on the user's next
20+/// call — no re-login required.
1421 async fn authed_user<S>(parts: &mut Parts, state: &S) -> Result<User, AppError>
1522 where
1623 S: Send + Sync,
1724 AppState: FromRef<S>,
1825 {
26+ if let Some(principal) = parts.extensions.get::<AiPrincipal>() {
27+ return Ok(principal.0.clone());
28+ }
29+
1930 let jar = CookieJar::from_request_parts(parts, state)
2031 .await
2132 .map_err(|_| AppError::Unauthorized)?;