| Crates.io | axum-keycloak-auth |
| lib.rs | axum-keycloak-auth |
| version | 0.8.3 |
| created_at | 2023-02-27 15:36:34.900966+00 |
| updated_at | 2025-05-18 16:25:17.687291+00 |
| description | Protect axum routes with a JWT emitted by Keycloak. |
| homepage | |
| repository | https://github.com/lpotthast/axum-keycloak-auth |
| max_upload_size | |
| id | 796137 |
| size | 194,156 |
Protect axum routes with a JWT emitted by Keycloak.
AuthError is converted into a response. Giving the user control and the
ability to add context, roll their own.This library provides KeycloakAuthLayer, a tower layer/service implementation that parses and validates a JWT.
See the Documentation for more detailed instructions!
enum Role {
Administrator,
Unknown(String),
}
pub fn protected_router(instance: KeycloakAuthInstance) -> Router {
Router::new()
.route("/protected", get(protected))
.layer(
KeycloakAuthLayer::<Role>::builder()
.instance(instance)
.passthrough_mode(PassthroughMode::Block)
.build(),
)
}
pub async fn protected(Extension(token): Extension<KeycloakToken<Role>>) -> Response {
expect_role!(&token, Role::Administrator);
info!("Token payload is {token:#?}");
(
StatusCode::OK,
format!(
"Hello {name} ({subject}). Your token is valid for another {valid_for} seconds.",
name = token.extra.profile.preferred_username,
subject = token.subject,
valid_for = (token.expires_at - time::OffsetDateTime::now_utc()).whole_seconds()
),
).into_response()
}
| axum-keycloak-auth | axum |
|---|---|
| 0.2 | 0.6 |
| 0.3 - 0.6 | 0.7 |
| 0.7 - 0.8 | 0.8 |
Run test with
cargo test
Pass the --nocapture flag when developing to be able to see log/tracing output.
cargo test -- --nocapture