User Guide Cancel

CreateEncryptedJWT

 

Description

Create an encrypted JWT.

Syntax

createEncryptedJWT(payload, encryptOptions, config)

History

  • New in ColdFusion (2023 release).

Parameters

Parameter Description Required
payload

If the payload is a string, the method accepts it as it is. If not, the following fields will need to be passed as a struct:

  • iss - The authority issuing the token
  • sub - The user/client for whom this token is established
  • aud - Who is allowed to view this token
  • exp - The time after which the token is not valid anymore
  • nbf - The time before which the token should not be processed
  • iat - If not set, defaults to current timestamp
  • jti - Corresponds to jti field to prevent replay attacks. Will be generated automatically if not already displayed.
Yes
encryptOptions

Create the signature using the struct below:

  • Key
  • KeyPair - private Key will be used
  • Keystore file, keystore password, keystore alias
Yes
config

A struct with the following values:

  • algorithm - Algorithm used for signing.
  • encryption - Algorithm used for encrypting the payload. The following are supported:
    • RSA-OAEP
    • RSA-OAEP-256
    • ECDH-ES
    • ECDH-ES+A128KW
    • ECDH-ES+A192KW
    • ECDH-ES+A256KW
    • A128KW
    • A192KW
    • A256KW
    • A128GCMKW
    • A192GCMKW
    • A256GCMKW
    • PBES2-HS256+A128KW
    • PBES2-HS384+A192KW
    • PBES2-HS512+A256KW
  • generateIssuedAt - boolean to know whether to generate "iat" field
  • generateJti - boolean to know whether to generate "jti" field
Yes

Returns

A JWT string.

Example

<cfset text = {
"iss" = "a",
"sub" = "b",
"abcd" = "efgh",
"aud" = "adobe",
"exp" = "#DateAdd("n", 30, now())#"
}>
<cfset encr = {
"keystore" = "test_encryption1.keystore",
"keystoreAlias" = "contentKey",
"keystorePassword" = "****",
"keypairPassword" = "****"
}>
<cfset c = {
"algorithm" = "RSA-OAEP",
"encryption" = "A128CBC-HS256"
}>
<cfset createjwe = CreateEncryptedJwt(text,encr,c)>
<cfdump var = "#createjwe#">
<cfset text = { "iss" = "a", "sub" = "b", "abcd" = "efgh", "aud" = "adobe", "exp" = "#DateAdd("n", 30, now())#" }> <cfset encr = { "keystore" = "test_encryption1.keystore", "keystoreAlias" = "contentKey", "keystorePassword" = "****", "keypairPassword" = "****" }> <cfset c = { "algorithm" = "RSA-OAEP", "encryption" = "A128CBC-HS256" }> <cfset createjwe = CreateEncryptedJwt(text,encr,c)> <cfdump var = "#createjwe#">
<cfset text = {
    "iss" = "a",
    "sub" = "b",
    "abcd" = "efgh",
    "aud" = "adobe",
    "exp" = "#DateAdd("n", 30, now())#"
}>
<cfset encr = {
    "keystore" = "test_encryption1.keystore",
    "keystoreAlias" = "contentKey",
    "keystorePassword" = "****",
    "keypairPassword" = "****"
}>
<cfset c = {
    "algorithm" = "RSA-OAEP",
    "encryption" = "A128CBC-HS256"
}>
<cfset createjwe = CreateEncryptedJwt(text,encr,c)>
<cfdump var = "#createjwe#">

Get help faster and easier

New user?