UNPKG

@coolgk/utils

Version:

javascript, typescript utility and wrapper functions and classes: array, string, base64, ampq, bcrypt, cache, captcha, csv, email, jwt, number, pdf, tmp, token, unit conversion, url params, session, form data, google sign in, facebook sign in

135 lines (133 loc) 5.28 kB
/*! * @package @coolgk/utils * @version 3.1.4 * @link https://github.com/coolgk/node-utils * @license MIT * @author Daniel Gong <daniel.k.gong@gmail.com> * * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ "use strict"; /*! * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const token_1 = require("@coolgk/token"); const jwt_1 = require("@coolgk/jwt"); const cookie_1 = require("cookie"); exports.SESSION_NAME = 'session'; exports.COOKIE_NAME = 'accessToken'; class Session extends token_1.Token { constructor(options) { const cookies = options.request.headers.cookie ? cookie_1.parse(options.request.headers.cookie) : {}; let token = cookies[exports.COOKIE_NAME]; if (!token && typeof (options.request.headers.authorization) === 'string') { token = options.request.headers.authorization.substr(7); } super({ token, redisClient: options.redisClient, expiry: options.expiry || 3600, prefix: exports.SESSION_NAME }); this._jwt = new jwt_1.Jwt({ secret: options.secret }); this._sessionToken = token; this._cookie = Object.assign({ path: '/' }, options.cookie, { maxAge: options.expiry }); this._response = options.response; } init(signature = {}) { return __awaiter(this, void 0, void 0, function* () { this._sessionToken = this._jwt.generate({ signature }); this.setToken(this._sessionToken); yield this._renewCacheAndCookie(); return this._sessionToken; }); } rotate(signature = {}) { return __awaiter(this, void 0, void 0, function* () { const sessionValues = yield this.getAll(); yield this.destroy(); const token = yield this.init(signature); for (const field in sessionValues) { yield this.set(field, sessionValues[field]); } return token; }); } start(signature = {}) { return __awaiter(this, void 0, void 0, function* () { if (!(yield this.verifyAndRenew(signature))) { return this.init(signature); } return this._sessionToken; }); } destroy() { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const destroyPromise = yield _super("destroy").call(this); if (this._response) { this._response.setHeader('Set-Cookie', cookie_1.serialize(exports.COOKIE_NAME, '', Object.assign({}, this._cookie, { maxAge: 0, expires: new Date(0) }))); } return destroyPromise; }); } verify(signature = {}) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const tokenData = this._verifyJwt(); if (!tokenData || !tokenData.data || JSON.stringify(tokenData.data.signature) !== JSON.stringify(signature)) { return false; } return yield _super("verify").call(this); }); } verifyAndRenew(signature = {}, expiry) { return __awaiter(this, void 0, void 0, function* () { if (yield this.verify(signature)) { yield this._renewCacheAndCookie(expiry); return true; } return false; }); } renew(expiry) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { if (this._verifyJwt() && (yield _super("verify").call(this))) { return this._renewCacheAndCookie(expiry); } return false; }); } _renewCacheAndCookie(expiry) { if (this._response) { this._response.setHeader('Set-Cookie', cookie_1.serialize(exports.COOKIE_NAME, this._sessionToken, expiry ? Object.assign({}, this._cookie, { maxAge: expiry }) : this._cookie)); } return super.renew(expiry); } _verifyJwt() { return this._jwt.verify(this._sessionToken); } } exports.Session = Session; exports.default = Session; function express(options) { return (request, response, next) => { request[options.requestFieldName || 'session'] = new Session(Object.assign({}, options, { request, response })); next(); }; } exports.express = express;