#google-cloud #authentication #gcp #google-api

google-cloud-auth

Google Cloud Platform server application authentication library

30 releases (16 breaking)

0.17.1 Sep 22, 2024
0.16.0 Jun 27, 2024
0.13.2 Mar 24, 2024
0.13.0 Oct 15, 2023
0.1.1 Nov 28, 2021

#1463 in Web programming

Download history 38205/week @ 2024-07-27 34738/week @ 2024-08-03 41065/week @ 2024-08-10 48839/week @ 2024-08-17 57266/week @ 2024-08-24 53282/week @ 2024-08-31 55287/week @ 2024-09-07 47293/week @ 2024-09-14 55315/week @ 2024-09-21 64363/week @ 2024-09-28 58861/week @ 2024-10-05 61399/week @ 2024-10-12 61381/week @ 2024-10-19 57414/week @ 2024-10-26 45630/week @ 2024-11-02 52834/week @ 2024-11-09

229,355 downloads per month
Used in 47 crates (13 directly)

MIT license

92KB
2K SLoC

google-cloud-auth

Google Cloud Platform server application authentication library.

crates.io

Installation

[dependencies]
google-cloud-auth = <version>
google-cloud-token = "0.1.2"

Quickstart

#[tokio::main]
async fn main() -> Result<(), error::Error> {
    use google_cloud_auth::{project::Config, token::DefaultTokenSourceProvider};
    use google_cloud_token::TokenSourceProvider as _;

    let audience = "https://spanner.googleapis.com/";
    let scopes = [
        "https://www.googleapis.com/auth/cloud-platform",
        "https://www.googleapis.com/auth/spanner.data",
    ];
    let config = Config {
        // audience is required only for service account jwt-auth
        // https://developers.google.com/identity/protocols/oauth2/service-account#jwt-auth
        audience: Some(audience),
        // scopes is required only for service account Oauth2
        // https://developers.google.com/identity/protocols/oauth2/service-account
        scopes: Some(&scopes),
        sub: None,
    };
    let tsp = DefaultTokenSourceProvider::new(config).await?;
    let ts = tsp.token_source();
    let token = ts.token().await?;
    println!("token is {}", token);
    Ok(())
}

DefaultTokenSourceProvider::new(config) looks for credentials in the following places, preferring the first location found:

  1. A JSON file whose path is specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable.
  2. A JSON file in a location known to the gcloud command-line tool. On Windows, this is %APPDATA%/gcloud/application_default_credentials.json. On other systems, $HOME/.config/gcloud/application_default_credentials.json.
  3. On Google Compute Engine, it fetches credentials from the metadata server.

Supported Credentials

Supported Workload Identity

https://cloud.google.com/iam/docs/workload-identity-federation

  • AWS
  • Azure Active Directory
  • On-premises Active Directory
  • Okta
  • Kubernetes clusters

Dependencies

~8–23MB
~369K SLoC