The EncryptJWT class is a utility for creating Compact JWE formatted JWT strings.
example
// ESM import
import EncryptJWT from 'jose/jwt/encrypt'
example
// CJS import
const { default: EncryptJWT } = require('jose/jwt/encrypt')
example
// usage
const secretKey = Uint8Array.from([
206, 203, 53, 165, 235, 214, 153, 188,
248, 225, 1, 132, 105, 204, 75, 42,
186, 185, 24, 223, 136, 66, 116, 59,
183, 155, 52, 52, 101, 167, 201, 85
])
const jwt = await new EncryptJWT({ 'urn:example:claim': true })
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
.setIssuedAt()
.setIssuer('urn:example:issuer')
.setAudience('urn:example:audience')
.setExpirationTime('2h')
.encrypt(secretKey)
console.log(jwt)
- encrypt
- replicateAudienceAsHeader
- replicateIssuerAsHeader
- replicateSubjectAsHeader
- setAudience
- setContentEncryptionKey
- setExpirationTime
- setInitializationVector
- setIssuedAt
- setIssuer
- setJti
- setKeyManagementParameters
- setNotBefore
- setProtectedHeader
- setSubject
+ new EncryptJWT(payload
: JWTPayload): EncryptJWT
Defined in src/lib/jwt_producer.ts:10
Name | Type | Description |
---|---|---|
payload |
JWTPayload | The JWT Claims Set object. |
Returns: EncryptJWT
▸ encrypt(key
: KeyLike, options?
: EncryptOptions): Promise<string>
Defined in src/jwt/encrypt.ts:160
Encrypts and returns the JWT.
Name | Type | Description |
---|---|---|
key |
KeyLike | Public Key or Secret to encrypt the JWT with. |
options? |
EncryptOptions | JWE Encryption options. |
Returns: Promise<string>
▸ replicateAudienceAsHeader(): this
Defined in src/jwt/encrypt.ts:149
Replicates the "aud" (Audience) Claim as a JWE Protected Header Parameter as per RFC7519#section-5.3.
Returns: this
▸ replicateIssuerAsHeader(): this
Defined in src/jwt/encrypt.ts:131
Replicates the "iss" (Issuer) Claim as a JWE Protected Header Parameter as per RFC7519#section-5.3.
Returns: this
▸ replicateSubjectAsHeader(): this
Defined in src/jwt/encrypt.ts:140
Replicates the "sub" (Subject) Claim as a JWE Protected Header Parameter as per RFC7519#section-5.3.
Returns: this
▸ setAudience(audience
: string | string[]): this
Defined in src/lib/jwt_producer.ts:47
Set "aud" (Audience) Claim.
Name | Type | Description |
---|---|---|
audience |
string | string[] | "aud" (Audience) Claim value to set on the JWT Claims Set. |
Returns: this
▸ setContentEncryptionKey(cek
: Uint8Array): this
Defined in src/jwt/encrypt.ts:103
Sets a content encryption key to use, by default a random suitable one is generated for the JWE enc" (Encryption Algorithm) Header Parameter. You do not need to invoke this method, it is only really intended for test and vector validation purposes.
Name | Type | Description |
---|---|---|
cek |
Uint8Array | JWE Content Encryption Key. |
Returns: this
▸ setExpirationTime(input
: number | string): this
Defined in src/lib/jwt_producer.ts:85
Set "exp" (Expiration Time) Claim.
Name | Type | Description |
---|---|---|
input |
number | string | "exp" (Expiration Time) Claim value to set on the JWT Claims Set. When number is passed that is used as a value, when string is passed it is resolved to a time span and added to the current timestamp. |
Returns: this
▸ setInitializationVector(iv
: Uint8Array): this
Defined in src/jwt/encrypt.ts:119
Sets the JWE Initialization Vector to use for content encryption, by default a random suitable one is generated for the JWE enc" (Encryption Algorithm) Header Parameter. You do not need to invoke this method, it is only really intended for test and vector validation purposes.
Name | Type | Description |
---|---|---|
iv |
Uint8Array | JWE Initialization Vector. |
Returns: this
▸ setIssuedAt(input?
: number): this
Defined in src/lib/jwt_producer.ts:100
Set "iat" (Issued At) Claim.
Name | Type | Description |
---|---|---|
input? |
number | "iat" (Issued At) Claim value to set on the JWT Claims Set. Default is current timestamp. |
Returns: this
▸ setIssuer(issuer
: string): this
Defined in src/lib/jwt_producer.ts:27
Set "iss" (Issuer) Claim.
Name | Type | Description |
---|---|---|
issuer |
string | "Issuer" Claim value to set on the JWT Claims Set. |
Returns: this
▸ setJti(jwtId
: string): this
Defined in src/lib/jwt_producer.ts:57
Set "jti" (JWT ID) Claim.
Name | Type | Description |
---|---|---|
jwtId |
string | "jti" (JWT ID) Claim value to set on the JWT Claims Set. |
Returns: this
▸ setKeyManagementParameters(parameters
: JWEKeyManagementHeaderParameters): this
Defined in src/jwt/encrypt.ts:87
Sets the JWE Key Management parameters to be used when encrypting. Use of this is method is really only needed for ECDH-ES based algorithms when utilizing the Agreement PartyUInfo or Agreement PartyVInfo parameters. Other parameters will always be randomly generated when needed and missing.
Name | Type | Description |
---|---|---|
parameters |
JWEKeyManagementHeaderParameters | JWE Key Management parameters. |
Returns: this
▸ setNotBefore(input
: number | string): this
Defined in src/lib/jwt_producer.ts:69
Set "nbf" (Not Before) Claim.
Name | Type | Description |
---|---|---|
input |
number | string | "nbf" (Not Before) Claim value to set on the JWT Claims Set. When number is passed that is used as a value, when string is passed it is resolved to a time span and added to the current timestamp. |
Returns: this
▸ setProtectedHeader(protectedHeader
: JWEHeaderParameters): this
Defined in src/jwt/encrypt.ts:71
Sets the JWE Protected Header on the EncryptJWT object.
Name | Type | Description |
---|---|---|
protectedHeader |
JWEHeaderParameters | JWE Protected Header. Must contain an "alg" (JWE Algorithm) and "enc" (JWE Encryption Algorithm) properties. |
Returns: this
▸ setSubject(subject
: string): this
Defined in src/lib/jwt_producer.ts:37
Set "sub" (Subject) Claim.
Name | Type | Description |
---|---|---|
subject |
string | "sub" (Subject) Claim value to set on the JWT Claims Set. |
Returns: this