依使用者 ID、應用程式 ID 或兩者撤銷 OAuth 2.0 存取權杖

本頁適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

您可以使用 RevokeOAuthV2 政策,依使用者 ID、應用程式 ID 或兩者撤銷 OAuth 2.0 存取權杖。所謂的使用者,是指呼叫 API 的應用程式使用者。

在存取權憑證中儲存使用者 ID 的時間點

根據預設,存取權杖中不會包含使用者 ID。有時,在存取權憑證中儲存使用者 ID 會很有幫助。例如:

  • 網站或應用程式中的功能,可讓使用者查看已授權的第三方應用程式,並提供撤銷這些應用程式存取權的選項。
  • 這項功能可讓授權使用者撤銷與特定開發人員應用程式相關聯的所有存取權杖。

關於 OAuth 存取權杖

根據預設,當 Apigee 產生 OAuth 2.0 存取權杖時,權杖的格式如下所示:

{
 "issued_at" : "1421847736581",
 "application_name" : "a68d01f8-b15c-4be3-b800-ceae8c456f5a",
 "scope" : "READ",
 "status" : "approved",
 "api_product_list" : "[PremiumWeatherAPI]",
 "expires_in" : "3599", //--in seconds
 "developer.email" : "tesla@weathersample.com",
 "organization_id" : "0",
 "token_type" : "BearerToken",
 "client_id" : "k3nJyFJIA3p62DWOkLO6OJNi87GYXFmP",
 "access_token" : "7S22UqXGJDTuUADGzJzjXzXSaGJL",
 "organization_name" : "myorg",
 "refresh_token_expires_in" : "0", //--in seconds
 "refresh_count" : "0"
}

注意事項:

  • 「application_name」application_name欄位包含與權杖相關聯的應用程式 UUID。如果您啟用 OAuth 2.0 存取權杖的應用程式 ID 撤銷功能,這就是您使用的應用程式 ID。
  • access_token 欄位包含 OAuth 2.0 存取權憑證值。
  • 預設 OAuth 存取權杖中沒有使用者 ID 欄位。

如要依終端使用者 ID 撤銷 OAuth 2.0 存取權杖,您必須設定 OAuth 2.0 政策,讓權杖中包含使用者 ID。設定政策後,系統會在權杖中加入使用者 ID,並以 app_enduser 欄位加入,如下所示:

{
 "issued_at" : "1421847736581",
 "application_name" : "a68d01f8-b15c-4be3-b800-ceae8c456f5a",
 "scope" : "READ",
 "app_enduser" : "6ZG094fgnjNf02EK",
 "status" : "approved",
 "api_product_list" : "[PremiumWeatherAPI]",
 "expires_in" : "3599", //--in seconds
 "developer.email" : "tesla@weathersample.com",
 "organization_id" : "0",
 "token_type" : "BearerToken",
 "client_id" : "k3nJyFJIA3p62DWOkLO6OJNi87GYXFmP",
 "access_token" : "7S22UqXGJDTuUADGzJzjXzXSaGJL",
 "organization_name" : "myorg",
 "refresh_token_expires_in" : "0", //--in seconds
 "refresh_count" : "0"
}

設定政策

如要依使用者 ID 撤銷權杖,您必須先設定 OAuth 2.0 政策,將使用者 ID 新增至存取權杖。只要在存取權權杖中加入使用者 ID,您就能依使用者 ID 撤銷權杖。

如要將政策設為在存取權存取金中加入使用者 ID,您必須指定含有使用者 ID 的輸入變數。使用 <AppEndUser> 標記指定變數:

<OAuthV2 async="false" continueOnError="false" enabled="true" name="GenerateAccessTokenClient">
  <DisplayName>OAuth 2.0.0 1</DisplayName>
  <ExternalAuthorization>false</ExternalAuthorization>
  <Operation>GenerateAccessToken</Operation>
  <SupportedGrantTypes>
    <GrantType>client_credentials</GrantType>
  </SupportedGrantTypes>
  <GenerateResponse enabled="true"/>
  <GrantType>request.queryparam.grant_type</GrantType>
  <AppEndUser>request.header.appuserID</AppEndUser>
  <ExpiresIn>960000</ExpiresIn>
</OAuthV2>

以下是設定為由 EndUserId 撤銷存取權的 RevokeOAuthV2 政策範例:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RevokeOAuthV2 continueOnError="false" enabled="true" name="GetOAuthV2Info-1">
    <DisplayName>Get OAuth v2.0 Info 1</DisplayName>
    <EndUserId ref="request.header.appuserID"></EndUserId>
    <Cascade>false</Cascade>
</RevokeOAuthV2>

另請參閱「撤銷 OAuth v2 政策」。

以下範例指令會產生權杖,並在 appuserID 標頭中傳遞使用者 ID:

curl -H "appuserID:6ZG094fgnjNf02EK" \
-H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Basic c3FIOG9vSGV4VHo4QzAyg5T1JvNnJoZ3ExaVNyQWw6WjRsanRKZG5lQk9qUE1BVQ" \
  -X POST "https://apitest.acme.com/oauth/token" \
  -d "grant_type=client_credentials"

您可以透過其他方式,將資訊傳遞為要求的一部分。舉例來說,您可以改為執行下列操作:

  • 使用表單參數變數:request.formparam.appuserID
  • 使用提供使用者 ID 的流程變數