added sas token authorization for azure relay

This commit is contained in:
robert.bond@test.gov.wales
2021-09-13 11:43:29 +01:00
parent 62066285cb
commit 84f75c9db8
3 changed files with 80 additions and 46 deletions
+67 -37
View File
@@ -1,5 +1,6 @@
import axios from "axios";
import https from "https";
import CryptoJS from "crypto-js";
//import {XMLHttpRequest} from 'xmlhttprequest';
@@ -16,42 +17,60 @@ if (process.browser) {
const API_PATH = "/api/data/v8.2/";
const WEBAPI_URL = "https://devcrm2016.llcdt.gov.wales/DVPINS" + API_PATH;
//const WEBAPI_URL = "https://devcrm2016.llcdt.gov.wales/DVPINS" + API_PATH;
const WEBAPI_URL = "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
const agent = new https.Agent({
rejectUnauthorized: false,
});
// export const getAccessToken = () => {
// var ns = "dev-pedw-ns.servicebus.windows.net";
// path = "dev-pedw-hc";
// keyrule = "devpedwnspolicy";
// key = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
export const getSASToken = () => {
// var ns = "dev-pedw-ns.servicebus.windows.net";
// path = "dev-pedw-hc";
// keyrule = "devpedwnspolicy";
// key = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
// let accessTokenHeader = {
// headers: {
// "ServiceBusAuthorization": https.createRelayToken(
// https.createRelayHttpsUri(ns, path),
// keyrule,
// key
// ),
// },
// };
var m_ResourceURI = "dev-pedw-ns.servicebus.windows.net";
var m_Path = "dev-pedw-hc";
var m_SasKey = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
var m_SasKeyName = "devpedwnspolicy";
// return axios
// .get(ns + path, accessTokenHeader)
// .then((res) => {
// console.log(res.data);
// return res.data;
// })
// .catch((error) => {
// console.log("thiserror", error);
// });
// };
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;
};
const accessToken =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSIsImtpZCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSJ9.eyJhdWQiOiJodHRwczovL2RldmNybTIwMTYubGxjZHQuZ292LndhbGVzLyIsImlzcyI6Imh0dHA6Ly9mcy50ZXN0Lmdvdi53YWxlcy9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNjMxMjU4ODAzLCJuYmYiOjE2MzEyNTg4MDMsImV4cCI6MTYzMTI4NzYwMywidXBuIjpbIlJvYmVydC5Cb25kQHRlc3QuZ292LndhbGVzIiwicm9iZXJ0LmJvbmRAdGVzdC5nb3Yud2FsZXMiXSwicHJpbWFyeXNpZCI6IlMtMS01LTIxLTE4MDA4NDIwNzYtMjQ1NTYwNjU5LTQyMTU3NzU0NjktMTY2MjgiLCJ1bmlxdWVfbmFtZSI6IlhNXFxCb25kUiIsImFwcHR5cGUiOiJQdWJsaWMiLCJhcHBpZCI6IjQyN2ZhNzljLWI1ZmMtNDJhZC04M2Q0LTQxMGIwMzk0OGFiNiIsImF1dGhtZXRob2QiOiJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvYXV0aGVudGljYXRpb25tZXRob2Qvd2luZG93cyIsImF1dGhfdGltZSI6IjIwMjEtMDktMTBUMDc6MjY6MzQuMjI0WiIsInZlciI6IjEuMCJ9.XFymIQJVavP-7-8V7LdoYDD4836Z-aSxveMQDD1P3y_9M3Tiy3zINiLxCbSr8JaymFOuLOzWcnNXwh7HHZKY6gDC6tD1ALVKCBXmgTDaoR9j7XAOxSUaRl6ArIecd7o6UcqsHAvfpb5zu-ZIy0KxV9PFRxY4JHF_tfLAeAMnqIBq-yDrNBOlbg4Kd9VuAbAK5lNWp-BiMIzMqZUIrmD9HjwE17SWaEnApCNqTcGZ4h2EqDBWgqKae7B1AzoV_7mfp7CscLUBLGdbM-XsXw3t_AVc0xDHoOMuWfsoMfioJONoMsPqTNUY7VQev1pMrFDEFN-jqP4yqSdNlev6H47SCg";
const SASToken = getSASToken();
const crmHeaders = {
withCredentials: true,
headers: {
@@ -75,6 +94,17 @@ const crmHeadersReturn = {
},
};
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,
},
};
export const getBasicSearch = (searchString) => {
return axios
.get(
@@ -82,7 +112,7 @@ export const getBasicSearch = (searchString) => {
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=contains(title, '" +
searchString +
"') and pinswg_appealcasetype ne null &$count=true",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -98,7 +128,7 @@ export const getBasicSearchDetails = (appealType, caseReference) => {
"?$filter=pinswg_name eq '" +
caseReference +
"'&$count=true",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -115,7 +145,7 @@ export const getFormData = (whichForm) => {
"systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode%20eq%20%27pinswg_" +
whichForm +
"%27and%20type%20eq%202)&$count=true",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -130,7 +160,7 @@ export const getMandatoryFields = (whichForm) => {
"EntityDefinitions(LogicalName='pinswg_" +
whichForm +
"')/Attributes?$count=true&$select=LogicalName,RequiredLevel",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -145,7 +175,7 @@ export const getPickLists = (whichForm) => {
"EntityDefinitions(LogicalName='pinswg_" +
whichForm +
"')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet&$count=true",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -158,7 +188,7 @@ export const getAppealsTypes = () => {
.get(
WEBAPI_URL +
"stringmaps?$filter=attributename%20eq%20%27pinswg_appealcasetype%27&$count=true&$select=value,stringmapid,organizationid,attributevalue",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -271,7 +301,7 @@ export const getAppealTypeDetail = (caseReference, appealcasetype) => {
export const getLPA = () => {
console.log("api_root: ", BASE_URL);
return axios
.get(WEBAPI_URL + "accounts?$count=true&$select=name", crmHeaders)
.get(WEBAPI_URL + "accounts?$count=true&$select=name", azureHeaders)
.then((res) => res.data)
.catch((error) => {
console.log("thi serror", error);
@@ -309,9 +339,9 @@ export const createCase = (appealTypeId) => {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json",
"Prefer": "return=representation",
"Prefer": 'odata.include-annotations="*",return=representation',
"Content-Type": "application/json",
"Authorization": "Bearer " + accessToken,
"ServiceBusAuthorization": SASToken,
},
data: data,
};
@@ -396,7 +426,7 @@ export const getMyCases = () => {
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=pinswg_appealcasetype ne null &$count=true",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -414,7 +444,7 @@ export const getMyRepresentations = () => {
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=pinswg_appealcasetype ne null &$count=true",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {
@@ -433,7 +463,7 @@ export const getWatchedCases = () => {
.get(
WEBAPI_URL +
"incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=pinswg_appealcasetype ne null &$count=true",
crmHeaders
azureHeaders
)
.then((res) => res.data)
.catch((error) => {