use async_trait::async_trait;
use crate::{
error::Result,
oidc_provider::OidcProvider,
provider::{OAuthProvider, TokenResponse, UserInfo},
};
#[derive(Debug)]
pub struct GoogleOAuth {
oidc: OidcProvider,
}
impl GoogleOAuth {
pub async fn new(
client_id: String,
client_secret: String,
redirect_uri: String,
) -> Result<Self> {
Self::with_issuer(client_id, client_secret, redirect_uri, "https://accounts.google.com")
.await
}
pub async fn with_issuer(
client_id: String,
client_secret: String,
redirect_uri: String,
issuer_url: &str,
) -> Result<Self> {
let oidc =
OidcProvider::new("google", issuer_url, &client_id, &client_secret, &redirect_uri)
.await?;
Ok(Self { oidc })
}
}
#[async_trait]
impl OAuthProvider for GoogleOAuth {
fn name(&self) -> &'static str {
"google"
}
fn authorization_url(&self, state: &str) -> String {
self.oidc.authorization_url(state)
}
async fn exchange_code(&self, code: &str) -> Result<TokenResponse> {
self.oidc.exchange_code(code).await
}
async fn user_info(&self, access_token: &str) -> Result<UserInfo> {
self.oidc.user_info(access_token).await
}
async fn refresh_token(&self, refresh_token: &str) -> Result<TokenResponse> {
self.oidc.refresh_token(refresh_token).await
}
async fn revoke_token(&self, token: &str) -> Result<()> {
self.oidc.revoke_token(token).await
}
}