Skip to main content

require_bearer

Function require_bearer 

Source
pub fn require_bearer(
    expected: impl Into<String>,
) -> impl Fn(Router<()>) -> Router<()> + Clone + Send + 'static
Expand description

Returns a Router -> Router transform that enforces Authorization: Bearer <token> on every request passing through the router it is applied to.

Returns 401 Unauthorized if the header is absent, malformed, or if the token does not match expected (compared in constant time to prevent timing oracles).

§Usage

Pass directly to .map() — the function signature matches .map()’s expected Fn(Router<()>) -> Router<()>:

use rust_api::prelude::*;

RouterPipeline::new()
    .mount_guarded::<AdminController, _>(admin_svc, || { /* config check */ })
    .map(require_bearer(admin_key))
    .build()?