createJWT()
Creates a new JWT. Claims are not included by default and must by defined with options
.
Definition
function createJWT(
algorithm: JWTAlgorithm,
key: ArrayBuffer | TypedArray,
payloadClaims: Record<any, any>,
options?: {
headers?: Record<any, any>;
expiresIn?: TimeSpan;
issuer?: string;
subject?: string;
audiences?: string[];
notBefore?: Date;
includeIssuedTimestamp?: boolean;
jwtId?: string;
}
): Promise<string>;
Parameters
algorithm
key
: Secret key for HMAC, and private key for ECDSA and RSApayloadClaims
options
:headers
: Custom headersexpiresIn
: How long the JWT is valid for (forexp
claim)issuer
:iss
claimsubject
:sub
claimaudiences
:aud
claimsnotBefore
:nbf
claimincludeIssuedTimestamp
(default:false
): Set totrue
to includeiat
claimjwtId
:jti
claim
Example
import { HMAC } from "oslo/crypto";
import { createJWT, validateJWT, parseJWT } from "oslo/jwt";
import { TimeSpan } from "oslo";
const secret = await new HMAC("SHA-256").generateKey();
const payload = {
message: "hello, world"
};
const jwt = await createJWT("HS256", secret, payload, {
headers: {
kid
},
expiresIn: new TimeSpan(30, "d"),
issuer,
subject,
audiences,
includeIssuedTimestamp: true
});