astro-ghostcms/.pnpm-store/v3/files/ec/b6e23db447c7a296d9be47d0281...

81 lines
2.8 KiB
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProduceJWT = void 0;
const epoch_js_1 = require("../lib/epoch.js");
const is_object_js_1 = require("../lib/is_object.js");
const secs_js_1 = require("../lib/secs.js");
function validateInput(label, input) {
if (!Number.isFinite(input)) {
throw new TypeError(`Invalid ${label} input`);
}
return input;
}
class ProduceJWT {
_payload;
constructor(payload = {}) {
if (!(0, is_object_js_1.default)(payload)) {
throw new TypeError('JWT Claims Set MUST be an object');
}
this._payload = payload;
}
setIssuer(issuer) {
this._payload = { ...this._payload, iss: issuer };
return this;
}
setSubject(subject) {
this._payload = { ...this._payload, sub: subject };
return this;
}
setAudience(audience) {
this._payload = { ...this._payload, aud: audience };
return this;
}
setJti(jwtId) {
this._payload = { ...this._payload, jti: jwtId };
return this;
}
setNotBefore(input) {
if (typeof input === 'number') {
this._payload = { ...this._payload, nbf: validateInput('setNotBefore', input) };
}
else if (input instanceof Date) {
this._payload = { ...this._payload, nbf: validateInput('setNotBefore', (0, epoch_js_1.default)(input)) };
}
else {
this._payload = { ...this._payload, nbf: (0, epoch_js_1.default)(new Date()) + (0, secs_js_1.default)(input) };
}
return this;
}
setExpirationTime(input) {
if (typeof input === 'number') {
this._payload = { ...this._payload, exp: validateInput('setExpirationTime', input) };
}
else if (input instanceof Date) {
this._payload = { ...this._payload, exp: validateInput('setExpirationTime', (0, epoch_js_1.default)(input)) };
}
else {
this._payload = { ...this._payload, exp: (0, epoch_js_1.default)(new Date()) + (0, secs_js_1.default)(input) };
}
return this;
}
setIssuedAt(input) {
if (typeof input === 'undefined') {
this._payload = { ...this._payload, iat: (0, epoch_js_1.default)(new Date()) };
}
else if (input instanceof Date) {
this._payload = { ...this._payload, iat: validateInput('setIssuedAt', (0, epoch_js_1.default)(input)) };
}
else if (typeof input === 'string') {
this._payload = {
...this._payload,
iat: validateInput('setIssuedAt', (0, epoch_js_1.default)(new Date()) + (0, secs_js_1.default)(input)),
};
}
else {
this._payload = { ...this._payload, iat: validateInput('setIssuedAt', input) };
}
return this;
}
}
exports.ProduceJWT = ProduceJWT;