Skip to content

feat(auth): reexport token_source && rename internal TokenSource #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions foundation/auth/src/idtoken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -39,15 +39,15 @@ impl IdTokenSourceConfig {
self
}

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

pub async fn create_id_token_source(
config: IdTokenSourceConfig,
audience: &str,
) -> Result<Box<dyn TokenSource>, error::Error> {
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
if audience.is_empty() {
return Err(error::Error::ScopeOrAudienceRequired);
}
Expand All @@ -72,7 +72,7 @@ pub(crate) async fn id_token_source_from_credentials(
custom_claims: &HashMap<String, serde_json::Value>,
credentials: &CredentialsFile,
audience: &str,
) -> Result<Box<dyn TokenSource>, error::Error> {
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
let ts = match credentials.tp.as_str() {
SERVICE_ACCOUNT_KEY => {
let mut claims = custom_claims.clone();
Expand Down
10 changes: 5 additions & 5 deletions foundation/auth/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -106,7 +106,7 @@ pub async fn project() -> Result<Project, error::Error> {
pub async fn create_token_source_from_credentials(
credentials: &CredentialsFile,
config: &Config<'_>,
) -> Result<Box<dyn TokenSource>, error::Error> {
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
let ts = credentials_from_json_with_params(credentials, config).await?;
let token = ts.token().await?;
Ok(Box::new(ReuseTokenSource::new(ts, token)))
Expand All @@ -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<Box<dyn TokenSource>, error::Error> {
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
match project {
Project::FromFile(file) => {
if config.use_id_token {
Expand Down Expand Up @@ -145,15 +145,15 @@ 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<Box<dyn TokenSource>, error::Error> {
pub async fn create_token_source(config: Config<'_>) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
let project = project().await?;
create_token_source_from_project(&project, config).await
}

async fn credentials_from_json_with_params(
credentials: &CredentialsFile,
config: &Config<'_>,
) -> Result<Box<dyn TokenSource>, error::Error> {
) -> Result<Box<dyn GoogleCloudTokenSource>, error::Error> {
match credentials.tp.as_str() {
SERVICE_ACCOUNT_KEY => {
match config.audience {
Expand Down
4 changes: 2 additions & 2 deletions foundation/auth/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -49,7 +49,7 @@ struct RequestBody<'a> {
}

#[async_trait]
impl TokenSource for UserAccountTokenSource {
impl GoogleCloudTokenSource for UserAccountTokenSource {
async fn token(&self) -> Result<Token, Error> {
let data = RequestBody {
client_id: &self.client_id,
Expand Down
4 changes: 2 additions & 2 deletions foundation/auth/src/token_source/compute_identity_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -64,7 +64,7 @@ struct ExpClaim {
}

#[async_trait]
impl TokenSource for ComputeIdentitySource {
impl GoogleCloudTokenSource for ComputeIdentitySource {
async fn token(&self) -> Result<Token, Error> {
let jwt = self
.client
Expand Down
4 changes: 2 additions & 2 deletions foundation/auth/src/token_source/compute_token_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -34,7 +34,7 @@ impl ComputeTokenSource {
}

#[async_trait]
impl TokenSource for ComputeTokenSource {
impl GoogleCloudTokenSource for ComputeTokenSource {
async fn token(&self) -> Result<Token, Error> {
let it = self
.client
Expand Down
8 changes: 4 additions & 4 deletions foundation/auth/src/token_source/impersonate_token_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn TokenSource>,
target: Box<dyn GoogleCloudTokenSource>,
scopes: Vec<String>,
delegates: Vec<String>,
url: String,
Expand All @@ -23,7 +23,7 @@ impl ImpersonateTokenSource {
delegates: Vec<String>,
scopes: Vec<String>,
lifetime: Option<i32>,
target: Box<dyn TokenSource>,
target: Box<dyn GoogleCloudTokenSource>,
) -> Self {
ImpersonateTokenSource {
target,
Expand All @@ -37,7 +37,7 @@ impl ImpersonateTokenSource {
}

#[async_trait]
impl TokenSource for ImpersonateTokenSource {
impl GoogleCloudTokenSource for ImpersonateTokenSource {
async fn token(&self) -> Result<Token, Error> {
let body = ImpersonateTokenRequest {
lifetime: format!("{}s", self.lifetime.unwrap_or(3600)),
Expand Down
5 changes: 3 additions & 2 deletions foundation/auth/src/token_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Token, Error>;
}

Expand Down Expand Up @@ -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> {
Expand Down
12 changes: 6 additions & 6 deletions foundation/auth/src/token_source/reuse_token_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn TokenSource>,
target: Box<dyn GoogleCloudTokenSource>,
current_token: std::sync::RwLock<Token>,
guard: tokio::sync::Mutex<()>,
}

impl ReuseTokenSource {
pub(crate) fn new(target: Box<dyn TokenSource>, token: Token) -> ReuseTokenSource {
pub(crate) fn new(target: Box<dyn GoogleCloudTokenSource>, token: Token) -> ReuseTokenSource {
ReuseTokenSource {
target,
current_token: std::sync::RwLock::new(token),
Expand All @@ -22,7 +22,7 @@ impl ReuseTokenSource {
}

#[async_trait]
impl TokenSource for ReuseTokenSource {
impl GoogleCloudTokenSource for ReuseTokenSource {
async fn token(&self) -> Result<Token, Error> {
if let Some(token) = self.r_lock_token() {
return Ok(token);
Expand Down Expand Up @@ -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<Token, Error> {
Ok(Token {
access_token: "empty".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -74,7 +74,7 @@ impl ServiceAccountTokenSource {
}

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

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