Skip to main content

Validate

Trait Validate 

Source
pub trait Validate:
    Clone
    + Send
    + Sync
    + 'static {
    // Required method
    fn validate(
        &self,
        credential: &str,
    ) -> impl Future<Output = AuthResult> + Send;
}
Expand description

Trait for validating authentication credentials.

Implement this trait to provide custom authentication logic for use with AuthLayer and AuthService.

The credential string passed to validate is the value extracted from the configured request header after parsing (e.g., the token portion of "Bearer sk-123").

§Example

use tower_mcp::auth::{Validate, AuthResult, AuthInfo, AuthError};

#[derive(Clone)]
struct MyValidator;

impl Validate for MyValidator {
    async fn validate(&self, credential: &str) -> AuthResult {
        if credential.starts_with("sk-") {
            AuthResult::Authenticated(Some(AuthInfo {
                client_id: credential.to_string(),
                claims: None,
            }))
        } else {
            AuthResult::Failed(AuthError {
                code: "invalid_credential".to_string(),
                message: "Credential must start with sk-".to_string(),
            })
        }
    }
}

Required Methods§

Source

fn validate(&self, credential: &str) -> impl Future<Output = AuthResult> + Send

Validate a credential and return the authentication result.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§