146 lines
4.6 KiB
JavaScript
146 lines
4.6 KiB
JavaScript
import axios from "axios";
|
|
import https from "https";
|
|
import CryptoJS from "crypto-js";
|
|
import { response } from "express";
|
|
|
|
const PROXY_RELAY_URL =
|
|
"https://" +
|
|
(process.env.RELAYURI || "dev-pedw-ns.servicebus.windows.net") +
|
|
"/" +
|
|
(process.env.RELAYPATH || "dev-pedw-hc") +
|
|
"/";
|
|
//"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
|
|
|
// const azureHeaders = {
|
|
// headers: {
|
|
// "OData-MaxVersion": "4.0",
|
|
// "OData-Version": "4.0",
|
|
// "Accept": "application/json",
|
|
// "Prefer": 'odata.include-annotations="*",return=representation',
|
|
// "Content-Type": "application/json",
|
|
// "ServiceBusAuthorization": SASToken,
|
|
// },
|
|
// };
|
|
|
|
const getSASToken = () => {
|
|
var m_ResourceURI =
|
|
process.env.RELAYURI || "dev-pedw-ns.servicebus.windows.net";
|
|
var m_Path = process.env.RELAYPATH || "dev-pedw-hc";
|
|
var m_SasKey =
|
|
process.env.SASKEY || "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
|
|
var m_SasKeyName = process.env.SASKEYNAME || "devpedwnspolicy";
|
|
|
|
var encodedResourceUri = encodeURIComponent(
|
|
"https://" + m_ResourceURI + "/" + m_Path + "/"
|
|
);
|
|
|
|
var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
|
|
var t1 = new Date();
|
|
var expireInSeconds =
|
|
+(31 * 24 * 3600) + 3600 + (((t1.getTime() - t0.getTime()) / 1000) | 0);
|
|
|
|
//the line below is a hack that converts to UTF8
|
|
var plainSignature = JSON.parse(
|
|
JSON.stringify(encodedResourceUri + "\n" + expireInSeconds)
|
|
);
|
|
|
|
var hash = CryptoJS.HmacSHA256(plainSignature, m_SasKey);
|
|
var base64HashValue = CryptoJS.enc.Base64.stringify(hash);
|
|
|
|
var token =
|
|
"SharedAccessSignature sr=" +
|
|
encodedResourceUri +
|
|
"&sig=" +
|
|
encodeURIComponent(base64HashValue) +
|
|
"&se=" +
|
|
expireInSeconds +
|
|
"&skn=" +
|
|
m_SasKeyName;
|
|
|
|
return token;
|
|
};
|
|
|
|
export default async function ApiProxy(req, res) {
|
|
console.log(req.method);
|
|
|
|
var data = JSON.stringify(req.body);
|
|
const SASToken = getSASToken();
|
|
|
|
var configNoData = {
|
|
method: req.method,
|
|
//url: PROXY_RELAY_URL + updateFormCollection + "(" + incidentId + ")",
|
|
url: PROXY_RELAY_URL + req.url.split("/api/proxy/")[1],
|
|
headers: {
|
|
"OData-MaxVersion": "4.0",
|
|
"OData-Version": "4.0",
|
|
"Accept": "application/json",
|
|
"Prefer": 'odata.include-annotations="*",return=representation',
|
|
"Authorization": SASToken,
|
|
"Content-Type": "application/json",
|
|
},
|
|
};
|
|
|
|
var config = {
|
|
method: req.method,
|
|
//url: PROXY_RELAY_URL + updateFormCollection + "(" + incidentId + ")",
|
|
url: PROXY_RELAY_URL + req.query.route[0],
|
|
headers: {
|
|
"OData-MaxVersion": "4.0",
|
|
"OData-Version": "4.0",
|
|
"Accept": "application/json",
|
|
"Prefer": 'odata.include-annotations="*",return=representation',
|
|
"Authorization": SASToken,
|
|
"Content-Type": "application/json",
|
|
},
|
|
data: data,
|
|
};
|
|
|
|
var configDocument = {
|
|
method: req.method,
|
|
url: PROXY_RELAY_URL + req.url.split("/api/proxy/")[1],
|
|
headers: {
|
|
"OData-MaxVersion": "4.0",
|
|
"OData-Version": "4.0",
|
|
"Accept": "application/json",
|
|
"Prefer": 'odata.include-annotations="*",return=representation',
|
|
"Authorization": SASToken,
|
|
"Content-Type": "application/json",
|
|
},
|
|
responseType: "arraybuffer",
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
let apiPath = req.url.split("/api/proxy/")[1];
|
|
let hasDocument = apiPath.indexOf("/download") > 0 ? true : false;
|
|
|
|
axios(
|
|
req.method == "GET"
|
|
? hasDocument
|
|
? configDocument
|
|
: configNoData
|
|
: config
|
|
)
|
|
.then((response) => {
|
|
// console.log(response);
|
|
res.statusCode = 200;
|
|
if (hasDocument) {
|
|
res.setHeader("Content-Type", "text/plain");
|
|
res.end(response.data);
|
|
} else {
|
|
res.setHeader("Content-Type", "application/json");
|
|
res.setHeader("Cache-Control", "max-age=1800000");
|
|
res.end(JSON.stringify(response.data));
|
|
}
|
|
|
|
//console.log("response data", response.data);
|
|
resolve();
|
|
})
|
|
.catch((error) => {
|
|
console.log("proxy response error", error);
|
|
res.json(error);
|
|
res.status(405).end();
|
|
return resolve();
|
|
});
|
|
});
|
|
}
|