@midware/mauth
Version:
A simple auth middleware for Node.js
174 lines • 6.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const permission_1 = __importDefault(require("./permission"));
const jsonWebToken_1 = __importDefault(require("./jsonWebToken"));
const unauthorizedError_1 = __importDefault(require("./util/unauthorizedError"));
class Mauth {
// eslint-disable-next-line @typescript-eslint/no-empty-function
constructor(getPersonAndIdentifications, verify, keyless, crypt) {
this.getPersonAndIdentifications = getPersonAndIdentifications;
this.verify = verify;
this.keyless = keyless;
this.crypt = crypt;
}
static getBearerAuthentication(bearer) {
const newBearer = bearer
? bearer.includes('Bearer ')
? bearer.replace('Bearer ', '')
: bearer.includes('Bearer')
? bearer.replace('Bearer', '')
: bearer
: bearer;
return newBearer && newBearer.length > 0 ? newBearer : undefined;
}
static checkAuthentication(req) {
return (req?.headers?.authorization ||
req?.headers?.Authorization ||
req?.headers?.token ||
req?.headers?.Token ||
req?.query?.token ||
req?.authorization ||
req?.auth);
}
static getAuthentication(req) {
const bearer = Mauth.getBearerAuthentication(Mauth.checkAuthentication(req));
return bearer;
}
async selfRestriction(req, _res,
// eslint-disable-next-line no-unused-vars
fn) {
if (req.authorization) {
try {
const auth = await jsonWebToken_1.default.getInstance(this.crypt).verify(req.authorization);
// console.log('authentication', auth);
if ((req.query && req.query.id === auth.id) ||
(req.params && req.params.filter === auth.id) ||
(req.params && req.params.filter && req.params.filter.id === auth.id))
await fn(auth);
else {
const error = new Error('Missing ID or Wrong ID.');
error.name = 'Unauthorized';
await fn(error);
}
}
catch (error) {
// console.log('Error NAME:' + error.name);
error.name = 'Unauthorized';
await fn(error);
}
}
else {
const error = new Error('Missing Credentials.');
error.name = 'Unauthorized';
await fn(error);
}
}
async signIn(identification, headers) {
let personAndIdentifications;
let person;
let identifications;
try {
if (this.getPersonAndIdentifications)
personAndIdentifications = await this.getPersonAndIdentifications(identification);
person = personAndIdentifications.person;
identifications = personAndIdentifications.identifications;
}
catch (error) {
error = new Error('Unauthorized');
error.name = 'Unauthorized';
throw error;
}
if (this.verify)
await this.verify[identification.type](identification, identifications, headers);
const cleanPerson = person?.receivedItem
? JSON.parse(JSON.stringify(person?.receivedItem))
: undefined;
if (cleanPerson) {
delete cleanPerson?.instances;
delete cleanPerson?.identifications;
cleanPerson.identification = identifications;
}
if (identification?.type?.toLocaleUpperCase() === 'KEYLESS') {
this.keyless?.[identification?.subType?.toLocaleUpperCase() || '']?.(cleanPerson, identification);
return { identification: [identification] };
}
return cleanPerson;
}
async checkToken(req, _res,
// eslint-disable-next-line no-unused-vars
fn) {
req.authorization = Mauth.getAuthentication(req);
if (req.authorization) {
try {
const auth = await jsonWebToken_1.default.getInstance(this.crypt).verify(req.authorization);
req.permissions = auth.permissions;
await fn(auth);
}
catch (error) {
error.name = 'Unauthorized';
await fn(error);
}
}
else {
await fn(new unauthorizedError_1.default('Missing Credentials.'));
}
}
async authentication(req, res,
// eslint-disable-next-line no-unused-vars
fn) {
if (Mauth.checkAuthentication(req)) {
await this.checkToken(req, res, fn);
}
else if (req.body?.identification) {
const identification = req.body;
try {
const person = (await this.signIn(identification, req.headers));
if (person.identification?.[0]?.type?.toLocaleUpperCase() === 'KEYLESS') {
console.log('person KEYLESS', person);
await fn(person);
return;
}
person.identification = person.identification.map((id) => {
delete id.key;
delete id.id;
return id;
});
if (!req.headers)
req.headers = { tokenid: '', picture: '' };
req.headers.authorization = await jsonWebToken_1.default.getInstance(this.crypt).sign(person, identification.type);
await fn(person);
}
catch (error) {
await fn(error);
}
}
else {
await fn(new unauthorizedError_1.default('Missing Credentials.'));
}
}
async permission(req, _res,
// eslint-disable-next-line no-unused-vars
fn) {
if (req.event && req.permissions) {
try {
const permission = await permission_1.default.getInstance().permission(req.event, req.permissions);
fn(permission);
}
catch (error) {
error.name = 'Unauthorized';
await fn(error);
}
}
else {
const error = new Error('Missing Permissions.');
error.name = 'Unauthorized';
await fn(error);
}
}
}
exports.default = Mauth;
//# sourceMappingURL=mauth.js.map