Google 驗證擴充功能

您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件
info

版本:2.0.2

向 Google 驗證身分,存取您指定的 Google API

使用這個擴充功能取得 Google Cloud 服務的權杖 (OAuth 或 JWT),然後將權杖用於後續的 Google API 呼叫,例如使用 ServiceCallout 政策

舉例來說,您可以在 API Proxy 中使用這個擴充功能取得權杖,然後使用 PopulateCache 政策快取權杖,再使用 ServiceCallout 政策傳遞權杖,從 API Proxy 流程中存取 Google Cloud 服務。

必要條件

這項內容提供設定及使用這項擴充功能的參考資料。使用 ExtensionCallout 政策從 API Proxy 使用擴充功能之前,請務必完成下列步驟:

  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 Proxy 中的程式碼,說明如何設定及使用快取權杖,透過 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 Proxy 使用。

      <?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 Proxy 中的政策稍後使用。

      <?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 Proxy 和 Google API 之間支援雙足式 OAuth。

在雙向 OAuth 中,這項擴充功能動作會使用服務帳戶 JSON (您在設定這項擴充功能時新增該 JSON) 向 Google 進行驗證,藉此擷取 OAuth 權杖。這個動作會擷取 OAuth 權杖,API Proxy 即可使用該權杖呼叫 Google API,代表 Google 服務帳戶呼叫 API。

Google Cloud API 的存取權會透過「Google API 適用的 OAuth 2.0 範圍」中列出的範圍進行篩選。

如要進一步瞭解 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 範圍」。 陣列 ,可授予服務帳戶存取所有 API 的權限。["https://www.googleapis.com/auth/cloud-platform"] 不會。

回應

物件,包含存取權杖、權杖類型和到期日,格式如下:

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

回應屬性

參數 說明 預設 必填
accessToken OAuth 2.0 存取權杖。
tokenType 權杖類型。 Bearer
expiresInSec 權杖失效前的秒數。 3600

getJWTAccessToken

取得 JSON Web Token (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>

要求參數

參數 說明 預設 必填
目標對象 權杖的預期接收者。這可能包括受 Cloud IAP 保護的用戶端 ID、Cloud Functions 網址

回應

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

回應屬性

參數 說明 預設 必填
accessToken 存取權杖。
tokenType 權杖類型。 Bearer
expiresInSec 以秒為單位的有效期限。 3600

設定參考資料

設定及部署這個擴充功能,以便在 API Proxy 中使用時,請參考下列資訊。如要瞭解如何使用 Apigee 控制台設定擴充功能,請參閱「新增及設定擴充功能」。

常見的擴充功能屬性

每個擴充功能都有下列屬性。

屬性 說明 預設 必要
name 您要為這項擴充功能設定的名稱。 相容
packageName Apigee Edge 指定的擴充功能套件名稱。 相容
version 擴充功能的擴充功能套件版本號碼。 相容
configuration 您要新增的擴充功能專屬的設定值。請參閱「這個擴充功能套件的屬性」一文 相容

這個擴充套件的屬性

指定下列設定屬性的值,這些屬性專屬於這個擴充功能。

屬性 說明 預設 必填
憑證 在 Apigee Edge 控制台中輸入時,這是服務帳戶金鑰 JSON 檔案的完整內容。透過 Management API 傳送時,這是從整個服務帳戶金鑰 JSON 檔案產生的 Base64 編碼值。