您正在查看 Apigee Edge 說明文件。
前往 Apigee X 說明文件。info

結果
OAuthV2 是執行 OAuth 2.0 授權類型作業的多面向政策。這是用於在 Apigee Edge 上設定 OAuth 2.0 端點的主要政策。
提示:如要進一步瞭解 Apigee Edge 上的 OAuth,請參閱 OAuth 首頁。當中提供了資源、範例、影片等項目的連結。如要瞭解如何在實際應用程式中使用這項政策,請參閱 GitHub 上的進階 OAuth 範例。
範例
VerifyAccessToken
VerifyAccessToken
這項 OAuthV2 政策設定 (搭配 VerifyAccessToken 作業) 會驗證提交至 Apigee Edge 的存取權杖是否有效。觸發這項政策操作時,Edge 會在要求中尋找有效的存取權權杖。如果存取權杖有效,系統就會允許要求繼續進行。如果無效,則所有處理作業都會停止,並在回應中傳回錯誤。
<OAuthV2 async="false" continueOnError="false" enabled="true" name="OAuth-v20-2"> <DisplayName>OAuth v2.0 2</DisplayName> <Operation>VerifyAccessToken</Operation> <AccessTokenPrefix>Bearer</AccessTokenPrefix> <!-- Optional, default is Bearer --> </OAuthV2>
注意:系統僅支援 OAuth 2.0 不記名憑證。不支援訊息驗證碼 (MAC) 權杖。
例如:
$ curl -H "Authorization: Bearer ylSkZIjbdWybfsUQe9BqP0LH5Z" http://{org_name}-test.apigee.net/weather/forecastrss?w=12797282
根據預設,Edge 會接受 Authorization
標頭中含有 Bearer
前置字元的存取權權杖。您可以使用 <AccessToken>
元素變更這項預設值。
GenerateAccessToken
產生存取權杖
如需針對每種支援的授權類型,說明如何要求存取權杖的範例,請參閱「要求存取權杖和授權碼」。本主題包含以下操作的範例:
GenerateAuthorizationCode
產生授權碼
如需要求授權碼的範例,請參閱「要求授權碼」。
RefreshAccessToken
重新整理存取權杖
如需使用更新權杖要求存取權杖的範例,請參閱「更新存取權杖」。
回應流程權杖
在回應流程中產生存取權杖
有時您可能需要在回應流程中產生存取權杖。舉例來說,您可以針對後端服務中執行的某些自訂驗證做出回應。在本例中,用途需要存取權杖和重新整理權杖,因此排除隱含授權類型。在這種情況下,我們會使用密碼授權類型產生權杖。如您所見,要讓這項功能運作,訣竅就是透過 JavaScript 政策傳入授權要求標頭。
首先,讓我們來看看範例政策:
<OAuthV2 enabled="true" continueOnError="false" async="false" name="generateAccessToken"> <Operation>GenerateAccessToken</Operation> <AppEndUser>Doe</AppEndUser> <UserName>jdoe</UserName> <PassWord>jdoe</PassWord> <GrantType>grant_type</GrantType> <ClientId>a_valid_client_id</ClientId> <SupportedGrantTypes> <GrantType>password</GrantType> </SupportedGrantTypes> </OAuthV2>
如果您將這項政策套用至回應流程,即使政策中指定了正確的登入參數,仍會失敗並顯示 401 未授權錯誤。如要解決這個問題,您必須設定授權要求標頭。
Authorization 標頭必須包含採用 Base64 編碼的 client_id:client_secret 基本存取配置。
如要新增這個標頭,您可以將 JavaScript 政策放在 OAuthV2 政策前面,如下所示。「local_clientid」和「local_secret」變數必須先前已設定,且可在流程中使用:
var client_id = context.getVariable("local_clientid"); var client_secret = context.getVariable("local_secret"); context.setVariable("request.header.Authorization","Basic "+CryptoJS.enc.Base64.stringify(CryptoJS.enc.Latin1 .parse(client_id + ':' + client_secret)));
另請參閱「編碼基本驗證憑證」。
元素參考
政策參考資料說明 OAuthV2 政策的元素和屬性。
下方顯示的政策範例是眾多可能設定的其中一種。這個範例顯示為 GenerateAccessToken 作業設定的 OAuthV2 政策。其中包含必要和選用元素。詳情請參閱本節的元素說明。
<OAuthV2 name="GenerateAccessToken"> <!-- This policy generates an OAuth 2.0 access token using the client_credentials grant type --> <Operation>GenerateAccessToken</Operation> <!-- This is in millseconds, so expire in an hour --> <ExpiresIn>3600000</ExpiresIn> <SupportedGrantTypes> <GrantType>client_credentials</GrantType> </SupportedGrantTypes> <GrantType>request.queryparam.grant_type</GrantType> <GenerateResponse/> </OAuthV2>
<OAuthV2> 屬性
<OAuthV2 async="false" continueOnError="false" enabled="true" name="MyOAuthPolicy">
下表說明所有政策父項元素的共同屬性:
屬性 | 說明 | 預設 | 存在必要性 |
---|---|---|---|
name |
政策的內部名稱。 視需要使用 |
不適用 | 必填 |
continueOnError |
如果設為「 如果設為 |
false | 選用 |
enabled |
如要強制執行政策,請設為 設為 |
true | 選用 |
async |
此屬性已淘汰。 |
false | 已淘汰 |
<DisplayName>元素
除 name
屬性外,一併使用
管理 UI Proxy 編輯器,使用不同的自然語言名稱。
<DisplayName>Policy Display Name</DisplayName>
預設 |
不適用 如果省略這個元素,政策的 |
---|---|
存在必要性 | 選用 |
類型 | 字串 |
<AccessToken> 元素
<AccessToken>request.header.access_token</AccessToken>
根據預設,VerifyAccessToken 會預期存取權杖會透過 Authorization
標頭傳送。您可以使用這個元素變更預設值。舉例來說,request.queryparam.access_token
表示存取權杖應做為名為 access_token
的查詢參數。
<AccessToken>request.header.access_token</AccessToken>
的範例:
curl https://myorg-myenv.apigee.net/oauth2/validate -H "access_token:Rft3dqrs56Blirls56a"
<AccessToken>request.queryparam.access_token</AccessToken>
的範例:
curl "https://myorg-myenv.apigee.net/oauth2/validate?access_token:Rft3dqrs56Blirls56a"
預設值: |
不適用 |
外觀狀態: |
選用 |
類型: | 字串 |
用於作業: |
|
<AccessTokenPrefix> 元素
<AccessTokenPrefix>Bearer</AccessTokenPrefix>
根據預設,VerifyAccessToken 會預期在授權標頭中傳送存取權杖做為不記名權杖。例如:
-H "Authorization: Bearer Rft3dqrs56Blirls56a"
目前 Bearer 是唯一支援的前置字串。
預設值: |
承載系統 |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: |
承載系統 |
用於作業: |
|
<AppEndUser> 元素
<AppEndUser>request.queryparam.app_enduser</AppEndUser>
如果應用程式必須將使用者 ID 傳送至授權伺服器,您可以使用這個元素指定 Edge 應尋找使用者 ID 的位置。例如以查詢參數的形式傳送,或在 HTTP 標頭中傳送。
舉例來說,request.queryparam.app_enduser
表示 AppEndUser 應顯示為查詢參數,例如 ?app_enduser=ntesla@theramin.com
。舉例來說,如要要求在 HTTP 標頭中加入 AppEndUser,請將這個值設為 request.header.app_enduser
。
提供這項設定後,您就能在存取權杖中加入應用程式使用者 ID。如果您想依據使用者 ID 擷取或撤銷 OAuth 2.0 存取權杖,這項功能就很實用。詳情請參閱「 啟用可透過使用者 ID、應用程式 ID 或兩者同時擷取及撤銷 OAuth 2.0 存取權杖的功能」。
預設值: |
不適用 |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: |
任何在執行階段可供政策存取的流程變數。 |
與授權類型搭配使用: |
|
<屬性/屬性>
<Attributes> <Attribute name="attr_name1" ref="flow.variable" display="true|false">value1</Attribute> <Attribute name="attr_name2" ref="flow.variable" display="true|false">value2</Attribute> </Attributes>
請使用這個元素將自訂屬性新增至存取權杖或授權碼。舉例來說,您可以將使用者 ID 或工作階段 ID 嵌入存取權杖,以便在執行階段擷取及檢查。
這個元素可讓您在流程變數中或從常值字串中指定值。如果您同時指定變數和字串,系統會使用流程變數中指定的值。如果無法解析變數,則字串為預設值。
如要進一步瞭解如何使用這個元素,請參閱「自訂權杖和授權碼」。
在回應中顯示或隱藏自訂屬性
請注意,如果您將此政策的 GenerateResponse 元素設為 true,則回應會傳回符記的完整 JSON 表示法,包括您設定的任何自訂屬性。在某些情況下,您可能想要隱藏回應中的部分或所有自訂屬性,讓用戶端應用程式看不到這些屬性。
根據預設,自訂屬性會出現在回應中。如要隱藏這類事件,您可以將 display 參數設為 false。例如:
<Attributes> <Attribute name="employee_id" ref="employee.id" display="false"/> <Attribute name="employee_name" ref="employee.name" display="false"/> </Attributes>
display
屬性的值不會保留。假設您產生存取權杖時,使用了要在產生的回應中隱藏的自訂屬性。設定 display=false
即可達成該目標。不過,如果之後使用更新權杖產生新的存取權杖,更新權杖回應中就會顯示存取權杖的原始自訂屬性。這是因為 Edge 不會記得在產生存取權杖政策中,display
屬性原先是設為 false
,自訂屬性只是存取權杖中繼資料的一部分。
如果您在授權碼中新增自訂屬性,也會看到相同的行為。當系統使用該代碼產生存取權杖時,這些自訂屬性就會顯示在存取權杖回應中。再次提醒,這可能不是你預期的行為。
如要在這些情況下隱藏自訂屬性,您可以選擇以下做法:
- 請在更新權杖政策中明確重設自訂屬性,並將顯示屬性設為「否」。在這種情況下,您可能需要使用 GetOAuthV2Info 政策,從原始存取權杖中擷取原始自訂值。
- 使用後置處理 JavaScript 政策,手動擷取您不想在回應中看到的任何自訂屬性。
另請參閱「自訂權杖和授權碼」。
預設值: |
|
外觀狀態: |
選用 |
有效值: |
|
適用於授權類型: |
|
<ClientId> 元素
<ClientId>request.formparam.client_id</ClientId>
在某些情況下,用戶端應用程式必須將用戶端 ID 傳送至授權伺服器。這個元素指定 Apigee 應在流程變數 request.formparam.client_id
中尋找客戶 ID。不支援將 ClientId
設為任何其他變數。另請參閱「要求存取權權杖和授權代碼」。
預設值: |
request.formparam.client_id (以 x-www-form-url-encoded 編碼,並在要求主體中指定) |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | 流程變數:request.formparam.client_id |
與授權類型搭配使用: |
也可與 GenerateAuthorizationCode 作業搭配使用。 |
<Code> 元素
<Code>request.queryparam.code</Code>
在授權核准類型流程中,用戶端必須向授權伺服器 (Apigee Edge) 提交授權碼。這個元素可讓您指定 Edge 應尋找授權碼的位置。舉例來說,您可以將其傳送為查詢參數、HTTP 標頭或表單參數 (預設)。
變數 request.queryparam.auth_code
表示授權碼應以查詢參數的形式提供,例如 ?auth_code=AfGlvs9
。舉例來說,如要在 HTTP 標頭中要求授權碼,請將這個值設為 request.header.auth_code
。另請參閱「要求存取權權杖和授權碼」。
預設值: |
request.formparam.code (以 x-www-form-url-encoded 編碼,並在要求主體中指定) |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | 任何在執行階段可供政策存取的流程變數 |
適用於授權類型: | authorization_code |
<ExpiresIn> 元素
<ExpiresIn>10000</ExpiresIn>
以毫秒為單位,強制執行存取權杖和授權碼的到期時間。(如果是重新整理權杖,請使用 <RefreshTokenExpiresIn>
)。到期時間值是系統產生的值加上 <ExpiresIn>
值。如果 <ExpiresIn>
設為 -1,權杖或代碼會依照OAuth 存取權杖的最大到期時間到期。如果未指定 <ExpiresIn>
,系統會套用在系統層級設定的預設值。
您也可以在執行階段使用硬式編碼的預設值,或參照資料流變數來設定到期時間。例如,您可以將權杖到期時間值儲存在鍵/值對應中、擷取、指派給變數,並在政策中參照該值。例如:kvm.oauth.expires_in
。
使用 Apigee Edge for Public Cloud 時,Edge 會在實體存取後,將下列實體保留在快取中至少 180 秒。
- OAuth 存取權杖。也就是說,在快取限制到期前,已撤銷的權杖最多可使用三分鐘。
- Key Management Service (KMS) 實體 (應用程式、開發人員、API 產品)。
- OAuth 權杖和 KMS 實體的自訂屬性。
以下 stanza 也會指定流程變數和預設值。請注意,流程變數值優先於指定的預設值。
<ExpiresIn ref="kvm.oauth.expires_in"> 3600000 <!--default value in milliseconds--> </ExpiresIn>
Edge 不支援在權杖建立後強制過期的方法。如果您需要強制符記到期 (例如根據條件),請參閱 這篇 Apigee 社群貼文,瞭解可能的解決方案。
根據預設,過期後 3 天,Apigee Edge 系統就會自動清除已過期的存取權權杖。另請參閱清除存取權杖
Private Cloud:對於 Edge for Private Cloud 安裝作業,預設值是由 conf_keymanagement_oauth_auth_code_expiry_time_in_millis
屬性設定。如要設定這項屬性,請按照下列步驟操作:
- 在編輯器中開啟
message-processor.properties
檔案。如果檔案不存在,請建立檔案:vi /opt/apigee/customer/application/message-processor.properties
- 視需要設定屬性:
conf_keymanagement_oauth_auth_code_expiry_time_in_millis=3600000
- 確認屬性檔案由「apigee」使用者擁有:
chown apigee:apigee /opt/apigee/customer/application/message-processor.properties
- 重新啟動「訊息處理器」。
/opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart
預設值: |
如未指定,系統會套用系統層級設定的預設值。 |
外觀狀態: |
選用 |
類型: | 整數 |
有效值: |
|
與授權類型搭配使用: |
也用於 GenerateAuthorizationCode 作業。 |
<ExternalAccessToken> 元素
<ExternalAccessToken>request.queryparam.external_access_token</ExternalAccessToken>
告訴 Apigee Edge 外部存取權杖 (非由 Apigee Edge 產生的存取權杖) 的位置。
變數 request.queryparam.external_access_token
表示外部存取權存證應以查詢參數的形式呈現,例如 ?external_access_token=12345678
。舉例來說,如果想在 HTTP 標頭中要求外部存取權杖,請將這個值設為 request.header.external_access_token
。另請參閱「使用第三方 OAuth 權杖」。
<ExternalAuthorization> 元素
<ExternalAuthorization>true</ExternalAuthorization>
如果這個元素為 false 或不存在,Edge 通常會以 Apigee Edge 授權存放區一般驗證 client_id 和 client_secret。如要使用第三方 OAuth 權杖,請使用這個元素。如要進一步瞭解如何使用此元素,請參閱使用第三方 OAuth 權杖。
預設值: |
false |
外觀狀態: |
選用 |
類型: | 布林值 |
有效值: | true 或 false |
與授權類型搭配使用: |
|
<ExternalAuthorizationCode> 元素
<ExternalAuthorizationCode>request.queryparam.external_auth_code</ExternalAuthorizationCode>
告訴 Apigee Edge 外部驗證碼 (非由 Apigee Edge 產生的驗證碼) 的位置。
變數 request.queryparam.external_auth_code
表示外部驗證碼應以查詢參數的形式呈現,例如 ?external_auth_code=12345678
。舉例來說,如要要求 HTTP 標頭中的外部驗證碼,請將這個值設為 request.header.external_auth_code
。另請參閱「使用第三方 OAuth 權杖」。
<ExternalRefreshToken> 元素
<ExternalRefreshToken>request.queryparam.external_refresh_token</ExternalRefreshToken>
告知 Apigee Edge 要在哪裡找到外部更新權杖 (由 Apigee Edge 產生的更新權杖)。
變數 request.queryparam.external_refresh_token
表示外部重新整理權杖應以查詢參數的形式提供,例如 ?external_refresh_token=12345678
。例如,如要在 HTTP 標頭中要求外部重新整理權杖,請將這個值設為 request.header.external_refresh_token
。另請參閱「使用第三方 OAuth 權杖」。
<GenerateResponse> 元素
<GenerateResponse enabled='true'/>
如果設為 true
,政策會產生並傳回回應。舉例來說,如果是 GenerateAccessToken,回應可能會像這樣:
{ "issued_at" : "1467841035013", "scope" : "read", "application_name" : "e31b8d06-d538-4f6b-9fe3-8796c11dc930", "refresh_token_issued_at" : "1467841035013", "status" : "approved", "refresh_token_status" : "approved", "api_product_list" : "[Product1, nhl_product]", "expires_in" : "1799", "developer.email" : "edward@slalom.org", "token_type" : "BearerToken", "refresh_token" : "rVSmm3QaNa0xBVFbUISz1NZI15akvgLJ", "client_id" : "Adfsdvoc7KX5Gezz9le745UEql5dDmj", "access_token" : "AnoHsh2oZ6EFWF4h0KrA0gC5og3a", "organization_name" : "cerruti", "refresh_token_expires_in" : "0", "refresh_count" : "0" }
如果為 false
,則不會傳送回應。而是使用與政策函式相關的值填入一組流程變數。舉例來說,系統會使用新產生的授權碼填入名為 oauthv2authcode.OAuthV2-GenerateAuthorizationCode.code
的流程變數。請注意,回應中的 expires_in 是以秒為單位。
預設值: |
false |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | true 或 false |
與授權類型搭配使用: |
|
<GenerateErrorResponse> 元素
<GenerateErrorResponse enabled='true'/>
如果設為 true
,則在 ContinueOnError 屬性設為 True 時,政策會產生並傳回回應。如果為 false
(預設值),系統不會傳送回應。而是使用一組流程變數,並填入與政策函式相關的值。
預設: |
false |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | true 或 false |
與授權類型搭配使用: |
|
<GrantType>
<GrantType>request.queryparam.grant_type</GrantType>
告訴政策該去哪裡尋找在要求中傳遞的授權類型參數。根據 OAuth 2.0 規格,您必須在要求存取權杖和授權碼時提供授權類型。變數可以是標頭、查詢參數或表單參數 (預設)。
例如 request.queryparam.grant_type
表示密碼應以查詢參數的形式提供,例如 ?grant_type=password
。例如,如要在 HTTP 標頭中要求授權類型,請將這個值設為 request.header.grant_type
。另請參閱「要求存取權權杖和授權代碼」。
預設值: |
request.formparam.grant_type (x-www-form-urlencoded,在要求主體中指定) |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | 變數,如上所述。 |
適用於授權類型: |
|
<Operation> 元素
<Operation>GenerateAuthorizationCode</Operation>
政策執行的 OAuth 2.0 作業。
預設: |
如果未指定 |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: |
|
<PassWord> 元素
<PassWord>request.queryparam.password</PassWord>
這個元素僅限 密碼授權類型使用。使用密碼授權類型時,必須將使用者憑證 (密碼和使用者名稱) 提供給 OAuthV2 政策。<PassWord>
和 <UserName>
元素用於指定 Edge 可找到這些值的變數。如果未指定這些元素,政策會在名為 username
和 password
的表單參數中尋找值 (預設)。如果找不到這些值,這項政策會擲回錯誤。您可以使用 <PassWord>
和 <UserName>
元素參照任何包含憑證的流程變數。
舉例來說,您可以使用查詢參數在權杖要求中傳遞密碼,並將元素設為以下格式:<PassWord>request.queryparam.password</PassWord>
.
如要要求在 HTTP 標頭中提供密碼,請將這個值設為 request.header.password
。
OAuthV2 政策不會對這些憑證值執行其他操作,Edge 只會驗證這些值是否存在。在權杖產生政策執行前,API 開發人員必須擷取要求的值,並將這些值傳送至身分提供者。
另請參閱要求存取權杖和授權碼。
預設值: |
request.formparam.password (以 x-www-form-urlencoded 編碼,並在要求主體中指定) |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | 任何在執行階段可供政策使用的流程變數。 |
適用於授權類型: | 密碼 |
<RedirectUri> 元素
<RedirectUri>request.queryparam.redirect_uri</RedirectUri>
指定 Edge 在要求中找出 redirect_uri
參數的位置。
關於重新導向 URI
重新導向 URI 會與授權碼和隱含授權類型搭配使用。重新導向 URI 會告知授權伺服器 (Edge) 將授權碼 (授權碼授權類型) 或存取權杖 (隱含授權類型) 傳送至何處。請務必瞭解何時必須使用這個參數、何時可選用,以及如何使用:
-
(必要) 如果開發人員應用程式已註冊回呼網址,且該網址與要求的用戶端金鑰相關聯,且要求中存在
redirect_uri
,則這兩者必須完全相符。如果兩者不相符,系統會傳回錯誤。如要瞭解如何在 Edge 上註冊開發人員應用程式,並指定回呼網址,請參閱「註冊應用程式及管理 API 金鑰」。 - (選用) 如果已註冊回呼網址,但要求中缺少
redirect_uri
,Edge 會重新導向至已註冊的回呼網址。 - (必要) 如果未註冊回呼網址,則必須使用
redirect_uri
。請注意,在這種情況下,Edge 接受任何網址。這種情況可能會有安全性問題, 因此只應搭配信任的用戶端應用程式使用。如果用戶端應用程式不受信任,最佳做法是要求註冊回呼網址。
您可以在查詢參數或標頭中傳送此參數。變數 request.queryparam.redirect_uri
表示 RedirectUri 應以查詢參數的形式呈現,例如 ?redirect_uri=login.myapp.com
。舉例來說,如要在 HTTP 標頭中使用 RedirectUri,請將這個值設為 request.header.redirect_uri
。另請參閱「要求存取權權杖和授權碼」。
預設值: |
request.formparam.redirect_uri (x-www-form-urlencoded,在要求主體中指定的) |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | 在執行階段期間,政策中可存取的任何流程變數 |
與授權類型搭配使用: |
也用於 GenerateAuthorizationCode 作業。 |
<RefreshToken> 元素
<RefreshToken>request.queryparam.refreshtoken</RefreshToken>
使用更新權杖要求存取權杖時,您必須在要求中提供更新權杖。這個元素可讓您指定 Edge 應在何處尋找重新整理權杖。例如,有可能以查詢參數、HTTP 標頭或表單參數 (預設) 的形式傳送。
request.queryparam.refreshtoken
變數表示更新權杖應以查詢參數的形式呈現,例如 ?refresh_token=login.myapp.com
。舉例來說,如要在 HTTP 標頭中使用 RefreshToken,請將這個值設為 request.header.refresh_token
。另請參閱要求存取權杖和授權碼。
預設值: |
request.formparam.refresh_token (以 x-www-form-urlencoded 編碼,並在要求主體中指定) |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | 在執行階段期間,政策中可存取的任何流程變數 |
與授權類型搭配使用: |
|
<RefreshTokenExpirationIn> 元素
<RefreshTokenExpiresIn>1000</RefreshTokenExpiresIn>
強制更新權杖的到期時間 (以毫秒為單位)。到期時間值是系統產生的值加上 <RefreshTokenExpiresIn>
值。如果 <RefreshTokenExpiresIn>
設為 -1,更新憑證的到期時間會依據 OAuth 更新憑證的到期時間上限。如果未指定 <RefreshTokenExpiresIn>
,系統會套用在系統層級設定的預設值。如要進一步瞭解預設系統設定,請與 Apigee Edge 支援團隊聯絡。
您也可以在執行階段使用硬式編碼的預設值或參照資料流變數來設定到期時間。舉例來說,您可以將權杖到期日值儲存在鍵值對應項目中,然後擷取該值、將其指派給變數,並在政策中參照該值。例如:kvm.oauth.expires_in
。
以下節落會指定流程變數和預設值。請注意,流程變數值優先於指定的預設值。
<RefreshTokenExpiresIn ref="kvm.oauth.expires_in"> 3600000 <!--default value in milliseconds--> </RefreshTokenExpiresIn>
Private Cloud:對於 Edge for Private Cloud 安裝作業,預設值是由 conf_keymanagement_oauth_refresh_token_expiry_time_in_millis
屬性設定。如要設定這項屬性,請按照下列步驟操作:
- 在編輯器中開啟
message-processor.properties
檔案。如果檔案不存在,請建立檔案:vi /opt/apigee/customer/application/message-processor.properties
- 視需要設定屬性:
conf_keymanagement_oauth_refresh_token_expiry_time_in_millis=3600000
- 確認屬性檔案由「apigee」使用者擁有:
chown apigee:apigee /opt/apigee/customer/application/message-processor.properties
- 重新啟動「訊息處理器」。
/opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart
預設值: |
63072000000 毫秒 (2 年) (2024 年 8 月 5 日生效) |
外觀狀態: |
選用 |
類型: | 整數 |
有效值: |
|
與授權類型搭配使用: |
|
<ResponseType> 元素
<ResponseType>request.queryparam.response_type</ResponseType>
這個元素會告知 Edge 用戶端應用程式要求的授權類型。這項參數僅用於授權碼和隱含授權類型流程。
根據預設,Edge 會在 response_type
查詢參數中尋找回應類型值。如果您想覆寫這項預設行為,請使用 <ResponseType> 元素設定含有回應類型值的流程變數。舉例來說,如果您將這個元素設為 request.header.response_type
,Edge 會在要求標頭中尋找要傳遞的回應類型。另請參閱「要求存取權權杖和授權代碼」。
預設: |
request.formparam.response_type (x-www-form-urlencoded,並在要求主體中指定) |
所在地: |
選用設定。如果您想覆寫預設行為,請使用這個元素。 |
類型: | 字串 |
有效值: | code (授權碼授權類型) 或 token (隱含授權類型) |
與授權類型搭配使用: |
|
<ReuseRefreshToken> 元素
<ReuseRefreshToken>true</ReuseRefreshToken>
設為 true
時,系統會重複使用現有的更新憑證,直到憑證到期為止。如為 false
,則在系統提供有效的更新權杖時,Apigee Edge 會核發新的更新權杖。
預設值: |
|
外觀狀態: |
選用 |
類型: | 布林值 |
有效值: |
|
與授權類型搭配使用: |
|
<Scope> 元素
<Scope>request.queryparam.scope</Scope>
如果這個元素出現在 GenerateAccessToken 或 GenerateAuthorizationCode 政策中,則會用來指定要授予權杖或代碼的範圍。這些值通常會從用戶端應用程式要求中傳遞至政策。您可以設定讓元素採用資料流變數,進而選擇如何在要求中傳遞範圍。在以下範例中,request.queryparam.scope
表示範圍應以查詢參數的形式呈現,例如 ?scope=READ
。舉例來說,如果想在 HTTP 標頭中設定範圍,請將這個值設為 request.header.scope
。
如果這個元素出現在「VerifyAccessToken」政策中,則系統會使用該元素指定政策應強制執行的範圍。在這種政策類型中,值必須是「硬式編碼」的範圍名稱,您無法使用變數。例如:
<Scope>A B</Scope>
另請參閱「使用 OAuth2 範圍」和「要求存取權杖和授權碼」。
預設值: |
無範圍 |
所在地: |
選用 |
類型: | 字串 |
有效值: |
如果與 Generate* 政策搭配使用,則為流程變數。 如果與 VerifyAccessToken 搭配使用,則為以空格分隔的範圍名稱 (字串) 清單。 |
與授權類型搭配使用: |
|
<State> 元素
<State>request.queryparam.state</State>
如果用戶端應用程式必須將狀態資訊傳送至授權伺服器,您可以使用這個元素指定 Edge 應尋找狀態值的位置。舉例來說,您可以將其做為查詢參數或 HTTP 標頭傳送。狀態值通常用於防範 CSRF 攻擊的安全措施。
舉例來說,request.queryparam.state
表示狀態應以查詢參數的形式呈現,例如 ?state=HjoiuKJH32
。例如,如要要求在 HTTP 標頭中顯示狀態,請將這個值設為 request.header.state
。另請參閱要求存取權杖和授權碼。
預設值: |
沒有任何狀態 |
外觀狀態: |
選用 |
類型: | 字串 |
有效值: | 任何在執行階段可供政策存取的流程變數 |
與授權類型搭配使用: |
|
<StoreToken> 元素
<StoreToken>true</StoreToken>
如果 <ExternalAuthorization>
元素為 true
,請將此元素設為 true
。<StoreToken>
元素會指示 Apigee Edge 儲存外部存取權權杖。否則不會保留。
預設: |
false |
外觀狀態: |
選用 |
類型: | 布林值 |
有效值: | true 或 false |
與授權類型搭配使用: |
|
<SupportedGrantTypes>/<GrantType> 元素
<SupportedGrantTypes> <GrantType>authorization_code</GrantType> <GrantType>client_credentials</GrantType> <GrantType>implicit</GrantType> <GrantType>password</GrantType> </SupportedGrantTypes>
指定 Apigee Edge 上的 OAuth 權杖端點支援的授權類型。端點可能支援多種授權類型,也就是說,單一端點可以設定成發布多個授權類型的存取權杖。如要進一步瞭解端點,請參閱「瞭解 OAuth 端點」。權限授予類型會透過 grant_type
參數在權杖要求中傳遞。
如果未指定支援的授權類型,則唯一允許的授權類型為 authorization_code
和 implicit
。另請參閱 <GrantType> 元素 (這是較高層級元素,用於指定 Apigee Edge 應在何處查找用戶端要求中傳入的 grant_type
參數)。Edge 會確保 grant_type
參數的值與其中一個支援的授權類型相符。
預設值: |
授權 _code 和隱含 |
所在地: |
必填 |
類型: | 字串 |
有效值: |
|
<Tokens>/<Token> 元素
可搭配 ValidateToken 和 InvalidateToken 作業使用。另請參閱核准及撤銷存取權杖。<Token> 元素會標示流程變數,定義要撤銷的符記來源。如果開發人員要提交存取權權杖,並以名為 access_token
的查詢參數提交,請使用 request.queryparam.access_token
。
<UserName> 元素
<UserName>request.queryparam.user_name</UserName>
這個元素僅適用於 密碼授權類型。若使用密碼授權類型,OAuthV2 政策就必須提供使用者憑證 (密碼和使用者名稱)。<PassWord>
和 <UserName>
元素用於指定 Edge 可找到這些值的變數。如果未指定這些元素,政策會在名為 username
和 password
的表單參數中尋找值 (預設)。如果找不到這些值,政策就會擲回錯誤。您可以使用 <PassWord>
和 <UserName>
元素參照任何包含憑證的流程變數。
舉例來說,您可以在查詢參數中傳遞使用者名稱,並將 <UserName>
元素設為以下格式:<UserName>request.queryparam.username</UserName>
.
如要要求在 HTTP 標頭中使用使用者名稱,請將這個值設為 request.header.username
。
OAuthV2 政策不會對這些憑證值執行其他操作,Edge 只會驗證這些值是否存在。在權杖產生政策執行前,API 開發人員必須擷取要求的值,並將這些值傳送至身分提供者。
另請參閱「要求存取權權杖和授權碼」。
預設值: |
request.formparam.username (以 x-www-form-url-encoded 編碼,並在要求主體中指定) |
所在地: |
選用 |
類型: | 字串 |
有效值: | 任何變數設定, |
與授權類型搭配使用: | 密碼 |
驗證存取權杖
為 API Proxy 設定權杖端點後,指定 VerifyAccessToken
作業的對應 OAuthV2 政策就會附加至公開受保護資源的流程中。
舉例來說,為確保向 API 發出的所有要求都獲得授權,下列政策會強制執行存取權杖驗證:
<OAuthV2 name="VerifyOAuthAccessToken"> <Operation>VerifyAccessToken</Operation> </OAuthV2>
這項政策會附加至要保護的 API 資源。為確保對 API 提出的所有要求都經過驗證,請將政策附加至 ProxyEndpoint 要求的 PreFlow,如下所示:
<PreFlow> <Request> <Step><Name>VerifyOAuthAccessToken</Name></Step> </Request> </PreFlow>
您可以使用下列選用元素,覆寫 VerifyAccessToken 作業的預設設定。
名稱 | 說明 |
---|---|
範圍 |
以空格分隔的範圍清單。如果存取權杖中至少有一個範圍包含指定範圍,驗證作業就會成功。舉例來說,下列政策會檢查存取權權杖,確保權杖至少包含所列其中一個範圍。如果有 READ 或 WRITE,驗證就會成功。 <OAuthV2 name="ValidateOauthScopePolicy"> <Operation>VerifyAccessToken</Operation> <Scope>READ WRITE</Scope> </OAuthV2> |
AccessToken | 預期存取權存取權杖所在位置的變數。例如 request.queryparam.accesstoken 。依照 OAuth 2.0 規格,應用程式預設會在 Authorization HTTP 標頭中提供存取權杖。如果存取權杖預期會顯示在非標準位置 (例如查詢參數),或名稱不是 Authorization 的 HTTP 標頭,請使用這項設定。 |
另請參閱「驗證存取權杖」和「要求存取權杖和授權碼」。
指定要求變數位置
針對每個授權類型,政策會針對要求訊息中的位置或必要資訊做出假設。這些假設是根據 OAuth 2.0 規格制定。如果您的應用程式需要偏離 OAuth 2.0 規格,您可以為每個參數指定預期位置。舉例來說,在處理授權碼時,您可以指定授權碼、用戶端 ID、重新導向 URI 和範圍的位置。這些參數可以指定為 HTTP 標頭、查詢參數或表單參數。
以下範例說明如何將必要授權碼參數的位置指定為 HTTP 標頭:
... <GrantType>request.header.grant_type</GrantType> <Code>request.header.code</Code> <ClientId>request.header.client_id</ClientId> <RedirectUri>request.header.redirect_uri</RedirectUri> <Scope>request.header.scope</Scope> ...
或者,如有需要,您也可以混用標頭和查詢參數來支援用戶端應用程式:
... <GrantType>request.header.grant_type</GrantType> <Code>request.header.code</Code> <ClientId>request.queryparam.client_id</ClientId> <RedirectUri>request.queryparam.redirect_uri</RedirectUri> <Scope>request.queryparam.scope</Scope> ...
每個參數只能設定一個位置。
流程變數
執行相應 OAuth 政策時,系統會填入此表格中定義的流程變數,因此其他在 API Proxy 流程中執行的政策或應用程式可使用這些變數。
VerifyAccessToken 作業
執行 VerifyAccessToken 作業時,代理程式執行內容會填入大量流程變數。這些變數會提供與存取權權杖、開發人員應用程式、開發人員和公司相關的屬性。舉例來說,您可以使用 AssignMessage 或 JavaScript 政策來讀取任何這些變數,並視需要在流程中使用這些變數。這些變數也非常適合用於偵錯。
proxy.pathsuffix
),系統會自動填入 API 產品變數。您不需要明確設定 flow.resource.name 變數。
如果 API 產品未使用有效的環境和 API Proxy 進行設定,則必須明確設定 flow.resource.name
,才能在流程中填入 API 產品變數。如要進一步瞭解產品設定,請參閱「使用 Edge Management API 發布 API」。
權杖專屬變數
變數 | 說明 |
---|---|
organization_name |
執行 Proxy 的機構名稱。 |
developer.id |
與註冊用戶端應用程式相關聯的開發人員 ID。 |
developer.app.name |
與已註冊的用戶端應用程式相關聯的開發人員名稱。 |
client_id |
已註冊用戶端應用程式的用戶端 ID。 |
grant_type |
與要求相關聯的授權類型。 |
token_type |
與要求相關聯的權杖類型。 |
access_token |
要驗證的存取權杖。 |
accesstoken.{custom_attribute} |
存取權杖中的具名自訂屬性。 |
issued_at |
存取權權杖核發日期,以 Unix 紀元時間 (以毫秒為單位) 表示。 |
expires_in |
存取權杖的到期時間。以秒為單位。雖然 ExpiresIn 元素會以毫秒為單位設定到期時間,但在符記回應和流程變數中,該值會以秒為單位表示。 |
status |
存取權杖的狀態 (例如已核准或已撤銷)。 |
scope |
與存取權杖相關聯的範圍 (如果有的話)。 |
apiproduct.<custom_attribute_name> |
與已註冊用戶端應用程式相關聯的 API 產品名稱自訂屬性。 |
apiproduct.name |
與註冊用戶端應用程式相關聯的 API 產品名稱。 |
revoke_reason |
(僅限 Apigee hybrid) 表示存取權杖撤銷的原因。 值可以是 |
應用程式專屬變數
這些變數與與權杖相關聯的開發人員應用程式有關。
變數 | 說明 |
---|---|
app.name |
|
app.id |
|
app.accessType |
|
app.callbackUrl |
|
app.status |
已核准或撤銷 |
app.scopes |
|
app.appFamily |
|
app.apiproducts |
|
app.appParentStatus |
|
app.appType |
例如:開發人員 |
app.appParentId |
|
app.created_by |
|
app.created_at |
|
app.last_modified_at |
|
app.last_modified_by |
|
app.{custom_attributes} |
已註冊用戶端應用程式的命名自訂屬性。 |
開發人員專用變數
如果 app.appType 是「Company」,系統會填入公司屬性;如果 app.appType 是「Developer」,系統會填入開發人員屬性。
變數 | 說明 |
---|---|
開發人員專用變數 | |
developer.id |
|
developer.userName |
|
developer.firstName |
|
developer.lastName |
|
developer.email |
|
developer.status |
有效或無效 |
developer.apps |
|
developer.created_by |
|
developer.created_at |
|
developer.last_modified_at |
|
developer.last_modified_by |
|
developer.{custom_attributes} |
開發人員的命名自訂屬性。 |
公司專屬變數
如果 app.appType 是「Company」,系統會填入公司屬性;如果 app.appType 是「Developer」,系統會填入開發人員屬性。
變數 | 說明 |
---|---|
company.id |
|
company.displayName |
|
company.apps |
|
company.appOwnerStatus |
|
company.created_by |
|
company.created_at |
|
company.last_modified_at |
|
company.last_modified_by |
|
company.{custom_attributes} |
公司的命名自訂屬性。 |
GenerateAuthorizationCode 作業
系統會在 GenerateAuthorizationCode 作業成功執行時設定下列變數:
前置字串: oauthv2authcode.{policy_name}.{variable_name}
範例:oauthv2authcode.GenerateCodePolicy.code
變數 | 說明 |
---|---|
code |
執行政策時產生的授權碼。 |
redirect_uri |
與已註冊用戶端應用程式相關聯的重新導向 URI。 |
scope |
在用戶端要求中傳遞的選用 OAuth 範圍。 |
client_id |
用戶端要求中傳遞的用戶端 ID。 |
GenerateAccessToken 和 RefreshAccessToken 作業
系統會在 GenerateAccessToken 和 RefreshAccessToken 作業成功執行時設定這些變數。請注意,重新整理權杖變數不適用於用戶端憑證授權類型流程。
前置字串: oauthv2accesstoken.{policy_name}.{variable_name}
範例:oauthv2accesstoken.GenerateTokenPolicy.access_token
變數名稱 | 說明 |
---|---|
access_token |
系統產生的存取權杖。 |
client_id |
與此權杖相關聯的開發人員應用程式用戶端 ID。 |
expires_in |
權杖的到期時間值。詳情請參閱 <ExpiresIn> 元素。請注意,在回應中,expires_in 是以秒為單位。 |
scope |
代碼中設定的可用範圍清單。請參閱「使用 OAuth2 範圍」。 |
status |
approved 或 revoked 。 |
token_type |
已設為 BearerToken 。 |
developer.email |
擁有與權杖相關聯的開發人員應用程式的註冊開發人員電子郵件地址。 |
organization_name |
Proxy 執行的機構。 |
api_product_list |
與權杖對應的開發人員應用程式相關聯的產品清單。 |
refresh_count |
|
refresh_token |
產生的更新權杖。請注意,系統不會為用戶端憑證授權類型產生重新整理權杖。 |
refresh_token_expires_in |
更新權杖的效期 (以秒為單位)。 |
refresh_token_issued_at |
這個時間值是相應 32 位元時間戳記數量的字串表示法。舉例來說,「Wed, 21 8 2013 19:16:47 UTC」對應至時間戳記值 1377112607413。 |
refresh_token_status |
approved 或 revoked 。 |
GenerateAccessTokenImplicitGrant
當 GenerateAccessTokenImplicit 作業成功執行隱含授權類型流程時,系統就會設定這些變數。
前置字串: oauthv2accesstoken.{policy_name}.{variable_name}
範例:oauthv2accesstoken.RefreshTokenPolicy.access_token
變數 | 說明 |
---|---|
oauthv2accesstoken.access_token |
政策執行時產生的存取權杖。 |
oauthv2accesstoken.{policy_name}.expires_in |
權杖的到期值,以秒為單位。詳情請參閱 <ExpirationIn> 元素。 |
錯誤參考資料
This section describes the fault codes and error messages that are returned and fault variables that are set by Edge when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.
Runtime errors
These errors can occur when the policy executes.
Fault code | HTTP status | Cause | Thrown by operations |
---|---|---|---|
steps.oauth.v2.access_token_expired |
401 | The access token is expired. |
VerifyAccessToken |
steps.oauth.v2.access_token_not_approved |
401 | The access token was revoked. | VerifyAccessToken |
steps.oauth.v2.apiresource_doesnot_exist |
401 | The requested resource does not exist any of the API products associated with the access token. | VerifyAccessToken |
steps.oauth.v2.FailedToResolveAccessToken |
500 | The policy expected to find an access token in a variable specified in the
<AccessToken> element, but the variable could not be resolved. |
GenerateAccessToken |
steps.oauth.v2.FailedToResolveAuthorizationCode |
500 | The policy expected to find an authorization code in a variable specified in the
<Code> element, but the variable could not be resolved. |
GenerateAuthorizationCode |
steps.oauth.v2.FailedToResolveClientId |
500 | The policy expected to find the Client ID in a variable specified in the
<ClientId> element, but the variable could not be resolved. |
GenerateAccessToken GenerateAuthorizationCode GenerateAccessTokenImplicitGrant RefreshAccessToken |
steps.oauth.v2.FailedToResolveRefreshToken |
500 | The policy expected to find a refresh token in a variable specified in the
<RefreshToken> element, but the variable could not be resolved. |
RefreshAccessToken |
steps.oauth.v2.FailedToResolveToken |
500 | The policy expected to find a token in a variable specified in the
<Tokens> element, but the variable could not be resolved. |
ValidateToken |
steps.oauth.v2.InsufficientScope |
403 | The access token presented in the request has a scope that does not match the scope specified in the verify access token policy. To learn about scope, see Working with OAuth2 scopes. | VerifyAccessToken |
steps.oauth.v2.invalid_access_token |
401 | The access token sent from the client is invalid. | VerifyAccessToken |
steps.oauth.v2.invalid_client |
401 |
This error name is returned when the Note: It is recommended that you change existing fault rule
conditions to catch both the |
GenerateAccessToken RefreshAccessToken |
steps.oauth.v2.InvalidRequest |
400 | This error name is used for multiple different kinds of errors, typically for missing
or incorrect parameters sent in the request. If <GenerateResponse> is
set to false , use fault variables (described below) to retrieve details about
the error, such as the fault name and cause. |
GenerateAccessToken GenerateAuthorizationCode GenerateAccessTokenImplicitGrant RefreshAccessToken |
steps.oauth.v2.InvalidAccessToken |
401 | The authorization header does not have the word "Bearer", which is required. For
example: Authorization: Bearer your_access_token |
VerifyAccessToken |
steps.oauth.v2.InvalidAPICallAsNoApiProductMatchFound |
401 |
The API proxy is not in the Product associated with the access token. Tips: Be sure that the product associated with the access token is configured correctly. For example, if you use wildcards in resource paths, be sure the wildcards are being used correctly. See Create API products for details. See also this Apigee Community post for more guidance on causes for this error. |
VerifyAccessToken |
steps.oauth.v2.InvalidClientIdentifier |
500 |
This error name is returned when the |
GenerateAccessToken |
steps.oauth.v2.InvalidParameter |
500 | The policy must specify either an access token or an authorization code, but not both. | GenerateAuthorizationCode GenerateAccessTokenImplicitGrant |
steps.oauth.v2.InvalidTokenType |
500 | The <Tokens>/<Token> element requires you to specify the token
type (for example, refreshtoken ). If the client passes the wrong type, this
error is thrown. |
ValidateToken InvalidateToken |
steps.oauth.v2.MissingParameter |
500 | The response type is token , but no grant types are specified. |
GenerateAuthorizationCode GenerateAccessTokenImplicitGrant |
steps.oauth.v2.UnSupportedGrantType |
500 |
The client specified a grant type that is unsupported by the policy (not listed in the <SupportedGrantTypes> element). Note: There is currently a bug where unsupported grant type errors are not thrown correctly. If an unsupported grant type error occurs, the proxy does not enter the Error Flow, as expected. |
GenerateAccessToken GenerateAuthorizationCode GenerateAccessTokenImplicitGrant RefreshAccessToken |
Deployment errors
These errors can occur when you deploy a proxy containing this policy.
Error name | Cause |
---|---|
InvalidValueForExpiresIn |
For the |
InvalidValueForRefreshTokenExpiresIn |
For the <RefreshTokenExpiresIn> element, valid values are positive
integers and -1 . |
InvalidGrantType |
An invalid grant type is specified in the <SupportedGrantTypes>
element. See the policy reference for a list of valid types. |
ExpiresInNotApplicableForOperation |
Be sure that the operations specified in the <Operations> element support expiration. For example, the VerifyToken operation does not. |
RefreshTokenExpiresInNotApplicableForOperation |
Be sure that the operations specified in the <Operations> element support refresh token expiration. For example, the VerifyToken operation does not. |
GrantTypesNotApplicableForOperation |
Be sure that the grant types specified in <SupportedGrantTypes> are supported for the specified operation. |
OperationRequired |
You must specify an operation in this policy using the Note: If the |
InvalidOperation |
You must specify a valid operation in this policy using the
Note: If the |
TokenValueRequired |
You must specify a token <Token> value in the
<Tokens> element. |
Fault variables
These variables are set when this policy triggers an error at runtime.
<GenerateResponse>
is set to
false
. If <GenerateResponse>
is
true
, the policy returns a response to the client immediately if an error occurs --
the error flow is skipped and these variables are not populated. For more information, see
What you need to
know about policy errors.Variables | Where | Example |
---|---|---|
fault.name="fault_name" |
fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. | fault.name = "InvalidRequest" |
oauthV2.policy_name.failed |
policy_name is the user-specified name of the policy that threw the fault. | oauthV2.GenerateAccesstoken.failed = true |
oauthV2.policy_name.fault.name |
policy_name is the user-specified name of the policy that threw the fault. | oauthV2.GenerateAccesstoken.fault.name = InvalidRequest
Note: For the VerifyAccessToken operation, the fault name includes this suffix: |
oauthV2.policy_name.fault.cause |
policy_name is the user-specified name of the policy that threw the fault. | oauthV2.GenerateAccesstoken.cause = Required param : grant_type |
Example error response
These responses are sent back to the client if the <GenerateResponse>
element is true.
errorcode
part of the error response. Do not rely on the text in the
faultstring
, because it could change.If <GenerateResponse>
is true, the policy returns errors
in this format for operations that generate tokens and codes. For a complete list, see see
OAuth HTTP error
response reference.
{"ErrorCode" : "invalid_client", "Error" :"ClientId is Invalid"}
If <GenerateResponse>
is true, the policy returns errors
in this format for verify and validate operations. For a complete list, see see OAuth HTTP error
response reference.
{ { "fault":{ "faultstring":"Invalid Access Token", "detail":{ "errorcode":"keymanagement.service.invalid_access_token" } } }
Example fault rule
<FaultRule name=OAuthV2 Faults"> <Step> <Name>AM-InvalidClientResponse</Name> <Condition>(fault.name = "invalid_client") OR (fault.name = "InvalidClientIdentifier")</Condition> </Step> <Step> <Name>AM-InvalidTokenResponse</Name> <Condition>(fault.name = "invalid_access_token")</Condition> </Step> <Condition>(oauthV2.failed = true) </Condition> </FaultRule>
政策結構定義
每個政策類型都由 XML 架構 (.xsd
) 定義。如需參考,請前往 GitHub 查看政策架構。
使用預設 OAuth 設定
Apigee Edge 上的每個機構 (甚至是免費試用機構) 都會以 OAuth 權杖端點佈建。端點會在名為 oauth 的 API Proxy 中,預先設定政策。在 Apigee Edge 建立帳戶後,即可開始使用權杖端點。詳情請參閱「瞭解 OAuth 端點」。
清除存取權杖
根據預設,在存取權杖和更新權杖 (如有) 到期後的 3 天 (259200 秒),Apigee Edge 系統會清除 OAuth2 權杖。在某些情況下,您可能需要變更這個預設值。舉例來說,如果系統產生大量符記,您可能會想縮短清除時間,以節省磁碟空間。
如果您使用的是 Edge for Private Cloud,可以按照本節所述設定機構屬性,變更這個預設值。(過期權杖的 3 天清除作業適用於 Private Cloud 版 Edge 4.19.01 以上版本。如果是較舊版本,預設清除間隔為 180 天)。
更新 Edge Private Cloud 4.16.01 以上版本的清除設定
注意:只有在套用這些設定後產生的符記會受到影響;這些設定不會套用至先前產生的符記。
- 開啟以下檔案進行編輯:
/opt/apigee/customer/application/message-processor.properties
- 新增下列屬性,設定在權杖到期後,等待清除權杖的秒數:
conf_keymanagement_oauth.access.token.purge.after.seconds=<number of seconds>
- 重新啟動訊息處理工具。例如:
/opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart
<ExpiresIn>
和 <RefreshTokenExpiresIn>
屬性,在 OAuthV2 政策中設定權杖到期日。正在更新 Edge Private Cloud 4.15.07 的清除設定
注意:只有在套用這些設定後產生的符記會受到影響;這些設定不會套用至先前產生的符記。
-
在 OAuthV2 政策中為
<ExpiresIn>
和<RefreshTokenExpiresIn>
屬性設定正值。值以毫秒為單位。例如:<ExpiresIn>1000</ExpiresIn> <RefreshTokenExpiresIn>10000</RefreshTokenExpiresIn>
-
重新部署 Proxy。
-
使用這個 API 更新貴機構的權杖清除屬性:
POST https://<host-name>/v1/organizations/<org-name>
酬載:
<Organization name="AutomationOrganization"> <Description>Desc</Description> <Properties> <Property name="keymanagement.oauth20.access.token.purge">true</Property> <Property name="keymanagement.oauth20.access.token.purge.after.seconds">120</Property> </Properties> </Organization>
-
重新啟動訊息處理工具。例如:
/opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart
這個 API 會將權杖清除屬性設為 AutomationOrganization 這個機構的「true」。在這種情況下,系統會在權杖和重新整理權杖都到期後的 120 秒內,從資料庫中清除存取權杖。
不符合 RFC 的行為
OAuthV2 政策會傳回權杖回應,其中包含某些不符合 RFC 的屬性。下表列出 OAuthV2 政策傳回的不符規定屬性,以及對應的符合規定屬性。
OAuthV2 傳回: | 符合 RFC 規定的屬性如下: |
---|---|
"token_type":"BearerToken" |
"token_type":"Bearer"
|
"expires_in":"3600" |
"expires_in":3600
(合規值為數字,而不是字串)。 |
此外,當 grant_type = refresh_token
為以下值時,過期的重新整理權杖會傳回錯誤回應:
{"ErrorCode" : "InvalidRequest", "Error" :"Refresh Token expired"}
不過,符合 RFC 規範的回應如下:
{"error" : "invalid_grant", "error_description" :"refresh token expired"}