jwt/sign.SignJWT
The SignJWT class is a utility for creating Compact JWS formatted JWT strings.
example
ESM import
import { SignJWT } from 'jose/jwt/sign'
example
CJS import
const { SignJWT } = require('jose/jwt/sign')
example
Deno import
import { SignJWT } from 'https://deno.land/x/jose@v3.18.0/jwt/sign.ts'
example
Usage
const jwt = await new SignJWT({ 'urn:example:claim': true })
.setProtectedHeader({ alg: 'ES256' })
.setIssuedAt()
.setIssuer('urn:example:issuer')
.setAudience('urn:example:audience')
.setExpirationTime('2h')
.sign(privateKey)
console.log(jwt)
-
ProduceJWT
↳
SignJWT
- setAudience
- setExpirationTime
- setIssuedAt
- setIssuer
- setJti
- setNotBefore
- setProtectedHeader
- setSubject
- sign
• new SignJWT(payload
)
Name | Type | Description |
---|---|---|
payload |
JWTPayload |
The JWT Claims Set object. |
ProduceJWT.constructor
▸ setAudience(audience
): SignJWT
Set "aud" (Audience) Claim.
Name | Type | Description |
---|---|---|
audience |
string | string [] |
"aud" (Audience) Claim value to set on the JWT Claims Set. |
ProduceJWT.setAudience
▸ setExpirationTime(input
): SignJWT
Set "exp" (Expiration Time) Claim.
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. |
ProduceJWT.setExpirationTime
▸ setIssuedAt(input?
): SignJWT
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. |
ProduceJWT.setIssuedAt
▸ setIssuer(issuer
): SignJWT
Set "iss" (Issuer) Claim.
Name | Type | Description |
---|---|---|
issuer |
string |
"Issuer" Claim value to set on the JWT Claims Set. |
ProduceJWT.setIssuer
▸ setJti(jwtId
): SignJWT
Set "jti" (JWT ID) Claim.
Name | Type | Description |
---|---|---|
jwtId |
string |
"jti" (JWT ID) Claim value to set on the JWT Claims Set. |
ProduceJWT.setJti
▸ setNotBefore(input
): SignJWT
Set "nbf" (Not Before) Claim.
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. |
ProduceJWT.setNotBefore
▸ setProtectedHeader(protectedHeader
): SignJWT
Sets the JWS Protected Header on the SignJWT object.
Name | Type | Description |
---|---|---|
protectedHeader |
JWSHeaderParameters |
JWS Protected Header. |
▸ setSubject(subject
): SignJWT
Set "sub" (Subject) Claim.
Name | Type | Description |
---|---|---|
subject |
string |
"sub" (Subject) Claim value to set on the JWT Claims Set. |
ProduceJWT.setSubject
▸ sign(key
, options?
): Promise
<string
>
Signs and returns the JWT.
Name | Type | Description |
---|---|---|
key |
KeyLike |
Private Key or Secret to sign the JWT with. |
options? |
SignOptions |
JWT Sign options. |
Promise
<string
>