openapi-nexus 0.1.8

OpenAPI 3.x multi-language code generator
Documentation
/// Trait for authenticating requests.
pub trait Authenticator: Send + Sync + std::fmt::Debug {
    /// Return header key-value pairs to apply to the request.
    fn auth_headers(&self) -> Vec<(&str, String)>;
}

/// Bearer token authentication.
#[derive(Debug, Clone)]
pub struct BearerAuth {
    token: String,
}

impl BearerAuth {
    pub fn new(token: impl Into<String>) -> Self {
        Self {
            token: token.into(),
        }
    }
}

impl Authenticator for BearerAuth {
    fn auth_headers(&self) -> Vec<(&str, String)> {
        vec![("Authorization", format!("Bearer {}", self.token))]
    }
}

/// API key authentication.
#[derive(Debug, Clone)]
pub struct ApiKeyAuth {
    header_name: String,
    api_key: String,
}

impl ApiKeyAuth {
    pub fn new(header_name: impl Into<String>, api_key: impl Into<String>) -> Self {
        Self {
            header_name: header_name.into(),
            api_key: api_key.into(),
        }
    }
}

impl Authenticator for ApiKeyAuth {
    fn auth_headers(&self) -> Vec<(&str, String)> {
        vec![(&self.header_name, self.api_key.clone())]
    }
}