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 | use crate::error::AppError; | 8 | use crate::error::AppError; |
| 9 | use crate::state::AppState; | 9 | use crate::state::AppState; |
| 10 | 10 | ||
| 11 | -/// Resolve the user behind the `session` cookie, or `401`. The role is read | 11 | +/// A principal injected as a request extension by the AI bridge, for the |
| 12 | -/// fresh from the row on every request, so a role change takes effect on the | 12 | +/// synthetic requests it dispatches into the router. Extensions cannot be set |
| 13 | -/// user's next call — no re-login required. | 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 | async fn authed_user<S>(parts: &mut Parts, state: &S) -> Result<User, AppError> | 21 | async fn authed_user<S>(parts: &mut Parts, state: &S) -> Result<User, AppError> |
| 15 | where | 22 | where |
| 16 | S: Send + Sync, | 23 | S: Send + Sync, |
| 17 | AppState: FromRef<S>, | 24 | AppState: FromRef<S>, |
| 18 | { | 25 | { |
| 26 | + if let Some(principal) = parts.extensions.get::<AiPrincipal>() { | ||
| 27 | + return Ok(principal.0.clone()); | ||
| 28 | + } | ||
| 29 | + | ||
| 19 | let jar = CookieJar::from_request_parts(parts, state) | 30 | let jar = CookieJar::from_request_parts(parts, state) |
| 20 | .await | 31 | .await |
| 21 | .map_err(|_| AppError::Unauthorized)?; | 32 | .map_err(|_| AppError::Unauthorized)?; |