UNPKG

@bitblit/epsilon

Version:

Tiny adapter to simplify building API gateway Lambda APIS

58 lines 3.01 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApolloUtil = void 0; const event_util_1 = require("../../http/event-util"); const common_1 = require("@bitblit/ratchet/common"); const common_2 = require("@bitblit/ratchet/common"); const unauthorized_error_1 = require("../../http/error/unauthorized-error"); class ApolloUtil { // Prevent instantiation // eslint-disable-next-line @typescript-eslint/no-empty-function constructor() { } //const defaultContextFn: ContextFunction<[EpsilonLambdaApolloContextFunctionArgument], any> = async () => ({}); static emptyContext() { return __awaiter(this, void 0, void 0, function* () { return {}; }); } static defaultEpsilonApolloContext(args, jwt) { return __awaiter(this, void 0, void 0, function* () { const authTokenSt = event_util_1.EventUtil.extractBearerTokenFromEvent(args.lambdaEvent); let token = null; if (common_2.StringRatchet.trimToNull(authTokenSt) && jwt) { common_1.Logger.info('Got : %s', authTokenSt); token = yield jwt.decodeToken(authTokenSt, 0 /* ExpiredJwtHandling.RETURN_NULL */); } const rval = { user: token, bearerTokenString: authTokenSt, headers: args.lambdaEvent.headers, functionName: args.lambdaContext.functionName, lambdaEvent: args.lambdaEvent, lambdaContext: args.lambdaContext, }; return rval; }); } static nonRouteableOnlyEpsilonApolloContext(args, jwt) { return __awaiter(this, void 0, void 0, function* () { const hostName = common_2.StringRatchet.trimToNull(common_1.MapRatchet.extractValueFromMapIgnoreCase(args.lambdaEvent.headers, 'host')); const hostIsLocalOrNotRoutableIP4 = event_util_1.EventUtil.hostIsLocalOrNotRoutableIP4(hostName); if (!hostIsLocalOrNotRoutableIP4) { throw new unauthorized_error_1.UnauthorizedError('May only run local / non-routeable : ' + hostName); } return ApolloUtil.defaultEpsilonApolloContext(args, jwt); }); } } exports.ApolloUtil = ApolloUtil; //# sourceMappingURL=apollo-util.js.map