umbral_auth/session_user.rs
1//! `AuthUser`-aware session helpers — moved from umbral-sessions so
2//! sessions can stay free of any user-model dependency.
3//!
4//! The split mirrors the dep arrow: `umbral-auth` depends on
5//! `umbral-sessions` (it needs cookie + session-table primitives),
6//! `umbral-sessions` does not depend on `umbral-auth` (it knows
7//! nothing about users). All the AuthUser hydration happens here.
8//!
9//! ## What this module owns
10//!
11//! - [`current_user`] — read the cookie, hydrate the [`AuthUser`].
12//! - [`login`] / [`login_with_request`] — the one-call shape
13//! for credential check + session creation + cookie set +
14//! `last_login` bump.
15//! - [`logout`] — re-exported convenience; same as
16//! `umbral_sessions::logout` plus a forwarding doc-comment.
17//! - [`SessionAuthentication`] — the `umbral-rest` `Authentication`
18//! impl that produces an `Identity` for the permission layer
19//! (was in `umbral-sessions`; needs AuthUser to populate
20//! `is_staff`).
21//! - [`User`] / [`OptionalUser`] — axum extractors that pull
22//! `AuthUser` from the request.
23//! - [`user_context_layer`] — middleware that injects the current
24//! user into `umbral::templates::CURRENT_USER` so HTML templates
25//! can write `{% if user.is_authenticated %}` uniformly.
26//!
27//! ## Custom user models
28//!
29//! Everything in here is hard-bound to [`AuthUser`]. Apps using a
30//! custom [`UserModel`] roll their own helpers — the building
31//! blocks are all `pub`:
32//!
33//! - `umbral_sessions::current_user_id_str(&headers)` → user PK as
34//! a string (already user-agnostic).
35//! - Their own user lookup against that PK.
36//! - Their own `Identity` builder.
37//!
38//! [`UserModel`]: crate::UserModel
39
40use crate::{AuthUser, auth_user};
41use async_trait::async_trait;
42use axum_core::extract::FromRequestParts;
43use http::StatusCode;
44use http::request::Parts;
45use umbral::auth::{Authentication, Identity};
46use umbral::web::HeaderMap;
47use umbral_sessions::SessionError;
48
49// =========================================================================
50// current_user — the AuthUser-flavored wrapper around
51// umbral_sessions::current_session.
52// =========================================================================
53
54/// Read the request's session cookie, look up the session row, then
55/// hydrate the [`AuthUser`] it points at. Returns `None` for any
56/// of: no cookie, expired session, anonymous session
57/// (`user_id IS NULL`), parse failure on a non-i64 user_id, missing
58/// user row, or inactive user.
59///
60/// One DB read (session row) + one DB read (user row). The
61/// `is_active` predicate is part of the user query, so a deactivated
62/// account silently looks anonymous from this helper's perspective
63/// without an explicit second filter at the call site.
64pub async fn current_user(headers: &HeaderMap) -> Result<Option<AuthUser>, SessionError> {
65 let Some(user_id_str) = umbral_sessions::current_user_id_str(headers).await? else {
66 return Ok(None);
67 };
68 // Session.user_id is text (gap #59) — parse back to AuthUser's
69 // i64 PK. A non-parseable value means the session was written
70 // by a different UserModel impl; from AuthUser's perspective
71 // that's anonymous.
72 let Ok(user_id) = user_id_str.parse::<i64>() else {
73 return Ok(None);
74 };
75 let user: Option<AuthUser> = AuthUser::objects()
76 .filter(auth_user::ID.eq(user_id) & auth_user::IS_ACTIVE.eq(true))
77 .first()
78 .await?;
79 Ok(user)
80}
81
82// =========================================================================
83// login / login_with_request — credential check ran outside, we just
84// mint the session + cookie + bump last_login.
85// =========================================================================
86
87/// Convenience: [`login_with_request`] with an empty request
88/// HeaderMap. Use when the handler doesn't already have a
89/// `HeaderMap` extractor and you're not worried about preserving
90/// an anonymous session's `data` (flash messages, cart) across the
91/// login.
92pub async fn login(
93 response_headers: &mut HeaderMap,
94 user: &AuthUser,
95) -> Result<String, SessionError> {
96 login_with_request(&HeaderMap::new(), response_headers, user).await
97}
98
99/// Mint an authenticated session for `user`, rotate the cookie, and
100/// bump `auth_user.last_login`. The session-fixation defense fires
101/// inside `umbral_sessions::login_user_id`: any anonymous session
102/// the request carried is destroyed before the new authenticated
103/// row is written.
104///
105/// `last_login` is a best-effort update: a failure logs a warning
106/// but doesn't invalidate the login (the session was created and
107/// the cookie was set, so the user is in).
108pub async fn login_with_request(
109 request_headers: &HeaderMap,
110 response_headers: &mut HeaderMap,
111 user: &AuthUser,
112) -> Result<String, SessionError> {
113 let token = umbral_sessions::login_user_id(
114 request_headers,
115 response_headers,
116 Some(user.id.to_string()),
117 )
118 .await?;
119
120 let mut patch = serde_json::Map::new();
121 patch.insert(
122 "last_login".to_string(),
123 serde_json::to_value(chrono::Utc::now()).unwrap_or(serde_json::Value::Null),
124 );
125 if let Err(e) = AuthUser::objects()
126 .filter(auth_user::ID.eq(user.id))
127 .update_values(patch)
128 .await
129 {
130 tracing::warn!(
131 error = ?e,
132 user_id = user.id,
133 "umbral-auth::login: failed to update last_login (session still active)",
134 );
135 }
136 Ok(token)
137}
138
139// =========================================================================
140// SessionAuthentication — produce an `Identity` for the REST
141// permission layer.
142// =========================================================================
143
144/// The session-cookie authenticator for `umbral-rest`. Reads the
145/// cookie, hydrates the [`AuthUser`], turns it into an [`Identity`]
146/// with `is_staff` set. Same shape `current_user` produces, packaged
147/// for `RestPlugin::authenticate`.
148///
149/// Was in `umbral-sessions` before the de-coupling; now here so it
150/// can name `AuthUser`.
151#[derive(Debug, Default, Clone, Copy)]
152pub struct SessionAuthentication;
153
154impl SessionAuthentication {
155 /// Convenience constructor identical to `Default::default()`.
156 pub fn new() -> Self {
157 Self
158 }
159}
160
161#[async_trait]
162impl Authentication for SessionAuthentication {
163 async fn authenticate(&self, headers: &HeaderMap) -> Option<Identity> {
164 let user = current_user(headers).await.ok().flatten()?;
165 // `user.id_string()` is the UserModel-level stringifier —
166 // it stays correct when an app swaps AuthUser for a custom
167 // user model with a non-i64 PK. `Identity::user_id` is
168 // String regardless because Identity must be uniform across
169 // user models.
170 Some(
171 Identity::user(crate::UserModel::id_string(&user))
172 .with_staff(user.is_staff)
173 .with_superuser(user.is_superuser)
174 .with_extra("auth", serde_json::json!("session")),
175 )
176 }
177
178 fn security_scheme(&self) -> Option<(String, serde_json::Value)> {
179 // Standard "session cookie" scheme. The actual cookie name
180 // (`umbral_session`) is documented in the description so
181 // Swagger UI users know what they're authorising with.
182 Some((
183 "SessionAuth".to_string(),
184 serde_json::json!({
185 "type": "apiKey",
186 "in": "cookie",
187 "name": "umbral_session",
188 "description": "umbral session cookie. Set by `POST /api/auth/login`; cleared by `/logout`."
189 }),
190 ))
191 }
192}
193
194// =========================================================================
195// User / OptionalUser axum extractors. Same shapes that used to live
196// in umbral-sessions::extractors.
197// =========================================================================
198
199/// Required-user extractor. 401 on anonymous requests.
200///
201/// ```ignore
202/// async fn dashboard(User(user): User) -> Html<String> {
203/// Html(format!("Welcome, {}!", user.username))
204/// }
205/// ```
206#[derive(Debug, Clone)]
207pub struct User(pub AuthUser);
208
209/// Optional-user extractor. Anonymous requests get `None`.
210///
211/// ```ignore
212/// async fn home(OptionalUser(maybe): OptionalUser) -> Html<String> {
213/// match maybe {
214/// Some(u) => Html(format!("Hi, {}", u.username)),
215/// None => Html("<a href=\"/login\">Log in</a>".into()),
216/// }
217/// }
218/// ```
219#[derive(Debug, Clone)]
220pub struct OptionalUser(pub Option<AuthUser>);
221
222impl<S> FromRequestParts<S> for User
223where
224 S: Send + Sync,
225{
226 type Rejection = (StatusCode, &'static str);
227
228 async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
229 match current_user(&parts.headers).await.ok().flatten() {
230 Some(u) => Ok(User(u)),
231 None => Err((StatusCode::UNAUTHORIZED, "authentication required")),
232 }
233 }
234}
235
236impl<S> FromRequestParts<S> for OptionalUser
237where
238 S: Send + Sync,
239{
240 type Rejection = std::convert::Infallible;
241
242 async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
243 Ok(OptionalUser(
244 current_user(&parts.headers).await.ok().flatten(),
245 ))
246 }
247}
248
249// =========================================================================
250// Template-injection middleware. Stash the current user under the
251// `umbral::templates::CURRENT_USER` task-local so HTML renders can
252// pick it up as `{{ user }}`.
253// =========================================================================
254
255/// Install a per-request lazy resolver on the
256/// [`umbral::templates::CURRENT_USER_LAZY`] channel.
257///
258/// The resolver clones the request headers and runs **at most once**,
259/// only when a template actually accesses `{{ user }}`. Requests that
260/// never render a template (JSON/API responses) pay zero DB reads.
261/// When the resolver does run it performs the session + user lookup
262/// and memoizes the result for the rest of the request.
263///
264/// Opt in via [`crate::AuthPlugin::with_user_in_templates`] when the
265/// app is HTML-heavy; leave off for REST-only services.
266pub async fn user_context_layer(
267 req: axum::extract::Request,
268 next: axum::middleware::Next,
269) -> axum::response::Response {
270 // Install a LAZY resolver instead of resolving eagerly. The closure runs
271 // (at most once) only if a template actually reads `user`; a JSON/API
272 // response that never renders the template pays nothing.
273 let headers = req.headers().clone();
274 let lazy = umbral::templates::LazyUser::new(move || {
275 let headers = headers.clone();
276 async move {
277 match current_user(&headers).await {
278 Ok(Some(u)) => serialize_authenticated_with_relations(&u).await,
279 _ => anonymous_user_value(),
280 }
281 }
282 });
283 umbral::templates::with_current_user_lazy(lazy, next.run(req)).await
284}
285
286/// Depth cap for the recursive relation expansion in
287/// [`serialize_authenticated_with_relations`]. Closes gap2 #14:
288/// templates can write `user.customer.loyalty_points` and get the
289/// resolved value without the handler having to declare the prefetch.
290///
291/// Why 2: covers the common case `user.<one_to_one>.<scalar>` (1 hop
292/// to load the child, scalars are free) AND `user.<one_to_one>.<fk>`
293/// (2 hops if the template wants to walk back into another object).
294/// Beyond 2 the query budget grows with the graph fan-out and the
295/// "templates pay for every relation, every request" trade-off
296/// stops being honest.
297const USER_RELATION_DEPTH: usize = 2;
298
299async fn serialize_authenticated_with_relations(user: &AuthUser) -> umbral::templates::Value {
300 let mut json = match serde_json::to_value(user) {
301 Ok(serde_json::Value::Object(map)) => map,
302 _ => serde_json::Map::new(),
303 };
304 json.insert(
305 "is_authenticated".to_string(),
306 serde_json::Value::Bool(true),
307 );
308
309 // gap2 #14: recursively expand reverse-O2O and forward-FK
310 // relations on the serialized user, up to `USER_RELATION_DEPTH`
311 // hops, with `(table, pk)` cycle detection so
312 // `user.customer.user.customer...` terminates.
313 //
314 // PK lift Pass C: the visited set keys on `(table_name,
315 // pk_json_key(value))` so non-i64 user PKs (UUID-keyed
316 // AuthUser variants, codename-keyed permissions, etc.) ride
317 // through the same cycle detector. The pre-fix shape was
318 // `HashSet<(String, i64)>` which silently coerced everything
319 // to i64 and broke for any UserModel impl with a non-i64 PK.
320 //
321 // The auth_user table must exist in the registry (AuthPlugin
322 // registers it during App::build); if for some reason it's
323 // missing we silently fall back to the un-expanded user JSON
324 // rather than failing the request.
325 let registered = umbral::migrate::registered_models();
326 if let Some(meta) = registered.iter().find(|m| m.table == "auth_user") {
327 let mut visited: std::collections::HashSet<(String, String)> =
328 std::collections::HashSet::new();
329 let seed_pk = serde_json::Value::Number(user.id.into());
330 visited.insert(("auth_user".to_string(), pk_json_key(&seed_pk)));
331 expand_relations(
332 meta,
333 ®istered,
334 &mut json,
335 USER_RELATION_DEPTH,
336 &mut visited,
337 )
338 .await;
339 }
340
341 umbral::templates::Value::from_serialize(serde_json::Value::Object(json))
342}
343
344/// Recursive depth-bounded expansion of a row's FK relations
345/// (gap2 #14). Mutates `row` in place to:
346///
347/// - Replace every forward-FK integer id with the resolved target
348/// row (when known to the model registry).
349/// - Inject every reverse-OneToOne candidate (child models with a
350/// UNIQUE FK pointing at `meta`) under the child table's name as
351/// the key — so `Customer { user: ForeignKey<AuthUser> (unique) }`
352/// surfaces as `user.customer` on the parent.
353///
354/// `visited` carries `(table, pk)` pairs already loaded in this
355/// expansion. New rows are checked against it before recursion and
356/// inserted before descending, so any cycle in the FK graph
357/// terminates at the first revisit.
358///
359/// One query per loaded relation per request — the middleware's
360/// query budget grows by `count(relations within depth)`, not by
361/// the fan-out of subsequent template renders. Sparse relation
362/// graphs (the common case) add 1-3 queries; pathological graphs
363/// hit the depth cap and stop.
364fn expand_relations<'a>(
365 meta: &'a umbral::migrate::ModelMeta,
366 registered: &'a [umbral::migrate::ModelMeta],
367 row: &'a mut serde_json::Map<String, serde_json::Value>,
368 depth: usize,
369 visited: &'a mut std::collections::HashSet<(String, String)>,
370) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
371 Box::pin(async move {
372 if depth == 0 {
373 return;
374 }
375
376 // -- Forward FKs: replace integer / string / UUID ids with
377 // the full target row. Mirrors the dynamic
378 // `select_related_dyn` semantics (gap2 #15) but driven by
379 // the registry walk here so the user middleware doesn't
380 // need to know which columns to expand ahead of time.
381 //
382 // PK lift Pass C: read the FK value as `serde_json::Value`
383 // (not i64) so non-integer-PK targets (codename-keyed
384 // permissions, UUID-keyed custom user models, etc.) flow
385 // through. The cycle key uses `pk_json_key` so a numeric
386 // 42 and a string "42" stay in different buckets.
387 let forward_fks: Vec<(String, String, serde_json::Value)> = meta
388 .fields
389 .iter()
390 .filter_map(|col| {
391 let target_table = col.fk_target.as_deref()?;
392 let fk_val = row.get(&col.name)?.clone();
393 if fk_val.is_null() {
394 return None;
395 }
396 Some((col.name.clone(), target_table.to_string(), fk_val))
397 })
398 .collect();
399 for (col_name, target_table, fk_val) in forward_fks {
400 let visit_key = (target_table.clone(), pk_json_key(&fk_val));
401 if visited.contains(&visit_key) {
402 continue;
403 }
404 let Some(target_meta) = registered.iter().find(|m| m.table == target_table) else {
405 continue;
406 };
407 let Some(target_pk) = target_meta.pk_column() else {
408 continue;
409 };
410 let fetched = umbral::orm::DynQuerySet::for_meta(target_meta)
411 .filter_eq_string(&target_pk.name, &json_value_to_pk_string(&fk_val))
412 .first_as_json()
413 .await;
414 let Ok(Some(mut target_row)) = fetched else {
415 continue;
416 };
417 visited.insert(visit_key);
418 expand_relations(target_meta, registered, &mut target_row, depth - 1, visited).await;
419 row.insert(col_name, serde_json::Value::Object(target_row));
420 }
421
422 // -- Reverse-O2O: child models with a UNIQUE FK to this
423 // table get injected under the child's table name. Naming
424 // convention uses the lower-case-model-name idiom
425 // (`Customer { user: FK<User> (unique) }` → `user.customer`).
426 let Some(parent_pk_col) = meta.pk_column() else {
427 return;
428 };
429 // PK lift Pass C: parent PK as `serde_json::Value`, not i64.
430 let parent_pk = match row.get(&parent_pk_col.name).cloned() {
431 Some(v) if !v.is_null() => v,
432 _ => return,
433 };
434 let candidates: Vec<(&umbral::migrate::ModelMeta, String)> = registered
435 .iter()
436 .filter_map(|child| {
437 // Need exactly one UNIQUE FK pointing at this
438 // table; ambiguous matches (e.g. `primary_user` +
439 // `backup_user` both UNIQUE FKs to auth_user) are
440 // skipped — there's no single right answer for
441 // which one becomes `user.customer`.
442 let mut matches = child
443 .fields
444 .iter()
445 .filter(|c| c.fk_target.as_deref() == Some(&meta.table) && c.unique);
446 let first = matches.next()?;
447 if matches.next().is_some() {
448 return None;
449 }
450 Some((child, first.name.clone()))
451 })
452 .collect();
453 for (child_meta, fk_col_name) in candidates {
454 // Don't clobber a same-named scalar on the parent — if
455 // a model genuinely names a column the same as its
456 // child table (rare), the existing column wins.
457 if row.contains_key(&child_meta.table) {
458 continue;
459 }
460 let fetched = umbral::orm::DynQuerySet::for_meta(child_meta)
461 .filter_eq_string(&fk_col_name, &json_value_to_pk_string(&parent_pk))
462 .first_as_json()
463 .await;
464 let Ok(Some(mut child_row)) = fetched else {
465 continue;
466 };
467 let Some(child_pk_col) = child_meta.pk_column() else {
468 continue;
469 };
470 // PK lift Pass C: child PK as `serde_json::Value`.
471 let Some(child_pk) = child_row.get(&child_pk_col.name).cloned() else {
472 continue;
473 };
474 if child_pk.is_null() {
475 continue;
476 }
477 let visit_key = (child_meta.table.clone(), pk_json_key(&child_pk));
478 if visited.contains(&visit_key) {
479 continue;
480 }
481 visited.insert(visit_key);
482 expand_relations(child_meta, registered, &mut child_row, depth - 1, visited).await;
483 row.insert(
484 child_meta.table.clone(),
485 serde_json::Value::Object(child_row),
486 );
487 }
488 })
489}
490
491/// PK lift Pass C: stable cycle-key for the `visited` HashSet in
492/// [`expand_relations`]. `serde_json::Value` isn't `Hash`, so we
493/// flatten to a namespaced `String` per shape. Mirrors the
494/// `pk_json_key` helper in `umbral-core::orm::dynamic` — kept local
495/// here to avoid widening `umbral-core`'s pub surface for one tiny
496/// helper. If a third call site needs the same namespacing, the
497/// two should converge into one canonical pub fn in the facade.
498fn pk_json_key(v: &serde_json::Value) -> String {
499 match v {
500 serde_json::Value::Number(n) => format!("n:{n}"),
501 serde_json::Value::String(s) => format!("s:{s}"),
502 other => format!("o:{other}"),
503 }
504}
505
506/// Render a PK JSON value as the string `DynQuerySet::filter_eq_string`
507/// expects to bind against. `filter_eq_string` already coerces per the
508/// column's `SqlType` so the right operand type lands on the wire —
509/// we just need to hand it the value's `Display` form.
510fn json_value_to_pk_string(v: &serde_json::Value) -> String {
511 match v {
512 serde_json::Value::Number(n) => n.to_string(),
513 serde_json::Value::String(s) => s.clone(),
514 other => other.to_string(),
515 }
516}
517
518fn anonymous_user_value() -> umbral::templates::Value {
519 let mut json = serde_json::Map::new();
520 json.insert(
521 "is_authenticated".to_string(),
522 serde_json::Value::Bool(false),
523 );
524 umbral::templates::Value::from_serialize(serde_json::Value::Object(json))
525}
526
527// logout is now a proper `pub async fn` in `crate` (lib.rs) that wraps
528// `umbral_sessions::logout` and maps the error to `AuthError::Session`.
529// It is the single reusable logout for all surfaces. The old forwarding
530// alias that returned `SessionError` has been removed.