expression

Axum: deny by default

When policy information is missing or does not recognize an action, do not infer permission. The example uses a pure policy with a fixed set of allowed actions, independent of any credentials. In-memory standalone Cargo example. Expected stdout (not executed): true false false false Common mistake: Allowing unknown actions through a permissive fallback.

Formal Syntax & Structure

rust syntaxrust
match permission { Some(allowed) => ..., _ => deny }

Working Example

Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    #[derive(Clone, Copy)] enum Role { Reader, Writer }    fn permits(role: Option<Role>, action: &str) -> bool {        matches!((role, action), (Some(Role::Reader), "read") | (Some(Role::Writer), "read" | "write"))    }    for (role, action) in [(Some(Role::Reader), "read"), (Some(Role::Reader), "write"), (Some(Role::Writer), "delete"), (None, "read")] {        println!("{}", permits(role, action));    }}
Rust Reference