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

google-cloud-auth

Google Cloud Platform 服务器应用程序身份验证库

28 个版本 (15 个破坏性更新)

0.16.0 2024年6月27日
0.14.0 2024年4月18日
0.13.2 2024年3月24日
0.13.0 2023年10月15日
0.1.1 2021年11月28日

#140 in 网页编程

Download history 29980/week @ 2024-04-27 32271/week @ 2024-05-04 40819/week @ 2024-05-11 45851/week @ 2024-05-18 37348/week @ 2024-05-25 42954/week @ 2024-06-01 42682/week @ 2024-06-08 38562/week @ 2024-06-15 39527/week @ 2024-06-22 34681/week @ 2024-06-29 42892/week @ 2024-07-06 23976/week @ 2024-07-13 37385/week @ 2024-07-20 38205/week @ 2024-07-27 34738/week @ 2024-08-03 34480/week @ 2024-08-10

每月下载量 148,296
46 个 crate 中使用 (13 个直接使用)

MIT 许可证

91KB
2K SLoC

google-cloud-auth

Google Cloud Platform 服务器应用程序身份验证库。

crates.io

安装

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

快速入门

#[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) 将在以下位置查找凭证,优先查找第一个找到的位置

  1. 一个由 GOOGLE_APPLICATION_CREDENTIALS 环境变量指定的路径的 JSON 文件。
  2. 一个已知于 gcloud 命令行工具的位置的 JSON 文件。在 Windows 上,这是 %APPDATA%/gcloud/application_default_credentials.json。在其他系统上,$HOME/.config/gcloud/application_default_credentials.json。
  3. 在 Google Compute Engine 上,它从元数据服务器获取凭证。

支持的凭证

支持的 Workload Identity

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

  • AWS
  • Azure Active Directory
  • 本地 Active Directory
  • Okta
  • Kubernetes 集群

依赖关系

~7–23MB
~379K SLoC