UNPKG

@intuitionrobotics/thunderstorm

Version:
88 lines 3.54 kB
/* * Thunderstorm is a full web app framework! * * Typescript & Express backend infrastructure that natively runs on firebase function * Typescript & React frontend infrastructure * * Copyright (C) 2020 Intuition Robotics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ImplementationMissingException, Module } from "@intuitionrobotics/ts-common"; import { HeaderKey } from "../server/HttpServer.js"; import { ApiException } from "../../exceptions.js"; import {} from "../server/server-api.js"; import {} from "../../utils/types.js"; export class RemoteProxy_Class extends Module { constructor() { super("RemoteProxy"); } async __queryRequestInfo(request) { let data; try { data = this.getProxyHeader(request); } catch (_e) { } return { key: this.getName(), data }; } getProxyHeader(request) { return this.proxyHeader.get(request); } getSecretHeader(request) { return this.secretHeader.get(request); } Middleware = async (request) => { const extras = this.assertSecret(request); return { extras, proxyId: this.getProxyHeader(request) }; }; secretHeader; proxyHeader; init() { if (!this.config) throw new ImplementationMissingException("MUST specify config for this module!!"); if (!this.config.secretHeaderName) this.config.secretHeaderName = 'x-secret'; if (!this.config.proxyHeaderName) this.config.proxyHeaderName = 'x-proxy'; this.secretHeader = new HeaderKey(this.config.secretHeaderName); this.proxyHeader = new HeaderKey(this.config.proxyHeaderName); } assertSecret(request) { if (!this.secretHeader || !this.proxyHeader) throw new ImplementationMissingException("MUST add RemoteProxy to your module list!!!"); const secret = this.getSecretHeader(request); const proxyId = this.getProxyHeader(request); const expectedSecret = this.config.remotes[proxyId]; if (!proxyId) throw new ApiException(403, `Missing proxy declaration in config for ${proxyId} !!`); if (!secret) throw new ApiException(403, `Missing secret !!`); if (!expectedSecret) throw new ApiException(403, `ProxyId '${proxyId}' is not registered for remote access !!`); if (expectedSecret.secret !== secret) throw new ApiException(403, `Secret does not match for proxyId: ${proxyId}`); const requestUrl = request.path; if (!expectedSecret.urls || !expectedSecret.urls.includes(requestUrl)) throw new ApiException(403, `Requested url '${requestUrl}' is not allowed from proxyId: ${proxyId}`); return expectedSecret.extras; } async processApi(request, _requestData) { return this.assertSecret(request); } } export const RemoteProxy = new RemoteProxy_Class(); //# sourceMappingURL=RemoteProxy.js.map