axum-keycloak-auth

Protect axum routes with a JWT emitted by Keycloak.

Note: This is still in an early stage and not security-audited.

Features

Planned

Usage

This library provides KeycloakAuthLayer, a tower layer/service implementation that parses and validates a JWT.

Note that this is an extremely abbreviated example.

See the Documentation for detailed instructions.

```rust enum Role { Administrator, Unknown(String), }

pub fn protectedrouter(decodingkey: Arc) -> Router { Router::new() .route("/protected", get(protected)) .layer( KeycloakAuthLayer::::builder() .decodingkey(decodingkey) .passthroughmode(PassthroughMode::Block) .persistraw_claims(false) .build(), ) }

pub async fn protected(Extension(token): Extension>) -> 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.full_name,
        subject = token.subject,
        valid_for = (token.expires_at - time::OffsetDateTime::now_utc()).whole_seconds()
    ),
).into_response()

} ```