VerifyJWS policy

You're viewing Apigee Edge documentation.
Go to the Apigee X documentation.
info

What

Verifies the signature on a JWS received from clients or other systems. This policy also extracts headers into context variables so that subsequent policies or conditions can examine those values to make authorization or routing decisions. See JWS and JWT policies overview for a detailed introduction.

If the JWS is verified and valid, then the request is allowed to proceed. If the JWS signature cannot be verified or if the JWS is invalid because of some type of error, all processing stops and an error is returned in the response.

To learn about the parts of a JWS and how they are encrypted and signed, refer to RFC7515.

Video

Watch a short video to learn how to verify the signature on a JWS. While this video is specific to verifying a JWT, many of the concepts are the same for JWS.

Samples

Verify an attached JWS signed with the HS256 algorithm

This example policy verifies an attached JWS that was signed with the HS256 encryption algorithm, HMAC using a SHA-256 checksum. The JWS is passed in the proxy request by using a form parameter named JWS. The key is contained in a variable named private.secretkey.

An attached JWS contains the encoded header, payload, and signature:

header.payload.signature

The policy configuration includes the information Edge needs to decode and evaluate the JWS, such as where to find the JWS (in a flow variable specified in the <Source> element), the required signing algorithm, and where to find the secret key (stored in an Edge flow variable, which could have been retrieved from the Edge KVM, for example).

<VerifyJWS name="JWS-Verify-HS256">
    <DisplayName>JWS Verify HS256</DisplayName>
    <Algorithm>HS256</Algorithm>
    <Source>request.formparam.JWS</Source>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <SecretKey>
        <Value ref="private.secretkey"/>
    </SecretKey>
</VerifyJWS>

The policy writes its output to context variables so that subsequent policies or conditions in the API proxy can examine those values. See Flow variables for a list of the variables set by this policy.

Verify a detached JWS signed with the RS256 algorithm

This example policy verifies a detached JWS that was signed with the RS256 algorithm. To verify, you need to provide the public key. The JWS is passed in the proxy request by using a form parameter named JWS. The public key is contained in a variable named public.publickey.

A detached JWS omits the payload from the JWS:

header..signature

It is up to you to pass the payload to the VerifyJWS policy by specifying the variable name containing the payload to the <DetachedContent> element. The specified content in <DetachedContent> must be in the original unencoded form it was when the JWS signature was created.

<VerifyJWS name="JWS-Verify-RS256">
    <DisplayName>JWS Verify RS256</DisplayName>
    <Algorithm>RS256</Algorithm>
    <Source>request.formparam.JWS</Source>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <PublicKey>
        <Value ref="public.publickey"/>
    </PublicKey>
    <DetachedContent>private.payload</DetachedContent>
</VerifyJWS>

The policy writes its output to context variables so that subsequent policies or conditions in the API proxy can examine those values. See Flow variables for a list of the variables set by this policy.

Setting the key elements

The elements that you use to specify the key used to verify the JWS depend on the chosen algorithm, as shown in the following table:

Algorithm key elements
HS*
<SecretKey>
  <Value ref="private.secretkey"/>
</SecretKey>
RS*, ES*, PS*
<PublicKey>
  <Value ref="rsa_public_key"/>
</PublicKey>

or:

<PublicKey>
  <JWKS ref="jwks_val_ref_or_url"/>
</PublicKey>
*For more on the key requirements, see About signature encryption algorithms.

Element reference

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

Note: Configuration will differ somewhat depending on the encryption algorithm you use. Refer to the Samples for examples that demonstrate configurations for specific use cases.

Attributes that apply to the top-level element

<VerifyJWS 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 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

<Algorithm>

<Algorithm>HS256</Algorithm>

Specifies the encryption algorithm to sign the token. RS*/PS*/ES* algorithms employ a public/secret key pair, while HS* algorithms employ a shared secret. See also About signature encryption algorithms.

You can specify multiple values separated by commas. For example "HS256, HS512" or "RS256, PS256". However, you cannot combine HS* algorithms with any others or ES* algorithms with any others because they require a specific key type. You can combine RS* and PS* algorithms.

Default N/A
Presence Required
Type String of comma-separated values
Valid values HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384, PS512

<AdditionalHeaders/Claim>

<AdditionalHeaders>
    <Claim name='claim1'>explicit-value-of-claim-here</Claim>
    <Claim name='claim2' ref='variable-name-here'/>
    <Claim name='claim3' ref='variable-name-here' type='boolean'/>
    <Claim name='claim4' ref='variable-name' type='string' array='true'/>
 </AdditionalHeaders>

Validates that the JWS header contains the specified additional claim name/value pair(s) and that the asserted claim values match.

An additionl claim uses a name that is not one of the standard, registered JWS claim names. The value of an additional claim can be a string, a number, a boolean, a map, or an array. A map is simply a set of name/value pairs. The value for a claim of any of these types can be specified explicitly in the policy configuration, or indirectly via a reference to a flow variable.

Default N/A
Presence Optional
Type

String (default), number, boolean, or map.

The type defaults to String if no type is specified.

Array Set to true to indicate if the value is an array of types. Default: false
Valid values Any value that you want to use for an additional claim.

The <Claim> element takes these attributes:

  • name - (Required) The name of the claim.
  • ref - (Optional) The name of a flow variable. If present, the policy will use the value of this variable as the claim. If both a ref attribute and an explicit claim value are specified, the explicit value is the default, and is used if the referenced flow variable is unresolved.
  • type - (Optional) One of: string (default), number, boolean, or map
  • array - (Optional) Set to true to indicate if the value is an array of types. Default: false.

<DetachedContent>

<DetachedContent>variable-name-here</DetachedContent>

A generated JWS with a content payload is in the form:

header.payload.signature

If you use the GenerateJWS policy to create detached payload, the generated JWS omits the payload and is in the form:

header..signature

For a detached payload, it is up to you to pass the payload to the VerifyJWS policy by using the <DetachedContent> element. The specified content payload must be in the original unencoded form it was when the JWS signature was created.

The policy throws an error when:

  • <DetachedContent> is specified when the JWS does not contain a detached content payload (fault code is steps.jws.ContentIsNotDetached).
  • <DetachedContent> is omitted and the JWS has a detached content payload (fault code is steps.jws.InvalidSignature).
Default N/A
Presence Optional
Type Variable reference

<IgnoreCriticalHeaders>

<IgnoreCriticalHeaders>true|false</IgnoreCriticalHeaders>

Set to false if you want the policy to throw an error when any header listed in the crit header of the JWS is not listed in the <KnownHeaders> element. Set to true to cause the VerifyJWS policy to ignore the crit header.

One reason to set this element to true is if you are in a testing environment and you do not want the policy to fail because of a missing header.

Default false
Presence Optional
Type Boolean
Valid values true or false

<IgnoreUnresolvedVariables>

<IgnoreUnresolvedVariables>true|false</IgnoreUnresolvedVariables>

Set to false if you want the policy to throw an error when any referenced variable specified in the policy is unresolvable. Set to true to treat any unresolvable variable as an empty string (null).

Default false
Presence Optional
Type Boolean
Valid values true or false

<KnownHeaders>

<KnownHeaders>a,b,c</KnownHeaders>

or:

<KnownHeaders ref=’variable_containing_headers’/>

The GenerateJWS policy uses the <CriticalHeaders> element to populate the crit header in a token. For example:

{
  “typ: “...”,
  “alg” : “...”,
  “crit” : [ “a”, “b”, “c” ],
}

The VerifyJWS policy examines crit header in the JWS, if it exists, and for each item listed it checks that the <KnownHeaders> element also lists that header. The <KnownHeaders> element can contain a superset of the items listed in crit. It is only necessary that all the headers listed in crit are listed in the <KnownHeaders> element. Any header that the policy finds in crit that is not also listed in <KnownHeaders> causes the VerifyJWS policy to fail.

You can optionally configure the VerifyJWS policy to ignore the crit header by setting the <IgnoreCriticalHeaders> element to true.

Default N/A
Presence Optional
Type Comma separated array of strings
Valid values Either an array or the name of a variable containing the array.

<PublicKey/JWKS>

<!-- Specify the JWKS. -->
<PublicKey>
   <JWKS>jwks-value-here</JWKS>
</PublicKey>

or:

<!-- Specify a variable containing the JWKS. -->
<PublicKey>
   <JWKS ref="public.jwks"/>
</PublicKey>

or:

<!-- Specify a public URL that returns the JWKS.
The URL is static, meaning you cannot set it using a variable. -->
<PublicKey>
   <JWKS uri="jwks-url"/>
</PublicKey>

Specifies a value in JWKS format (RFC 7517) containing a set of public keys. Use only when the algorithm is one of RS256/RS384/RS512, PS256/PS384/PS512, or ES256/ES384/ES512.

If the inbound JWS bears a key ID which present in the set of JWKS, then the policy will use the correct public key to verify the JWS signature. For details about this feature, see Using a JSON Web Key Set (JWKS) to verify a JWS.

If you fetch the value from a public URL, Edge caches the JWKS for a period of 300 seconds. When the cache expires, Edge fetches the JWKS again.

Default N/A
Presence To verify a JWS using an RSA algorithm, you must either either use the JWKS or Value element.
Type String
Valid values A flow variable, string value, or URL.

<PublicKey/Value>

<PublicKey>
   <Value ref="public.publickey"/>
</PublicKey>
-or-
<PublicKey>
    <Value>
    -----BEGIN PUBLIC KEY-----
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw2kPrRzcufvUNHvTH/WW
    Q0UrCw5c0+Y707KX3PpXkZGbtTT4nvU1jC0d1lHV8MfUyRXmpmnNxJHAC2F73IyN
    C5TBtXMORc+us7A2cTtC4gZV256bT4h3sIEMsDl0Joz9K9MPzVPFxa1i0RgNt06n
    Xn/Bs2UbbLlKP5Q1HPxewUDEh0gVMqz9wdIGwH1pPxKvd3NltYGfPsUQovlof3l2
    ALvO7i5Yrm96kknfFEWf1EjmCCKvz2vjVbBb6mp1ZpYfc9MOTZVpQcXSbzb/BWUo
    ZmkDb/DRW5onclGzxQITBFP3S6JXd4LNESJcTp705ec1cQ9Wp2Kl+nKrKyv1E5Xx
    DQIDAQAB
    -----END PUBLIC KEY-----
    </Value>
</PublicKey>

Specifies the public key used to verify the signature on the JWS. Use the ref attribute to pass the key in a flow variable, or specify the PEM-encoded key directly. Use only when the algorithm is one of RS256/RS384/RS512, PS256/PS384/PS512, or ES256/ES384/ES512.

Default N/A
Presence To verify a JWS signed with an RSA algorithm, you must either either use the JWKS or Value elements.
Type String
Valid values A flow variable or string.

<SecretKey/Value>

<SecretKey>
  <Value ref="private.your-variable-name"/>
</SecretKey>

Provides the secret key used to verify or sign tokens with an HMAC algorithm. Use only when the algorithm is one of HS256, HS384, HS512. Use the ref attribute to pass the key in a flow variable.

Default N/A
Presence Required for HMAC algorithms.
Type String
Valid values A flow variable referring to a string.

Note: If a flow variable, it must have the prefix "private". For example, private.mysecret

<Source>

<Source>JWS-variable</Source>

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

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

Error reference

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.AlgorithmInTokenNotPresentInConfiguration 401 Occurs when the verification policy has multiple algorithms
steps.jws.AlgorithmMismatch 401 The algorithm specified in the header by the Generate policy did not match the one expected in the Verify policy. The algorithms specified must match.
steps.jws.ContentIsNotDetached 401 <DetachedContent> is specified when the JWS does not contain a detached content payload.
steps.jws.FailedToDecode 401 The policy was unable to decode the JWS. The JWS is possibly corrupted.
steps.jws.InsufficientKeyLength 401 For a key less than 32 bytes for the HS256 algorithm
steps.jws.InvalidClaim 401 For a missing claim or claim mismatch, or a missing header or header mismatch.
steps.jws.InvalidCurve 401 The curve specified by the key is not valid for the Elliptic Curve algorithm.
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.KeyIdMissing 401 The Verify policy uses a JWKS as a source for public keys, but the signed JWS does not include a kid property in the header.
steps.jws.KeyParsingFailed 401 The public key could not be parsed from the given key information.
steps.jws.MissingPayload 401 The JWS payload is missing.
steps.jws.NoAlgorithmFoundInHeader 401 Occurs when the JWS omits the algorithm header.
steps.jws.NoMatchingPublicKey 401 The Verify policy uses a JWKS as a source for public keys, but the kid in the signed JWS is not listed in the JWKS.
steps.jws.UnhandledCriticalHeader 401 A header found by the Verify JWS policy in the crit header is not listed in KnownHeaders.
steps.jws.UnknownException 401 An unknown exception occurred.
steps.jws.WrongKeyType 401 Wrong type of key specified. For example, if you specify an RSA key for an Elliptic Curve algorithm, or a curve key for an RSA algorithm.

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>