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
algorithmkey: Secret key for HMAC, and private key for ECDSA and RSApayloadClaimsoptions:headers: Custom headersexpiresIn: How long the JWT is valid for (forexpclaim)issuer:issclaimsubject:subclaimaudiences:audclaimsnotBefore:nbfclaimincludeIssuedTimestamp(default:false): Set totrueto includeiatclaimjwtId:jticlaim
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
});