Skip to main content

Authenticator

Trait Authenticator 

Source
pub trait Authenticator: Send + Sync {
    // Required method
    fn authenticate<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
        &'life0 self,
        token: &'life1 str,
        connection_id: &'life2 str,
        device_info: Option<&'life3 DeviceInfo>,
        metadata: Option<&'life4 HashMap<String, Vec<u8>>>,
    ) -> Pin<Box<dyn Future<Output = Result<AuthResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait;

    // Provided methods
    fn is_authenticated<'life0, 'life1, 'async_trait>(
        &'life0 self,
        connection_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn revoke_authentication<'life0, 'life1, 'async_trait>(
        &'life0 self,
        connection_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

认证器 Trait

实现此 trait 以提供自定义的 token 验证逻辑 例如:JWT 验证、数据库查询、Redis 验证等

Required Methods§

Source

fn authenticate<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>( &'life0 self, token: &'life1 str, connection_id: &'life2 str, device_info: Option<&'life3 DeviceInfo>, metadata: Option<&'life4 HashMap<String, Vec<u8>>>, ) -> Pin<Box<dyn Future<Output = Result<AuthResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,

验证 token

§参数
  • token: 客户端提供的 token(从 CONNECT 消息的 metadata 中提取)
  • connection_id: 连接 ID
  • device_info: 设备信息(可选,客户端可能提供)
  • metadata: CONNECT 消息的其他元数据(可选)
§返回
  • Ok(AuthResult): 认证结果
  • Err: 认证过程出错(非验证失败,而是系统错误)

Provided Methods§

Source

fn is_authenticated<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

检查连接是否已验证(可选,用于验证状态管理)

默认实现返回 true(假设验证通过后连接状态由 ConnectionManager 管理) 如果需要自定义验证状态管理(如 Redis、数据库),可以覆盖此方法

Source

fn revoke_authentication<'life0, 'life1, 'async_trait>( &'life0 self, connection_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

使连接失效(可选,用于主动撤销认证)

默认实现不做任何操作 如果需要支持主动撤销认证(如用户登出、token 撤销),可以覆盖此方法

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§