本頁適用於 Apigee 和 Apigee Hybrid。
查看
Apigee Edge 說明文件。
結果
解碼 JWT 時,不驗證 JWT 上的簽章。當您必須先知道 JWT 中的憑證值,才能驗證 JWT 簽名時,此做法最實用,因為這時可與 VerifyJWT 政策搭配使用。
無論用來簽署 JWT 的演算法為何,JWT 解碼政策都能正常運作。如需詳細介紹,請參閱「JWS 和 JWT 政策總覽」。
這項政策是標準政策,可部署至任何環境類型。如要瞭解政策類型和各環境類型的可用性,請參閱「政策類型」。
影片
觀看短片,瞭解如何解碼 JWT。
範例:解碼 JWT
下方政策會對流程變數 var.jwt 中的 JWT 進行解碼。這個變數必須存在,且包含可行 (可解碼) 的 JWT。這項政策可從任何流程變數取得 JWT。
<DecodeJWT name="JWT-Decode-HS256"> <DisplayName>JWT Verify HS256</DisplayName> <Source>var.jwt</Source> </DecodeJWT>
這項政策會將輸出內容寫入內容變數,以便 API Proxy 中的後續政策或條件檢查這些值。如需這項政策設定的變數清單,請參閱「流程變數」。
Decode JWT 的元素參考資料
政策參考資料說明解碼 JWT 政策的元素和屬性。
套用至頂層元素的屬性
<DecodeJWT name="JWT" continueOnError="false" enabled="true" async="false">
以下屬性適用於所有政策父元素。
屬性 | 說明 | 預設 | 在家狀態 |
---|---|---|---|
名稱 |
政策的內部名稱。您可以在名稱中使用的字元僅限:
A-Z0-9._\-$ % 。不過,Apigee UI 會強制執行其他限制,例如自動移除非英數字元的字元。您可以選擇使用 |
不適用 | 必填 |
continueOnError |
將其設為 false ,即可在政策失敗時傳回錯誤。這是大多數政策的預期行為。將其設為 |
false | 選用 |
已啟用 |
設為 true 即可強制執行政策。
將其設為 |
是 | 選用 |
非同步 | 此屬性已淘汰。 | false | 已淘汰 |
<DisplayName>
<DisplayName>Policy Display Name</DisplayName>
除了使用名稱屬性,您也可以在 Apigee UI 代理程式編輯器中使用其他自然語言名稱標示政策。
預設 | 如果省略這個元素,系統會使用政策的 name 屬性值。 |
在家狀態 | 選用 |
類型 | 字串 |
<Source>
<Source>jwt-variable</Source>
如果存在,則會指定政策預期在哪個流程中找到 JWT 進行解碼。
預設 | request.header.authorization (如需預設值的相關重要資訊,請參閱上述附註)。 |
在家狀態 | 選用 |
類型 | 字串 |
有效值 | Apigee 流程變數名稱 |
Flow variables
Upon success, the Verify JWT and Decode JWT policies set context variables according to this pattern:
jwt.{policy_name}.{variable_name}
For example, if the policy name is jwt-parse-token
, then the policy will store
the subject specified in the JWT to the context variable named jwt.jwt-parse-token.decoded.claim.sub
.
(For backward compatibility, it will also be available in jwt.jwt-parse-token.claim.subject
)
Variable name | Description |
---|---|
claim.audience |
The JWT audience claim. This value may be a string, or an array of strings. |
claim.expiry |
The expiration date/time, expressed in milliseconds since epoch. |
claim.issuedat |
The Date the token was issued, expressed in milliseconds since epoch. |
claim.issuer |
The JWT issuer claim. |
claim.notbefore |
If the JWT includes a nbf claim, this variable will contain the value, expressed in milliseconds since epoch. |
claim.subject |
The JWT subject claim. |
claim.name |
The value of the named claim (standard or additional) in the payload. One of these will be set for every claim in the payload. |
decoded.claim.name |
The JSON-parsable value of the named claim (standard or additional) in the payload. One variable is set for
every claim in the payload. For example, you can use decoded.claim.iat to
retrieve the issued-at time of the JWT, expressed in seconds since epoch. While you
can also use the claim.name flow variables, this is the
recommended variable to use to access a claim. |
decoded.header.name |
The JSON-parsable value of a header in the payload. One variable is set for
every header in the payload. While you can also use the header.name flow variables,
this is the recommended variable to use to access a header. |
expiry_formatted |
The expiration date/time, formatted as a human-readable string. Example: 2017-09-28T21:30:45.000+0000 |
header.algorithm |
The signing algorithm used on the JWT. For example, RS256, HS384, and so on. See (Algorithm) Header Parameter for more. |
header.kid |
The Key ID, if added when the JWT was generated. See also "Using a JSON Web Key Set (JWKS)" at JWT policies overview to verify a JWT. See (Key ID) Header Parameter for more. |
header.type |
Will be set to JWT . |
header.name |
The value of the named header (standard or additional). One of these will be set for every additional header in the header portion of the JWT. |
header-json |
The header in JSON format. |
is_expired |
true or false |
payload-claim-names |
An array of claims supported by the JWT. |
payload-json |
The payload in JSON format.
|
seconds_remaining |
The number of seconds before the token will expire. If the token is expired, this number will be negative. |
time_remaining_formatted |
The time remaining before the token will expire, formatted as a human-readable string. Example: 00:59:59.926 |
valid |
In the case of VerifyJWT, this variable will be true when the signature is verified, and
the current time is before the token expiry, and after the token notBefore value, if they
are present. Otherwise false.
In the case of DecodeJWT, this variable is not set. |
錯誤參考資料
本節說明這項政策觸發錯誤時,Apigee 傳回的錯誤代碼和錯誤訊息,以及 Apigee 設定的錯誤變數。如果您要開發錯誤處理錯誤規則,就必須瞭解這項資訊。如需更多資訊,請參閱「政策錯誤的相關資訊」和「處理錯誤」。
執行階段錯誤
政策執行時可能會發生這些錯誤。
錯誤代碼 | HTTP 狀態 | 原因 | 修正 |
---|---|---|---|
steps.jwt.FailedToDecode |
401 |
當政策無法解碼 JWT 時,就會發生這個錯誤。JWT 可能格式錯誤、無效或無法解碼。 | build |
steps.jwt.FailedToResolveVariable |
401 |
當政策 <Source> 元素中指定的流程變數不存在時,就會發生這個錯誤。 |
|
steps.jwt.InvalidToken |
401 |
當政策 <Source> 元素中指定的流程變數超出範圍或無法解析時,就會發生此錯誤。 |
build |
部署錯誤
部署含有這項政策的 Proxy 時,可能會發生這些錯誤。
錯誤名稱 | 原因 | 修正 |
---|---|---|
InvalidEmptyElement |
當政策的 <Source> 元素未指定含有待解碼 JWT 的流程變數時,就會發生此錯誤。 |
build |
錯誤變數
這些變數會在發生執行階段錯誤時設定。詳情請參閱「關於政策錯誤的相關資訊」。
變數 | 地點 | 範例 |
---|---|---|
fault.name="fault_name" |
fault_name 是錯誤名稱,如上方「執行階段錯誤」表格所列。錯誤名稱是錯誤代碼的最後一個部分。 | fault.name Matches "InvalidToken" |
JWT.failed |
所有 JWT 政策在失敗的情況下都會設定相同的變數。 | JWT.failed = true |
錯誤回應範例
針對錯誤處理,最佳做法是擷取錯誤回應的 errorcode
部分。請勿依賴 faultstring
中的文字,因為該文字可能會變更。
錯誤規則範例
<FaultRules> <FaultRule name="JWT Policy Errors"> <Step> <Name>JavaScript-1</Name> <Condition>(fault.name Matches "InvalidToken")</Condition> </Step> <Condition>JWT.failed=true</Condition> </FaultRule> </FaultRules>