use std::sync::Arc;
use std::time::{Duration, SystemTime};
use async_trait::async_trait;
use awaken_contract::secret::RedactedString;
use super::error::CredentialError;
use super::token::Token;
const STATIC_BEARER_TTL: Duration = Duration::from_secs(30 * 24 * 3600);
#[async_trait]
pub(crate) trait Minter: Send + Sync + std::fmt::Debug {
fn kind_label(&self) -> &'static str;
async fn mint(&self, scope: &str, http: &reqwest::Client) -> Result<Token, CredentialError>;
}
#[derive(Debug)]
pub(crate) struct StaticBearerMinter {
bearer: RedactedString,
}
impl StaticBearerMinter {
pub(crate) fn new(bearer: RedactedString) -> Self {
Self { bearer }
}
}
#[async_trait]
impl Minter for StaticBearerMinter {
fn kind_label(&self) -> &'static str {
"bearer"
}
async fn mint(&self, _scope: &str, _http: &reqwest::Client) -> Result<Token, CredentialError> {
Ok(Token {
bearer: self.bearer.clone(),
expires_at: SystemTime::now() + STATIC_BEARER_TTL,
})
}
}
#[cfg(any(test, feature = "credentials-google"))]
#[derive(Debug)]
pub(crate) struct GoogleServiceAccountMinter {
provider_id: String,
key: Arc<super::material::GoogleServiceAccountKey>,
}
#[cfg(any(test, feature = "credentials-google"))]
impl GoogleServiceAccountMinter {
pub(crate) fn new(
provider_id: String,
key: Arc<super::material::GoogleServiceAccountKey>,
) -> Self {
Self { provider_id, key }
}
}
#[cfg(any(test, feature = "credentials-google"))]
#[async_trait]
impl Minter for GoogleServiceAccountMinter {
fn kind_label(&self) -> &'static str {
"service_account_json"
}
async fn mint(&self, scope: &str, http: &reqwest::Client) -> Result<Token, CredentialError> {
super::google_oauth::mint(&self.provider_id, &self.key, scope, http).await
}
}
pub(super) fn static_bearer_arc(bearer: RedactedString) -> Arc<dyn Minter> {
Arc::new(StaticBearerMinter::new(bearer))
}
#[cfg(any(test, feature = "credentials-google"))]
pub(super) fn google_service_account_arc(
provider_id: String,
key: Arc<super::material::GoogleServiceAccountKey>,
) -> Arc<dyn Minter> {
Arc::new(GoogleServiceAccountMinter::new(provider_id, key))
}