use async_trait::async_trait;
use boltr::error::BoltError;
use boltr::server::{AuthCredentials, AuthInfo, AuthValidator};
pub struct BasicAuthValidator {
user: String,
pass: String,
}
impl BasicAuthValidator {
pub fn new(user: String, pass: String) -> Self {
Self { user, pass }
}
}
#[async_trait]
impl AuthValidator for BasicAuthValidator {
async fn validate(&self, credentials: &AuthCredentials) -> Result<AuthInfo, BoltError> {
if credentials.scheme != "basic" {
return Err(BoltError::Authentication(format!(
"scheme '{}' not supported — kglite-bolt-server --auth basic only accepts 'basic'",
credentials.scheme
)));
}
let principal_ok = credentials.principal.as_deref() == Some(self.user.as_str());
let credentials_ok = credentials.credentials.as_deref() == Some(self.pass.as_str());
if !principal_ok || !credentials_ok {
return Err(BoltError::Authentication(
"invalid username or password".into(),
));
}
Ok(AuthInfo {
principal: self.user.clone(),
credentials_expired: false,
})
}
}