UNPKG

@ords/modules

Version:

Modules for ords-core microservices based upon proposals

89 lines (88 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const rxjs_1 = require("rxjs"); let root = 'auth'; var requireCredentialsErros; (function (requireCredentialsErros) { requireCredentialsErros.MISSING_PASSWORD = 'password is missing'; requireCredentialsErros.MISSING_USERNAME = 'username is missing'; })(requireCredentialsErros = exports.requireCredentialsErros || (exports.requireCredentialsErros = {})); class RequireCredentials { // signup and patch validate that password is present signUp(request) { // tmp holder for pre this hook results from request so far let tmp = {}; // tmp holder handler to the observer that is gonna be extended let handler = undefined; // flag to indicate how many things are done let isDone = 0; // function that perform the next once something is done in this function let next = () => { // count one more thing done isDone += 1; // check if suffcient amount of thigns are done if (isDone === 2) { if (tmp.existing === undefined) { tmp.existing = {}; } tmp.existing.username = tmp.meta.username; // send next results handler.next(['existing', tmp.existing]); handler.complete(); } }; // check that username and password is provided request.package.subscribe((x) => { tmp[x[0]] = x[1]; }, () => { }, () => { if (tmp.meta.password === undefined) { throw new Error(requireCredentialsErros.MISSING_PASSWORD); } if (tmp.meta.username === false) { throw new Error(requireCredentialsErros.MISSING_USERNAME); } // send found existing query to be emittet next(); }); // now map the 'existing' field to contain correct values request.package = request.package.concat(rxjs_1.Observable.create((h) => { // set reference for handler handler = h; next(); })); return request; } ; signIn(request) { // flag for passoword has been found let passwordFound = false; // flag for username has been found let usernameFound = false; // perform md5 mapping for signin request.package.subscribe((val) => { // check if password is being passed if (val[0] == 'password') { passwordFound = true; } // check if username is being passed if (val[0] == 'username') { usernameFound = true; } }, () => { }, () => { if (passwordFound === false) { throw new Error(requireCredentialsErros.MISSING_PASSWORD); } if (usernameFound === false) { throw new Error(requireCredentialsErros.MISSING_USERNAME); } }); return request; } ; constructor(msr) { // bind hooks msr.addPreHook(root, '/signin/g', this.signIn.bind(this)); msr.addPreHook(root, '/signup|patch/g', this.signUp.bind(this)); } } exports.RequireCredentials = RequireCredentials;