use super::super::{JwksCache, TokenClaims, TokenError, TokenValidator};
use async_trait::async_trait;
use std::sync::Arc;
pub struct GoogleProvider {
client_id: String,
issuer: String,
#[cfg(feature = "sso")]
jwks_cache: Arc<JwksCache>,
}
impl GoogleProvider {
#[cfg(feature = "sso")]
pub fn new(client_id: impl Into<String>) -> Self {
Self {
client_id: client_id.into(),
issuer: "https://accounts.google.com".into(),
jwks_cache: Arc::new(JwksCache::new("https://www.googleapis.com/oauth2/v3/certs")),
}
}
pub fn client_id(&self) -> &str {
&self.client_id
}
}
#[cfg(feature = "sso")]
#[async_trait]
impl TokenValidator for GoogleProvider {
async fn validate(&self, token: &str) -> Result<TokenClaims, TokenError> {
let header = jsonwebtoken::decode_header(token)?;
let kid = header.kid.ok_or_else(|| TokenError::MissingClaim("kid".into()))?;
let key = self.jwks_cache.get_key(&kid).await?;
let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::RS256);
validation.set_issuer(&["https://accounts.google.com", "accounts.google.com"]);
validation.set_audience(&[&self.client_id]);
validation.validate_exp = true;
validation.validate_nbf = true;
let token_data = jsonwebtoken::decode::<TokenClaims>(token, &key, &validation)?;
Ok(token_data.claims)
}
fn issuer(&self) -> &str {
&self.issuer
}
}
#[cfg(not(feature = "sso"))]
impl GoogleProvider {
pub fn new(client_id: impl Into<String>) -> Self {
Self { client_id: client_id.into(), issuer: String::new() }
}
}