zents
Version:
ZenTS is a Node.js & TypeScript MVC-Framework for building rich web applications, released as free and open-source software under the MIT License. It is designed for building web applications with modern tools and design patterns.
63 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JWT = void 0;
const jsonwebtoken_1 = require("jsonwebtoken");
const config_1 = require("../config/config");
const isObject_1 = require("../utils/isObject");
class JWT {
static async sign(payload) {
return new Promise((resolve, reject) => {
const options = Object.assign({}, this.getOptions(), { noTimestamp: true });
jsonwebtoken_1.sign(payload, config_1.config.security.secretKey, options, (err, jwt) => {
if (err) {
return reject(err);
}
return resolve(jwt);
});
});
}
static async verify(token) {
return new Promise((resolve) => {
const options = Object.assign({}, this.getOptions(), {
ignoreExpiration: true,
ignoreNotBefore: true,
});
jsonwebtoken_1.verify(token, config_1.config.security.secretKey, options, (err) => {
if (err) {
return resolve(false);
}
return resolve(true);
});
});
}
static decode(token) {
return jsonwebtoken_1.decode(token, {
json: true,
});
}
static getOptions() {
var _a, _b, _c, _d, _e, _f, _g;
const options = {};
if (isObject_1.isObject((_a = config_1.config.security) === null || _a === void 0 ? void 0 : _a.token)) {
const stringKeys = [
'issuer',
'algorithm',
'subject',
'jwtid',
'keyid',
];
for (const key of stringKeys) {
if (typeof ((_b = config_1.config.security) === null || _b === void 0 ? void 0 : _b.token[key]) === 'string') {
options[key] = (_c = config_1.config.security) === null || _c === void 0 ? void 0 : _c.token[key];
}
}
if (typeof ((_e = (_d = config_1.config.security) === null || _d === void 0 ? void 0 : _d.token) === null || _e === void 0 ? void 0 : _e.audience) === 'string' ||
Array.isArray((_g = (_f = config_1.config.security) === null || _f === void 0 ? void 0 : _f.token) === null || _g === void 0 ? void 0 : _g.audience)) {
options.audience = config_1.config.security.token.audience;
}
}
return options;
}
}
exports.JWT = JWT;
//# sourceMappingURL=JWT.js.map