Architecture Overview

Design Philosophy

The lighty-auth crate follows a trait-based design pattern that enables:

  • Modularity: Each authentication provider is independent and self-contained

  • Extensibility: Easy to add custom authentication methods

  • Type Safety: Compile-time validation of authentication flows

  • Async-First: Built on tokio for non-blocking I/O operations

Core Architecture

spinner

Authenticator Trait

The core abstraction that all authentication providers implement:

pub trait Authenticator {
    /// Primary authentication method
    async fn authenticate(
        &mut self,
        event_bus: Option<&EventBus>,
    ) -> AuthResult<UserProfile>;

    /// Optional: Verify if a token is still valid
    async fn verify(&self, token: &str) -> AuthResult<UserProfile> {
        Err(AuthError::Custom("Verification not supported".into()))
    }

    /// Optional: Invalidate a token
    async fn logout(&self, token: &str) -> AuthResult<()> {
        Ok(())
    }
}

Design Decisions:

  • &mut self for authenticate() allows providers to cache state (device codes, tokens)

  • event_bus is optional, enabling use without event system dependency

  • Default implementations for verify() and logout() allow simple providers to skip them

UserProfile Structure

The unified profile type returned by all authenticators:

Field Usage by Provider:

Field
Offline
Microsoft
Azuriom

id

❌ None

❌ None

✅ Server ID

username

✅ Input

✅ From profile

✅ From server

uuid

✅ Generated

✅ From profile

✅ From server

access_token

❌ None

✅ MC token

✅ Session token

email

❌ None

❌ None

✅ User email

email_verified

false

true

✅ From server

money

❌ None

❌ None

✅ From server

role

❌ None

❌ None

✅ From server

banned

false

false

✅ From server

Error Handling

Comprehensive error types with specific variants:

Authentication Flows

Offline Mode

spinner

Characteristics:

  • Synchronous (no network I/O)

  • Deterministic UUID generation

  • No token management

  • Instant response time

Microsoft OAuth2

spinner

Characteristics:

  • Asynchronous with polling

  • 6-step token exchange chain

  • User interaction required (browser)

  • Typical duration: 30-60 seconds

  • Error handling at each step

Azuriom CMS

spinner

Characteristics:

  • Single HTTP request (without 2FA)

  • Two requests (with 2FA)

  • Server-side validation

  • Role/permission support

  • Fast response time (< 1 second)

Event System Integration

When the events feature is enabled, authentication progress is broadcast:

Event Flow Example (Microsoft):

HTTP Client Architecture

All network-based providers share a global HTTP client:

Benefits:

  • Connection reuse across multiple authentications

  • Reduced memory footprint

  • Consistent network behavior

Thread Safety

Shared State

  • HTTP_CLIENT: Thread-safe via Arc + internal locking in reqwest

  • EventBus: Lock-free broadcast channels via tokio

  • Authenticator instances: Not thread-safe (designed for single-threaded use)

Concurrent Authentication

Security Considerations

Password Handling

  • Passwords are never stored persistently

  • Transmitted only over HTTPS

  • Cleared from memory after use (via Drop trait)

Token Storage

  • Access tokens should be encrypted at rest

  • Refresh tokens (Microsoft) enable re-authentication without credentials

  • Tokens should be rotated periodically

UUID Generation

  • Offline UUIDs use SHA1 (sufficient for deterministic generation)

  • Microsoft/Azuriom UUIDs are server-provided

  • UUID v5 namespace prevents collisions

Performance Characteristics

Provider
Network Requests
Typical Latency
Blocking Operations

Offline

0

< 1ms

Username validation only

Microsoft

6-8

30-60s

User authorization wait

Azuriom

1-2

100-500ms

HTTP roundtrip

Extension Points

Custom Providers

Implement Authenticator for custom backends:

Custom Event Types

Emit custom events during authentication:

Best Practices

Error Handling

Event Bus Usage

Token Management

Last updated