Skip to main content

Crate ch_auth_macros

Crate ch_auth_macros 

Source
Expand description

§ch-auth-macros

An authorization attribute for Axum handlers using ch-auth.

#[auth(...)] checks the application’s role before the handler runs and makes the authorized account available as current_user.

ⓘ
use ch_auth_macros::auth;

#[auth(accounts::Role::User)]
#[route(GET "/api/account")]
async fn account(auth_session: AuthSession) -> Json<AccountResponse> {
    Json(AccountResponse::from(current_user))
}

Place #[auth(...)] above the route attribute. The default session argument is auth_session. Use session = my_session for another argument name. Custom session types implement ch_auth::authorization::AccountSession.

Roles can use a qualified path as above. For #[auth(USER)], re-export your role type as AuthRole at the crate root.

The macro supports concrete responses, Result<T, E> and impl IntoResponse. For return type aliases, choose the failure behavior with failure = result, failure = response or failure = into_response.

See the API documentation for details. Requires Rust 1.99 or newer. Licensed under the Sustainable Use License 1.0.

Attribute Macros§

auth
Protects an asynchronous Axum handler with an account role check.