Accept an extension-injected principal in auth
Browse files at this commit362bd1c9d302b6ef18e0fb81531d92a0facc43dfParent: b032b41- feat
- accept an extension-injected principal in auth
1 changed file +14 -3
- src/web/extractor.rs +14 -3
src/web/extractor.rsmodified+14 -3
| @@ -8,14 +8,25 @@ use crate::domain::user::User; | ||
| 8 | 8 | use crate::error::AppError; |
| 9 | 9 | use crate::state::AppState; |
| 10 | 10 | |
| 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. | |
| 14 | 21 | async fn authed_user<S>(parts: &mut Parts, state: &S) -> Result<User, AppError> |
| 15 | 22 | where |
| 16 | 23 | S: Send + Sync, |
| 17 | 24 | AppState: FromRef<S>, |
| 18 | 25 | { |
| 26 | + if let Some(principal) = parts.extensions.get::<AiPrincipal>() { | |
| 27 | + return Ok(principal.0.clone()); | |
| 28 | + } | |
| 29 | + | |
| 19 | 30 | let jar = CookieJar::from_request_parts(parts, state) |
| 20 | 31 | .await |
| 21 | 32 | .map_err(|_| AppError::Unauthorized)?; |