@intuitionrobotics/thunderstorm
Version:
101 lines • 4.62 kB
JavaScript
;
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.RemoteProxy = exports.RemoteProxy_Class = void 0;
/*
* 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.
*/
const ts_common_1 = require("@intuitionrobotics/ts-common");
const HttpServer_1 = require("../server/HttpServer");
const exceptions_1 = require("../../exceptions");
class RemoteProxy_Class extends ts_common_1.Module {
constructor() {
super("RemoteProxy");
this.Middleware = (request) => __awaiter(this, void 0, void 0, function* () {
const extras = this.assertSecret(request);
return { extras, proxyId: this.getProxyHeader(request) };
});
}
__queryRequestInfo(request) {
return __awaiter(this, void 0, void 0, function* () {
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);
}
init() {
if (!this.config)
throw new ts_common_1.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 HttpServer_1.HeaderKey(this.config.secretHeaderName);
this.proxyHeader = new HttpServer_1.HeaderKey(this.config.proxyHeaderName);
}
assertSecret(request) {
if (!this.secretHeader || !this.proxyHeader)
throw new ts_common_1.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 exceptions_1.ApiException(403, `Missing proxy declaration in config for ${proxyId} !!`);
if (!secret)
throw new exceptions_1.ApiException(403, `Missing secret !!`);
if (!expectedSecret)
throw new exceptions_1.ApiException(403, `ProxyId '${proxyId}' is not registered for remote access !!`);
if (expectedSecret.secret !== secret)
throw new exceptions_1.ApiException(403, `Secret does not match for proxyId: ${proxyId}`);
const requestUrl = request.path;
if (!expectedSecret.urls || !expectedSecret.urls.includes(requestUrl))
throw new exceptions_1.ApiException(403, `Requested url '${requestUrl}' is not allowed from proxyId: ${proxyId}`);
return expectedSecret.extras;
}
processApi(request, requestData) {
return __awaiter(this, void 0, void 0, function* () {
return this.assertSecret(request);
});
}
}
exports.RemoteProxy_Class = RemoteProxy_Class;
exports.RemoteProxy = new RemoteProxy_Class();
//# sourceMappingURL=RemoteProxy.js.map