9.1 KiB
Class: EncryptJWT
💗 Help the project
Support from the community to continue maintaining and improving this module is welcome. If you find the module useful, please consider supporting the project by becoming a sponsor.
The EncryptJWT class is used to build and encrypt Compact JWE formatted JSON Web Tokens.
Example
const secret = jose.base64url.decode('zH4NRP1HMALxxCFnRZABFA7GOJtzU_gIj02alfL1lvI')
const jwt = await new jose.EncryptJWT({ 'urn:example:claim': true })
.setProtectedHeader({ alg: 'dir', enc: 'A128CBC-HS256' })
.setIssuedAt()
.setIssuer('urn:example:issuer')
.setAudience('urn:example:audience')
.setExpirationTime('2h')
.encrypt(secret)
console.log(jwt)
Table of contents
Constructors
Methods
- encrypt
- replicateAudienceAsHeader
- replicateIssuerAsHeader
- replicateSubjectAsHeader
- setAudience
- setContentEncryptionKey
- setExpirationTime
- setInitializationVector
- setIssuedAt
- setIssuer
- setJti
- setKeyManagementParameters
- setNotBefore
- setProtectedHeader
- setSubject
Constructors
constructor
• new EncryptJWT(payload)
Parameters
| Name | Type | Description |
|---|---|---|
payload |
JWTPayload |
The JWT Claims Set object. |
Methods
encrypt
â–¸ encrypt(key, options?): Promise<string>
Encrypts and returns the JWT.
Parameters
| Name | Type | Description |
|---|---|---|
key |
Uint8Array | KeyLike |
Public Key or Secret to encrypt the JWT with. See Algorithm Key Requirements. |
options? |
EncryptOptions |
JWE Encryption options. |
Returns
Promise<string>
replicateAudienceAsHeader
â–¸ replicateAudienceAsHeader(): EncryptJWT
Replicates the "aud" (Audience) Claim as a JWE Protected Header Parameter.
Returns
See
replicateIssuerAsHeader
â–¸ replicateIssuerAsHeader(): EncryptJWT
Replicates the "iss" (Issuer) Claim as a JWE Protected Header Parameter.
Returns
See
replicateSubjectAsHeader
â–¸ replicateSubjectAsHeader(): EncryptJWT
Replicates the "sub" (Subject) Claim as a JWE Protected Header Parameter.
Returns
See
setAudience
â–¸ setAudience(audience): EncryptJWT
Set "aud" (Audience) Claim.
Parameters
| Name | Type | Description |
|---|---|---|
audience |
string | string[] |
"aud" (Audience) Claim value to set on the JWT Claims Set. |
Returns
setContentEncryptionKey
â–¸ setContentEncryptionKey(cek): EncryptJWT
Sets a content encryption key to use, by default a random suitable one is generated for the JWE enc" (Encryption Algorithm) Header Parameter.
Parameters
| Name | Type | Description |
|---|---|---|
cek |
Uint8Array |
JWE Content Encryption Key. |
Returns
Deprecated
You should not use this method. It is only really intended for test and vector validation purposes.
setExpirationTime
â–¸ setExpirationTime(input): EncryptJWT
Set "exp" (Expiration Time) Claim.
Parameters
| Name | Type | Description |
|---|---|---|
input |
string | number |
"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
setInitializationVector
â–¸ setInitializationVector(iv): EncryptJWT
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.
Parameters
| Name | Type | Description |
|---|---|---|
iv |
Uint8Array |
JWE Initialization Vector. |
Returns
Deprecated
You should not use this method. It is only really intended for test and vector validation purposes.
setIssuedAt
â–¸ setIssuedAt(input?): EncryptJWT
Set "iat" (Issued At) Claim.
Parameters
| Name | Type | Description |
|---|---|---|
input? |
number |
"iat" (Issued At) Claim value to set on the JWT Claims Set. Default is current timestamp. |
Returns
setIssuer
â–¸ setIssuer(issuer): EncryptJWT
Set "iss" (Issuer) Claim.
Parameters
| Name | Type | Description |
|---|---|---|
issuer |
string |
"Issuer" Claim value to set on the JWT Claims Set. |
Returns
setJti
â–¸ setJti(jwtId): EncryptJWT
Set "jti" (JWT ID) Claim.
Parameters
| Name | Type | Description |
|---|---|---|
jwtId |
string |
"jti" (JWT ID) Claim value to set on the JWT Claims Set. |
Returns
setKeyManagementParameters
â–¸ setKeyManagementParameters(parameters): EncryptJWT
Sets the JWE Key Management parameters to be used when encrypting. Use of this is method is really only needed for ECDH based algorithms when utilizing the Agreement PartyUInfo or Agreement PartyVInfo parameters. Other parameters will always be randomly generated when needed and missing.
Parameters
| Name | Type | Description |
|---|---|---|
parameters |
JWEKeyManagementHeaderParameters |
JWE Key Management parameters. |
Returns
setNotBefore
â–¸ setNotBefore(input): EncryptJWT
Set "nbf" (Not Before) Claim.
Parameters
| Name | Type | Description |
|---|---|---|
input |
string | number |
"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
setProtectedHeader
â–¸ setProtectedHeader(protectedHeader): EncryptJWT
Sets the JWE Protected Header on the EncryptJWT object.
Parameters
| Name | Type | Description |
|---|---|---|
protectedHeader |
CompactJWEHeaderParameters |
JWE Protected Header. Must contain an "alg" (JWE Algorithm) and "enc" (JWE Encryption Algorithm) properties. |
Returns
setSubject
â–¸ setSubject(subject): EncryptJWT
Set "sub" (Subject) Claim.
Parameters
| Name | Type | Description |
|---|---|---|
subject |
string |
"sub" (Subject) Claim value to set on the JWT Claims Set. |