Skip to content

Commit

Permalink
Merge pull request #26 from andreclaudino/feature/new-from-custom-fil…
Browse files Browse the repository at this point in the history
…e-path

Expose a function create an instance from a path instead of a variable
  • Loading branch information
hrvolapeter authored Feb 8, 2021
2 parents dc6b4d5 + e1b269f commit 13da39d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ let authentication_manager = gcp_auth::init().await?;
let token = authentication_manager.get_token(&scopes).await?;
```

You may instantiate `authentication_manager` from a credentials file path using the method `from_credentials_file`:

```async
// `credentials_path` variable is the path for the credentials `.json` file.
let authentication_manager = gcp_auth::from_credentials_file(credentials_path).await?;
let token = authentication_manager.get_token().await?;
```

## Local user authentication

This authentication method allows developers to authenticate again GCP when
Expand Down
6 changes: 1 addition & 5 deletions src/custom_service_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ pub struct CustomServiceAccount {
}

impl CustomServiceAccount {
const GOOGLE_APPLICATION_CREDENTIALS: &'static str = "GOOGLE_APPLICATION_CREDENTIALS";

pub async fn new() -> Result<Self, Error> {
let path = std::env::var(Self::GOOGLE_APPLICATION_CREDENTIALS)
.map_err(|_| Error::AplicationProfileMissing)?;
pub async fn from_file(path: &str) -> Result<Self, Error> {
let credentials = ApplicationCredentials::from_file(path).await?;
Ok(Self {
credentials,
Expand Down
25 changes: 22 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
//! let authentication_manager = gcp_auth::init().await?;
//! let token = authentication_manager.get_token().await?;
//! ```
//! You may instantiate `authentication_manager` from a credentials file path using the method `from_credentials_file`:
//!
//! ```async
//! // `credentials_path` variable is the path for the credentials `.json` file.
//! let authentication_manager = gcp_auth::from_credentials_file(credentials_path).await?;
//! let token = authentication_manager.get_token().await?;
//! ```
//!
//! # Local user authentication
//! This authentication method allows developers to authenticate again GCP services when developign locally.
//! The method is intended only for development. Credentials can be set-up using `gcloud auth` utility.
Expand Down Expand Up @@ -74,17 +82,17 @@ pub use types::Token;
use hyper::Client;
use hyper_rustls::HttpsConnector;

/// Initialize GCP authentication
/// Initialize GCP authentication based on a credentials file
///
/// Returns `AuthenticationManager` which can be used to obtain tokens
pub async fn init() -> Result<AuthenticationManager, Error> {
pub async fn from_credentials_file(path: &str) -> Result<AuthenticationManager, Error> {
#[cfg(feature = "webpki-roots")]
let https = HttpsConnector::with_webpki_roots();
#[cfg(not(feature = "webpki-roots"))]
let https = HttpsConnector::with_native_roots();

let client = Client::builder().build::<_, hyper::Body>(https);
let custom = custom_service_account::CustomServiceAccount::new().await;
let custom = custom_service_account::CustomServiceAccount::from_file(path).await;
if let Ok(service_account) = custom {
return Ok(AuthenticationManager {
client,
Expand All @@ -111,3 +119,14 @@ pub async fn init() -> Result<AuthenticationManager, Error> {
Box::new(user.unwrap_err()),
))
}

/// Initialize GCP authentication
///
/// Returns `AuthenticationManager` which can be used to obtain tokens
pub async fn init() -> Result<AuthenticationManager, Error> {
const GOOGLE_APPLICATION_CREDENTIALS: &'static str = "GOOGLE_APPLICATION_CREDENTIALS";
let path = std::env::var(GOOGLE_APPLICATION_CREDENTIALS)
.map_err(|_| Error::AplicationProfileMissing)?;

from_credentials_file(&path).await
}

0 comments on commit 13da39d

Please sign in to comment.