@midware/mauth
Version:
A simple auth middleware for Node.js
132 lines • 4.38 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const timers_1 = require("timers");
let bcrypt;
if (process.env.BCRYPT_USE_NODE?.toLocaleLowerCase() === 'true') {
bcrypt = require('bcrypt');
}
else {
bcrypt = require('bcryptjs');
}
const crypt_1 = __importDefault(require("../config/crypt"));
const _1 = require(".");
const utils_1 = require("./utils");
class Key {
async key() {
if (!this.host && this._publicKey) {
return this._publicKey;
}
else if (this._publicKey && this.host) {
if (!this.keyTimerRunning) {
(0, timers_1.setTimeout)(this.refreshKey.bind(this), process.env.AUTH_KEY_EXPIRES_IN_MS
? +process.env.AUTH_KEY_EXPIRES_IN_MS
: 15 * 60 * 1000);
this.keyTimerRunning = true;
}
return this._publicKey;
}
else {
return this.getKey();
}
}
async privateKey() {
return this._privateKey;
}
async getKey() {
const received = this.host
? (await (0, utils_1.sendGet)(this.host, '/signIn', undefined, await this.config()))
: undefined;
this._publicKey = received?.key;
return this._publicKey;
}
async refreshKey() {
this.keyTimerRunning = false;
await this.getKey();
}
constructor(crypt) {
this.configREST = {
headers: {
Type: 'application/json',
Accept: 'application/json',
},
};
this.host = process.env.AUTH_HOST;
this._privateKey = process.env.JWT_PRIVATE_KEY;
this._publicKey = process.env.JWT_PUBLIC_KEY;
this.credential = process.env.SERVICE_NAME && process.env.SERVICE_KEY
? {
type: 'SERVICE',
identification: process.env.SERVICE_NAME,
key: process.env.SERVICE_KEY,
}
: undefined;
this.keyTimerRunning = false;
this.crypt = crypt ? crypt : bcrypt;
}
static getInstance(crypt) {
if (!this._instance) {
this._instance = new this(crypt);
}
return this._instance;
}
async config() {
return {
...this.configREST,
headers: {
authorization: `Bearer ${await this.token()}`,
},
};
}
async getToken() {
const received = this.host && this.credential
? (await (0, utils_1.sendPost)(this.host, '/signIn', this.credential, this.configREST))
: undefined;
this.authToken = received?.token;
return this.authToken;
}
async refreshToken() {
this.tokenTimerRunning = false;
await this.getToken();
}
async token() {
if (this.authToken) {
if (!this.tokenTimerRunning) {
(0, timers_1.setTimeout)(this.refreshToken.bind(this), process.env.AUTH_TOKEN_EXPIRES_IN_MS
? +process.env.AUTH_TOKEN_EXPIRES_IN_MS
: 15 * 24 * 60 * 60 * 1000);
this.tokenTimerRunning = true;
}
return this.authToken;
}
else {
return this.getToken();
}
}
async verify(rIdentification, identifications) {
// console.log(rIdentification, identifications);
if (this.crypt === undefined) {
throw new Error('Crypt undefined');
}
for (const identification of identifications) {
if (rIdentification.key &&
identification.key &&
identification.identification === rIdentification.identification)
if (await this.crypt.compare(rIdentification.key, identification.key))
return;
}
const error = new _1.UnauthorizedError();
throw error;
}
async generateHash(key) {
// console.log('generateHash key:', key);
if (this.crypt === undefined) {
throw new Error('Crypt undefined');
}
return await this.crypt.hash(key, crypt_1.default.hashSaltRounds);
}
}
exports.default = Key;
//# sourceMappingURL=key.js.map