hashing proxy

This commit is contained in:
2021-11-16 14:18:59 +00:00
parent a7d6cf9386
commit c6d4743f73
3 changed files with 216 additions and 172 deletions
+50 -18
View File
@@ -2,6 +2,7 @@ import axios from "axios";
import https from "https";
import CryptoJS from "crypto-js";
import HmacSHA256 from "crypto-js/hmac-sha256";
import { rest } from "lodash";
const PROXY_RELAY_URL =
"https://" +
@@ -48,19 +49,25 @@ const getSASToken = () => {
return token;
};
function hashProxyPath(queryPath) {
const wordKey = process.env.HASHKEY;
function checkProxyPath(queryPath) {
var urlHasHash = queryPath.indexOf("&hash=");
//console.log(queryPath); //, urlHasHash, queryPath.split("&hash="));
const WORDKEY =
"028ffbae928d7191c0017f298260995f901d78eabde1436c0af0423459cc3715832136d84f68818d3a96399467b52143e273d4f3bf4ae190848891535421cd6c";
const wordKey = WORDKEY;
//console.log(CryptoJS.enc.Hex.parse(wordKey));
var hashlink = CryptoJS.HmacSHA256(
queryPath,
CryptoJS.enc.Hex.parse(wordKey)
);
hashlink = hashlink.toString(CryptoJS.enc.Base64);
hashlink = hashlink.toString();
hashlink = encodeURI(hashlink);
console.log("&hash=" + hashlink);
return queryPath; //+ "&hash=" + hashlink;
return hashlink;
}
export default async function ApiProxy(req, res) {
@@ -74,7 +81,9 @@ export default async function ApiProxy(req, res) {
var configNoData = {
method: req.method,
//url: PROXY_RELAY_URL + updateFormCollection + "(" + incidentId + ")",
url: PROXY_RELAY_URL + hashProxyPath(req.url.split("/api/proxy/")[1]),
url:
PROXY_RELAY_URL +
req.url.split("&hash=")[0].split("/api/proxy/")[1],
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
@@ -88,7 +97,7 @@ export default async function ApiProxy(req, res) {
var config = {
method: req.method,
url: PROXY_RELAY_URL + hashProxyPath(req.query.route[0]),
url: PROXY_RELAY_URL + req.query.route[0],
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
@@ -102,7 +111,9 @@ export default async function ApiProxy(req, res) {
var configDocument = {
method: req.method,
url: PROXY_RELAY_URL + hashProxyPath(req.url.split("/api/proxy/")[1]),
url:
PROXY_RELAY_URL +
req.url.split("&hash=")[0].split("/api/proxy/")[1],
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
@@ -115,15 +126,34 @@ export default async function ApiProxy(req, res) {
};
return new Promise((resolve, reject) => {
let apiPath = req.url.split("/api/proxy/")[1];
let hashCheckPath = req.url.split("&hash=")[0];
let hashCheckValue = req.url.split("&hash=")[1];
let hashFromRequest = checkProxyPath(hashCheckPath);
let apiPath = "";
// console.log(req.url);
console.log("Check:", hashCheckValue);
// if (hashCheckPath > 0) {
// hashCheckValue = req.url.split("&hash=")[1];
// apiPath = req.url.split("&hash=")[0].split("/api/proxy")[1];
// } else {
// apiPath = req.url.split("/api/proxy")[1];
// }
//console.log(hashCheckPath);
console.log("Calculated:", hashFromRequest);
//console.log(req.url.split("&hash=")[0].split("/api/proxy/")[1]);
//console.log(hashCheckPath);
// console.log("from proxy :", hashCheckValue);
// // console.log(req.url);
// // console.log(apiPath);
// console.log("hashing path", hashFromRequest);
let hasDocument = apiPath.indexOf("/download") > 0 ? true : false;
// console.log(
// "//////////////",
// req.url.split("/api/proxy/")[1],
// PROXY_RELAY_URL + req.query.route[0],
// req.method,
// hasDocument
// );
// hashCheckValue == hashFromRequest
//?
axios(
req.method == "GET"
? hasDocument
@@ -151,9 +181,11 @@ export default async function ApiProxy(req, res) {
})
.catch((error) => {
console.log("proxy response error", error);
res.json(error);
// res.json(error);
res.status(405).end();
return resolve();
});
// : res.status(405).end();
// return resolve();
});
}