use std::time::Duration;
use reqwest::Client;
use crate::error::Error;
use crate::error::Result;
#[derive(Debug, Clone)]
pub struct JwtVerifierConfig {
pub(crate) expected_issuer: String,
pub(crate) expected_audiences: Vec<String>,
pub(crate) jwks_cache_ttl: Duration,
pub(crate) http_client: Option<Client>,
}
const DEFAULT_JWKS_CACHE_TTL_SECS: u64 = 3600;
impl JwtVerifierConfig {
pub fn new(expected_issuer: impl Into<String>, audience: impl Into<String>) -> Self {
Self {
expected_issuer: expected_issuer.into(),
expected_audiences: vec![audience.into()],
jwks_cache_ttl: Duration::from_secs(DEFAULT_JWKS_CACHE_TTL_SECS),
http_client: None,
}
}
pub fn new_with_audiences(
expected_issuer: impl Into<String>,
expected_audiences: Vec<String>,
) -> Result<Self> {
if expected_audiences.is_empty() {
return Err(Error::NoAudiencesConfigured);
}
Ok(Self {
expected_issuer: expected_issuer.into(),
expected_audiences,
jwks_cache_ttl: Duration::from_secs(DEFAULT_JWKS_CACHE_TTL_SECS),
http_client: None,
})
}
pub fn with_audiences(mut self, audiences: Vec<String>) -> Result<Self> {
if audiences.is_empty() {
return Err(Error::NoAudiencesConfigured);
}
self.expected_audiences = audiences;
Ok(self)
}
pub fn with_audience(mut self, audience: impl Into<String>) -> Self {
self.expected_audiences.push(audience.into());
self
}
pub fn with_cache_ttl(mut self, ttl: Duration) -> Self {
self.jwks_cache_ttl = ttl;
self
}
pub fn with_http_client(mut self, client: Client) -> Self {
self.http_client = Some(client);
self
}
}