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 axios from "axios";
import https from "https"; import https from "https";
import CryptoJS from "crypto-js";
//import {XMLHttpRequest} from 'xmlhttprequest'; //import {XMLHttpRequest} from 'xmlhttprequest';
@@ -16,42 +17,60 @@ if (process.browser) {
const API_PATH = "/api/data/v8.2/"; 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({ const agent = new https.Agent({
rejectUnauthorized: false, rejectUnauthorized: false,
}); });
// export const getAccessToken = () => { export const getSASToken = () => {
// var ns = "dev-pedw-ns.servicebus.windows.net"; // var ns = "dev-pedw-ns.servicebus.windows.net";
// path = "dev-pedw-hc"; // path = "dev-pedw-hc";
// keyrule = "devpedwnspolicy"; // keyrule = "devpedwnspolicy";
// key = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y="; // key = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
// let accessTokenHeader = { var m_ResourceURI = "dev-pedw-ns.servicebus.windows.net";
// headers: { var m_Path = "dev-pedw-hc";
// "ServiceBusAuthorization": https.createRelayToken( var m_SasKey = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
// https.createRelayHttpsUri(ns, path), var m_SasKeyName = "devpedwnspolicy";
// keyrule,
// key
// ),
// },
// };
// return axios var encodedResourceUri = encodeURIComponent(
// .get(ns + path, accessTokenHeader) "https://" + m_ResourceURI + "/" + m_Path + "/"
// .then((res) => { );
// console.log(res.data);
// return res.data; var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
// }) var t1 = new Date();
// .catch((error) => { var expireInSeconds =
// console.log("thiserror", error); +(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 = const accessToken =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSIsImtpZCI6IlVXelVaa0U3OEx0NVBGRkJ0LV9seVdoMjdjTSJ9.eyJhdWQiOiJodHRwczovL2RldmNybTIwMTYubGxjZHQuZ292LndhbGVzLyIsImlzcyI6Imh0dHA6Ly9mcy50ZXN0Lmdvdi53YWxlcy9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNjMxMjU4ODAzLCJuYmYiOjE2MzEyNTg4MDMsImV4cCI6MTYzMTI4NzYwMywidXBuIjpbIlJvYmVydC5Cb25kQHRlc3QuZ292LndhbGVzIiwicm9iZXJ0LmJvbmRAdGVzdC5nb3Yud2FsZXMiXSwicHJpbWFyeXNpZCI6IlMtMS01LTIxLTE4MDA4NDIwNzYtMjQ1NTYwNjU5LTQyMTU3NzU0NjktMTY2MjgiLCJ1bmlxdWVfbmFtZSI6IlhNXFxCb25kUiIsImFwcHR5cGUiOiJQdWJsaWMiLCJhcHBpZCI6IjQyN2ZhNzljLWI1ZmMtNDJhZC04M2Q0LTQxMGIwMzk0OGFiNiIsImF1dGhtZXRob2QiOiJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dz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1ZmMtNDJhZC04M2Q0LTQxMGIwMzk0OGFiNiIsImF1dGhtZXRob2QiOiJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvYXV0aGVudGljYXRpb25tZXRob2Qvd2luZG93cyIsImF1dGhfdGltZSI6IjIwMjEtMDktMTBUMDc6MjY6MzQuMjI0WiIsInZlciI6IjEuMCJ9.XFymIQJVavP-7-8V7LdoYDD4836Z-aSxveMQDD1P3y_9M3Tiy3zINiLxCbSr8JaymFOuLOzWcnNXwh7HHZKY6gDC6tD1ALVKCBXmgTDaoR9j7XAOxSUaRl6ArIecd7o6UcqsHAvfpb5zu-ZIy0KxV9PFRxY4JHF_tfLAeAMnqIBq-yDrNBOlbg4Kd9VuAbAK5lNWp-BiMIzMqZUIrmD9HjwE17SWaEnApCNqTcGZ4h2EqDBWgqKae7B1AzoV_7mfp7CscLUBLGdbM-XsXw3t_AVc0xDHoOMuWfsoMfioJONoMsPqTNUY7VQev1pMrFDEFN-jqP4yqSdNlev6H47SCg";
const SASToken = getSASToken();
const crmHeaders = { const crmHeaders = {
withCredentials: true, withCredentials: true,
headers: { 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) => { export const getBasicSearch = (searchString) => {
return axios return axios
.get( .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, '" + "incidents?$select=_accountid_value,_customerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value&$expand=primarycontactid($select=fullname)&$filter=contains(title, '" +
searchString + searchString +
"') and pinswg_appealcasetype ne null &$count=true", "') and pinswg_appealcasetype ne null &$count=true",
crmHeaders azureHeaders
) )
.then((res) => res.data) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -98,7 +128,7 @@ export const getBasicSearchDetails = (appealType, caseReference) => {
"?$filter=pinswg_name eq '" + "?$filter=pinswg_name eq '" +
caseReference + caseReference +
"'&$count=true", "'&$count=true",
crmHeaders azureHeaders
) )
.then((res) => res.data) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -115,7 +145,7 @@ export const getFormData = (whichForm) => {
"systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode%20eq%20%27pinswg_" + "systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode%20eq%20%27pinswg_" +
whichForm + whichForm +
"%27and%20type%20eq%202)&$count=true", "%27and%20type%20eq%202)&$count=true",
crmHeaders azureHeaders
) )
.then((res) => res.data) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -130,7 +160,7 @@ export const getMandatoryFields = (whichForm) => {
"EntityDefinitions(LogicalName='pinswg_" + "EntityDefinitions(LogicalName='pinswg_" +
whichForm + whichForm +
"')/Attributes?$count=true&$select=LogicalName,RequiredLevel", "')/Attributes?$count=true&$select=LogicalName,RequiredLevel",
crmHeaders azureHeaders
) )
.then((res) => res.data) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -145,7 +175,7 @@ export const getPickLists = (whichForm) => {
"EntityDefinitions(LogicalName='pinswg_" + "EntityDefinitions(LogicalName='pinswg_" +
whichForm + whichForm +
"')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet&$count=true", "')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet&$count=true",
crmHeaders azureHeaders
) )
.then((res) => res.data) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -158,7 +188,7 @@ export const getAppealsTypes = () => {
.get( .get(
WEBAPI_URL + WEBAPI_URL +
"stringmaps?$filter=attributename%20eq%20%27pinswg_appealcasetype%27&$count=true&$select=value,stringmapid,organizationid,attributevalue", "stringmaps?$filter=attributename%20eq%20%27pinswg_appealcasetype%27&$count=true&$select=value,stringmapid,organizationid,attributevalue",
crmHeaders azureHeaders
) )
.then((res) => res.data) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -271,7 +301,7 @@ export const getAppealTypeDetail = (caseReference, appealcasetype) => {
export const getLPA = () => { export const getLPA = () => {
console.log("api_root: ", BASE_URL); console.log("api_root: ", BASE_URL);
return axios return axios
.get(WEBAPI_URL + "accounts?$count=true&$select=name", crmHeaders) .get(WEBAPI_URL + "accounts?$count=true&$select=name", azureHeaders)
.then((res) => res.data) .then((res) => res.data)
.catch((error) => { .catch((error) => {
console.log("thi serror", error); console.log("thi serror", error);
@@ -309,9 +339,9 @@ export const createCase = (appealTypeId) => {
"OData-MaxVersion": "4.0", "OData-MaxVersion": "4.0",
"OData-Version": "4.0", "OData-Version": "4.0",
"Accept": "application/json", "Accept": "application/json",
"Prefer": "return=representation", "Prefer": 'odata.include-annotations="*",return=representation',
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": "Bearer " + accessToken, "ServiceBusAuthorization": SASToken,
}, },
data: data, data: data,
}; };
@@ -396,7 +426,7 @@ export const getMyCases = () => {
.get( .get(
WEBAPI_URL + 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", "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) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -414,7 +444,7 @@ export const getMyRepresentations = () => {
.get( .get(
WEBAPI_URL + 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", "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) .then((res) => res.data)
.catch((error) => { .catch((error) => {
@@ -433,7 +463,7 @@ export const getWatchedCases = () => {
.get( .get(
WEBAPI_URL + 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", "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) .then((res) => res.data)
.catch((error) => { .catch((error) => {
+4 -2
View File
@@ -4,7 +4,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev", "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", "build": "NODE_ENV=production node_modules/next/dist/bin/next build",
"start": "node_modules/next/dist/bin/next start" "start": "node_modules/next/dist/bin/next start"
}, },
@@ -14,11 +14,13 @@
"axios": "^0.21.1", "axios": "^0.21.1",
"compression": "^1.7.4", "compression": "^1.7.4",
"cookies": "^0.8.0", "cookies": "^0.8.0",
"crypto-js": "^4.1.1",
"dom": "^0.0.3", "dom": "^0.0.3",
"dynamics-web-api": "^1.7.4", "dynamics-web-api": "^1.7.4",
"express": "^4.17.1", "express": "^4.17.1",
"govuk-frontend": "^3.13.0", "govuk-frontend": "^3.13.0",
"https": "^1.0.0", "https": "^1.0.0",
"hyco-https": "^1.4.5",
"jsonpath": "^1.1.1", "jsonpath": "^1.1.1",
"jsonpath-plus": "^5.1.0", "jsonpath-plus": "^5.1.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
@@ -55,4 +57,4 @@
"eslint-config-next": "^11.0.1", "eslint-config-next": "^11.0.1",
"prettier": "2.3.2" "prettier": "2.3.2"
} }
} }
+9 -7
View File
@@ -12,7 +12,7 @@ import { useSelector, shallowEqual, connect } from "react-redux";
import { wrapper } from "../../store/store"; import { wrapper } from "../../store/store";
import Cookies from "cookies"; import Cookies from "cookies";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { useState } from "react"; import { useEffect, useState } from "react";
import useTranslation from "next-translate/useTranslation"; import useTranslation from "next-translate/useTranslation";
import jsonpath from "jsonpath"; import jsonpath from "jsonpath";
import { reduxForm, formValueSelector } from "redux-form"; import { reduxForm, formValueSelector } from "redux-form";
@@ -26,10 +26,15 @@ import {
setAppealLPA, setAppealLPA,
getAppealTypeObj, getAppealTypeObj,
} from "../../store/appealType/action"; } from "../../store/appealType/action";
import { getAppealsTypes, getLPA } from "../../actions"; import { getAppealsTypes, getLPA, updateCase, createCase } from "../../actions";
import { setLPA } from "../../store/lpa/action"; import { setLPA } from "../../store/lpa/action";
const Home = (props) => { const Home = (props) => {
// useEffect(() => {
// // updateCase("3ffcf47c-5811-ec11-aac9-00224800be9c", "846040004");
// createCase();
// });
const { footerLinks, pages, formData } = props; const { footerLinks, pages, formData } = props;
let { t, lang } = useTranslation(); let { t, lang } = useTranslation();
@@ -75,14 +80,11 @@ const Home = (props) => {
export const getServerSideProps = wrapper.getServerSideProps( export const getServerSideProps = wrapper.getServerSideProps(
(store) => (store) =>
async ({ query, req, res }) => { async ({ query, req, res }) => {
res.setHeader( const [appealTypeData, lpaData, caseData] = await Promise.all([
"Cache-Control",
"public, s-maxage=604800, stale-while-revalidate"
);
const [appealTypeData, lpaData] = await Promise.all([
await getAppealsTypes(), await getAppealsTypes(),
await getLPA(), await getLPA(),
]); ]);
store.dispatch(setAppealType(appealTypeData)); store.dispatch(setAppealType(appealTypeData));
store.dispatch(setLPA(lpaData)); store.dispatch(setLPA(lpaData));
} }