updated translations and oauth
This commit is contained in:
+127
-86
@@ -1,7 +1,8 @@
|
||||
import axios from "axios";
|
||||
import https from "https";
|
||||
import CryptoJS from "crypto-js";
|
||||
import { response } from "express";
|
||||
import HmacSHA256 from "crypto-js/hmac-sha256";
|
||||
import { rest } from "lodash";
|
||||
|
||||
const PROXY_RELAY_URL =
|
||||
"https://" +
|
||||
@@ -10,74 +11,111 @@ const PROXY_RELAY_URL =
|
||||
(process.env.RELAYPATH || "dev-pedw-hc") +
|
||||
"/";
|
||||
|
||||
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";
|
||||
const accessTokenEndpoint = process.env.ACCESS_TOKEN_ENDPOINT;
|
||||
const tenantId = process.env.TENANT;
|
||||
|
||||
var encodedResourceUri = encodeURIComponent(
|
||||
"https://" + m_ResourceURI + "/" + m_Path + "/"
|
||||
);
|
||||
const tokenBody =
|
||||
"grant_type=" +
|
||||
process.env.GRANT_TYPE +
|
||||
"&client_id=" +
|
||||
process.env.CLIENT_ID +
|
||||
"&client_secret=" +
|
||||
process.env.CLIENT_SECRET +
|
||||
"&scope=" +
|
||||
"https://" +
|
||||
process.env.RELAYURI +
|
||||
"/.default";
|
||||
|
||||
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;
|
||||
const tokenConfig = {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Cache-Control": "no-cache",
|
||||
},
|
||||
};
|
||||
|
||||
export default async function ApiProxy(req, res) {
|
||||
//console.log(req.method);
|
||||
export const getToken = () => {
|
||||
return axios
|
||||
.post(
|
||||
`${accessTokenEndpoint}${tenantId}/oauth2/v2.0/token`,
|
||||
tokenBody,
|
||||
tokenConfig
|
||||
)
|
||||
.then((res) => res.data)
|
||||
.then((data) => {
|
||||
return data;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("error :", error.response);
|
||||
// return error;
|
||||
});
|
||||
};
|
||||
|
||||
function checkProxyPath(queryPath) {
|
||||
const WORDKEY = process.env.NEXT_PUBLIC_HASHKEY;
|
||||
|
||||
var hashPath =
|
||||
queryPath.indexOf("/api/proxy/") == -1
|
||||
? queryPath
|
||||
: queryPath.split("/api/proxy/")[1];
|
||||
|
||||
const wordKey = WORDKEY;
|
||||
var hashlink = CryptoJS.HmacSHA256(
|
||||
decodeURI(hashPath),
|
||||
CryptoJS.enc.Hex.parse(wordKey)
|
||||
);
|
||||
|
||||
hashlink = hashlink.toString(CryptoJS.enc.Hex);
|
||||
|
||||
return hashlink;
|
||||
}
|
||||
|
||||
export default async function ApiProxy(req, res) {
|
||||
var data = JSON.stringify(req.body);
|
||||
const SASToken = getSASToken();
|
||||
var accessToken = await getToken();
|
||||
accessToken = accessToken.access_token;
|
||||
|
||||
const isDocument = req.url.indexOf("/documents/download") > -1;
|
||||
|
||||
let hashCheckPath = isDocument
|
||||
? req.url.split("?hash=")[0]
|
||||
: req.url.split("&hash=")[0];
|
||||
let hashCheckValue = isDocument
|
||||
? req.url.split("?hash=")[1]
|
||||
: req.url.split("&hash=")[1];
|
||||
let hashFromRequest = checkProxyPath(hashCheckPath);
|
||||
|
||||
var configNoData = {
|
||||
method: req.method,
|
||||
//url: PROXY_RELAY_URL + updateFormCollection + "(" + incidentId + ")",
|
||||
url: PROXY_RELAY_URL + req.url.split("/api/proxy/")[1],
|
||||
url:
|
||||
PROXY_RELAY_URL +
|
||||
req.url.split("&hash=")[0].split("/api/proxy/")[1] +
|
||||
"&hash=" +
|
||||
req.url.split("&hash=")[1],
|
||||
headers: {
|
||||
"OData-MaxVersion": "4.0",
|
||||
"OData-Version": "4.0",
|
||||
"Accept": "application/json",
|
||||
"Prefer":
|
||||
'odata.include-annotations="*",return=representation,odata.maxpagesize=10',
|
||||
"Authorization": SASToken,
|
||||
"Authorization": "Bearer " + accessToken,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
var config = {
|
||||
method: req.method,
|
||||
url: PROXY_RELAY_URL + req.query.route[0],
|
||||
url:
|
||||
PROXY_RELAY_URL +
|
||||
req.query.route[0] +
|
||||
"&hash=" +
|
||||
req.url.split("&hash=")[1],
|
||||
headers: {
|
||||
"OData-MaxVersion": "4.0",
|
||||
"OData-Version": "4.0",
|
||||
"Accept": "application/json",
|
||||
"Prefer": 'odata.include-annotations="*",return=representation',
|
||||
"Authorization": SASToken,
|
||||
"Authorization": "Bearer " + accessToken,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
data: data,
|
||||
@@ -85,58 +123,61 @@ export default async function ApiProxy(req, res) {
|
||||
|
||||
var configDocument = {
|
||||
method: req.method,
|
||||
url: PROXY_RELAY_URL + req.url.split("/api/proxy/")[1],
|
||||
url:
|
||||
PROXY_RELAY_URL +
|
||||
req.url.split("?hash=")[0].split("/api/proxy/")[1] +
|
||||
"?hash=" +
|
||||
req.url.split("?hash=")[1],
|
||||
headers: {
|
||||
"OData-MaxVersion": "4.0",
|
||||
"OData-Version": "4.0",
|
||||
"Accept": "application/json",
|
||||
"Prefer": 'odata.include-annotations="*",return=representation',
|
||||
"Authorization": SASToken,
|
||||
"Authorization": "Bearer " + accessToken,
|
||||
"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;
|
||||
// console.log(
|
||||
// "//////////////",
|
||||
// req.url.split("/api/proxy/")[1],
|
||||
// PROXY_RELAY_URL + req.query.route[0],
|
||||
// req.method,
|
||||
// hasDocument
|
||||
// );
|
||||
axios(
|
||||
req.method == "GET"
|
||||
? hasDocument
|
||||
? configDocument
|
||||
: configNoData
|
||||
: config
|
||||
)
|
||||
.then((response) => {
|
||||
res.statusCode = 200;
|
||||
if (hasDocument) {
|
||||
res.setHeader(
|
||||
"Content-disposition",
|
||||
"attachment; filename=" +
|
||||
response.headers["content-disposition"].split(
|
||||
"filename="
|
||||
)[1]
|
||||
);
|
||||
res.end(response.data);
|
||||
} else {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
// res.setHeader("Cache-Control", "max-age=1800000");
|
||||
res.end(JSON.stringify(response.data));
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("proxy response error", error);
|
||||
res.json(error);
|
||||
res.status(405).end();
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
if (hashCheckValue == hashFromRequest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let hasDocument =
|
||||
hashCheckPath.indexOf("/download") > 0 ? true : false;
|
||||
|
||||
axios(
|
||||
req.method == "GET"
|
||||
? hasDocument
|
||||
? configDocument
|
||||
: configNoData
|
||||
: config
|
||||
)
|
||||
.then((response) => {
|
||||
res.statusCode = 200;
|
||||
if (hasDocument) {
|
||||
res.setHeader(
|
||||
"Content-disposition",
|
||||
"attachment; filename=" +
|
||||
response.headers["content-disposition"].split(
|
||||
"filename="
|
||||
)[1]
|
||||
);
|
||||
res.end(response.data);
|
||||
} else {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
// res.setHeader("Cache-Control", "max-age=1800000");
|
||||
res.end(JSON.stringify(response.data));
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("proxy response error", error);
|
||||
//res.json(error);
|
||||
res.status(405).end();
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.status(405).end();
|
||||
return resolve();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user