vulcain-corejs
Version:
Vulcain micro-service framework
102 lines (100 loc) • 5.02 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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());
});
};
const annotations_1 = require("../../di/annotations");
const conventions_1 = require("../../utils/conventions");
const system_1 = require("../../configurations/globals/system");
const jwt = require('jsonwebtoken');
const ms = require('ms');
let TokenService = class TokenService {
constructor() {
this.issuer = system_1.System.createSharedConfigurationProperty(conventions_1.Conventions.instance.ENV_TOKEN_ISSUER, "string");
this.tokenExpiration = system_1.System.createSharedConfigurationProperty(conventions_1.Conventions.instance.ENV_TOKEN_EXPIRATION, "string", conventions_1.Conventions.instance.defaultTokenExpiration);
this.secretKey = system_1.System.createSharedConfigurationProperty(conventions_1.Conventions.instance.ENV_VULCAIN_SECRET_KEY, "string", conventions_1.Conventions.instance.defaultSecretKey);
}
createTokenAsync(user) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const payload = {
value: {
user: {
displayName: user.displayName,
id: user.id,
email: user.email,
name: user.name,
tenant: user.tenant
},
scopes: user.scopes
}
};
let options = { issuer: this.issuer.value, expiresIn: this.tokenExpiration.value };
try {
let jwtToken = this.createToken(payload, options);
let renewToken = this.createToken({}, options);
let expiresIn;
if (typeof this.tokenExpiration.value === 'string') {
const milliseconds = ms(this.tokenExpiration.value);
expiresIn = Math.floor(milliseconds / 1000);
}
else {
expiresIn = this.tokenExpiration.value;
}
// token payload contains iat (absolute expiration date in sec)
resolve({ expiresIn, token: jwtToken, renewToken: renewToken });
}
catch (err) {
reject({ error: err, message: "Error when creating new token for user :" + user.name + " - " + (err.message || err) });
}
}));
}
createToken(payload, options) {
let token;
token = jwt.sign(payload, this.secretKey.value, options);
return token;
}
verifyTokenAsync(p) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
if (!p.token) {
reject("You must provided a valid token");
return;
}
let options = { "issuer": this.issuer.value };
try {
let key = this.secretKey.value;
//options.algorithms=[ALGORITHM];
jwt.verify(p.token, key, options, (err, payload) => {
if (err) {
reject(err);
}
else {
const token = payload.value;
resolve(token);
}
});
}
catch (err) {
reject({ error: err, message: "Invalid JWT token" });
}
}));
}
};
TokenService = __decorate([
annotations_1.Injectable(annotations_1.LifeTime.Singleton, annotations_1.DefaultServiceNames.TokenService),
__metadata("design:paramtypes", [])
], TokenService);
exports.TokenService = TokenService;
//# sourceMappingURL=tokenService.js.map
;