DecodeJWS 政策

查看 Apigee Edge 說明文件。
前往 Apigee X說明文件
資訊

結果

這個外掛程式能解碼 JWS 標頭,但不會在 JWS 上驗證簽章,並將每個標頭寫入 流程變數這項政策與 VerifyJWS 政策搭配使用時最實用。 必須在驗證 JWS 簽章之前知道 JWS 中的標頭值。

JWS 可以有「附加」酬載,格式如下:

header.payload.signature

或者,JWS 可以省略酬載 (稱為「卸離」酬載),格式如下:

header..signature

DecodeJWS 政策僅將 JWS 的標頭部分解碼,因此適用於這兩種表單。 無論用於簽署 JWS 的演算法為何,DecodeJWS 政策也能正常運作。

如需詳細的簡介,請參閱 JWS 和 JWT 政策總覽 以及 JWS 格式的總覽

影片

請觀看短片,瞭解如何解碼 JWT。這部影片 因為 JWT 的許多概念都與 JWS 相同。

範例:解碼 JWS

下列政策會將流程變數 var.JWS 中的 JWS 解碼。這個 變數,以及可宣告的 JWS。政策可從中取得 JWS 任何流程變數都沒問題

<DecodeJWS name="JWS-Decode-HS256">
    <DisplayName>JWS Verify HS256</DisplayName>
    <Source>var.JWS</Source>
</DecodeJWS>

這項政策會針對 JWS 標頭部分中的每個標頭,設定名為:

jws.policy-name.header.header-name

如果 JWS 有附加的酬載,則會設定 jws.policy-name.header.payload 傳送至酬載如果是卸離的酬載,payload 會是空白。 如需這項政策設定的完整變數清單,請參閱流程變數

解碼 JWS 的元素參照

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

具備的屬性 套用至頂層元素

<DecodeJWS name="JWS" continueOnError="false" enabled="true" async="false">

下列屬性適用於所有政策父項元素。

屬性 說明 預設 外觀狀態
名稱 政策的內部名稱。名稱中可使用的字元僅限於: A-Z0-9._\-$ %。不過,邊緣管理 UI 會強制執行額外的 限制 (例如自動移除非英數字元的字元)。

視需要使用 <displayname></displayname> 元素 在管理 UI Proxy 編輯器中,以不同的自然語言為政策加上標籤 名稱。

不適用 必填
continueOnError 如果設為「false」,系統會在政策失敗時傳回錯誤。這是可預期的情況 大多數政策的行為

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

false 選用
已啟用 如要強制執行政策,請設為 true

false 設為「關閉」政策。不會強制執行這項政策 即使服務仍附加於資料流

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

&lt;DisplayName&gt;

<DisplayName>Policy Display Name</DisplayName>

除了名稱屬性以外,還能在管理 UI Proxy 編輯器中為政策加上標籤 使用不同的自然語言名稱

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

&lt;Source&gt;

<Source>JWS-variable</Source>

如果有,請指定政策預期在哪個流程變數中找出 JWS 解碼器。

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

Flow variables

Upon success, the Verify JWS and Decode JWS policies set context variables according to this pattern:

jws.{policy_name}.{variable_name}

For example, if the policy name is verify-jws, then the policy will store the algorithm specified in the JWS to this context variable: jws.verify-jws.header.algorithm

Variable name Description
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.
header.algorithm The signing algorithm used on the JWS. For example, RS256, HS384, and so on. See (Algorithm) Header Parameter for more.
header.kid The Key ID, if added when the JWS was generated. See also "Using a JSON Web Key Set (JWKS)" at JWT and JWS policies overview to verify a JWS. See (Key ID) Header Parameter for more.
header.type The header type value. See (Type) Header Parameter for more.
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 JWS.
header-json The header in JSON format.
payload The JWS payload if the JWS has an attached payload. For a detached payload, this variable is empty.
valid In the case of VerifyJWS, 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 DecodeJWS, 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 Occurs when
steps.jws.FailedToDecode 401 The policy was unable to decode the JWS. The JWS is possibly corrupted.
steps.jws.FailedToResolveVariable 401 Occurs when the flow variable specified in the <Source> element of the policy does not exist.
steps.jws.InvalidClaim 401 For a missing claim or claim mismatch, or a missing header or header mismatch.
steps.jws.InvalidJsonFormat 401 Invalid JSON found in the JWS header.
steps.jws.InvalidJws 401 This error occurs when the JWS signature verification fails.
steps.jws.InvalidPayload 401 The JWS payload is invalid.
steps.jws.InvalidSignature 401 <DetachedContent> is omitted and the JWS has a detached content payload.
steps.jws.MissingPayload 401 The JWS payload is missing.
steps.jws.NoAlgorithmFoundInHeader 401 Occurs when the JWS omits the algorithm header.
steps.jws.UnknownException 401 An unknown exception occurred.

Deployment errors

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

Error name Occurs when
InvalidAlgorithm The only valid values are: RS256, RS384, RS512, PS256, PS384, PS512, ES256, ES384, ES512, HS256, HS384, HS512.

EmptyElementForKeyConfiguration

FailedToResolveVariable

InvalidConfigurationForActionAndAlgorithmFamily

InvalidConfigurationForVerify

InvalidEmptyElement

InvalidFamiliesForAlgorithm

InvalidKeyConfiguration

InvalidNameForAdditionalClaim

InvalidNameForAdditionalHeader

InvalidPublicKeyId

InvalidPublicKeyValue

InvalidSecretInConfig

InvalidTypeForAdditionalClaim

InvalidTypeForAdditionalHeader

InvalidValueForElement

InvalidValueOfArrayAttribute

InvalidVariableNameForSecret

MissingConfigurationElement

MissingElementForKeyConfiguration

MissingNameForAdditionalClaim

MissingNameForAdditionalHeader

Other possible deployment errors.

Fault variables

These variables are set when a runtime error occurs. 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 Matches "TokenExpired"
JWS.failed All JWS policies set the same variable in the case of a failure. jws.JWS-Policy.failed = true

Example error response

For error handling, the best practice is to trap the errorcode part of the error response. Do not rely on the text in the faultstring, because it could change.

Example fault rule

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