Skip to main content

Module auth

Module auth 

Source
Expand description

Authentication support for WebSocket connections WebSocket authentication support

This module provides authentication infrastructure for WebSocket connections, allowing token validation before the WebSocket upgrade completes.

§Example

use rustapi_ws::auth::{WsAuthConfig, TokenExtractor, TokenValidator, Claims};
use async_trait::async_trait;

struct MyTokenValidator;

#[async_trait]
impl TokenValidator for MyTokenValidator {
    async fn validate(&self, token: &str) -> Result<Claims, AuthError> {
        // Validate JWT or other token format
        Ok(Claims::new("user_123"))
    }
}

let config = WsAuthConfig::new(Box::new(MyTokenValidator))
    .extractor(TokenExtractor::Header("Authorization".to_string()));

Structs§

AcceptAllValidator
A simple token validator that accepts any non-empty token
Claims
Claims extracted from a validated token
RejectAllValidator
A token validator that rejects all tokens
StaticTokenValidator
A token validator that validates against a static list of valid tokens
WsAuthConfig
Configuration for WebSocket authentication

Enums§

AuthError
Error type for WebSocket authentication
TokenExtractor
Specifies where to extract the authentication token from

Traits§

TokenValidator
Trait for validating authentication tokens