Google Authentication 扩展程序

您正在查看 Apigee Edge 文档。
前往 Apigee X 文档
信息

版本:2.0.0

使用 Google 进行身份验证,以访问您指定的 Google API

使用此扩展程序获取 Google Cloud 服务的令牌(OAuth 或 JWT),然后使用该令牌进行后续对 Google API 的调用,例如使用 ServiceCallout 政策

例如,在 API 代理中,您可以使用此扩展程序获取令牌,使用 PopulateCache 政策将令牌缓存起来,然后通过 ServiceCallout 政策传递令牌,以便从 API 代理流程内访问 Google Cloud 服务。

前提条件

本文档提供了有关配置和使用此扩展程序的参考信息。在使用 ExtensionCallout 政策从 API 代理使用扩展程序之前,您必须满足以下条件:

  1. 确保该扩展程序将使用的账号(即您将用作凭据的服务账号所代表的账号)有权访问该扩展程序将用于身份验证的 Google Cloud 服务。

  2. 使用 Google Cloud 控制台为服务账号生成密钥

  3. 使用配置参考添加和配置扩展程序时,请使用生成的服务账号密钥 JSON 文件的内容。

关于使用 Google Cloud 进行身份验证

此扩展程序通过代表您在 Google Cloud 项目中定义的特定成员,向 Google Cloud 请求进行身份验证。在配置此扩展程序时,您需要使用该项目成员的服务账号 JSON 文件。

因此,此扩展程序只能访问该成员拥有权限的资源。换句话说,此扩展程序能否成功进行身份验证,取决于 Google Cloud 控制台中授予的权限与扩展程序在运行时请求的访问权限(通过镜重或受众群体)是否匹配。

通常,您需要执行以下步骤才能通过此扩展程序对 API 进行身份验证并获得访问权限:

  1. 确保此扩展程序所代表的成员服务账号有权访问您要访问的 Google 资源。您可以使用 Google Cloud 控制台中的 Cloud Identity and Access Management (Cloud IAM) 页面,向此扩展程序代表的项目成员授予角色

  2. 在配置此扩展程序时,请使用该成员的服务账号密钥 JSON。

  3. 在配置 ExtensionCallout 政策以使用此扩展程序时,请仅针对您的项目成员有权访问的资源请求身份验证。

示例

以下示例展示了如何使用 ExtensionCallout 政策对 Google Cloud 进行身份验证。

获取访问令牌

在以下示例中,扩展程序的 getOauth2AccessToken 操作会检索令牌,以便在向 Cloud Translation API 发出请求时使用。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ConnectorCallout async="false" continueOnError="true" enabled="true" name="Get-Access-Token">
    <DisplayName>Get Access Token</DisplayName>
    <Connector>google-auth</Connector>
    <Action>getOauth2AccessToken</Action>
    <Input><![CDATA[{
      "scope" : [
        "https://www.googleapis.com/auth/cloud-translation"
      ]
    }]]></Input>
    <Output>google.credentials</Output>
</ConnectorCallout>

响应值如下所示:

{
  "access_token":"ya29.c.ElpSB...BMgkALBJ0kou-8",
  "token_type":"Bearer",
  "expiresInSec": 3600
}

以下 AssignMessage 政策会从上述 ExtensionCallout 政策检索响应值,并将其复制到响应载荷中。这对调试可能很有用。在实际操作中,您可能不想将令牌返回给客户端。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Retrieve-Auth-Token">
    <DisplayName>Retrieve Auth Token</DisplayName>
    <AssignTo type="response" createNew="false"/>
    <Set>
        <Payload contentType="application/json">{google.credentials.access_token}</Payload>
    </Set>
</AssignMessage>

缓存访问令牌

为避免进行不必要的调用来检索令牌,不妨考虑缓存收到的令牌。对于需要令牌的后续调用,从 Apigee Edge 缓存中检索令牌比获取新令牌更快。当缓存的令牌过期时,检索新的令牌并使用该令牌刷新缓存。

以下示例 API 代理中的代码展示了如何设置和使用缓存的令牌,以便使用 ServiceCallout 政策调用 Google Translation API。此处的每个代码示例都适用于流程中的不同政策。

系统会按照以下流程 XML 中所述的顺序执行以下政策:

  <Request>
    <!-- Attempt to get a token from the cache. -->
    <Step>
        <Name>Get-Cached-Auth-Token</Name>
    </Step>
    <!-- Only execute the following ExtensionCallout policy if the call to the
      cache couldn't retrieve a cached token. -->
    <Step>
        <Name>Google-Auth-Callout</Name>
        <Condition>lookupcache.Get-Cached-Auth-Token.cachehit is false</Condition>
    </Step>
    <!-- Only execute the following PopulateCache policy if the call to the
      cache couldn't retrieve a cached token. -->
    <Step>
        <Name>Cache-Auth-Token</Name>
        <Condition>lookupcache.Get-Cached-Auth-Token.cachehit is false</Condition>
    </Step>
    <!-- Use the ServiceCallout policy to call the translate API. -->
    <Step>
        <Name>Translate-Text</Name>
    </Step>
</Request>
  1. 以下 LookupCache 政策会尝试从缓存中获取令牌。如果令牌已获取并缓存,此政策将获取该令牌以供 API 代理使用。

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <LookupCache async="false" continueOnError="false" enabled="true" name="Get-Cached-Auth-Token">
          <DisplayName>Get Cached Auth Token</DisplayName>
          <!-- Give cache key and scope to specify the entry for the cached token. -->
          <CacheKey>
              <Prefix/>
              <KeyFragment>gcp_translate_token_</KeyFragment>
          </CacheKey>
          <Scope>Exclusive</Scope>
          <!-- Assign the retrieved token (if any) to a variable, where it
           can be retrieved by policies. -->
          <AssignTo>cloud.translation.auth.token</AssignTo>
      </LookupCache>
    
  2. 如果缓存查找未检索到缓存的令牌,以下 ExtensionCallout 政策会检索新的 OAuth 令牌,并将 Google Cloud Translation API 指定为令牌的范围。如果配置 Google-Auth-Callout 扩展程序时使用的服务账号凭据代表有权访问该 API 的项目成员,Google Cloud 会返回有效令牌。

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <ConnectorCallout async="false" continueOnError="true" enabled="true" name="Google-Auth-Callout">
          <DisplayName>Google-Auth-Callout</DisplayName>
          <Connector>example-auth-extension</Connector>
          <Action>getOauth2AccessToken</Action>
          <Input><![CDATA[{
            "scope" : ["https://www.googleapis.com/auth/cloud-translation"]
          }]]></Input>
          <Output parsed="false">cloud.translation.auth.token</Output>
      </ConnectorCallout>
    
  3. ExtensionCallout 政策检索到新令牌后,PopulateCache 政策会将其缓存起来,以供 API 代理中的政策稍后使用。

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <PopulateCache async="false" continueOnError="false" enabled="true" name="Cache-Auth-Token">
          <DisplayName>Cache Auth Token</DisplayName>
          <Properties/>
          <!-- Set cache key information to specify a unique key for this entry. -->
          <CacheKey>
              <Prefix/>
              <KeyFragment>gcp_translate_token_</KeyFragment>
          </CacheKey>
          <Scope>Exclusive</Scope>
          <ExpirySettings>
              <TimeoutInSec>5</TimeoutInSec>
          </ExpirySettings>
          <!-- Get the token to cache from the variable where the ExtensionCallout put it. -->
          <Source>cloud.translation.auth.token</Source>
      </PopulateCache>
    

操作

getOauth2AccessToken

获取 OAuth 2.0 访问令牌。当 Google API 需要 OAuth 令牌时,您可以使用此操作在 API 代理和 Google API 之间支持两段式 OAuth。

在两段式 OAuth 中,此扩展程序操作会使用服务账号 JSON(您在配置此扩展程序时添加该 JSON)向 Google 进行身份验证,以检索 OAuth 令牌。此操作检索到 OAuth 令牌后,您的 API 代理便可以使用该令牌调用 Google API,从而有效地代表 Google 服务账号调用 API。

系统会根据Google API 适用的 OAuth 2.0 范围中列出的范围来过滤对 Google Cloud API 的访问权限。

如需详细了解使用 OAuth 2.0 进行服务器到服务器交互,请参阅将 OAuth 2.0 用于服务器到服务器应用

语法

<Action>getOauth2AccessToken</Action>
<Input><![CDATA[{
  "scope" : [
    "scope1",
    "scope2"
  ]
}]]></Input>

示例

在以下示例中,扩展程序的 getOauth2AccessToken 操作会检索令牌,以便在向 Cloud Translation API 发出请求时使用。

<Action>getOauth2AccessToken</Action>
<Input><![CDATA[{
    "scope" : [
      "https://www.googleapis.com/auth/cloud-translation"
  ]
}]]></Input>

请求参数

参数 说明 类型 默认值 必需
范围 一个 OAuth 2.0 范围数组。如需详细了解范围,请参阅适用于 Google API 的 OAuth 2.0 范围 数组 ["https://www.googleapis.com/auth/cloud-platform"],用于授予对服务账号有权访问的所有 API 的访问权限。 单元编号

响应

一个对象,其中包含访问令牌、其类型和到期日期,格式如下:

{
  "accessToken": "ewogICJ0eXB...C5jb20iCn0K",
  "token_type": "Bearer",
  "expiresInSec": 3600
}

响应属性

参数 说明 默认 必需
accessToken OAuth 2.0 访问令牌。
tokenType 令牌类型。 承载系统
expiresInSec 令牌到期前的秒数。 3600

getJWTAccessToken

获取 JSON Web 令牌 (JWT) 访问令牌。如果您要调用的 API 在 Google API GitHub 代码库中发布了服务定义,您可以使用此令牌对 Google API 进行身份验证。

对于某些 Google API,您可以直接使用已签名的 JWT 作为不记名令牌(而不是 OAuth 2.0 访问令牌)进行已授权的 API 调用。这样一来,您就可以避免在发出 API 调用之前向 Google 的授权服务器发出网络请求。

如需详细了解如何使用 JWT 访问令牌进行身份验证,请参阅将 OAuth 2.0 用于服务器到服务器应用

语法

<Action>getJWTAccessToken</Action>
<Input><![CDATA[{
    "audience" : "audience"
}]]></Input>

示例:Cloud Functions 函数网址

在以下示例中,扩展程序的 getOauth2AccessToken 操作会检索令牌,以便在向 Cloud Translation API 发出请求时使用。

<Action>getJWTAccessToken</Action>
<Input><![CDATA[{
  "audience" : "https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME"
}]]></Input>

示例:受 Cloud IAP 保护的客户端 ID

在以下示例中,扩展程序的 getOauth2AccessToken 操作会检索令牌,以便在向 Cloud Translation API 发出请求时使用。

<Action>getJWTAccessToken</Action>
<Input><![CDATA[{
  "audience" : "Cloud-IAP-secured-client-ID"
}]]></Input>

请求参数

参数 说明 默认 必需
audience 令牌的目标收件人。这可以是受 Cloud IAP 保护的客户端 ID、Cloud Functions 网址等。

响应

{
  "accessToken": "token",
  "tokenType": "Bearer",
  "expiresInSec": 3600
}

响应属性

参数 说明 默认 必需
accessToken 访问令牌。
tokenType 令牌类型。 承载系统
expiresInSec 到期时间(以秒为单位)。 3600

配置参考文档

在配置和部署此扩展以在 API 代理中使用时,请使用以下内容。如需了解使用 Apigee 控制台配置扩展程序的步骤,请参阅添加和配置扩展程序

常见的扩展属性

每个扩展程序都有以下属性。

属性 说明 默认 必需
name 您为扩展程序配置此名称。
packageName Apigee Edge 提供的扩展包的名称。
version 配置扩展程序所用的扩展程序软件包的版本号。
configuration 特定于您要添加的附加信息的配置值。请参阅此扩展程序软件包的属性

此扩展程序软件包的属性

为此扩展程序专用的以下配置属性指定值。

属性 说明 默认 必需
凭据 在 Apigee Edge 控制台中输入此内容后,即为服务账号密钥 JSON 文件的完整内容。通过 Management API 发送时,此参数是从整个服务账号密钥 JSON 文件生成的 base64 编码值。