@leismore/authappself_handler
Version:
An Express.js HTTP handler of authentication and authorization services for self-owned LMOS (NodeJS) applications.
59 lines (58 loc) • 1.78 kB
JavaScript
;
/**
* Generate authAppSelf_handler
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generator = void 0;
const author_1 = require("../lib/author");
const parse_httpAuth_1 = require("../lib/parse_httpAuth");
const unknown2error_1 = require("@leismore/unknown2error");
function generator(hostApp, authAppSelf_api, errors) {
function authAppSelf_handler(req, _res, next) {
let authenInputs;
let authorInputs;
// Parse HTTP Authorization header
try {
let httpAuthor = req.get('Authorization');
if (httpAuthor === undefined) {
next(errors.auth);
return;
}
else {
authenInputs = (0, parse_httpAuth_1.parse_httpAuth)(httpAuthor);
}
}
catch (e) {
const error = (0, unknown2error_1.unknown2error)(e);
errors.auth.addPrevious(error);
next(errors.auth);
return;
}
// Query auth_app_self
authorInputs = {
authen: authenInputs,
hostID: hostApp.hostID,
permission: hostApp.permission
};
(0, author_1.author)(authorInputs, authAppSelf_api).then(r => {
if (r.status === 403) {
next(errors.auth);
return;
}
if (r.data.result === true) {
next();
return;
}
else {
next(errors.auth);
return;
}
}).catch(e => {
errors.authAppSelf.addPrevious(e);
next(errors.authAppSelf);
return;
});
}
return authAppSelf_handler;
}
exports.generator = generator;