#guard #layer #extracted #data #logic #router #request

axum_guard_logic

Compare extracted and expected data at the router layer with logic

9 releases

0.3.3 Dec 28, 2022
0.3.2 Dec 28, 2022
0.3.1 Sep 29, 2022
0.2.3 Aug 29, 2022
0.1.0 Aug 27, 2022

#991 in HTTP server

32 downloads per month

MIT license

30KB
667 lines

axum_guard_logic

https://crates.io/crates/axum_guard_logic

Impl Guard for T

pub trait Guard {
    fn check_guard(&self, expected:&Self) -> bool;
}

Where the values for self are extracted from the request based on FromRequestParts<State> for T and the values for expected are given inside the layer on the route.

Router::with(state.clone())
            .route("/", get(ok))
            .layer(
                    GuardService::new(
                        state.clone(),
                        expected.clone()).into_layer()
            );

The argument state passed to GuardService::new() will be the state called inside the FromRequestParts implementation on T

You can also nest using AND/OR logic.

Router::new()
    .route("/", get(ok))
    .layer(
        GuardService::new(state.clone(), expected)
            .and(
                GuardService::new(
                    other_state.clone(), 
                    other_expected
                )
            )
        .into_layer()
);

Will reject StatusCode::UNAUTHORIZED when check_guard returns false.

Dependencies

~6–12MB
~143K SLoC