UNPKG

@63pokupki/nodejs-common

Version:
62 lines 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mJwtEncode = exports.mJwtDecode = exports.JwtAlgT = void 0; const jsonwebtoken_1 = require("jsonwebtoken"); var JwtAlgT; (function (JwtAlgT) { JwtAlgT["HS256"] = "HS256"; JwtAlgT["HS384"] = "HS384"; JwtAlgT["HS512"] = "HS512"; JwtAlgT["RS256"] = "RS256"; JwtAlgT["RS384"] = "RS384"; JwtAlgT["RS512"] = "RS512"; JwtAlgT["PS256"] = "PS256"; JwtAlgT["PS384"] = "PS384"; JwtAlgT["PS512"] = "PS512"; JwtAlgT["ES256"] = "ES256"; JwtAlgT["ES384"] = "ES384"; JwtAlgT["ES512"] = "ES512"; })(JwtAlgT = exports.JwtAlgT || (exports.JwtAlgT = {})); /** * Декодирование jwt токена * @param param - данные для расшифровки */ function mJwtDecode(param) { let decodedJwt = null; let dataJwt = null; try { decodedJwt = (0, jsonwebtoken_1.verify)(param.jwt, param.secret, { algorithms: [param.algorithm] }); } catch (e) { decodedJwt = null; } // Проверяем что прошло меньше месяца if (decodedJwt) { // Проверяем время жизни токена if (Date.now() / 1000 < decodedJwt.exp) { dataJwt = decodedJwt.data; } } return dataJwt; } exports.mJwtDecode = mJwtDecode; /** * jwt кодирование * @param param - данные для подписи */ function mJwtEncode(param) { let iDeviation = 0; if (param.deviation) { iDeviation = 60 * param.deviation; } const sJwt = (0, jsonwebtoken_1.sign)({ data: param.data, iat: Math.floor(Date.now() / 1000) - iDeviation, // создаетм jwt задним числом - 15 минут }, param.secret, { algorithm: param.algorithm, expiresIn: param.exp + iDeviation, // Длительность }); return sJwt; } exports.mJwtEncode = mJwtEncode; //# sourceMappingURL=JwtH.js.map