# rok-auth-basic
HTTP Basic Authentication middleware for Axum. Validates `Authorization: Basic ...`
headers against a configurable credential store.
## Installation
```toml
[dependencies]
rok-auth-basic = { version = "0.1" }
```
## Quick Start
```rust
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)
```rust
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
```rust
async fn admin(BasicAuth(user): BasicAuth<AdminUser>) -> impl IntoResponse {
Json(json!({ "user": user.username }))
}
```