解碼 JWT 政策

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

結果

解碼 JWT,但不驗證 JWT 的簽章。與 VerifyJWT 政策搭配使用時,這項政策最為實用,因為在驗證 JWT 的簽名之前,必須先知道 JWT 內附加資訊的值。

無論簽署 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 中的後續政策或條件檢查這些值。如要查看這項政策設定的變數清單,請參閱「流程變數」。

解碼 JWT 的元素參考資料

政策參考資料說明「解碼 JWT」政策的元素和屬性。

適用於頂層元素的屬性

<DecodeJWT name="JWT" continueOnError="false" enabled="true" async="false">

所有政策父項元素都有下列屬性。

屬性 說明 預設 外觀狀態
名稱 政策的內部名稱。名稱只能使用以下字元: A-Z0-9._\-$ %。不過,Edge 管理 UI 會強制執行其他限制,例如自動移除非英數字元的字元。

視需要使用 <displayname></displayname> 元素,在管理 UI 代理伺服器編輯器中,以其他自然語言名稱標記政策。

N/A 必填
continueOnError 設為 false,在政策失敗時傳回錯誤。這是大多數政策的預期行為。

設為 true,即使政策失敗,流程執行作業仍會繼續。

false 選用
已啟用 設為 true 即可強制執行政策。

設為 false 即可「關閉」這項政策。即使政策仍附加至流程,系統也不會強制執行。

true 選用
非同步 這項屬性已淘汰。 false 已淘汰

<DisplayName>

<DisplayName>Policy Display Name</DisplayName>

除了名稱屬性外,您還可以使用這個屬性,在管理 UI 代理項目編輯器中,以其他自然語言名稱標示政策。

預設 如果省略這個元素,系統會使用政策名稱屬性的值。
外觀狀態 選用
類型 字串

<Source>

<Source>jwt-variable</Source>

如有,則指定政策預期會找到要解碼的 JWT 的流程變數。

預設 request.header.authorization (請參閱上方的附註,瞭解預設值的重要資訊)。
外觀狀態 選用
類型 字串
有效值 Edge 流程變數名稱

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.

錯誤參考資料

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 Fix
steps.jwt.FailedToDecode 401 Occurs when the policy is unable to decode the JWT. The JWT may be malformed, invalid or otherwise not decodable.
steps.jwt.FailedToResolveVariable 401 Occurs when the flow variable specified in the <Source> element of the policy does not exist.
steps.jwt.InvalidToken 401 Occurs when the flow variable specified in the <Source> element of the policy is out of scope or can't be resolved.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name Cause Fix
InvalidEmptyElement Occurs when the flow variable containing the JWT to be decoded is not specified in the <Source> element of the policy.

錯誤變數

系統會在發生執行階段錯誤時設定這些變數。詳情請參閱重要須知 政策錯誤。

變數 地點 範例
fault.name="fault_name" fault_name 是錯誤的名稱,如上方「執行階段錯誤」表格所列。錯誤名稱是錯誤程式碼的最後部分。 fault.name Matches "TokenExpired"
JWT.failed 如果失敗時,所有 JWT 政策都會設定相同的變數。 JWT.failed = true

錯誤回應範例

JWT 政策錯誤代碼

針對錯誤處理,最佳做法是將錯誤的 errorcode 部分加以包裝 回應。請勿參考 faultstring 中的文字,因為可能會變動。

錯誤規則範例

    <FaultRules>
        <FaultRule name="JWT Policy Errors">
            <Step>
                <Name>JavaScript-1</Name>
                <Condition>(fault.name Matches "TokenExpired")</Condition>
            </Step>
            <Condition>JWT.failed=true</Condition>
        </FaultRule>
    </FaultRules>