Skip to content

Commit 7d819d7

Browse files
committed
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
1 parent 5c30f41 commit 7d819d7

10 files changed

+33
-32
lines changed

foundation/auth/src/idtoken.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
project::{project, Project, SERVICE_ACCOUNT_KEY},
77
token_source::{
88
compute_identity_source::ComputeIdentitySource, reuse_token_source::ReuseTokenSource,
9-
service_account_token_source::OAuth2ServiceAccountTokenSource, TokenSource,
9+
service_account_token_source::OAuth2ServiceAccountTokenSource, GoogleCloudTokenSource,
1010
},
1111
};
1212

@@ -39,15 +39,15 @@ impl IdTokenSourceConfig {
3939
self
4040
}
4141

42-
pub async fn build(self, audience: &str) -> Result<Box<dyn TokenSource>, error::Error> {
42+
pub async fn build(self, audience: &str) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
4343
create_id_token_source(self, audience).await
4444
}
4545
}
4646

4747
pub async fn create_id_token_source(
4848
config: IdTokenSourceConfig,
4949
audience: &str,
50-
) -> Result<Box<dyn TokenSource>, error::Error> {
50+
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
5151
if audience.is_empty() {
5252
return Err(error::Error::ScopeOrAudienceRequired);
5353
}
@@ -72,7 +72,7 @@ pub(crate) async fn id_token_source_from_credentials(
7272
custom_claims: &HashMap<String, serde_json::Value>,
7373
credentials: &CredentialsFile,
7474
audience: &str,
75-
) -> Result<Box<dyn TokenSource>, error::Error> {
75+
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
7676
let ts = match credentials.tp.as_str() {
7777
SERVICE_ACCOUNT_KEY => {
7878
let mut claims = custom_claims.clone();

foundation/auth/src/project.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::token_source::compute_token_source::ComputeTokenSource;
99
use crate::token_source::reuse_token_source::ReuseTokenSource;
1010
use crate::token_source::service_account_token_source::OAuth2ServiceAccountTokenSource;
1111
use crate::token_source::service_account_token_source::ServiceAccountTokenSource;
12-
use crate::token_source::TokenSource;
12+
use crate::token_source::GoogleCloudTokenSource;
1313
use crate::{credentials, error};
1414

1515
pub(crate) const SERVICE_ACCOUNT_KEY: &str = "service_account";
@@ -106,7 +106,7 @@ pub async fn project() -> Result<Project, error::Error> {
106106
pub async fn create_token_source_from_credentials(
107107
credentials: &CredentialsFile,
108108
config: &Config<'_>,
109-
) -> Result<Box<dyn TokenSource>, error::Error> {
109+
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
110110
let ts = credentials_from_json_with_params(credentials, config).await?;
111111
let token = ts.token().await?;
112112
Ok(Box::new(ReuseTokenSource::new(ts, token)))
@@ -116,7 +116,7 @@ pub async fn create_token_source_from_credentials(
116116
pub async fn create_token_source_from_project(
117117
project: &Project,
118118
config: Config<'_>,
119-
) -> Result<Box<dyn TokenSource>, error::Error> {
119+
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
120120
match project {
121121
Project::FromFile(file) => {
122122
if config.use_id_token {
@@ -145,15 +145,15 @@ pub async fn create_token_source_from_project(
145145
/// create_token_source creates the token source
146146
/// use [DefaultTokenSourceProvider](crate::token::DefaultTokenSourceProvider) or impl [TokenSourceProvider](google_cloud_token::TokenSourceProvider) instead.
147147
#[deprecated(note = "Use DefaultTokenSourceProvider instead")]
148-
pub async fn create_token_source(config: Config<'_>) -> Result<Box<dyn TokenSource>, error::Error> {
148+
pub async fn create_token_source(config: Config<'_>) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
149149
let project = project().await?;
150150
create_token_source_from_project(&project, config).await
151151
}
152152

153153
async fn credentials_from_json_with_params(
154154
credentials: &CredentialsFile,
155155
config: &Config<'_>,
156-
) -> Result<Box<dyn TokenSource>, error::Error> {
156+
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
157157
match credentials.tp.as_str() {
158158
SERVICE_ACCOUNT_KEY => {
159159
match config.audience {

foundation/auth/src/token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::sync::Arc;
33

44
use async_trait::async_trait;
55

6-
use token_source::{TokenSource, TokenSourceProvider};
6+
pub use token_source::{TokenSource, TokenSourceProvider};
77

88
use crate::credentials::CredentialsFile;
99
use crate::error::Error;
1010
use crate::project::{
1111
create_token_source_from_credentials, create_token_source_from_project, project, Config, Project,
1212
};
13-
use crate::token_source::TokenSource as InternalTokenSource;
13+
use crate::token_source::GoogleCloudTokenSource as InternalTokenSource;
1414

1515
pub const TOKEN_URL: &str = "https://oauth2.googleapis.com/token";
1616

foundation/auth/src/token_source/authorized_user_token_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::credentials;
44
use crate::error::Error;
55
use crate::misc::{UnwrapOrEmpty, EMPTY};
66
use crate::token::{Token, TOKEN_URL};
7-
use crate::token_source::TokenSource;
7+
use crate::token_source::GoogleCloudTokenSource;
88
use crate::token_source::{default_http_client, InternalToken};
99

1010
#[allow(dead_code)]
@@ -49,7 +49,7 @@ struct RequestBody<'a> {
4949
}
5050

5151
#[async_trait]
52-
impl TokenSource for UserAccountTokenSource {
52+
impl GoogleCloudTokenSource for UserAccountTokenSource {
5353
async fn token(&self) -> Result<Token, Error> {
5454
let data = RequestBody {
5555
client_id: &self.client_id,

foundation/auth/src/token_source/compute_identity_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use google_cloud_metadata::{METADATA_FLAVOR_KEY, METADATA_GOOGLE, METADATA_HOST_
88

99
use crate::error::Error;
1010
use crate::token::Token;
11-
use crate::token_source::{default_http_client, TokenSource};
11+
use crate::token_source::{default_http_client, GoogleCloudTokenSource};
1212

1313
/// Fetches a JWT token from the metadata server.
1414
/// using the `identity` endpoint.
@@ -64,7 +64,7 @@ struct ExpClaim {
6464
}
6565

6666
#[async_trait]
67-
impl TokenSource for ComputeIdentitySource {
67+
impl GoogleCloudTokenSource for ComputeIdentitySource {
6868
async fn token(&self) -> Result<Token, Error> {
6969
let jwt = self
7070
.client

foundation/auth/src/token_source/compute_token_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use google_cloud_metadata::{METADATA_FLAVOR_KEY, METADATA_GOOGLE, METADATA_HOST_
66
use crate::error::Error;
77
use crate::token::Token;
88
use crate::token_source::InternalToken;
9-
use crate::token_source::{default_http_client, TokenSource};
9+
use crate::token_source::{default_http_client, GoogleCloudTokenSource};
1010

1111
#[allow(dead_code)]
1212
#[derive(Debug)]
@@ -34,7 +34,7 @@ impl ComputeTokenSource {
3434
}
3535

3636
#[async_trait]
37-
impl TokenSource for ComputeTokenSource {
37+
impl GoogleCloudTokenSource for ComputeTokenSource {
3838
async fn token(&self) -> Result<Token, Error> {
3939
let it = self
4040
.client

foundation/auth/src/token_source/impersonate_token_source.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use time::format_description::well_known::Rfc3339;
44

55
use crate::error::Error;
66
use crate::token::Token;
7-
use crate::token_source::{default_http_client, TokenSource};
7+
use crate::token_source::{default_http_client, GoogleCloudTokenSource};
88

99
#[derive(Debug)]
1010
pub struct ImpersonateTokenSource {
11-
target: Box<dyn TokenSource>,
11+
target: Box<dyn GoogleCloudTokenSource>,
1212
scopes: Vec<String>,
1313
delegates: Vec<String>,
1414
url: String,
@@ -23,7 +23,7 @@ impl ImpersonateTokenSource {
2323
delegates: Vec<String>,
2424
scopes: Vec<String>,
2525
lifetime: Option<i32>,
26-
target: Box<dyn TokenSource>,
26+
target: Box<dyn GoogleCloudTokenSource>,
2727
) -> Self {
2828
ImpersonateTokenSource {
2929
target,
@@ -37,7 +37,7 @@ impl ImpersonateTokenSource {
3737
}
3838

3939
#[async_trait]
40-
impl TokenSource for ImpersonateTokenSource {
40+
impl GoogleCloudTokenSource for ImpersonateTokenSource {
4141
async fn token(&self) -> Result<Token, Error> {
4242
let body = ImpersonateTokenRequest {
4343
lifetime: format!("{}s", self.lifetime.unwrap_or(3600)),

foundation/auth/src/token_source/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use serde::Deserialize;
77

88
use crate::error::Error;
99
use crate::token::Token;
10+
// pub use token_source::TokenSource;
1011

1112
pub mod authorized_user_token_source;
1213
pub mod compute_identity_source;
@@ -19,7 +20,7 @@ pub mod service_account_token_source;
1920
pub mod external_account_source;
2021

2122
#[async_trait]
22-
pub trait TokenSource: Send + Sync + Debug {
23+
pub trait GoogleCloudTokenSource: Send + Sync + Debug {
2324
async fn token(&self) -> Result<Token, Error>;
2425
}
2526

@@ -86,7 +87,7 @@ mod tests {
8687
use crate::token_source::service_account_token_source::{
8788
OAuth2ServiceAccountTokenSource, ServiceAccountTokenSource,
8889
};
89-
use crate::token_source::TokenSource;
90+
use crate::token_source::GoogleCloudTokenSource;
9091

9192
#[tokio::test]
9293
async fn test_jwt_token_source() -> Result<(), Error> {

foundation/auth/src/token_source/reuse_token_source.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ use async_trait::async_trait;
22

33
use crate::error::Error;
44
use crate::token::Token;
5-
use crate::token_source::TokenSource;
5+
use crate::token_source::GoogleCloudTokenSource;
66

77
#[derive(Debug)]
88
pub struct ReuseTokenSource {
9-
target: Box<dyn TokenSource>,
9+
target: Box<dyn GoogleCloudTokenSource>,
1010
current_token: std::sync::RwLock<Token>,
1111
guard: tokio::sync::Mutex<()>,
1212
}
1313

1414
impl ReuseTokenSource {
15-
pub(crate) fn new(target: Box<dyn TokenSource>, token: Token) -> ReuseTokenSource {
15+
pub(crate) fn new(target: Box<dyn GoogleCloudTokenSource>, token: Token) -> ReuseTokenSource {
1616
ReuseTokenSource {
1717
target,
1818
current_token: std::sync::RwLock::new(token),
@@ -22,7 +22,7 @@ impl ReuseTokenSource {
2222
}
2323

2424
#[async_trait]
25-
impl TokenSource for ReuseTokenSource {
25+
impl GoogleCloudTokenSource for ReuseTokenSource {
2626
async fn token(&self) -> Result<Token, Error> {
2727
if let Some(token) = self.r_lock_token() {
2828
return Ok(token);
@@ -65,14 +65,14 @@ mod test {
6565
use crate::error::Error;
6666
use crate::token::Token;
6767
use crate::token_source::reuse_token_source::ReuseTokenSource;
68-
use crate::token_source::TokenSource;
68+
use crate::token_source::GoogleCloudTokenSource;
6969

7070
#[derive(Debug)]
7171
struct EmptyTokenSource {
7272
pub expiry: OffsetDateTime,
7373
}
7474
#[async_trait]
75-
impl TokenSource for EmptyTokenSource {
75+
impl GoogleCloudTokenSource for EmptyTokenSource {
7676
async fn token(&self) -> Result<Token, Error> {
7777
Ok(Token {
7878
access_token: "empty".to_string(),

foundation/auth/src/token_source/service_account_token_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::credentials;
1010
use crate::error::{Error, TokenErrorResponse};
1111
use crate::misc::UnwrapOrEmpty;
1212
use crate::token::{Token, TOKEN_URL};
13-
use crate::token_source::{default_http_client, InternalIdToken, InternalToken, TokenSource};
13+
use crate::token_source::{default_http_client, GoogleCloudTokenSource, InternalIdToken, InternalToken};
1414

1515
#[derive(Clone, Serialize)]
1616
struct Claims<'a> {
@@ -74,7 +74,7 @@ impl ServiceAccountTokenSource {
7474
}
7575

7676
#[async_trait]
77-
impl TokenSource for ServiceAccountTokenSource {
77+
impl GoogleCloudTokenSource for ServiceAccountTokenSource {
7878
async fn token(&self) -> Result<Token, Error> {
7979
let iat = OffsetDateTime::now_utc();
8080
let exp = iat + time::Duration::hours(1);
@@ -193,7 +193,7 @@ impl OAuth2ServiceAccountTokenSource {
193193
}
194194

195195
#[async_trait]
196-
impl TokenSource for OAuth2ServiceAccountTokenSource {
196+
impl GoogleCloudTokenSource for OAuth2ServiceAccountTokenSource {
197197
async fn token(&self) -> Result<Token, Error> {
198198
let iat = OffsetDateTime::now_utc();
199199
let exp = iat + time::Duration::hours(1);

0 commit comments

Comments
 (0)