implemented oauth removed sas

This commit is contained in:
2021-11-18 11:28:20 +00:00
parent f798bf89b0
commit 201febfa73
2 changed files with 46 additions and 102 deletions
+5 -64
View File
@@ -72,56 +72,7 @@ const tokenConfig = {
}, },
}; };
export const getSASToken = () => { const azureHeaders = (access_token) => {
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;
// console.log("///////////////////////");
// console.log(
// "encodedURI:" + "https://" + m_ResourceURI + "/" + m_Path + "/"
// );
// console.log("WEBAPI_URL: " + WEBAPI_URL);
// console.log("m_ResourceURI: " + m_ResourceURI);
// console.log("m_Path: " + m_Path);
// console.log("m_SasKey: " + m_SasKey);
// console.log("m_SasKeyName: " + m_SasKeyName);
// console.log("token: " + token);
// console.log("///////////////////////");
return token;
};
const azureHeaders = (SASToken) => {
return { return {
headers: { headers: {
"OData-MaxVersion": "4.0", "OData-MaxVersion": "4.0",
@@ -129,12 +80,12 @@ const azureHeaders = (SASToken) => {
"Accept": "application/json", "Accept": "application/json",
"Prefer": 'odata.include-annotations="*",return=representation', "Prefer": 'odata.include-annotations="*",return=representation',
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": "Bearer " + SASToken, "Authorization": "Bearer " + access_token,
}, },
}; };
}; };
const azureHeadersPaged = (SASToken) => { const azureHeadersPaged = (access_token) => {
return { return {
headers: { headers: {
"OData-MaxVersion": "4.0", "OData-MaxVersion": "4.0",
@@ -143,7 +94,7 @@ const azureHeadersPaged = (SASToken) => {
"Prefer": "Prefer":
'odata.include-annotations="*",return=representation, odata.maxpagesize=10', 'odata.include-annotations="*",return=representation, odata.maxpagesize=10',
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": "Bearer " + SASToken, "Authorization": "Bearer " + access_token,
}, },
}; };
}; };
@@ -172,7 +123,6 @@ export const getBasicSearch = (token, searchString) => {
"') or contains(ticketnumber, '" + "') or contains(ticketnumber, '" +
searchString + searchString +
"')) and pinswg_appealcasetype ne null and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true"; "')) and pinswg_appealcasetype ne null and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true";
return axios return axios
.get( .get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
@@ -476,8 +426,6 @@ export const getSearchDocumentHistoryPaged = (documentID, pageNumber) => {
}; };
export const getSearchDocumentDetails1 = (incidentID) => { export const getSearchDocumentDetails1 = (incidentID) => {
//console.log(incidentID);
var token = getSASToken();
var queryUrl = var queryUrl =
"/api/proxy/pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + "/api/proxy/pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " +
incidentID + incidentID +
@@ -491,8 +439,6 @@ export const getSearchDocumentDetails1 = (incidentID) => {
}; };
export const getSearchDocumentDetails1Paged = (incidentID, pageNumber) => { export const getSearchDocumentDetails1Paged = (incidentID, pageNumber) => {
//console.log(incidentID);
var token = getSASToken();
var queryUrl = var queryUrl =
"/api/proxy/pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + "/api/proxy/pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " +
incidentID + incidentID +
@@ -509,8 +455,6 @@ export const getSearchDocumentDetails1Paged = (incidentID, pageNumber) => {
}; };
export const getSearchDocumentHistory1 = (documentID) => { export const getSearchDocumentHistory1 = (documentID) => {
//console.log(documentID);
var token = getSASToken();
var queryUrl = var queryUrl =
"/api/proxy/pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " + "/api/proxy/pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " +
documentID; documentID;
@@ -525,8 +469,6 @@ export const getSearchDocumentHistory1 = (documentID) => {
}; };
export const getSearchDocumentHistory1Paged = (documentID, pageNumber) => { export const getSearchDocumentHistory1Paged = (documentID, pageNumber) => {
//console.log(documentID);
var token = getSASToken();
var queryUrl = var queryUrl =
"/api/proxy/pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " + "/api/proxy/pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " +
documentID + documentID +
@@ -711,7 +653,7 @@ export const getAppealID = (
return axios return axios
.get( .get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeaders(getSASToken()) azureHeaders(getToken())
) )
.then((res) => { .then((res) => {
var appealID = res.data.value[0][primaryAttribute]; var appealID = res.data.value[0][primaryAttribute];
@@ -1036,7 +978,6 @@ export const getPortalModuleDetailsProxy = (
}; };
export const getEmailAccountCheck = (emailAddress) => { export const getEmailAccountCheck = (emailAddress) => {
let token = getSASToken();
var queryUrl = var queryUrl =
"/api/proxy/contacts?$filter=emailaddress1 eq '" + "/api/proxy/contacts?$filter=emailaddress1 eq '" +
emailAddress + emailAddress +
+41 -38
View File
@@ -11,42 +11,43 @@ const PROXY_RELAY_URL =
(process.env.RELAYPATH || "dev-pedw-hc") + (process.env.RELAYPATH || "dev-pedw-hc") +
"/"; "/";
const getSASToken = () => { const accessTokenEndpoint = process.env.ACCESS_TOKEN_ENDPOINT;
var m_ResourceURI = const tenantId = process.env.TENANT;
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( const tokenBody =
"https://" + m_ResourceURI + "/" + m_Path + "/" "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); const tokenConfig = {
var t1 = new Date(); headers: {
var expireInSeconds = "Content-Type": "application/x-www-form-urlencoded",
+(31 * 24 * 3600) + 3600 + (((t1.getTime() - t0.getTime()) / 1000) | 0); "Cache-Control": "no-cache",
},
};
//the line below is a hack that converts to UTF8 export const getToken = () => {
var plainSignature = JSON.parse( return axios
JSON.stringify(encodedResourceUri + "\n" + expireInSeconds) .post(
); `${accessTokenEndpoint}${tenantId}/oauth2/v2.0/token`,
tokenBody,
var hash = CryptoJS.HmacSHA256(plainSignature, m_SasKey); tokenConfig
var base64HashValue = CryptoJS.enc.Base64.stringify(hash); )
.then((res) => res.data)
var token = .then((data) => {
"SharedAccessSignature sr=" + return data;
encodedResourceUri + })
"&sig=" + .catch((error) => {
encodeURIComponent(base64HashValue) + console.log("error :", error.response);
"&se=" + // return error;
expireInSeconds + });
"&skn=" +
m_SasKeyName;
return token;
}; };
function checkProxyPath(queryPath) { function checkProxyPath(queryPath) {
@@ -70,7 +71,9 @@ function checkProxyPath(queryPath) {
export default async function ApiProxy(req, res) { export default async function ApiProxy(req, res) {
var data = JSON.stringify(req.body); 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; const isDocument = req.url.indexOf("/documents/download") > -1;
let hashCheckPath = isDocument let hashCheckPath = isDocument
@@ -95,7 +98,7 @@ export default async function ApiProxy(req, res) {
"Accept": "application/json", "Accept": "application/json",
"Prefer": "Prefer":
'odata.include-annotations="*",return=representation,odata.maxpagesize=10', 'odata.include-annotations="*",return=representation,odata.maxpagesize=10',
"Authorization": SASToken, "Authorization": "Bearer " + accessToken,
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
}; };
@@ -112,7 +115,7 @@ export default async function ApiProxy(req, res) {
"OData-Version": "4.0", "OData-Version": "4.0",
"Accept": "application/json", "Accept": "application/json",
"Prefer": 'odata.include-annotations="*",return=representation', "Prefer": 'odata.include-annotations="*",return=representation',
"Authorization": SASToken, "Authorization": "Bearer " + accessToken,
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
data: data, data: data,
@@ -130,7 +133,7 @@ export default async function ApiProxy(req, res) {
"OData-Version": "4.0", "OData-Version": "4.0",
"Accept": "application/json", "Accept": "application/json",
"Prefer": 'odata.include-annotations="*",return=representation', "Prefer": 'odata.include-annotations="*",return=representation',
"Authorization": SASToken, "Authorization": "Bearer " + accessToken,
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
responseType: "arraybuffer", responseType: "arraybuffer",
@@ -168,7 +171,7 @@ export default async function ApiProxy(req, res) {
}) })
.catch((error) => { .catch((error) => {
console.log("proxy response error", error); console.log("proxy response error", error);
// res.json(error); //res.json(error);
res.status(405).end(); res.status(405).end();
return resolve(); return resolve();
}); });