Skip to main content

AuthProvider

Trait AuthProvider 

Source
pub trait AuthProvider:
    Send
    + Sync
    + Debug {
    // Required method
    fn apply(
        &self,
        request: RequestBuilder,
        requirements: &[HashMap<String, Vec<String>>],
        schemes: &HashMap<String, SecurityScheme>,
    ) -> RequestBuilder;
}
Expand description

Hook for satisfying an operation’s SecurityRequirements before the request is sent.

OpenAPI specifications declare auth via securitySchemes (apiKey, http bearer/basic, oauth2, openIdConnect) and per-operation/spec-level security. This crate parses both — OpenApiProvider::security_schemes returns the scheme definitions, and each ExtractedOperation::security holds the operation’s effective requirements. Implement this trait to inject credentials matching one of the requirement alternatives.

§Example

use std::collections::HashMap;
use std::sync::Arc;
use turbomcp_openapi::{AuthProvider, OpenApiProvider};

#[derive(Debug)]
struct StaticBearer(String);

impl AuthProvider for StaticBearer {
    fn apply(
        &self,
        request: reqwest::RequestBuilder,
        _requirements: &[HashMap<String, Vec<String>>],
        _schemes: &HashMap<String, openapiv3::SecurityScheme>,
    ) -> reqwest::RequestBuilder {
        request.bearer_auth(&self.0)
    }
}

let provider = OpenApiProvider::from_string(spec)?
    .with_auth_provider(Arc::new(StaticBearer("token".into())));

Required Methods§

Source

fn apply( &self, request: RequestBuilder, requirements: &[HashMap<String, Vec<String>>], schemes: &HashMap<String, SecurityScheme>, ) -> RequestBuilder

Apply auth to an outgoing request.

requirements is the list of alternative SecurityRequirement objects from the operation; satisfying any one alternative is sufficient. Each entry maps a scheme name (from components.securitySchemes) to the required scopes. schemes is the spec’s components.securitySchemes map, with references already resolved.

Returning the unmodified request is acceptable for operations whose requirements your implementation cannot satisfy — the request will then fail with whatever auth error the upstream returns.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§