5.9 KiB
Class: 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 { default: SignJWT } = require('jose/jwt/sign')
example
// usage
import parseJwk from 'jose/jwk/parse'
const privateKey = await parseJwk({
alg: 'ES256',
crv: 'P-256',
kty: 'EC',
d: 'VhsfgSRKcvHCGpLyygMbO_YpXc7bVKwi12KQTE4yOR4',
x: 'ySK38C1jBdLwDsNWKzzBHqKYEE5Cgv-qjWvorUXk9fw',
y: '_LeQBw07cf5t57Iavn4j-BqJsAD1dpoz8gokd3sBsOo'
})
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)
Index
Constructors
Methods
- setAudience
- setExpirationTime
- setIssuedAt
- setIssuer
- setJti
- setNotBefore
- setProtectedHeader
- setSubject
- sign
Constructors
constructor
+ new SignJWT(payload: JWTPayload): SignJWT
Defined in src/lib/jwt_producer.ts:10
Parameters:
| Name | Type | Description |
|---|---|---|
payload |
JWTPayload | The JWT Claims Set object. |
Returns: SignJWT
Methods
setAudience
▸ setAudience(audience: string | string[]): this
Defined in src/lib/jwt_producer.ts:47
Set "aud" (Audience) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
audience |
string | string[] | "aud" (Audience) Claim value to set on the JWT Claims Set. |
Returns: this
setExpirationTime
▸ setExpirationTime(input: number | string): this
Defined in src/lib/jwt_producer.ts:85
Set "exp" (Expiration Time) Claim.
Parameters:
| 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
setIssuedAt
▸ setIssuedAt(input?: number): this
Defined in src/lib/jwt_producer.ts:100
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: this
setIssuer
▸ setIssuer(issuer: string): this
Defined in src/lib/jwt_producer.ts:27
Set "iss" (Issuer) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
issuer |
string | "Issuer" Claim value to set on the JWT Claims Set. |
Returns: this
setJti
▸ setJti(jwtId: string): this
Defined in src/lib/jwt_producer.ts:57
Set "jti" (JWT ID) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
jwtId |
string | "jti" (JWT ID) Claim value to set on the JWT Claims Set. |
Returns: this
setNotBefore
▸ setNotBefore(input: number | string): this
Defined in src/lib/jwt_producer.ts:69
Set "nbf" (Not Before) Claim.
Parameters:
| 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
▸ setProtectedHeader(protectedHeader: JWSHeaderParameters): this
Defined in src/jwt/sign.ts:57
Sets the JWS Protected Header on the SignJWT object.
Parameters:
| Name | Type | Description |
|---|---|---|
protectedHeader |
JWSHeaderParameters | JWS Protected Header. |
Returns: this
setSubject
▸ setSubject(subject: string): this
Defined in src/lib/jwt_producer.ts:37
Set "sub" (Subject) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
subject |
string | "sub" (Subject) Claim value to set on the JWT Claims Set. |
Returns: this
sign
▸ sign(key: KeyLike, options?: SignOptions): Promise<string>
Defined in src/jwt/sign.ts:68
Signs and returns the JWT.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
KeyLike | Private Key or Secret to sign the JWT with. |
options? |
SignOptions | JWT Sign options. |
Returns: Promise<string>