GenerateJWS policy

This pageapplies toApigee andApigee hybrid.

View Apigee Edge documentation.

The GenerateJWS policy 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. SeeJWS and JWT policies overview for a detailed introduction.

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

This policy is anExtensible 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, seePolicy types.

Samples

Generate a JWS signed with HS256

This example policy generates a JWS that is signed using the HS256 algorithm. HS256 relies on a shared secret for both signing and verifying the signature. This JWS uses "attached" content, meaning the encoded header, payload, and signature are dot-concatenated to produce the final JWS:

[header].[payload].[signature]

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, Apigee 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 variablemy-payload, and stores the resulting JWS in the variableoutput-variable.

<GenerateJWSname="JWS-Generate-HS256"><DisplayName>JWSGenerateHS256</DisplayName><Algorithm>HS256</Algorithm><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><SecretKey><Valueref="private.secretkey"/><Id>1918290</Id></SecretKey><Payloadref="my-payload"/><OutputVariable>output-variable</OutputVariable></GenerateJWS>

Generate an HS256-signed JWT

This example also generates a JWS with attached content that is signed using the HS256 algorithm. In this case, the payload is JSON. Setting thetyp header to JWT results in a signed JWS that is also a signed JWT. (reference)

The policy configuration below creates a JWS from a payload contained in the variablejson-content, and stores the resulting JWS in the variableoutput-variable. The result will be a signed JWT if and only if thejson-content variable holds a JSON payload, and the properties within that JSON payload are valid for JWT. For example, theexp property, if it is present, must hold a numeric value. Theaud property, if it is present, must be a string or an array of strings. And so on. ConsultIETF RFC7519 for details on the valid values for JWT claims.

<GenerateJWSname="JWS-Generate-HS256-JWT"><Algorithm>HS256</Algorithm><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><SecretKey><Valueref="private.secretkey"/></SecretKey><Payloadref="json-content"/><AdditionalHeaders><Claimname="typ">JWT</Claim></AdditionalHeaders><OutputVariable>output-variable</OutputVariable></GenerateJWS>

Generate a detached JWS

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

A JWS with detached content omits the payload from the generated JWS:

[header]..[signature]
SeeParts 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. When this policy is triggered, Apigee encodes the JWS header and payload, and then uses them to generate the encoded signature. However, the generated JWS omits the encoded payload from the serialized JWS. This is helpful when the signed content is large, or binary (such as an image or PDF), or both. To allow validation, you must pass both elements, the JWS and the payload that was included in the signed content, to the verifying party. If you are using theVerifyJWS policy to verify the JWS, you can specify the variable containing the payload with the<DetachedContent> element of the VerifyJWS policy.

<GenerateJWSname="JWS-Generate-RS256"><DisplayName>JWSGenerateRS256</DisplayName><Algorithm>RS256</Algorithm><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><PrivateKey><Valueref="private.privatekey"/><Passwordref="private.privatekey-password"/><Idref="private.privatekey-id"/></PrivateKey><Payloadref="my-payload"/><DetachContent>true</DetachContent><OutputVariable>output-variable</OutputVariable></GenerateJWS>
Note:You can use a slightly modified version of this example to generate a JWS for the PS* or ES* algorithms. Just change the value of<Algorithm>RS256</Algorithm> toPS256, orES256, etc. The key you specify must be compatible with the algorithm. For more on the key requirements, seeAbout signature encryption algorithms.

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:

Note:The RS*, PS*, and ES* algorithms all use the same elements to specify the key. The key data you provide must be compatible with the algorithm you specify. For more on the key requirements, seeAbout signature encryption algorithms.
AlgorithmKey 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 requirements, seeAbout 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 theSamples 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.

AttributeDescriptionDefaultPresence
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 Apigee UI proxy editor with a different, natural-language name.

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

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

falseOptional
enabled Set totrue to enforce the policy.

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

trueOptional
asyncThis attribute is deprecated.falseDeprecated

<DisplayName>

<DisplayName>Policy Display Name</DisplayName>

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

DefaultIf you omit this element, the value of the policy's name attribute is used.
PresenceOptional
TypeString

<Algorithm>

<Algorithm>algorithm-here</Algorithm>

Specifies the encryption algorithm to sign the token.

DefaultN/A
PresenceRequired
TypeString
Valid valuesHS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384, PS512

<AdditionalHeaders/Claim>

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

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

The<Claim> element supports the dynamic string substitution feature calledmessage templating whentype=map.
DefaultN/A
PresenceOptional
Valid valuesAny 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, also known as the parameter.
  • ref - (Optional) The name of a flow variable. If present, the policy will use the value of this variable as the claim. If both aref 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 totrue to indicate if the value is an array of types. Default: false.

<CriticalHeaders>

<CriticalHeaders>a,b,c</CriticalHeaders>or:<CriticalHeadersref="variable_containing_headers"/>

Adds the critical header,crit, to the JWS. Thecrit header is an array of header names that must be known and recognized by the JWS receiver. For example, you can use this configuration element to produce a JWS header that when decoded, looks like this:

{  "typ": "...",  "alg" : "...",  "hyb" : "some-value-here",  "crit" : [ "hyb" ],}

This JWS header asserts that thehyb header parameter is of critical importance, and any recipient of the JWS must understand the meaning and value of that parameter.

PerIETF RFC 7515, the recipient of a JWS should reject the JWS as invalid if the recipient does not understand one or more of the parameters referenced in thecrit parameter. In Apigee, theVerifyJWS policy conforms to this behavior. For each parameter listed in thecrit parameter, it checks that the<KnownHeaders> element of the VerifyJWS policy also lists that parameter. Any header that the VerifyJWS policy finds incrit that is not also listed in<KnownHeaders> causes the VerifyJWS policy to reject the JWS.

Note: It is nonsensical to include in the list of CriticalHeaders any header parameters that do not exist in the actual JWS header. The<CriticalHeaders> does not actually add the referenced parameters to the JWS header. It only defines the contents of thecrit parameter. You can use the<AdditionalHeader/Claims> to add the referenced header claims or parameters.
DefaultN/A
PresenceOptional
TypeComma separated array of one or more strings
Valid valuesEither an array or a reference to 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 a JWS with a detached payload, the generated JWS omits the payload and is in the form:

[header]..[signature]

With a JWS using a detached payload, it is up to you to pass the original unencoded payload to the verifying party, along with the serialized JWS.

Defaultfalse
PresenceOptional
TypeBoolean
Valid valuestrue 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).

Defaultfalse
PresenceOptional
TypeBoolean
Valid valuestrue or false

<OutputVariable>

<OutputVariable>output-variable</OutputVariable>

Specifies the name of the context variable that the policy will set with the generated JWS. By default, the policy places the JWS into the context variable namedjws.POLICYNAME.generated_jws.

Defaultjws.POLICYNAME.generated_jws
PresenceOptional
TypeString (a flow variable name)

<Payload>

<Payloadref="flow-variable-name-here"/>or<Payload>payload-value</Payload>

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

The<Payload> element supports the dynamic string substitution feature calledmessage templating. A message template permits you to perform variable string substitution in thepayload-value string. In the following example, the payload would be constructed from a combination of text and the value of a variable.
<Payload>fixed-text-{request.formparam.payload}-more-fixed-text</Payload>
DefaultN/A
PresenceRequired
TypeString, byte array, stream, or any other representation of the unencoded JWS payload.
Valid valuesEither a message template, or a reference to a variable containing the payload.

<PrivateKey> element

This is optional, for use only when the<Algorithm> is one of the RS*, PS*, or ES* options. It specifies the private key to use for signing, as well as other information related to the private key. It is used when the algorithm is an asymmetric algorithm.

<PrivateKey>   <Value ref="private.privatekey"</Value></PrivateKey>
Default:N/A
Presence:Optional. However, you must include exactly one of either the<PrivateKey> or<SecretKey> element. Use the<PrivateKey> element when the algorithm is RS*, PS*, or ES*, and use the<SecretKey> element when the algorithm is HS*.
Type:N/A

<PrivateKey/Id>

<PrivateKey><Idref="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.

DefaultN/A
PresenceOptional
TypeString
Valid valuesA 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 theref attribute to pass the key in a flow variable.

DefaultN/A
PresenceOptional
TypeString
Valid values

A flow variable reference.Note: You must specify a flow variable with theref attribute. Apigee 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><Valueref="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.

DefaultN/A
PresenceRequired when using the policy to generate a JWS using one of the RS*, PS*, or ES* algorithms.
TypeString
Valid values A flow variable containing a string representing a PEM-encoded private key value.

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

<SecretKey>

<SecretKeyencoding="base16|hex|base64|base64url"><Idref="variable-containing-key-id-here">secret-key-id</Id><Valueref="private.variable-here"/></SecretKey>

Specifies the secret key to use when generating a JWS that uses a symmetric (HS*) algorithm, one of HS256, HS384, or HS512.

This element is optional. However, you must include exactly one of either the<PrivateKey> or<SecretKey> element. Use the<PrivateKey> element when generating a JWS signed with an asymmetric algorithm (one of RS*, PS*, or ES*), and use the<SecretKey> element when generating a JWS signed with a symmetric algorithm (algorithm like HS*).

Children of<SecretKey>

The following table provides a description of the child elements and attributes of<SecretKey>:

ChildPresenceDescription
encoding (attribute)Optional

Specifies how the key is encoded in the referenced variable. By default, when noencoding attribute is present, the encoding of the key is treated as UTF-8. Valid values for the attribute are: hex, base16, base64, or base64url. The encoding values hex and base16 are synonyms.

<SecretKeyencoding="hex"><Idref="variable-containing-key-id-here">secret-key-id</Id><Valueref="private.secretkey"/></SecretKey>

In the above example, because the encoding ishex, if the contents of the variableprivate.secretkey is494c6f766541504973, the key will be decoded as a set of 9 bytes, which in hex will be49 4c 6f 76 65 41 50 49 73.

Note: Apigee enforces a minimum key strength. See the documentation for theValue element for details.
Id (element)Optional

The key identifier. The value can be any string. You can specify the value as a literal text value, or indirectly, via a variable reference, with theref attribute.

<SecretKey><Idref="flow-variable-name-here"/><Valueref="private.variable-here"/></SecretKey>or<SecretKey><Id>your-id-value-here</Id><Valueref="private.variable-here"/></SecretKey>

The policy will include this key identifier as thekid claim in the header of the generated JWS.

Value (element)Required

An encoded secret key. Specifies the secret key used to sign the payload. Use theref attribute to provide the key indirectly via a variable, such asprivate.secret-key .

<SecretKey><Idref="flow-variable-name-here"/><Valueref="private.my-secret-variable"/></SecretKey>
Note: The variable name must include theprivate prefix as shown in the example above.

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

<Type>

<Type>type-string-here</Type>

Optional element whose only allowed value isSigned, specifying that the policy generates a signed JWS.<Type> is provided just to match the corresponding element for the GenerateJWT and VerifyJWT policies (where it can take on either of the valuesSigned orEncrypted).

DefaultN/A
PresenceOptional
TypeString
Valid valueSigned

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 Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, seeWhat you need to know about policy errors andHandling faults.

Runtime errors

These errors can occur when the policy executes.

Fault codeHTTP statusOccurs when
steps.jws.GenerationFailed401The policy was unable to generate the JWS.
steps.jws.InsufficientKeyLength401For a key less than 32 bytes for the HS256 algorithm
steps.jws.InvalidClaim401For a missing claim or claim mismatch, or a missing header or header mismatch.
steps.jws.InvalidCurve401The curve specified by the key is not valid for the Elliptic Curve algorithm.
steps.jws.InvalidJsonFormat401Invalid JSON found in the JWS header.
steps.jws.InvalidPayload401The JWS payload is invalid.
steps.jws.InvalidSignature401<DetachedContent> is omitted and the JWS has a detached content payload.
steps.jws.KeyIdMissing401The Verify policy uses a JWKS as a source for public keys, but the signed JWS does not include akid property in the header.
steps.jws.KeyParsingFailed401The public key could not be parsed from the given key information.
steps.jws.MissingPayload401The JWS payload is missing.
steps.jws.NoAlgorithmFoundInHeader401Occurs when the JWS omits the algorithm header.
steps.jws.SigningFailed401In GenerateJWS, for a key less than the minimum size for the HS384 or HS512 algorithms
steps.jws.UnknownException401An unknown exception occurred.
steps.jws.WrongKeyType401Wrong 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 nameOccurs when
InvalidAlgorithmThe 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, seeWhat you need to know about policy errors.

VariablesWhereExample
fault.name="fault_name"fault_name is the name of the fault, as listed in theRuntime errors table above. The fault name is the last part of the fault code.fault.name Matches "TokenExpired"
JWS.failedAll 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 theerrorcode part of the error response. Do not rely on the text in thefaultstring, 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>

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.