DecodeJWS policy

Extensible policy

This page applies to Apigee and Apigee hybrid.

View Apigee Edge documentation.

The DecodeJWS policy decodes the JWS header without verifying the signature on the JWS, and writes each header to a flow variable. This policy is most useful when used in concert with the VerifyJWS policy, when the value of a header from within the JWS must be known before verifying the signature of the JWS.

A JWS can have an attached payload, as in the form:

header.payload.signature

Or, the JWS can omit the payload, called a detached payload, and be in the form:

header..signature

The DecodeJWS policy works with both forms because it only decodes the header portion of the JWS. The DecodeJWS policy also works regardless of the algorithm that was used to sign the JWS.

See JWS and JWT policies overview for a detailed introduction and an overview of the format of a JWS.

This policy is an Extensible policy and use of this policy might have cost or utilization implications, depending on your Apigee license. For information on policy types and usage implications, see Policy types.

Sample: Decode a JWS

The policy shown below decodes a JWS found in the flow variable var.JWS. This variable must be present and contain a viable (decodable) JWS. The policy can obtain the JWS from any flow variable.

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

For each header in the header portion of the JWS, the policy sets a flow variable named:

jws.policy-name.header.header-name

If the JWS has an attached payload, then it sets the jws.policy-name.header.payload flow variable to the payload. For a detached payload, payload is empty. See Flow variables for a complete list of the variables set by this policy.

Element reference for Decode JWS

The policy reference describes the elements and attributes of the Decode JWS policy.

Attributes that apply to the top-level element

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

The following attributes are common to all policy parent elements.

Attribute Description Default Presence
name The internal name of the policy. Characters you can use in the name are restricted to: A-Z0-9._\-$ %. However, the Apigee UI enforces additional restrictions, such as automatically removing characters that are not alphanumeric.

Optionally, use the <displayname></displayname> element to label the policy in the management UI proxy editor with a different, natural-language name.

N/A Required
continueOnError Set to false to return an error when a policy fails. This is expected behavior for most policies.

Set to true to have flow execution continue even after a policy fails.

false Optional
enabled Set to true to enforce the policy.

Set to false to "turn off" the policy. The policy will not be enforced even if it remains attached to a flow.

true Optional
async This attribute is deprecated. false Deprecated

<DisplayName>

<DisplayName>Policy Display Name</DisplayName>

Use in addition to the name attribute to label the policy in the management UI proxy editor with a different, natural-language name.

Default If you omit this element, the value of the policy's name attribute is used.
Presence Optional
Type String

<Source>

<Source>JWS-variable</Source>

If present, specifies the flow variable in which the policy expects to find the JWS to decode.

Default request.header.authorization (See the note above for important information about the default).
Presence Optional
Type String
Valid values An Apigee flow variable name

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 paylod, 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.

Error reference

本部分介绍当此政策触发错误时返回的故障代码和错误消息,以及由 Apigee 设置的故障变量。在开发故障规则以处理故障时,请务必了解此信息。如需了解详情,请参阅您需要了解的有关政策错误的信息和处理故障。

运行时错误

政策执行时可能会发生这些错误。

故障代码 HTTP 状态 发生的条件
steps.jws.FailedToDecode 401 该政策无法解码 JWS。JWS 可能已损坏。
steps.jws.FailedToResolveVariable 401 在政策的 <Source> 元素中指定的流变量不存在时发生。
steps.jws.InvalidClaim 401 用于缺失声明或声明不匹配,或者缺少标题或标头不匹配的情况。
steps.jws.InvalidJsonFormat 401 可在 JWS 标头中找到无效的 JSON。
steps.jws.InvalidJws 401 当 JWS 签名验证失败时,会发生此错误。
steps.jws.InvalidPayload 401 JWS 负载无效。
steps.jws.InvalidSignature 401 省略 <DetachedContent> 并且 JWS 具有分离的内容负载。
steps.jws.MissingPayload 401 JWS 负载缺失。
steps.jws.NoAlgorithmFoundInHeader 401 在 JWS 省略算法标头时发生。
steps.jws.UnknownException 401 发生未知异常。

部署错误

在您部署包含此政策的代理时,可能会发生这些错误。

错误名称 发生的条件
InvalidAlgorithm 唯一的有效值为: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

其他可能的部署错误。

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>