moved channel automation code into helper

This commit is contained in:
Peter Morton 2023-12-20 23:16:39 -06:00
parent 9334bb6384
commit c3d5aef5e0
9 changed files with 107 additions and 87 deletions

7
package-lock.json generated
View File

@ -22,6 +22,7 @@
},
"devDependencies": {
"@types/d3": "^7.4.3",
"@types/jwk-to-pem": "^2.0.3",
"@types/jwt-decode": "^3.1.0",
"@vitejs/plugin-vue": "^4.2.3",
"@vue/eslint-config-prettier": "^8.0.0",
@ -1101,6 +1102,12 @@
"integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==",
"dev": true
},
"node_modules/@types/jwk-to-pem": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@types/jwk-to-pem/-/jwk-to-pem-2.0.3.tgz",
"integrity": "sha512-I/WFyFgk5GrNbkpmt14auGO3yFK1Wt4jXzkLuI+fDBNtO5ZI2rbymyGd6bKzfSBEuyRdM64ZUwxU1+eDcPSOEQ==",
"dev": true
},
"node_modules/@types/jwt-decode": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/@types/jwt-decode/-/jwt-decode-3.1.0.tgz",

View File

@ -25,6 +25,7 @@
},
"devDependencies": {
"@types/d3": "^7.4.3",
"@types/jwk-to-pem": "^2.0.3",
"@types/jwt-decode": "^3.1.0",
"@vitejs/plugin-vue": "^4.2.3",
"@vue/eslint-config-prettier": "^8.0.0",

View File

@ -0,0 +1,5 @@
export interface ChannelAutomationAPI {
host: string;
tenant: string;
authentication: string;
}

View File

@ -0,0 +1,3 @@
export interface JSONWebToken {
iss: string;
}

View File

@ -1,3 +0,0 @@
export interface SessionIdentiferProps {
sessionIdentifier: string;
}

View File

@ -1,54 +1,61 @@
import { apiBaseUrl } from "../app.config.js";
import type { SessionIdentiferProps } from "../@types/SessionIdentiferProps";
import type { ChannelAutomationAPI } from "../@types/ChannelAutomationAPI";
import { jwtDecode } from "jwt-decode";
import type { VueCookies } from "vue-cookies";
import { inject } from "vue";
export function getAuthKeyFromProperties(props: SessionIdentiferProps) {
let authKey;
export const COOKIE_PREFIX = "__Host-VRNTOTCT";
if (props.sessionIdentifier && props.sessionIdentifier.length > 0) {
authKey = props.sessionIdentifier;
} else {
throw new Error(
"_sessionIdentifier property not found (check query params)",
);
}
return authKey;
}
export function getTenantProperty(key: string, props: SessionIdentiferProps) {
return new Promise((resolve, reject) => {
if (!props) {
reject("no props provided for authentication");
return;
}
if (!key || key.length == 0) {
reject("no key provided");
return;
}
const authKey = getAuthKeyFromProperties(props);
console.log(`Fetching TPS Property [${key}]`);
fetch(`${apiBaseUrl}/tps/${key}?authKey=${authKey}`, {
credentials: "include", // fetch won't send cookies unless you set credentials
})
.then((response) => {
// check for error response
if (!response.ok) {
reject(response.body || response.statusText);
export function getChannnelAutomationAPI():
| Readonly<ChannelAutomationAPI>
| undefined {
const $cookies = inject<VueCookies>("$cookies");
if ($cookies) {
for (const cookieKey of $cookies.keys()) {
if (cookieKey.startsWith(COOKIE_PREFIX)) {
const authCookie = $cookies.get(cookieKey) as string;
const jwtDecoded = jwtDecode(authCookie);
if (jwtDecoded && jwtDecoded.iss) {
const issSplit = jwtDecoded.iss.split("/oidc-token-service/");
return {
host: issSplit[0],
tenant: issSplit[1],
authentication: authCookie,
};
}
response
.json()
.then((data: { data: { value: string } }) => {
console.log("Found Property:" + JSON.stringify(data));
resolve(data.data.value);
})
.catch((error) => {
console.error(error);
reject(error);
});
})
.catch((error) => {
console.error(error);
reject(error);
});
});
}
}
}
return undefined;
}
export function getTenantProperty(key?: string): string | undefined {
const channelAutomationAPI = getChannnelAutomationAPI();
if (!channelAutomationAPI) {
return undefined;
}
const headers = {
Authorization: `OIDC_id_token ${channelAutomationAPI.authentication}`,
"Content-Type": "application/json",
};
fetch(
`${channelAutomationAPI.host}/tenant-properties-service/${channelAutomationAPI.tenant}/properties?fields=name,value,lastModifiedDate,lastModifiedBy?q=${key}`,
{ headers },
)
.then(async (response) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const data = await response.json();
// check for error response
if (!response.ok) {
// get error message from body or default to response statusText
// const error = (data && data.message) || response.statusText;
return Promise.reject(response.statusText);
}
console.debug(data);
})
.catch((error) => {
console.error("There was an error!", error);
});
}

View File

@ -1,35 +1,38 @@
<script lang="ts" setup>
import { inject, onMounted, ref } from "vue";
import type { VueCookies } from "vue-cookies";
import type { JSONWebToken } from "../@types/JSONWebToken";
import type { ChannelAutomationAPI } from "../@types/ChannelAutomationAPI";
import { DataTable } from "@jobinsjp/vue3-datatable";
import "@jobinsjp/vue3-datatable/dist/style.css";
import {
COOKIE_PREFIX,
getChannnelAutomationAPI,
getTenantProperty,
} from "../helpers";
import { jwtDecode } from "jwt-decode";
const cookiePrefix = "__Host-VRNTOTCT";
const $cookies = inject<VueCookies>("$cookies");
const data = ref([{ name: "", value: "" }]);
const authCookie = ref("");
const jwtDecoded = ref("");
const tps = ref();
const channelAutomationAPI = ref({} as ChannelAutomationAPI | undefined);
const jwtDecoded = ref({} as JSONWebToken);
onMounted(() => {
if ($cookies) {
data.value = $cookies?.keys().map(function (value) {
if (value.startsWith(COOKIE_PREFIX)) {
jwtDecoded.value = jwtDecode($cookies.get(value) as string);
}
return {
name: value,
value: $cookies.get(value) as string,
};
});
$cookies.keys().forEach((key) => {
if (key.startsWith(cookiePrefix)) {
console.log(`Found ${key} cookie using value for token`);
authCookie.value = $cookies.get(key) as string;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
jwtDecoded.value = JSON.stringify(jwtDecode(authCookie.value), null, 2);
// console.log(`Decoded [${JSON.stringify(decoded, null, 2)}]`);
}
});
channelAutomationAPI.value = getChannnelAutomationAPI();
tps.value = getTenantProperty();
}
});
</script>
@ -39,8 +42,8 @@ onMounted(() => {
<DataTable :rows="data"></DataTable>
<h3>VRNTOTCT authentication</h3>
<p>OIDC Token is {{ authCookie }}</p>
<h3>JWT Decoded</h3>
<pre>{{ jwtDecoded }}</pre>
<pre>{{ JSON.stringify(jwtDecoded, null, 2) }}</pre>
<h3>API Connection from JWT Token</h3>
<pre>{{ channelAutomationAPI }}</pre>
</template>

View File

@ -7,14 +7,13 @@ import IntegrationCard from "../components/IntegrationCard.vue";
import DaVinciView from "./DaVinciView.vue";
import type { VueCookies } from "vue-cookies";
import type { SessionIdentiferProps } from "../@types/SessionIdentiferProps";
const $cookies = inject<VueCookies>("$cookies");
if ($cookies) {
const key = "__Host-VRNTOTCT404fdb6f";
$cookies.set(
key,
"eyJhbGciOiJSUzI1NiIsImtpZCI6IjM4MThlZDMxMmVkOGRhNTVkZWZkM2EzZmI0OGY1NjQzMWFjMWMwMmEzZjZkMmFkMjVjNDA5ZmEwOTA1NDU3ZTkiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOlsiZGVmYXVsdCJdLCJhenAiOiJkZWZhdWx0IiwiY29tLnZlcmludC5pc0V4dGVybmFsbHlBdXRoZW50aWNhdGVkIjpmYWxzZSwiY29tLnZlcmludC5sb2dpblJlc3VsdCI6MSwiY29tLnZlcmludC5zY3JlZW5IZWlnaHQiOiI5MTQiLCJjb20udmVyaW50LnNjcmVlbldpZHRoIjoiMTQxNCIsImNvbnRlbnRfZW50aXRsZW1lbnRzIjpbIlRlYW1FRDpjY2FnZW50dGVhbSIsIlRlYW1Sb2xlRUQ6Y2NhZ2VudHRlYW1yb2xlIiwiVGVhbUVEOkNsYWltcyIsIlJlZ2lzdGVyZWRVc2VyQ29udGVudCIsIlBhZ2VTZXRFeGVjdXRpb25FbnRpdGxlbWVudDAiXSwiY29udGV4dF9lbnRpdGxlbWVudHMiOlsiQ29udGV4dFVwZGF0ZXJBUElBY2Nlc3MiXSwiZW1fYXBpX2FjY2VzcyI6WyJlbWFpbF9vdXRnb2luZ19jcmVhdGUiLCJlbWFpbF9vdXRnb2luZ19mb3J3YXJkIiwiZW1haWxfb3V0Z29pbmdfcmVwbHkiLCJhY2Nlc3N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RlIiwiYWNjZXNzX2FnZW50X3JlYWQiLCJhY2Nlc3NfdGVuYW50X3Byb3BlcnRpZXNfdXBkYXQiLCJhY2Nlc3NfdGVuYW50X3Byb3BlcnRpZXNfYmF0Y2giLCJhY2Nlc3NfYXR0YWNobWVudF9kZWxldGUiLCJlbWFpbF9vdXRnb2luZ19jcmVhdGUiLCJwcmVmZXJlbmNlc19kZXNrdG9wbG9jYWxlIiwiY2FzZV91cGRhdGUiLCJwcmVmZXJlbmNlc19jb250ZW50bG9jYWxlIl0sImV4cCI6MTcwMzE1NjkzNywiaWF0IjoxNzAzMTIwOTM3LCJpc3MiOiJodHRwczovL2VtMjAudmVyaW50LmxpdmU6NDQzL29pZGMtdG9rZW4tc2VydmljZS9kZWZhdWx0IiwicmVhbG0iOiIvZGVmYXVsdCIsInN1YiI6ImFwaWNsaWVudCJ9.WQdvTn6sLKbEakxdWaXRiIXD-RAca1ts-dW3sF1WoXLCuzBVm-Zq7yfCjZ2OhT4qQzECSlRHco1oaHf8j-Mo21qnvexQnXhYO98A0FRsvB0vAefD5WWUlkynGDkfgvKuG69e_5L-QJpetdH_pvIgNg5Mr_wrNiMLPs6XLyi4x8u1ZRpVgseSJyrCnOFzAT54D3sJUi_SrZTDtCKQv2N91uCY0l5mu3CPosDqgIU1qBux_ZCWeyUab0vI-Zu6QLaduMaCsYBweZY8r52RGEP1MXBFY_p3apGmp7WRumVy9_Q0KYVw6PpIyqyLRbD0zU0C_NdujALojg9-kB1Yq19ZlA",
"1d",
"/",
undefined,
@ -56,20 +55,10 @@ onMounted(() => {
fetchData();
});
function setValueFromTPS(
reference: { value: string },
param: string,
props: SessionIdentiferProps,
) {
function setValueFromTPS(reference: { value: string }, param: string) {
if (param.startsWith("tps:")) {
const tpsProperty = param.substring(4, param.length);
getTenantProperty(tpsProperty, props)
.then((value: unknown) => {
reference.value = value as string;
})
.catch((err: Error) => {
errorMessage.value = `${err.message} for TPS property ${tpsProperty}`;
});
reference.value = getTenantProperty(tpsProperty) as string;
} else {
reference.value = param;
}
@ -79,9 +68,9 @@ function fetchData() {
//clear errors
errorMessage.value = "";
setValueFromTPS(transferSummaryValue, props.transferSummary, props);
setValueFromTPS(integrationCardTitleValue, props.integrationCardTitle, props);
setValueFromTPS(integrationCardDocValue, props.integrationCardDoc, props);
setValueFromTPS(transferSummaryValue, props.transferSummary);
setValueFromTPS(integrationCardTitleValue, props.integrationCardTitle);
setValueFromTPS(integrationCardDocValue, props.integrationCardDoc);
}
</script>

View File

@ -8,6 +8,14 @@ export default defineConfig(({ command }) => {
if (command === "build") {
return { plugins: [vue()], base: "/ca_vue_apps/dist" };
} else {
return { plugins: [eslintPlugin(), mkcert(), vue()] };
return {
cors: {
origin: "*",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
preflightContinue: false,
optionsSuccessStatus: 204,
},
plugins: [eslintPlugin(), mkcert(), vue()],
};
}
});