GenerateJWS policy

What

Generates a signed JWS, with a configurable set of claims. The JWS can then be returned to clients, transmitted to backend targets, or used in other ways. See JWS and JWT policies overview for a detailed introduction.

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 generate a signed JWT. While this video is specific to generating a JWT, many of the concepts are the same for JWS.

Samples

Generate an attached JWS signed with the HS256 algorithm

This example policy generates an attached JWS and signs it using the HS256 algorithm. HS256 relies on a shared secret for both signing and verifying the signature.

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

header.payload.signature

Set <DetachContent> to true to generate detached content. See Parts of a JWS/JWT for more on the structure and format of a JWS.

Use the <Payload> element to specify the raw, unencoded JWS payload. In this example, a variable contains the payload. When this policy action is triggered, Edge encodes the JWS header and payload, then adds the encoded signature to digitally sign the JWS.

The policy configuration below creates a JWS from a payload contained in the variable private.payload.

<GenerateJWS name="JWS-Generate-HS256">
    <DisplayName>JWS Generate HS256</DisplayName>
    <Algorithm>HS256</Algorithm>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <SecretKey>
        <Value ref="private.secretkey"/>
        <Id>1918290</Id>
    </SecretKey>
    <Payload ref="private.payload" />
    <OutputVariable>jws-variable</OutputVariable>
</GenerateJWS>

Generate a detached JWS signed with the RS256 algorithm

This example policy generates a detached JWS and signs it using the RS256 algorithm. Generating an RS256 signature relies on an RSA private key, which must be provided in PEM-encoded form.

A detached JWS omits the payload from the JWS:

header..signature

Use the <Payload> element to specify the raw, unencoded JWS payload. When this policy is triggered, Edge encodes the JWS header and payload, and then uses them to generate the encoded signature. However, the generated JWS omits the payload. It is up to you to pass the payload to the VerifyJWS policy by using the <DetachedContent> element of the VerifyJWS policy.

<GenerateJWS name="JWS-Generate-RS256">
    <DisplayName>JWS Generate RS256</DisplayName>
    <Algorithm>RS256</Algorithm>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <PrivateKey>
        <Value ref="private.privatekey"/>
        <Password ref="private.privatekey-password"/>
        <Id ref="private.privatekey-id"/>
    </PrivateKey>
    <Payload ref="private.payload" />
    <DetachContent>true</DetachContent>
    <OutputVariable>jws-variable</OutputVariable>
</GenerateJWS>

Setting the key elements

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

Algorithm Key elements
HS{256/384/512}*
<SecretKey>
  <Value ref="private.secretkey"/>
  <Id>1918290</Id>
</SecretKey>
RS/PS/ES{256/384/512}*
<PrivateKey>
  <Value ref="private.privatekey"/>
  <Password ref="private.privatekey-password"/>
  <Id ref="private.privatekey-id"/>
</PrivateKey>

The <Password> and <Id> elements are optional.

*For more on the key requirments, see About signature encryption algorithms.

Element reference for Generate JWS

The policy reference describes the elements and attributes of the Generate 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

<GenerateJWS 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>algorithm-here</Algorithm>

Specifies the encryption algorithm to sign the token.

Default N/A
Presence Required
Type String
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>

Puts the additional claim name/value pair(s) in the header for the JWS.

Default N/A
Presence Optional
Valid values Any value that you want to use for an additional claim. You can specify the claim explicitly as string, a number, a boolean, a map, or an array.

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.

<CriticalHeaders>

<CriticalHeaders>a,b,c</CriticalHeaders>

or:

<CriticalHeaders ref=’variable_containing_headers’/>

Adds the critical header, crit, to the JWS. The crit header is an array of header names that must be known and recognized by the JWS receiver. For example:

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

At runtime, the VerifyJWS policy examines the crit header. For each header listed in the crit header, it checks that the <KnownHeaders> element of the VerifyJWS policy also lists that header. Any header that the VerifyJWS policy finds in crit that is not also listed in <KnownHeaders> causes the VerifyJWS policy to fail.

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.

<DetachContent>

<DetachContent>true|false</DetachContent>

Specifies whether to generate the JWS with a detached payload, <DetachContent>true</DetachContent>, or not, <DetachContent>false</DetachContent>.

If you specify false, the default, the generated JWS is in the form:

header.payload.signature

If you specify true to create detached payload, the generated JWS omits the payload and is in the form:

header..signature

With a detached payload, it is up to you to pass the original unencoded payload to the VerifyJWS policy by using the <DetachedContent> element of the VerifyJWS policy.

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

<OutputVariable>

<OutputVariable>JWS-variable</OutputVariable>

Specifies where to place the JWS generated by this policy. By default it is placed into the flow variable jws.POLICYNAME.generated_jws.

Default jws.POLICYNAME.generated_jws
Presence Optional
Type String (a flow variable name)

<Payload>

<Payload ref="flow-variable-name-here" />

or

<Payload>payload-value</Payload>

Specifies the raw, unencoded JWS payload. Specify a variable containing the payload, or a string.

Default N/A
Presence Required
Type String, byte array, stream, or any other representation of the unencoded JWS payload.

<PrivateKey/Id>

<PrivateKey>
  <Id ref="flow-variable-name-here"/>
</PrivateKey>

or

<PrivateKey>
  <Id>your-id-value-here</Id>
</PrivateKey>

Specifies the key ID (kid) to include in the JWS header. Use only when the algorithm is one of RS256/RS384/RS512, PS256/PS384/PS512, or ES256/ES384/ES512.

Default N/A
Presence Optional
Type String
Valid values A flow variable or string

<PrivateKey/Password>

<PrivateKey>
  <Password ref="private.privatekey-password"/>
</PrivateKey>

Specify the password the policy should use to decrypt the private key, if necessary. Use the ref attribute to pass the key in a flow variable. Use only when the algorithm is one of RS256/RS384/RS512, PS256/PS384/PS512, or ES256/ES384/ES512.

Default N/A
Presence Optional
Type String
Valid values A flow variable reference.

Note: You must specify a flow variable. Edge will reject as invalid a policy configuration in which the password is specified in plaintext. The flow variable must have the prefix "private". For example, private.mypassword

<PrivateKey/Value>

<PrivateKey>
  <Value ref="private.variable-name-here"/>
</PrivateKey>

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

Default N/A
Presence Required to generate a JWS using the RS256 algorithm.
Type String
Valid values A flow variable containing a string representing a PEM-encoded RSA private key value.

Note: The flow variable must have the prefix "private". For example, private.mykey

<SecretKey/Id>

<SecretKey>
  <Id ref="flow-variable-name-here"/>
</SecretKey>

or

<SecretKey>
  <Id>your-id-value-here</Id>
</SecretKey>

Specifies the key ID (kid) to include in the JWS header of a JWS signed with an HMAC algorithm. Use only when the algorithm is one of HS256/HS384/HS512.

Default N/A
Presence Optional
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.

Edge enforces a minimum key strength for the HS256/HS384/HS512 algorithms. The minimum key length for HS256 is 32 bytes, for HS384 it is 48 bytes, and for HS512 it is 64 bytes. Using a lower-strength key causes a runtime error.

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

Flow variables

The Generate JWS policy does not set flow variables.

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.GenerationFailed 401 The policy was unable to generate the JWS.
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.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.SigningFailed 401 In GenerateJWS, for a key less than the minimum size for the HS384 or HS512 algorithms
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>