Module auth

Module auth 

Source
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 support
  • auth-axum: Axum extractors and middleware
  • auth-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 dependencies
  • auth-jwt: JWT validation using jsonwebtoken
  • auth-axum: Axum extractors and middleware
  • auth-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

FeatureDescription
authCore traits (this module)
auth-jwtJWT validation with HS256/RS256 support
auth-axumAxum extractors and middleware
auth-tonicgRPC interceptors

Re-exports§

pub use jwt::JwtAlgorithm;auth-jwt
pub use jwt::JwtConfig;auth-jwt
pub use jwt::JwtValidator;auth-jwt
pub use self::axum::AuthLayer;auth-axum
pub use self::axum::AuthenticatedUser;auth-axum
pub use self::tonic::AuthInterceptor;auth-tonic

Modules§

axumauth-axum
Axum integration for authentication.
jwtauth-jwt
JWT (JSON Web Token) validation.
tonicauth-tonic
Tonic (gRPC) integration for authentication.

Structs§

AuthContext
Context holding authenticated user information.

Enums§

AuthError
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.