Available on crate feature
auth only.Expand description
Authentication primitives with layered feature flags.
This module provides authentication infrastructure that can be used independently or integrated with your web framework:
auth: Core traits only (zero dependencies)auth-jwt: JWT validation with HS256/RS256 supportauth-axum: Axum extractors and middlewareauth-tonic: gRPC interceptors
§Example
ⓘ
use allframe_core::auth::{JwtValidator, JwtConfig, Authenticator};
let validator = JwtValidator::<Claims>::new(
JwtConfig::hs256("secret").with_issuer("my-app")
);
let claims = validator.authenticate("eyJ...").await?;Authentication primitives for AllFrame.
This module provides a layered authentication system:
auth(this module): Core traits with zero dependenciesauth-jwt: JWT validation usingjsonwebtokenauth-axum: Axum extractors and middlewareauth-tonic: gRPC interceptors
§Core Concepts
The authentication system is built around a few key traits:
- [
Authenticator]: Validates tokens and returns claims - [
Claims]: Marker trait for claim types - [
AuthContext]: Holds authenticated user information
§Example: Using Core Traits
use allframe_core::auth::{Authenticator, AuthError, AuthContext};
// Define your claims type
#[derive(Clone, Debug)]
struct MyClaims {
sub: String,
email: Option<String>,
}
// Implement your authenticator
struct MyAuthenticator;
#[async_trait::async_trait]
impl Authenticator for MyAuthenticator {
type Claims = MyClaims;
async fn authenticate(&self, token: &str) -> Result<Self::Claims, AuthError> {
// Your validation logic here
Ok(MyClaims {
sub: "user123".to_string(),
email: Some("user@example.com".to_string()),
})
}
}§Feature Flags
| Feature | Description |
|---|---|
auth | Core traits (this module) |
auth-jwt | JWT validation with HS256/RS256 support |
auth-axum | Axum extractors and middleware |
auth-tonic | gRPC interceptors |
Re-exports§
pub use jwt::JwtAlgorithm;auth-jwtpub use jwt::JwtConfig;auth-jwtpub use jwt::JwtValidator;auth-jwtpub use self::axum::AuthLayer;auth-axumpub use self::axum::AuthenticatedUser;auth-axumpub use self::tonic::AuthInterceptor;auth-tonic
Modules§
- axum
auth-axum - Axum integration for authentication.
- jwt
auth-jwt - JWT (JSON Web Token) validation.
- tonic
auth-tonic - Tonic (gRPC) integration for authentication.
Structs§
- Auth
Context - Context holding authenticated user information.
Enums§
- Auth
Error - Error type for authentication failures.
Traits§
- Authenticator
- Trait for types that can validate authentication tokens.
- HasExpiration
- Trait for claims that have an expiration time.
- HasSubject
- Trait for claims that have a subject (user ID).
Functions§
- extract_
bearer_ token - Extract bearer token from an authorization header value.