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) => {
+3 -1
View File
@@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"dev:app": "node ./server/server.js && yarn lint:js",
"dev:app": "node ./server/server.js",
"build": "NODE_ENV=production node_modules/next/dist/bin/next build",
"start": "node_modules/next/dist/bin/next start"
},
@@ -14,11 +14,13 @@
"axios": "^0.21.1",
"compression": "^1.7.4",
"cookies": "^0.8.0",
"crypto-js": "^4.1.1",
"dom": "^0.0.3",
"dynamics-web-api": "^1.7.4",
"express": "^4.17.1",
"govuk-frontend": "^3.13.0",
"https": "^1.0.0",
"hyco-https": "^1.4.5",
"jsonpath": "^1.1.1",
"jsonpath-plus": "^5.1.0",
"lodash": "^4.17.21",
+9 -7
View File
@@ -12,7 +12,7 @@ import { useSelector, shallowEqual, connect } from "react-redux";
import { wrapper } from "../../store/store";
import Cookies from "cookies";
import { useRouter } from "next/router";
import { useState } from "react";
import { useEffect, useState } from "react";
import useTranslation from "next-translate/useTranslation";
import jsonpath from "jsonpath";
import { reduxForm, formValueSelector } from "redux-form";
@@ -26,10 +26,15 @@ import {
setAppealLPA,
getAppealTypeObj,
} from "../../store/appealType/action";
import { getAppealsTypes, getLPA } from "../../actions";
import { getAppealsTypes, getLPA, updateCase, createCase } from "../../actions";
import { setLPA } from "../../store/lpa/action";
const Home = (props) => {
// useEffect(() => {
// // updateCase("3ffcf47c-5811-ec11-aac9-00224800be9c", "846040004");
// createCase();
// });
const { footerLinks, pages, formData } = props;
let { t, lang } = useTranslation();
@@ -75,14 +80,11 @@ const Home = (props) => {
export const getServerSideProps = wrapper.getServerSideProps(
(store) =>
async ({ query, req, res }) => {
res.setHeader(
"Cache-Control",
"public, s-maxage=604800, stale-while-revalidate"
);
const [appealTypeData, lpaData] = await Promise.all([
const [appealTypeData, lpaData, caseData] = await Promise.all([
await getAppealsTypes(),
await getLPA(),
]);
store.dispatch(setAppealType(appealTypeData));
store.dispatch(setLPA(lpaData));
}