UNPKG

@bitblit/epsilon

Version:

Tiny adapter to simplify building API gateway Lambda APIS

90 lines 4.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.DaemonHandler = void 0; const logger_1 = require("@bitblit/ratchet/common/logger"); const not_found_error_1 = require("../../http/error/not-found-error"); const common_1 = require("@bitblit/ratchet/common"); /** * A helper class to simplify adding Ratchet "Daemon" handling to your application */ class DaemonHandler { //private groupSelectionFunction: DaemonGroupSelectionFunction; //private authorizer: DaemonAuthorizerFunction; /** * Initialize the Router */ constructor(daemon, inConfig) { this.daemon = daemon; this.inConfig = inConfig; this.config = inConfig || {}; this.config.authorizer = this.config.authorizer || _a.ALLOW_EVERYTHING_AUTHORIZER; this.config.groupSelector = this.config.groupSelector || ((evt) => Promise.resolve(daemon.defaultGroup)); this.config.fetchDaemonStatusByPublicTokenPathParameter = common_1.StringRatchet.trimToNull(this.config.fetchDaemonStatusByPublicTokenPathParameter) || 'publicToken'; this.config.fetchDaemonStatusPathParameter = common_1.StringRatchet.trimToNull(this.config.fetchDaemonStatusPathParameter) || 'key'; } // If you are going to map this function, be sure that your Daemon is setup with a JwtRatchet... fetchDaemonStatusByPublicToken(evt) { return __awaiter(this, void 0, void 0, function* () { // TODO: verify has access to this key const publicToken = evt.pathParameters[this.config.fetchDaemonStatusByPublicTokenPathParameter]; logger_1.Logger.info('Fetching daemon status for token: %s', publicToken); let rval = yield this.daemon.statFromPublicToken(publicToken); const canRead = rval ? yield this.config.authorizer(evt, rval) : false; rval = canRead ? rval : null; if (rval === null) { throw new not_found_error_1.NotFoundError('No such token : ' + publicToken); } return rval; }); } fetchDaemonStatus(evt) { return __awaiter(this, void 0, void 0, function* () { // TODO: verify has access to this key const daemonKey = evt.pathParameters[this.config.fetchDaemonStatusPathParameter]; logger_1.Logger.info('Fetching daemon status for : %s', daemonKey); let rval = yield this.daemon.stat(daemonKey); const canRead = rval ? yield this.config.authorizer(evt, rval) : false; rval = canRead ? rval : null; if (rval === null) { throw new not_found_error_1.NotFoundError('No such key : ' + daemonKey); } return rval; }); } listDaemonStatus(evt) { return __awaiter(this, void 0, void 0, function* () { const group = yield this.config.groupSelector(evt); const keys = yield this.daemon.list(group); const allowed = []; for (let i = 0; i < keys.length; i++) { const canRead = yield this.config.authorizer(evt, keys[i]); if (canRead) { allowed.push(keys[i]); } } // Token here for future expansion into pagination, not implemented yet const rval = { results: allowed, nextToken: null, }; return rval; }); } } exports.DaemonHandler = DaemonHandler; _a = DaemonHandler; DaemonHandler.ALLOW_EVERYTHING_AUTHORIZER = (evt, proc) => __awaiter(void 0, void 0, void 0, function* () { return true; }); //# sourceMappingURL=daemon-handler.js.map