rok-auth-basic 0.1.0

HTTP Basic authentication guard for the rok ecosystem
Documentation

rok-auth-basic

HTTP Basic Authentication middleware for Axum. Validates Authorization: Basic ... headers against a configurable credential store.

Installation

[dependencies]
rok-auth-basic = { version = "0.1" }

Quick Start

use rok_auth_basic::{BasicAuth, BasicAuthConfig};

// Static credentials (for internal APIs / health checks)
let basic = BasicAuth::static_credentials([
    ("admin", "secret-password"),
    ("monitor", "monitor-pass"),
]);

let app = Router::new()
    .route("/admin", get(admin_panel))
    .layer(basic.layer());

// Returns 401 + WWW-Authenticate header if credentials are missing or wrong

Dynamic Validation (database)

let basic = BasicAuth::new(BasicAuthConfig {
    realm: "My API".into(),
    validate: Arc::new(|username, password, pool| async move {
        let user = User::find_by_username(username, pool).await?;
        hasher.check(password, &user.password_hash).await
    }),
});

Extracting the Authenticated User

async fn admin(BasicAuth(user): BasicAuth<AdminUser>) -> impl IntoResponse {
    Json(json!({ "user": user.username }))
}