Authenticator

Trait Authenticator 

Source
pub trait Authenticator: Send + Sync {
    type Claims: Clone + Send + Sync + 'static;

    // Required method
    fn authenticate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        token: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Claims, AuthError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Available on crate feature auth only.
Expand description

Trait for types that can validate authentication tokens.

Implement this trait to create custom authenticators for different token types (JWT, API keys, session tokens, etc.).

§Example

use allframe_core::auth::{Authenticator, AuthError};

struct ApiKeyAuthenticator {
    valid_keys: Vec<String>,
}

#[async_trait::async_trait]
impl Authenticator for ApiKeyAuthenticator {
    type Claims = String; // Just the key itself

    async fn authenticate(&self, token: &str) -> Result<Self::Claims, AuthError> {
        if self.valid_keys.contains(&token.to_string()) {
            Ok(token.to_string())
        } else {
            Err(AuthError::InvalidToken("unknown API key".into()))
        }
    }
}

Required Associated Types§

Source

type Claims: Clone + Send + Sync + 'static

The claims type returned on successful authentication.

Required Methods§

Source

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

Validate a token and extract claims.

§Arguments
  • token - The raw token string (without “Bearer “ prefix)
§Returns
  • Ok(Claims) - Authentication successful
  • Err(AuthError) - Authentication failed

Implementors§