Google Cloud 身份验证 (Swift 版)

此软件包为 Google Cloud 服务身份验证提供了一个 Swift 实现。它旨在成为一个服务器优先的解决方案,而不是官方的 google-auth-library-swift

用法示例

let authorization = Authorization(scopes: ["https://www.googleapis.com/auth/cloud-platform"], eventLoopGroup: <#eventLoopGroup#>)
let accessToken = try await authorization.accessToken()
print(accessToken)

您还可以隐式地使用一个特定的方法(又名提供程序)进行身份验证

let authorization = Authorization(
    scopes: ["https://www.googleapis.com/auth/cloud-platform"],
    provider: ServiceAccountProvider(credentials: <#credentials#>),
    eventLoopGroup: <#eventLoopGroup#>
)

默认提供程序可以通过使用 AuthorizationSystem 进行全局配置。

await AuthorizationSystem.bootstrap(
    ServiceAccountProvider(credentials: <#credentials#>)
)

支持的身份验证方法

以下是当前支持的身份验证方法以及默认提供程序的默认顺序。

1. 服务帐户 (JSON 密钥文件)

如果设置了 GOOGLE_APPLICATION_CREDENTIALS 环境变量,将使用服务帐户 JSON 文件。

2. Google Cloud SDK

如果 ~/.config/gcloud/application_default_credentials.json 文件存在,将使用 Google Cloud SDK 凭据。

3. Compute Engine、Kubernetes Engine、Cloud Run、App Engine、Cloud Functions

如果在 Google Cloud Platform 上运行,将使用元数据服务器来检索凭据。