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 supportcache
(enabled by default): Authentication result cachingregex
(enabled by default): Regex path matchingtiming-safe
(enabled by default): Timing-safe password comparisonbcrypt
: BCrypt password hash supportsecure-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§
- Auth
Cache - Auth cache with TTL and auto cleanup
- Auth
Metrics - Authentication metrics for monitoring
- Basic
Auth - Basic authentication middleware
- Basic
Auth Builder - Builder for BasicAuth, provides extra convenience methods
- Basic
Auth Config - Basic authentication config
- Basic
Auth Middleware - Basic authentication middleware
- Bcrypt
User Validator - BCrypt password validator (requires bcrypt feature)
- Cache
Config - Cache configuration
- Cache
Stats - Cache statistics
- Credentials
- User credentials
- LibInfo
- Library detailed info
- Path
Filter - Path filter for conditional authentication
- Static
User Validator - Static user list validator
Enums§
- Auth
Error - Authentication error type
Constants§
- VERSION
- Version info
Traits§
- User
Validator - 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§
- Auth
Result - Authentication result type