Crate ntex_basicauth

Source
Expand description

§ntex-basicauth

Secure and high-performance Basic Authentication middleware for the ntex web framework.

§Quick Start

use ntex::web;
use ntex_basicauth::BasicAuthBuilder;
use std::collections::HashMap;

#[ntex::main]
async fn main() -> std::io::Result<()> {

    web::HttpServer::new(move || {
        // Create authentication middleware
        let auth = BasicAuthBuilder::new()
            .user("admin", "secret")
            .user("user", "password")
            .realm("My Application")
            .build()
            .expect("Failed to configure authentication");
        web::App::new()
            .wrap(auth)
            .route("/protected", web::get().to(protected_handler))
            .route("/public", web::get().to(public_handler))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

async fn protected_handler() -> &'static str {
    "This is protected content!"
}

async fn public_handler() -> &'static str {
    "This is public content"
}

§Feature Flags

  • json (enabled by default): JSON error response support
  • cache (enabled by default): Authentication result caching
  • regex (enabled by default): Regex path matching
  • timing-safe (enabled by default): Timing-safe password comparison
  • bcrypt: BCrypt password hash support
  • secure-memory (enabled by default): Secure memory cleanup for passwords

Modules§

prelude
Prelude module, includes common types and traits

Macros§

auth_error
Convenience macro for creating errors
path_filter
Convenience macro for creating PathFilter

Structs§

AuthCache
Auth cache with TTL and auto cleanup
AuthMetrics
Authentication metrics for monitoring
BasicAuth
Basic authentication middleware
BasicAuthBuilder
Builder for BasicAuth, provides extra convenience methods
BasicAuthConfig
Basic authentication config
BasicAuthMiddleware
Basic authentication middleware
BcryptUserValidator
BCrypt password validator (requires bcrypt feature)
CacheConfig
Cache configuration
CacheStats
Cache statistics
Credentials
User credentials
LibInfo
Library detailed info
PathFilter
Path filter for conditional authentication
StaticUserValidator
Static user list validator

Enums§

AuthError
Authentication error type

Constants§

VERSION
Version info

Traits§

UserValidator
User validator trait for custom authentication logic

Functions§

auth_with_common_skips
Convenience function to create BasicAuth with common skip path rules
bcrypt_auth
Convenience function to create BasicAuth using BCrypt
extract_credentials
Extract authenticated user credentials from request
extract_credentials_web
Extract authenticated user credentials from WebRequest
get_username
Get authenticated username from request
is_user
Check if current user matches a specific username
is_valid_username
Validate if username format is valid
lib_info
Get library detailed info
multi_user_auth
Convenience function to create BasicAuth with multiple users
single_user_auth
Convenience function to create BasicAuth with a single user
version
Get library version info

Type Aliases§

AuthResult
Authentication result type