use super::super::validator::REQUIRED_CLAIMS;
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")),
}
}
#[cfg(feature = "sso")]
#[must_use]
pub fn with_jwks_cache(mut self, cache: JwksCache) -> Self {
self.jwks_cache = Arc::new(cache);
self
}
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).map_err(TokenError::from_jwt)?;
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.set_required_spec_claims(&REQUIRED_CLAIMS);
validation.validate_exp = true;
validation.validate_nbf = true;
let token_data = jsonwebtoken::decode::<TokenClaims>(token, &key, &validation)
.map_err(TokenError::from_jwt)?;
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() }
}
}
#[cfg(all(test, feature = "sso"))]
mod tests {
use super::GoogleProvider;
use crate::sso::JwksCache;
use crate::sso::test_issuer::{
TestIssuer, assert_refuses_forged_and_stale_tokens, assert_requires_iss_and_aud,
};
#[tokio::test]
async fn tokens_need_the_iss_and_aud_claims() {
let issuer = TestIssuer::start().await;
let provider =
GoogleProvider::new("client-id").with_jwks_cache(JwksCache::new(issuer.jwks_uri()));
assert_requires_iss_and_aud(&provider, "https://accounts.google.com", "client-id").await;
assert_refuses_forged_and_stale_tokens(
&provider,
"https://accounts.google.com",
"client-id",
)
.await;
}
}