use async_trait::async_trait;
use meerkat_core::{AuthError, HttpAuthorizationRequest, HttpAuthorizer};
pub struct StaticBearerAuthorizer {
token: String,
label: &'static str,
}
impl StaticBearerAuthorizer {
pub fn new(token: String, label: &'static str) -> Self {
Self { token, label }
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl HttpAuthorizer for StaticBearerAuthorizer {
async fn authorize(&self, req: &mut HttpAuthorizationRequest<'_>) -> Result<(), AuthError> {
req.headers.push((
"Authorization".to_string(),
format!("Bearer {}", self.token),
));
Ok(())
}
fn label(&self) -> &str {
self.label
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[tokio::test]
async fn sets_bearer_header() {
let authorizer = StaticBearerAuthorizer::new("tok-123".to_string(), "bedrock-bearer");
let mut headers: Vec<(String, String)> = Vec::new();
let mut req = HttpAuthorizationRequest {
method: "POST",
url: "https://bedrock-runtime.us-east-1.amazonaws.com/model/claude/invoke",
headers: &mut headers,
};
authorizer.authorize(&mut req).await.unwrap();
assert_eq!(headers.len(), 1);
assert_eq!(headers[0].0, "Authorization");
assert_eq!(headers[0].1, "Bearer tok-123");
assert_eq!(authorizer.label(), "bedrock-bearer");
}
}