Skip to content

sjud/axum_guard_logic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages