Skip to content

Latest commit

 

History

History
212 lines (125 loc) · 4.65 KB

jwt_sign.SignJWT.md

File metadata and controls

212 lines (125 loc) · 4.65 KB

Class: SignJWT

💗 Help the project

The SignJWT class is a utility for creating Compact JWS formatted JWT strings.

example Usage

const jwt = await new jose.SignJWT({ 'urn:example:claim': true })
  .setProtectedHeader({ alg: 'ES256' })
  .setIssuedAt()
  .setIssuer('urn:example:issuer')
  .setAudience('urn:example:audience')
  .setExpirationTime('2h')
  .sign(privateKey)

console.log(jwt)

Table of contents

Constructors

Methods

Constructors

constructor

new SignJWT(payload)

Parameters

Name Type Description
payload JWTPayload The JWT Claims Set object.

Methods

setAudience

setAudience(audience): SignJWT

Set "aud" (Audience) Claim.

Parameters

Name Type Description
audience string | string[] "aud" (Audience) Claim value to set on the JWT Claims Set.

Returns

SignJWT


setExpirationTime

setExpirationTime(input): SignJWT

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

SignJWT


setIssuedAt

setIssuedAt(input?): SignJWT

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

SignJWT


setIssuer

setIssuer(issuer): SignJWT

Set "iss" (Issuer) Claim.

Parameters

Name Type Description
issuer string "Issuer" Claim value to set on the JWT Claims Set.

Returns

SignJWT


setJti

setJti(jwtId): SignJWT

Set "jti" (JWT ID) Claim.

Parameters

Name Type Description
jwtId string "jti" (JWT ID) Claim value to set on the JWT Claims Set.

Returns

SignJWT


setNotBefore

setNotBefore(input): SignJWT

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

SignJWT


setProtectedHeader

setProtectedHeader(protectedHeader): SignJWT

Sets the JWS Protected Header on the SignJWT object.

Parameters

Name Type Description
protectedHeader JWTHeaderParameters JWS Protected Header. Must contain an "alg" (JWS Algorithm) property.

Returns

SignJWT


setSubject

setSubject(subject): SignJWT

Set "sub" (Subject) Claim.

Parameters

Name Type Description
subject string "sub" (Subject) Claim value to set on the JWT Claims Set.

Returns

SignJWT


sign

sign(key, options?): Promise<string>

Signs and returns the JWT.

Parameters

Name Type Description
key Uint8Array | KeyLike Private Key or Secret to sign the JWT with.
options? SignOptions JWT Sign options.

Returns

Promise<string>