#google-cloud #gcp #authentication #jwt

google-cloud-auth

Google Cloud Platform server application authentication library

26 releases (13 breaking)

new 0.14.0 Apr 18, 2024
0.13.2 Mar 24, 2024
0.13.1 Feb 27, 2024
0.13.0 Oct 15, 2023
0.1.1 Nov 28, 2021

#1115 in Web programming

Download history 12810/week @ 2023-12-23 16408/week @ 2023-12-30 21258/week @ 2024-01-06 22476/week @ 2024-01-13 24241/week @ 2024-01-20 25177/week @ 2024-01-27 24368/week @ 2024-02-03 18794/week @ 2024-02-10 24171/week @ 2024-02-17 34464/week @ 2024-02-24 30879/week @ 2024-03-02 32939/week @ 2024-03-09 28658/week @ 2024-03-16 34178/week @ 2024-03-23 38355/week @ 2024-03-30 23440/week @ 2024-04-06

131,012 downloads per month
Used in 15 crates (10 directly)

MIT license

89KB
2K SLoC

google-cloud-auth

Google Cloud Platform server application authentication library.

crates.io

Installation

[dependencies]
google-cloud-auth = <version>

Quickstart

use google_cloud_auth::*;

#[tokio::main]
async fn main() -> Result<(), error::Error> {
    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 ts = create_token_source(config).await?;
    let token = ts.token().await?;
    println!("token is {}",token.access_token);
    Ok(())
}

create_token_sourcelooks 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–24MB
~388K SLoC