Skip to content
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

feat: add google drive auth url #1

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
20 changes: 10 additions & 10 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Authenticator for ServiceAccountAuthenticator {
impl ServiceAccountAuthenticator {
pub(crate) async fn from_service_account_key(
sa_key: ServiceAccountKey,
scopes: &[&str],
scopes: &Vec<String>,
youmts marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<Arc<dyn Authenticator>, BQError> {
let auth = yup_oauth2::ServiceAccountAuthenticator::builder(sa_key).build().await;

Expand All @@ -80,11 +80,11 @@ impl ServiceAccountAuthenticator {
}

pub(crate) async fn service_account_authenticator(
scopes: Vec<&str>,
scopes: &Vec<String>,
sa_key_file: &str,
) -> Result<Arc<dyn Authenticator>, BQError> {
let sa_key = yup_oauth2::read_service_account_key(sa_key_file).await?;
ServiceAccountAuthenticator::from_service_account_key(sa_key, &scopes).await
ServiceAccountAuthenticator::from_service_account_key(sa_key, scopes).await
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -116,7 +116,7 @@ pub struct InstalledFlowAuthenticator {
impl InstalledFlowAuthenticator {
pub(crate) async fn from_token_file_path<P: Into<PathBuf>>(
app_secret: ApplicationSecret,
scopes: &[&str],
scopes: &Vec<String>,
persistant_file_path: P,
) -> Result<Arc<dyn Authenticator>, BQError> {
let auth = YupInstalledFlowAuthenticator::builder(app_secret, InstalledFlowReturnMethod::HTTPRedirect)
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Authenticator for InstalledFlowAuthenticator {
/// See [Gooogle OAuth2.0 Documentation](https://developers.google.com/identity/protocols/oauth2/native-app).
pub(crate) async fn installed_flow_authenticator<S: AsRef<[u8]>, P: Into<PathBuf>>(
secret: S,
scopes: &[&str],
scopes: &Vec<String>,
persistant_file_path: P,
) -> Result<Arc<dyn Authenticator>, BQError> {
let app_secret = yup_oauth2::parse_application_secret(secret)?;
Expand All @@ -173,7 +173,7 @@ pub struct ApplicationDefaultCredentialsAuthenticator {
}

impl ApplicationDefaultCredentialsAuthenticator {
pub(crate) async fn from_scopes(scopes: &[&str]) -> Result<Arc<dyn Authenticator>, BQError> {
pub(crate) async fn from_scopes(scopes: &Vec<String>) -> Result<Arc<dyn Authenticator>, BQError> {
let opts = ApplicationDefaultCredentialsFlowOpts::default();
let auth = match YupApplicationDefaultCredentialsAuthenticator::builder(opts).await {
ApplicationDefaultCredentialsTypes::InstanceMetadata(auth) => auth.build().await,
Expand Down Expand Up @@ -206,7 +206,7 @@ impl Authenticator for ApplicationDefaultCredentialsAuthenticator {
}

pub(crate) async fn application_default_credentials_authenticator(
scopes: &[&str],
scopes: &Vec<String>,
) -> Result<Arc<dyn Authenticator>, BQError> {
ApplicationDefaultCredentialsAuthenticator::from_scopes(scopes).await
}
Expand All @@ -220,7 +220,7 @@ pub struct AuthorizedUserAuthenticator {
impl AuthorizedUserAuthenticator {
pub(crate) async fn from_authorized_user_secret(
authorized_user_secret: AuthorizedUserSecret,
scopes: &[&str],
scopes: &Vec<String>,
) -> Result<Arc<dyn Authenticator>, BQError> {
let auth = YupAuthorizedUserAuthenticator::builder(authorized_user_secret)
.build()
Expand Down Expand Up @@ -253,8 +253,8 @@ impl Authenticator for AuthorizedUserAuthenticator {

pub(crate) async fn authorized_user_authenticator<S: AsRef<Path>>(
secret: S,
scopes: &[&str],
scopes: &Vec<String>,
) -> Result<Arc<dyn Authenticator>, BQError> {
let authorized_user_secret = yup_oauth2::read_authorized_user_secret(secret).await?;
AuthorizedUserAuthenticator::from_authorized_user_secret(authorized_user_secret, scopes).await
AuthorizedUserAuthenticator::from_authorized_user_secret(authorized_user_secret, &scopes).await
}
40 changes: 21 additions & 19 deletions src/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ use crate::auth::{
service_account_authenticator, ServiceAccountAuthenticator,
};
use crate::error::BQError;
use crate::{Client, BIG_QUERY_AUTH_URL, BIG_QUERY_V2_URL};
use crate::{Client, BIG_QUERY_AUTH_URL, BIG_QUERY_V2_URL, DRIVE_AUTH_URL};

pub struct ClientBuilder {
v2_base_url: String,
auth_base_url: String,
auth_base_urls: Vec<String>,
}

impl ClientBuilder {
pub fn new() -> Self {
Self {
v2_base_url: BIG_QUERY_V2_URL.to_string(),
auth_base_url: BIG_QUERY_AUTH_URL.to_string(),
auth_base_urls: vec![BIG_QUERY_AUTH_URL.to_string(), DRIVE_AUTH_URL.to_string()],
}
}

Expand All @@ -27,8 +27,8 @@ impl ClientBuilder {
self
}

pub fn with_auth_base_url(&mut self, base_url: String) -> &mut Self {
self.auth_base_url = base_url;
pub fn with_auth_base_url(&mut self, base_urls: Vec<String>) -> &mut Self {
self.auth_base_urls = base_urls;
self
}

Expand All @@ -37,21 +37,26 @@ impl ClientBuilder {
sa_key: ServiceAccountKey,
readonly: bool,
) -> Result<Client, BQError> {
let scope = if readonly {
format!("{}.readonly", self.auth_base_url)
} else {
self.auth_base_url.clone()
};
let sa_auth = ServiceAccountAuthenticator::from_service_account_key(sa_key, &[&scope]).await?;
let scopes: Vec<String> = self
.auth_base_urls
.iter()
.map(|auth_base_url| {
if readonly {
format!("{}.readonly", auth_base_url)
} else {
auth_base_url.to_string()
}
})
.collect();
let sa_auth = ServiceAccountAuthenticator::from_service_account_key(sa_key, &scopes).await?;
youmts marked this conversation as resolved.
Show resolved Hide resolved

let mut client = Client::from_authenticator(sa_auth);
client.v2_base_url(self.v2_base_url.clone());
Ok(client)
}

pub async fn build_from_service_account_key_file(&self, sa_key_file: &str) -> Result<Client, BQError> {
let scopes = vec![self.auth_base_url.as_str()];
let sa_auth = service_account_authenticator(scopes, sa_key_file).await?;
let sa_auth = service_account_authenticator(&self.auth_base_urls, sa_key_file).await?;

let mut client = Client::from_authenticator(sa_auth);
client.v2_base_url(self.v2_base_url.clone());
Expand All @@ -77,8 +82,7 @@ impl ClientBuilder {
secret: S,
persistant_file_path: P,
) -> Result<Client, BQError> {
let scopes = vec![self.auth_base_url.as_str()];
let auth = installed_flow_authenticator(secret, &scopes, persistant_file_path).await?;
let auth = installed_flow_authenticator(secret, &self.auth_base_urls, persistant_file_path).await?;

let mut client = Client::from_authenticator(auth);
client.v2_base_url(self.v2_base_url.clone());
Expand All @@ -100,8 +104,7 @@ impl ClientBuilder {
}

pub async fn build_from_application_default_credentials(&self) -> Result<Client, BQError> {
let scopes = vec![self.auth_base_url.as_str()];
let auth = application_default_credentials_authenticator(&scopes).await?;
let auth = application_default_credentials_authenticator(&self.auth_base_urls).await?;

let mut client = Client::from_authenticator(auth);
client.v2_base_url(self.v2_base_url.clone());
Expand All @@ -112,8 +115,7 @@ impl ClientBuilder {
&self,
authorized_user_secret_path: P,
) -> Result<Client, BQError> {
let scopes = vec![self.auth_base_url.as_str()];
let auth = authorized_user_authenticator(authorized_user_secret_path, &scopes).await?;
let auth = authorized_user_authenticator(authorized_user_secret_path, &self.auth_base_urls).await?;

let mut client = Client::from_authenticator(auth);
client.v2_base_url(self.v2_base_url.clone());
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod tabledata;

const BIG_QUERY_V2_URL: &str = "https://bigquery.googleapis.com/bigquery/v2";
const BIG_QUERY_AUTH_URL: &str = "https://www.googleapis.com/auth/bigquery";
const DRIVE_AUTH_URL: &str = "https://www.googleapis.com/auth/drive";

/// An asynchronous BigQuery client.
#[derive(Clone)]
Expand Down