From 7d819d7e86c5cea766ee84ac4ded309146fc180e Mon Sep 17 00:00:00 2001 From: jingyu Date: Thu, 10 Apr 2025 15:16:16 +0800 Subject: [PATCH] feat(auth): reexport token_source && rename internal TokenSource 1. reexport token_source::{TokenSource,TokenSourceProvider} in gcloud_auth::token. 2. rename gcloud::token::token_source::TokenSource to gcloud::token::token_source:: GoogleCloudTokenSource --- foundation/auth/src/idtoken.rs | 8 ++++---- foundation/auth/src/project.rs | 10 +++++----- foundation/auth/src/token.rs | 4 ++-- .../src/token_source/authorized_user_token_source.rs | 4 ++-- .../auth/src/token_source/compute_identity_source.rs | 4 ++-- .../auth/src/token_source/compute_token_source.rs | 4 ++-- .../src/token_source/impersonate_token_source.rs | 8 ++++---- foundation/auth/src/token_source/mod.rs | 5 +++-- .../auth/src/token_source/reuse_token_source.rs | 12 ++++++------ .../src/token_source/service_account_token_source.rs | 6 +++--- 10 files changed, 33 insertions(+), 32 deletions(-) diff --git a/foundation/auth/src/idtoken.rs b/foundation/auth/src/idtoken.rs index 13cfb5dc..1b4c6f45 100644 --- a/foundation/auth/src/idtoken.rs +++ b/foundation/auth/src/idtoken.rs @@ -6,7 +6,7 @@ use crate::{ project::{project, Project, SERVICE_ACCOUNT_KEY}, token_source::{ compute_identity_source::ComputeIdentitySource, reuse_token_source::ReuseTokenSource, - service_account_token_source::OAuth2ServiceAccountTokenSource, TokenSource, + service_account_token_source::OAuth2ServiceAccountTokenSource, GoogleCloudTokenSource, }, }; @@ -39,7 +39,7 @@ impl IdTokenSourceConfig { self } - pub async fn build(self, audience: &str) -> Result, error::Error> { + pub async fn build(self, audience: &str) -> Result, error::Error> { create_id_token_source(self, audience).await } } @@ -47,7 +47,7 @@ impl IdTokenSourceConfig { pub async fn create_id_token_source( config: IdTokenSourceConfig, audience: &str, -) -> Result, error::Error> { +) -> Result, error::Error> { if audience.is_empty() { return Err(error::Error::ScopeOrAudienceRequired); } @@ -72,7 +72,7 @@ pub(crate) async fn id_token_source_from_credentials( custom_claims: &HashMap, credentials: &CredentialsFile, audience: &str, -) -> Result, error::Error> { +) -> Result, error::Error> { let ts = match credentials.tp.as_str() { SERVICE_ACCOUNT_KEY => { let mut claims = custom_claims.clone(); diff --git a/foundation/auth/src/project.rs b/foundation/auth/src/project.rs index c9fa7a32..e7e9d45b 100644 --- a/foundation/auth/src/project.rs +++ b/foundation/auth/src/project.rs @@ -9,7 +9,7 @@ use crate::token_source::compute_token_source::ComputeTokenSource; use crate::token_source::reuse_token_source::ReuseTokenSource; use crate::token_source::service_account_token_source::OAuth2ServiceAccountTokenSource; use crate::token_source::service_account_token_source::ServiceAccountTokenSource; -use crate::token_source::TokenSource; +use crate::token_source::GoogleCloudTokenSource; use crate::{credentials, error}; pub(crate) const SERVICE_ACCOUNT_KEY: &str = "service_account"; @@ -106,7 +106,7 @@ pub async fn project() -> Result { pub async fn create_token_source_from_credentials( credentials: &CredentialsFile, config: &Config<'_>, -) -> Result, error::Error> { +) -> Result, error::Error> { let ts = credentials_from_json_with_params(credentials, config).await?; let token = ts.token().await?; Ok(Box::new(ReuseTokenSource::new(ts, token))) @@ -116,7 +116,7 @@ pub async fn create_token_source_from_credentials( pub async fn create_token_source_from_project( project: &Project, config: Config<'_>, -) -> Result, error::Error> { +) -> Result, error::Error> { match project { Project::FromFile(file) => { if config.use_id_token { @@ -145,7 +145,7 @@ pub async fn create_token_source_from_project( /// create_token_source creates the token source /// use [DefaultTokenSourceProvider](crate::token::DefaultTokenSourceProvider) or impl [TokenSourceProvider](google_cloud_token::TokenSourceProvider) instead. #[deprecated(note = "Use DefaultTokenSourceProvider instead")] -pub async fn create_token_source(config: Config<'_>) -> Result, error::Error> { +pub async fn create_token_source(config: Config<'_>) -> Result, error::Error> { let project = project().await?; create_token_source_from_project(&project, config).await } @@ -153,7 +153,7 @@ pub async fn create_token_source(config: Config<'_>) -> Result, -) -> Result, error::Error> { +) -> Result, error::Error> { match credentials.tp.as_str() { SERVICE_ACCOUNT_KEY => { match config.audience { diff --git a/foundation/auth/src/token.rs b/foundation/auth/src/token.rs index 2a737684..8cc6480e 100644 --- a/foundation/auth/src/token.rs +++ b/foundation/auth/src/token.rs @@ -3,14 +3,14 @@ use std::sync::Arc; use async_trait::async_trait; -use token_source::{TokenSource, TokenSourceProvider}; +pub use token_source::{TokenSource, TokenSourceProvider}; use crate::credentials::CredentialsFile; use crate::error::Error; use crate::project::{ create_token_source_from_credentials, create_token_source_from_project, project, Config, Project, }; -use crate::token_source::TokenSource as InternalTokenSource; +use crate::token_source::GoogleCloudTokenSource as InternalTokenSource; pub const TOKEN_URL: &str = "https://oauth2.googleapis.com/token"; diff --git a/foundation/auth/src/token_source/authorized_user_token_source.rs b/foundation/auth/src/token_source/authorized_user_token_source.rs index e15dc459..03909e94 100644 --- a/foundation/auth/src/token_source/authorized_user_token_source.rs +++ b/foundation/auth/src/token_source/authorized_user_token_source.rs @@ -4,7 +4,7 @@ use crate::credentials; use crate::error::Error; use crate::misc::{UnwrapOrEmpty, EMPTY}; use crate::token::{Token, TOKEN_URL}; -use crate::token_source::TokenSource; +use crate::token_source::GoogleCloudTokenSource; use crate::token_source::{default_http_client, InternalToken}; #[allow(dead_code)] @@ -49,7 +49,7 @@ struct RequestBody<'a> { } #[async_trait] -impl TokenSource for UserAccountTokenSource { +impl GoogleCloudTokenSource for UserAccountTokenSource { async fn token(&self) -> Result { let data = RequestBody { client_id: &self.client_id, diff --git a/foundation/auth/src/token_source/compute_identity_source.rs b/foundation/auth/src/token_source/compute_identity_source.rs index 1ecf2558..76e86e63 100644 --- a/foundation/auth/src/token_source/compute_identity_source.rs +++ b/foundation/auth/src/token_source/compute_identity_source.rs @@ -8,7 +8,7 @@ use google_cloud_metadata::{METADATA_FLAVOR_KEY, METADATA_GOOGLE, METADATA_HOST_ use crate::error::Error; use crate::token::Token; -use crate::token_source::{default_http_client, TokenSource}; +use crate::token_source::{default_http_client, GoogleCloudTokenSource}; /// Fetches a JWT token from the metadata server. /// using the `identity` endpoint. @@ -64,7 +64,7 @@ struct ExpClaim { } #[async_trait] -impl TokenSource for ComputeIdentitySource { +impl GoogleCloudTokenSource for ComputeIdentitySource { async fn token(&self) -> Result { let jwt = self .client diff --git a/foundation/auth/src/token_source/compute_token_source.rs b/foundation/auth/src/token_source/compute_token_source.rs index 1b0530d3..beb0d516 100644 --- a/foundation/auth/src/token_source/compute_token_source.rs +++ b/foundation/auth/src/token_source/compute_token_source.rs @@ -6,7 +6,7 @@ use google_cloud_metadata::{METADATA_FLAVOR_KEY, METADATA_GOOGLE, METADATA_HOST_ use crate::error::Error; use crate::token::Token; use crate::token_source::InternalToken; -use crate::token_source::{default_http_client, TokenSource}; +use crate::token_source::{default_http_client, GoogleCloudTokenSource}; #[allow(dead_code)] #[derive(Debug)] @@ -34,7 +34,7 @@ impl ComputeTokenSource { } #[async_trait] -impl TokenSource for ComputeTokenSource { +impl GoogleCloudTokenSource for ComputeTokenSource { async fn token(&self) -> Result { let it = self .client diff --git a/foundation/auth/src/token_source/impersonate_token_source.rs b/foundation/auth/src/token_source/impersonate_token_source.rs index ccd5201d..ac0024a6 100644 --- a/foundation/auth/src/token_source/impersonate_token_source.rs +++ b/foundation/auth/src/token_source/impersonate_token_source.rs @@ -4,11 +4,11 @@ use time::format_description::well_known::Rfc3339; use crate::error::Error; use crate::token::Token; -use crate::token_source::{default_http_client, TokenSource}; +use crate::token_source::{default_http_client, GoogleCloudTokenSource}; #[derive(Debug)] pub struct ImpersonateTokenSource { - target: Box, + target: Box, scopes: Vec, delegates: Vec, url: String, @@ -23,7 +23,7 @@ impl ImpersonateTokenSource { delegates: Vec, scopes: Vec, lifetime: Option, - target: Box, + target: Box, ) -> Self { ImpersonateTokenSource { target, @@ -37,7 +37,7 @@ impl ImpersonateTokenSource { } #[async_trait] -impl TokenSource for ImpersonateTokenSource { +impl GoogleCloudTokenSource for ImpersonateTokenSource { async fn token(&self) -> Result { let body = ImpersonateTokenRequest { lifetime: format!("{}s", self.lifetime.unwrap_or(3600)), diff --git a/foundation/auth/src/token_source/mod.rs b/foundation/auth/src/token_source/mod.rs index d0469b81..a3b125bc 100644 --- a/foundation/auth/src/token_source/mod.rs +++ b/foundation/auth/src/token_source/mod.rs @@ -7,6 +7,7 @@ use serde::Deserialize; use crate::error::Error; use crate::token::Token; +// pub use token_source::TokenSource; pub mod authorized_user_token_source; pub mod compute_identity_source; @@ -19,7 +20,7 @@ pub mod service_account_token_source; pub mod external_account_source; #[async_trait] -pub trait TokenSource: Send + Sync + Debug { +pub trait GoogleCloudTokenSource: Send + Sync + Debug { async fn token(&self) -> Result; } @@ -86,7 +87,7 @@ mod tests { use crate::token_source::service_account_token_source::{ OAuth2ServiceAccountTokenSource, ServiceAccountTokenSource, }; - use crate::token_source::TokenSource; + use crate::token_source::GoogleCloudTokenSource; #[tokio::test] async fn test_jwt_token_source() -> Result<(), Error> { diff --git a/foundation/auth/src/token_source/reuse_token_source.rs b/foundation/auth/src/token_source/reuse_token_source.rs index cf816e0b..7d04002e 100644 --- a/foundation/auth/src/token_source/reuse_token_source.rs +++ b/foundation/auth/src/token_source/reuse_token_source.rs @@ -2,17 +2,17 @@ use async_trait::async_trait; use crate::error::Error; use crate::token::Token; -use crate::token_source::TokenSource; +use crate::token_source::GoogleCloudTokenSource; #[derive(Debug)] pub struct ReuseTokenSource { - target: Box, + target: Box, current_token: std::sync::RwLock, guard: tokio::sync::Mutex<()>, } impl ReuseTokenSource { - pub(crate) fn new(target: Box, token: Token) -> ReuseTokenSource { + pub(crate) fn new(target: Box, token: Token) -> ReuseTokenSource { ReuseTokenSource { target, current_token: std::sync::RwLock::new(token), @@ -22,7 +22,7 @@ impl ReuseTokenSource { } #[async_trait] -impl TokenSource for ReuseTokenSource { +impl GoogleCloudTokenSource for ReuseTokenSource { async fn token(&self) -> Result { if let Some(token) = self.r_lock_token() { return Ok(token); @@ -65,14 +65,14 @@ mod test { use crate::error::Error; use crate::token::Token; use crate::token_source::reuse_token_source::ReuseTokenSource; - use crate::token_source::TokenSource; + use crate::token_source::GoogleCloudTokenSource; #[derive(Debug)] struct EmptyTokenSource { pub expiry: OffsetDateTime, } #[async_trait] - impl TokenSource for EmptyTokenSource { + impl GoogleCloudTokenSource for EmptyTokenSource { async fn token(&self) -> Result { Ok(Token { access_token: "empty".to_string(), diff --git a/foundation/auth/src/token_source/service_account_token_source.rs b/foundation/auth/src/token_source/service_account_token_source.rs index 588000ad..4f2b371d 100644 --- a/foundation/auth/src/token_source/service_account_token_source.rs +++ b/foundation/auth/src/token_source/service_account_token_source.rs @@ -10,7 +10,7 @@ use crate::credentials; use crate::error::{Error, TokenErrorResponse}; use crate::misc::UnwrapOrEmpty; use crate::token::{Token, TOKEN_URL}; -use crate::token_source::{default_http_client, InternalIdToken, InternalToken, TokenSource}; +use crate::token_source::{default_http_client, GoogleCloudTokenSource, InternalIdToken, InternalToken}; #[derive(Clone, Serialize)] struct Claims<'a> { @@ -74,7 +74,7 @@ impl ServiceAccountTokenSource { } #[async_trait] -impl TokenSource for ServiceAccountTokenSource { +impl GoogleCloudTokenSource for ServiceAccountTokenSource { async fn token(&self) -> Result { let iat = OffsetDateTime::now_utc(); let exp = iat + time::Duration::hours(1); @@ -193,7 +193,7 @@ impl OAuth2ServiceAccountTokenSource { } #[async_trait] -impl TokenSource for OAuth2ServiceAccountTokenSource { +impl GoogleCloudTokenSource for OAuth2ServiceAccountTokenSource { async fn token(&self) -> Result { let iat = OffsetDateTime::now_utc(); let exp = iat + time::Duration::hours(1);