seiva-http 0.2.2

Delonix Seiva — HTTP transport (axum/tower): RFC 9457 problems, typed/validated bodies, descriptor-driven permission guard, health endpoints. DORM-free.
Documentation
//! Delonix Seiva — the HTTP transport.
//!
//! This crate provides the transport-level pieces a module's generated
//! controller builds on. It is **demo-free and DORM-free**: it links no example
//! module and no data layer, so it is a publishable building block. A module's
//! `#[seiva::controller]` router and the composition root (`seiva-server` or a
//! project's `apps/<name>-api`) merge the module routes into [`router`].
//!
//! - [`problem`] — RFC 9457 `application/problem+json` errors ([`Problem`]).
//! - [`valid`] — a typed, validated JSON body extractor ([`Valid`], [`Validate`]).
//! - [`guard`] — a descriptor-driven permission guard ([`permission_guard`],
//!   [`Principal`], [`RequiredPermission`]) plus tenant-ownership checks.
//! - [`bus`] — the context-bound pipeline behaviors ([`Authorize`],
//!   [`Idempotent`]) that complete the ADR 0020 chain over the `seiva-cqrs`
//!   [`Validate`](seiva_cqrs::Validate)/[`Transactional`](seiva_cqrs::Transactional).
//! - [`security`] — secure-by-default response headers and configurable CORS.
//! - [`mod@rate_limit`] — a per-client request rate limiter.
//! - [`health`] — `/health/live` and `/health/ready` over a
//!   `Starting -> Ready -> Draining` lifecycle ([`Health`]).
//! - [`docs`] — an interactive `/docs` API reference over `/openapi.json`.
//! - [`mod@request_id`] — per-request correlation ([`RequestId`]).
//!
//! [`router`] assembles the framework surface — health, `/openapi.json`
//! (generated from every facade linked into the binary) and `/docs` — wrapped in
//! the security headers, optional CORS, the body-size limit, the rate limiter and
//! the request-id middleware. It mounts no module route itself; the composition
//! root merges those in.

#![forbid(unsafe_code)]

pub mod audit;
pub mod authn;
pub mod backpressure;
pub mod bus;
pub mod ctx;
pub mod docs;
pub mod guard;
pub mod health;
pub mod idempotency;
pub mod metrics;
#[cfg(feature = "mtls")]
pub mod mtls;
pub mod problem;
pub mod rate_limit;
pub mod request_id;
pub mod security;
pub mod valid;

use axum::body::Bytes;
use axum::extract::DefaultBodyLimit;
use axum::http::{header, HeaderMap, StatusCode};
use axum::middleware::{from_fn, from_fn_with_state};
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use axum::Router;
use sha2::{Digest, Sha256};

pub use audit::audit;
pub use authn::{authn, AuthConfig, Authn};
pub use backpressure::{with_route_controls, RouteControls, RouteLimit};
pub use bus::{Authorize, Idempotent, DEFAULT_IDEMPOTENCY_TTL};
pub use ctx::Ctx;
pub use docs::{docs_handler, redoc_handler, scalar_handler, DOCS_HTML};
pub use guard::{permission_guard, require_permission, PrincipalExt, RequiredPermission, Scoped};
pub use health::{
    health_router, health_router_with_dependency, Health, HealthState, ReadinessCheck,
};
pub use idempotency::{
    fingerprint, in_progress_problem, mismatch_problem, scoped_key, IdempotencyKey,
    OptionalIdempotencyKey, IDEMPOTENCY_HEADER, MAX_KEY_LEN,
};
pub use metrics::{track_metrics, REQUESTS_IN_FLIGHT, REQUESTS_TOTAL, REQUEST_DURATION_SECONDS};
pub use problem::{Problem, ProblemResponse, PROBLEM_JSON};
pub use rate_limit::{
    rate_limit, rate_limit_with_store, DistributedRateLimit, RateLimit, RateLimiter,
};
pub use request_id::{request_id, RequestId, REQUEST_ID_HEADER};
pub use security::{cors_layer, security_headers, HttpSecurity};
pub use seiva_ports::{AuditEntry, AuditSink};
pub use seiva_ports::{RateLimitDecision, RateLimitStore};
pub use valid::{Valid, Validate, ValidationError};
// The identity model is owned by `seiva-identity` (transport-free, shared with
// MCP); re-exported here so a `seiva_http::Principal` import keeps working.
pub use seiva_identity::{
    ApiKeyAuthenticator, AuthError, Authenticator, JwtAuthenticator, Principal, TokenAuthenticator,
};

/// The default maximum request body size (1 MiB). Oversized bodies are rejected
/// before a handler runs. Mounted as [`DefaultBodyLimit`] in [`router`].
pub const DEFAULT_BODY_LIMIT_BYTES: usize = 1024 * 1024;

/// The default per-request timeout (30 s). A request whose handler (or whose slow
/// client streaming the body) outlives this is cut off with `408 Request Timeout`,
/// so a slowloris client cannot pin a worker indefinitely (OWASP API4). Mounted as
/// a [`tower_http::timeout::TimeoutLayer`] in the framework layers.
pub const DEFAULT_REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

/// Builds the service's OpenAPI 3.1 document from **every facade linked into the
/// binary**, discovered through [`seiva::contract::registered_facades`]
/// (each `#[seiva::facade]` registers itself in a link-time inventory). Nothing
/// here is hand-written and no central list is kept: a module mounted into the
/// runtime contributes its operations automatically (OWASP API9). This crate
/// links no facade of its own, so a plain `seiva-http` binary documents nothing;
/// the full runtime (see `seiva-server`) documents every mounted module.
pub fn openapi_document() -> serde_json::Value {
    let facades = seiva::contract::registered_facades();
    seiva_openapi::openapi_document(
        seiva_openapi::ApiInfo {
            title: "Delonix Seiva service".to_string(),
            version: seiva::VERSION.to_string(),
        },
        &facades,
    )
}

/// `GET /openapi.json` — serves the generated [`openapi_document`].
///
/// The document is derived entirely from the link-time facade inventory, so it is
/// immutable for the life of the process. It is therefore generated and serialized
/// **once** (behind a [`OnceLock`](std::sync::OnceLock)) and every request serves
/// the cached bytes — an `O(1)` [`Bytes`] clone, with no regeneration and no
/// re-serialization. The response carries a strong `ETag` (SHA-256 of the body)
/// and `Cache-Control`, and a conditional request whose `If-None-Match` matches is
/// answered `304 Not Modified`. This keeps an unauthenticated route from being a
/// cheap regenerate-per-request DoS vector (OWASP API4).
async fn openapi_handler(headers: HeaderMap) -> Response {
    let cached = cached_openapi();
    if let Some(inm) = headers.get(header::IF_NONE_MATCH) {
        if inm.as_bytes() == cached.etag.as_bytes() {
            return StatusCode::NOT_MODIFIED.into_response();
        }
    }
    (
        [
            (header::CONTENT_TYPE, "application/json"),
            (header::ETAG, cached.etag.as_str()),
            (header::CACHE_CONTROL, "public, max-age=300"),
        ],
        cached.body.clone(),
    )
        .into_response()
}

/// The generated OpenAPI document, serialized once per process.
struct CachedOpenApi {
    /// The serialized `application/json` body. Cloning it per request is an `O(1)`
    /// reference-count bump, not a copy of the bytes.
    body: Bytes,
    /// A strong validator: the quoted SHA-256 hex of `body` (RFC 9110 §8.8.3).
    /// Deterministic across replicas running the same binary.
    etag: String,
}

/// Builds (once) and returns the cached `/openapi.json` payload. The document is
/// immutable after link time, so the initializer runs exactly once per process.
fn cached_openapi() -> &'static CachedOpenApi {
    static CACHE: std::sync::OnceLock<CachedOpenApi> = std::sync::OnceLock::new();
    CACHE.get_or_init(|| {
        let body = serde_json::to_vec(&openapi_document())
            .expect("the generated OpenAPI document always serializes");
        let etag = format!("\"{}\"", idempotency::hex(Sha256::digest(&body).as_slice()));
        CachedOpenApi {
            body: Bytes::from(body),
            etag,
        }
    })
}

/// The `(version, resource)` route-prefix collisions among **every facade linked
/// into the binary** — two facades whose generated routers would mount on the same
/// prefix and make `Router::merge` panic at startup. Empty when the mounted
/// facades are collision-free. A composition root calls this at boot to fail fast
/// with a clear message instead of a bare axum panic (see
/// [`seiva::contract::route_collisions`]).
#[must_use]
pub fn route_collisions() -> Vec<String> {
    seiva::contract::route_collisions(&seiva::contract::registered_facades())
}

/// Builds the framework router with the **default** security posture
/// ([`HttpSecurity::default`]: secure response headers on, same-origin only — no
/// CORS). Equivalent to [`router_with_security`] with the default configuration;
/// the zero-config entrypoint.
pub fn router(health: Health) -> Router {
    router_with_security(health, &HttpSecurity::default())
}

/// Builds the framework router — the health endpoints, `/openapi.json` and
/// `/docs`. Request flows through the layers outermost-first:
/// [`request_id()`] → [`security_headers`] → the rate limiter → the optional CORS
/// layer → the body-size limit → the route.
///
/// `security_headers` and `request_id` are the **outermost** layers on purpose:
/// every response carries them even when an outer layer short-circuits before the
/// route runs — a rate-limiter `429` or a CORS preflight included. (With the
/// headers mounted inside the rate limiter, a `429` went out naked — no CSP, no
/// `nosniff`, no HSTS — measured end-to-end.)
///
/// It mounts no module route; the composition root merges each module's generated
/// controller router into the result. `security` controls the transport-level
/// API8 defences: the response headers are always applied, and CORS is mounted
/// only when [`HttpSecurity::cors_allowed_origins`] is non-empty (otherwise the
/// browser enforces same-origin).
pub fn router_with_security(health: Health, security: &HttpSecurity) -> Router {
    router_with_auth(health, security, AuthConfig::none())
}

/// Builds the framework router under `security` and an optional authentication
/// layer, described by `auth` (an [`AuthConfig`], or any `Option<Arc<dyn
/// Authenticator>>` which converts into a bearer-only one). When it carries any
/// authenticator, every request is authenticated (ADR 0006) ahead of any route's
/// [`permission_guard`] — a missing credential becomes [`Principal::anonymous`]
/// (public routes still serve, guarded routes `403`), a present-but-invalid one
/// is a `401`. [`AuthConfig::none`] mounts no authn layer (no caller is ever
/// authenticated, so guarded routes answer `403`): the composition root supplies
/// an [`Authenticator`] to actually admit callers. Pass
/// [`AuthConfig::with_api_key`] to also admit opaque `X-API-Key` callers.
///
/// Layer order, outermost-first: [`request_id()`] → [`security_headers`] →
/// [`authn()`] → the rate limiter → CORS → the body-size limit → the route.
/// `authn` sits inside the header layers (so a `401` still carries them) but
/// outside the routes (so it runs before each route's guard).
pub fn router_with_auth(
    health: Health,
    security: &HttpSecurity,
    auth: impl Into<AuthConfig>,
) -> Router {
    apply_framework_layers(
        framework_routes(health, security.expose_docs),
        security,
        auth.into(),
        None,
    )
}

/// Builds the full application router: the framework routes **plus the caller's
/// `modules`**, wrapped in the framework layers — the composition a real
/// `apps/<name>-api` needs.
///
/// Why this exists: axum's [`Router::layer`](axum::Router::layer) wraps only the
/// routes already present, so `router_with_auth(..).merge(modules)` would leave
/// the module routes **outside** every middleware — no authn, no security
/// headers, no rate limit. Merging the modules *before* layering (what this does)
/// is the only order that applies the framework's guarantees to module routes.
/// Mount each module's generated controller router into `modules` and pass it here.
///
/// `auth` is an [`AuthConfig`] (or any `Option<Arc<dyn Authenticator>>`, which
/// converts into a bearer-only one): pass [`AuthConfig::with_api_key`] to also
/// admit opaque `X-API-Key` callers against the module routes.
pub fn router_with_modules(
    health: Health,
    security: &HttpSecurity,
    auth: impl Into<AuthConfig>,
    modules: Router,
) -> Router {
    apply_framework_layers(
        framework_routes(health, security.expose_docs).merge(modules),
        security,
        auth.into(),
        None,
    )
}

/// Like [`router_with_modules`], but enforces the per-client rate limit with a
/// **distributed** store ([`DistributedRateLimit`], ADR 0015) instead of the
/// per-process limiter — the form that holds ONE budget across replicas behind a
/// load balancer. The distributed limiter takes the in-process one's exact layer
/// position (inside the security headers, so a `429` still carries them), so a
/// `429` keeps its correlation id and headers; `security.rate_limit` is ignored
/// when a distributed limiter is supplied. Set `security.trust_forwarded_for`
/// (and pass it through on the limiter) only behind a single trusted proxy.
pub fn router_with_modules_distributed(
    health: Health,
    security: &HttpSecurity,
    auth: impl Into<AuthConfig>,
    modules: Router,
    distributed: DistributedRateLimit,
) -> Router {
    apply_framework_layers(
        framework_routes(health, security.expose_docs).merge(modules),
        security,
        auth.into(),
        Some(distributed),
    )
}

/// The framework's own routes, unlayered: health, `/openapi.json`, and the three
/// interactive reference pages (`/docs` = Swagger UI, `/redoc` = ReDoc,
/// `/scalar` = Scalar) — the FastAPI `/docs` + `/redoc` parity, served by default
/// with no annotations from the developer (see [`docs`]).
///
/// There is deliberately **no** `/admin` here: the admin console is an optional
/// extension (`seiva-console-admin`, ADR 0008), not part of the base. A backend
/// built without that crate answers `404` for `/admin`; a composition root that
/// wants it calls `seiva_console_admin::mount(router)` before serving.
fn framework_routes(health: Health, expose_docs: bool) -> Router {
    let mut router = Router::new().merge(health_router(health));
    // The contract-discovery surface is opt-out: on by default (FastAPI parity),
    // but a deployment that must not publish its contract to anonymous callers
    // (OWASP API9) sets `HttpSecurity::expose_docs = false` and these answer `404`.
    if expose_docs {
        router = router
            .route("/openapi.json", get(openapi_handler))
            .route("/docs", get(docs::docs_handler))
            .route("/redoc", get(docs::redoc_handler))
            .route("/scalar", get(docs::scalar_handler));
    }
    router
}

/// Wraps `app` (framework routes, and any merged module routes) in the transport
/// middleware. Layer order, outermost-first: [`request_id()`] →
/// [`security_headers`] → [`authn()`] → the rate limiter → CORS → the body-size
/// limit → the route. `authn` sits inside the header layers (so a `401` still
/// carries them) but outside the routes (so it runs before each route's guard).
fn apply_framework_layers(
    app: Router,
    security: &HttpSecurity,
    auth: AuthConfig,
    distributed: Option<DistributedRateLimit>,
) -> Router {
    let mut app = app
        .layer(DefaultBodyLimit::max(DEFAULT_BODY_LIMIT_BYTES))
        // Inside the body limit: bound the lifetime of every request so a slow or
        // stuck handler (or a slowloris client) cannot hold a worker forever. A
        // breach returns `408 Request Timeout`.
        .layer(tower_http::timeout::TimeoutLayer::with_status_code(
            axum::http::StatusCode::REQUEST_TIMEOUT,
            DEFAULT_REQUEST_TIMEOUT,
        ));

    if let Some(cors) = cors_layer(security) {
        app = app.layer(cors);
    }

    // The distributed limiter, when supplied, takes the in-process one's place at
    // this exact layer position; otherwise the per-process limiter runs if the
    // security config enables it.
    if let Some(dist) = distributed {
        app = app.layer(from_fn_with_state(dist, rate_limit_with_store));
    } else if let Some(limit) = security.rate_limit {
        let limiter = RateLimiter::new(limit).trusting_forwarded_for(security.trust_forwarded_for);
        app = app.layer(from_fn_with_state(limiter, rate_limit));
    }

    if let Some(authn_state) = auth.into_authn() {
        app = app.layer(from_fn_with_state(authn_state, authn));
    }

    // Outermost: so a short-circuited response (a 429, a 401, a CORS preflight)
    // still leaves through the security headers and gets a correlation id.
    app = app
        .layer(from_fn(security_headers))
        .layer(from_fn(request_id));

    // The metrics layer is the very outermost so its latency histogram and
    // in-flight gauge measure the WHOLE handling (rate limit, auth, route). It
    // reads the matched route TEMPLATE for a low-cardinality label and is a no-op
    // until a recorder is installed at the root (ADR 0027); health probes are
    // exempt inside the middleware.
    app.layer(from_fn(metrics::track_metrics))
}