Module auth

Source
Expand description

Authentication middleware for the web dashboard.

This module provides authentication functionality including basic auth verification, rate limiting for failed attempts, and account lockout mechanisms.

§Examples

§Basic Authentication Setup

use hammerwork_web::auth::{AuthState, extract_basic_auth};
use hammerwork_web::config::AuthConfig;

let auth_config = AuthConfig {
    enabled: true,
    username: "admin".to_string(),
    password_hash: "plain_password".to_string(), // Use bcrypt in production
    ..Default::default()
};

let auth_state = AuthState::new(auth_config);
assert!(auth_state.is_enabled());

§Extracting Basic Auth Credentials

use hammerwork_web::auth::extract_basic_auth;

// "admin:password" in base64 is "YWRtaW46cGFzc3dvcmQ="
let auth_header = "Basic YWRtaW46cGFzc3dvcmQ=";
let (username, password) = extract_basic_auth(auth_header).unwrap();

assert_eq!(username, "admin");
assert_eq!(password, "password");

// Invalid format returns None
let result = extract_basic_auth("Bearer token123");
assert!(result.is_none());

Structs§

AuthState
Authentication middleware state

Enums§

AuthError
Custom authentication errors

Functions§

auth_filter
Authentication filter for Warp
extract_basic_auth
Extract basic auth credentials from request.
handle_auth_rejection
Handle authentication rejections