forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB914d095Ahmet Kaan Gümüş2 months ago454 commits

module_gate.rs71 lines, 2.9 KiBHistoryRawDownload
1//! The structural half of module entitlements: one gate per router nest.
2//!
3//! A module *is* a nest, so the gate is applied once where the nest is mounted
4//! (`crate::build_router`) rather than route by route — there is no list of
5//! guarded routes to keep in step with a list of routes, and a route added to
6//! a gated nest is gated by construction.
7//!
8//! It goes on with `route_layer`, not `layer`: an unmatched path inside a
9//! disabled nest must still be a `404`. A disabled module is not a wall around
10//! a URL space, it is a refusal on the routes that exist.
11//!
12//! # The nested exceptions
13//!
14//! Four route pairs live under `/courses` but belong to another module, and so
15//! carry a second gate of their own ([`crate::web::courses`] splits them out
16//! for exactly this):
17//!
18//! - `/courses/{id}/exams` → `Module::Exams`
19//! - `/courses/{id}/sessions` → `Module::Sessions`
20//! - `/courses/{id}/subjects` → `Module::Subjects`
21//! - `/courses/{id}/homework` → `Module::Homework`
22//!
23//! Both gates run (the child's, then the outer `/courses` one), so either
24//! module being off refuses — which is right: `POST /courses/{id}/exams` is an
25//! exams feature reached through a course.
26//!
27//! One surface this cannot reach: `crate::ai::server`'s blob stream, which
28//! answers `BlobRequest`s without going through the router at all. It checks
29//! `Module::CourseNotes` itself.
30
31use axum::extract::{Request, State};
32use axum::middleware::{Next, from_fn_with_state};
33use axum::response::{IntoResponse, Response};
34use utoipa_axum::router::OpenApiRouter;
35
36use crate::error::AppError;
37use crate::module::Module;
38use crate::state::AppState;
39use crate::web::tenant_state::resolve_tenant;
40
41/// Wrap every route currently in `router` in `module`'s entitlement check.
42///
43/// Takes and returns the router rather than handing back a bare `Layer`: the
44/// `from_fn_with_state` layer's type is unnameable (it wraps an async closure),
45/// so a function returning it could not be written down without boxing. The
46/// call site reads the same either way — `gate(notes::routes(), &state,
47/// Module::Notes)`.
48pub fn gate(
49 router: OpenApiRouter<AppState>,
50 state: &AppState,
51 module: Module,
52) -> OpenApiRouter<AppState> {
53 router.route_layer(from_fn_with_state((state.clone(), module), check))
54}
55
56async fn check(
57 State((state, module)): State<(AppState, Module)>,
58 request: Request,
59 next: Next,
60) -> Response {
61 let (mut parts, body) = request.into_parts();
62 match resolve_tenant(&mut parts, &state).await {
63 Err(err) => err.into_response(),
64 Ok(tenant) if !tenant.modules.contains(module) => {
65 AppError::ModuleDisabled(module).into_response()
66 }
67 // `resolve_tenant` memoized the school on `parts`, so the handler's own
68 // extractors reuse this read instead of making a second one.
69 Ok(_) => next.run(Request::from_parts(parts, body)).await,
70 }
71}