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

本頁內容適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

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

何時將 User-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 欄位包含與權杖相關聯的應用程式 UUID。如果您啟用依應用程式 ID 撤銷 OAuth 2.0 存取權杖,則這是您使用的應用程式 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 的流程變數