@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
114 lines (112 loc) • 4.25 kB
JavaScript
/*!
* @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 cache_1 = require("@coolgk/cache");
exports.DEFAULT_PREFIX = 'token';
var TokenError;
(function (TokenError) {
TokenError["INVALID_TOKEN"] = "INVALID_TOKEN";
TokenError["RESERVED_NAME"] = "RESERVED_NAME";
TokenError["EXPIRED_TOKEN"] = "EXPIRED_TOKEN";
})(TokenError = exports.TokenError || (exports.TokenError = {}));
class Token {
constructor(options) {
this._cache = options.redisClient ?
new cache_1.Cache(options) : options.cache;
this._expiry = options.expiry || 0;
this._prefix = options.prefix || exports.DEFAULT_PREFIX;
this.setToken(options.token);
}
renew(expiry) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._token) {
return { error: TokenError.INVALID_TOKEN };
}
if (expiry || expiry === 0) {
this._expiry = expiry;
}
yield this._cache.command('hset', this._name, '_timestamp', JSON.stringify(Date.now()));
return this._expiry ? this._cache.command('expire', this._name, this._expiry) : true;
});
}
set(name, value) {
return __awaiter(this, void 0, void 0, function* () {
if (name === '_timestamp') {
return { error: TokenError.RESERVED_NAME };
}
if (this._expiry && !(yield this.get('_timestamp'))) {
return { error: TokenError.EXPIRED_TOKEN };
}
return this._token ? this._cache.command('hset', this._name, name, JSON.stringify(value)) : { error: TokenError.INVALID_TOKEN };
});
}
verify() {
return __awaiter(this, void 0, void 0, function* () {
const timestamp = yield this.get('_timestamp');
return !!timestamp;
});
}
get(name) {
return __awaiter(this, void 0, void 0, function* () {
if (this._token) {
const value = yield this._cache.command('hget', this._name, name);
return JSON.parse(value);
}
return null;
});
}
destroy() {
this._token = '';
return this._cache.command('del', this._name);
}
delete(name) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._token) {
return { error: TokenError.INVALID_TOKEN };
}
return this._cache.command('hdel', this._name, name);
});
}
getAll() {
return __awaiter(this, void 0, void 0, function* () {
if (this._token) {
const values = yield this._cache.command('hgetall', this._name);
if (values) {
delete values._timestamp;
for (const property in values) {
values[property] = JSON.parse(values[property]);
}
return values;
}
}
return {};
});
}
setToken(token) {
this._token = token;
this._name = `${this._prefix}:${this._token}`;
}
}
exports.Token = Token;
exports.default = Token;