DecodeJWT policy

What

Decodes a JWT without verifying the signature on the JWT. This is most useful when used in concert with the Verify JWT Policy, when the value of a claim from within the JWT must be known before verifying the signature of the JWT.

The JWT Decode policy works regardless of the algorithm that was used to sign the JWT.

Video

Watch a short video to learn how to decode a JWT.

Sample: Decode a JWT

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

<DecodeJWT name="JWT-Decode-HS256">
    <DisplayName>JWT Verify HS256</DisplayName>
    <Source>var.jwt</Source>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</DecodeJWT>

Element reference for Decode JWT

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

Attributes that apply to the top-level element

<DecodeJWT name="JWT" 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 Edge management 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>jwt-variable</Source>

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

Important Note: By default, the JWT is retrieved from the variable request.header.authorization. In this case, Edge looks for the JWT in the request Authorization header. If you pass the JWT in the Authorization header, you do not need to include the Source element in the policy; however, you must include Bearer in the auth header. For example, you would pass the JWT in the Authorization header like this:

curl -v http://52.200.92.187:9001/doctest-jwt/verify-rs256 -H "Authorization: Bearer <your JWT>"

You can configure to policy to retrieve the JWT from a form or query parameter variable or any other variable. If the variable does not exist or if the policy otherwise can't find the JWT, the policy returns an error.

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

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 this context variable: jwt.jwt-parse-token.subject

Variable name Description
header.algorithm The signing algorithm used on the JWT. For example, RS256, HS384, and so on.
claim.subject The JWT subject claim.
claim.issuer The JWT issuer claim.
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 seconds since epoch.
expiry_formatted The expiration date/time, formatted as a human-readable string. Example: 2017-09-28T21:30:45.000+0000
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
is_expired true or false
claim.issuedat The Date the token was issued, expressed in seconds since epoch.
claim.notbefore If the JWT includes a nbf claim, this variable will contain the value. This is expressed in seconds since epoch.
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.

claim.name The value of the named claim (standard or additional). One of these will be set for every claim in the token.
header.name The value of the named header (standard or additional). One of these will be set for every additional header in the token.
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.
header.type Will be set to JWT.
payload-claim-names An array of claims supported by the JWT.
payload-json
Contains the payload in JSON format.
header-json Contains the header in JSON format.

Error reference

TThis 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
policies.jwt.AlgorithmInTokenNotPresentInConfiguration Occurs when the verification policy has multiple algorithms
policies.jwt.JwtAudienceMismatch 401 The audience claim failed on token verification.
policies.jwt.AlgorithmMismatch 401 The algorithm specified in the Generate policy did not match the one expected in the Verify policy. The algorithms specified must match.
policies.jwt.JwtClaimNotPresent 401 The JWT did not contain a claim on token verification.
policies.jwt.ClaimTypeMismatch 401
policies.jwt.FailedToDecode 401 The policy was unable to decode the JWT. The JWT is possibly corrupted.
policies.jwt.InvalidJwt 401 This error occurs when the JWT signature verification fails.
policies.jwt.InvalidClaim 401
policies.jwt.JwtIssuerMismatch 401 The issuer claim failed on token verification.
policies.jwt.JwtSubjectMismatch 401 The subject claim failed on token verification.
policies.jwt.KeyIdMissing
policies.jwt.KeyParsingFailed
policies.jwt.NoAlgorithmFoundInToken Occurs when the verification policy has multiple algorithms.
policies.jwt.NoMatchingPublicKey From JWKS.
policies.jwt.TokenExpired 401 The policy attempts to verify an expired token.

Deployment errors

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

Error name Occurs when
InvalidJwtAlgorithm The only valid values are: RS256, RS384, RS512, HS256, HS384, and HS512.
InvalidNameForAdditionalClaim

InvalidTypeForAdditionalClaim

InvalidTypeForAdditionalHeader

MissingNameForAdditionalClaim

InvalidNameForAdditionalHeader

MissingNameForAdditionalHeader

InvalidAlgorithm

InvalidConfigurationForVerify

InvalidValueOfArrayAttribute

InvalidConfigurationForActionAndAlgorithmFamily

InvalidEmptyElement

InvalidValueForElement

InvalidFamiliesForAlgorithm

MissingConfigurationElement

InvalidKeyConfiguration

MissingElementForKeyConfiguration

EmptyElementForKeyConfiguration

InvalidPublicKeyValue

InvalidVariableNameForSecret

InvalidSecretInConfig

FailedToResolveVariable

InvalidTimeFormat

InvalidPublicKeyId

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"
jwt.policy_name.failed policy_name is the user-specified name of the policy that threw the fault. jwt.JWT-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="JWT Policy Errors">
        <Step>
            <Name>JavaScript-1</Name>
            <Condition>(fault.name Matches "TokenExpired")</Condition>
        </Step>
        <Condition>jwt.JWT-1.failed=true</Condition>
    </FaultRule>
</FaultRules>