From 286c5c16276d09e2d8a3455a02d7be88e76605c5 Mon Sep 17 00:00:00 2001 From: Gregor Wolf Date: Fri, 19 Jan 2024 16:33:29 +0100 Subject: [PATCH] implement call to SAP Graph --- app/config.js | 4 +-- app/msal-2/authPopup.js | 16 ++++++++++++ app/msal-2/bookshopConfig.js | 5 ++++ app/msal-2/index.html | 7 ++++++ package.json | 7 ++++++ srv/catalog-service.cds | 5 ++-- srv/catalog-service.js | 49 ++++++++++++++++++++++++++++++------ 7 files changed, 82 insertions(+), 11 deletions(-) diff --git a/app/config.js b/app/config.js index 57746b6..f86343a 100644 --- a/app/config.js +++ b/app/config.js @@ -1,6 +1,6 @@ // Azure AD B2C sample endpoint const clientId = "e760cab2-b9a1-4c0d-86fb-ff7084abd902"; -const azureADconfig = { +const azureADconfigX = { clientId: clientId, scopes: [clientId], authority: @@ -16,7 +16,7 @@ const azureADconfigCSWb2c = { }; // CSWEntraID const clientIdCSWEntraID = "19702d90-5fb8-4d76-a6c9-557f55c771e8"; -const azureADconfigCSWEntraID = { +const azureADconfig = { clientId: clientIdCSWEntraID, scopes: ["openid","profile","api://19702d90-5fb8-4d76-a6c9-557f55c771e8/SAP.ReadWrite"], authority: diff --git a/app/msal-2/authPopup.js b/app/msal-2/authPopup.js index 14fb3e8..78a98ca 100644 --- a/app/msal-2/authPopup.js +++ b/app/msal-2/authPopup.js @@ -147,4 +147,20 @@ function getBTPJWT() { }); } +function readProductsFromSAPGraph() { + getTokenPopup(tokenRequest) + .then((response) => { + callBookshop( + bookshopConfig.readProductsFromSAPGraphEndpoint.method, + bookshopConfig.readProductsFromSAPGraphEndpoint.path, + bookshopConfig.readProductsFromSAPGraphEndpoint?.body, + response.accessToken, + updateUI + ); + }) + .catch((error) => { + console.error(error); + }); +} + selectAccount(); diff --git a/app/msal-2/bookshopConfig.js b/app/msal-2/bookshopConfig.js index b305c5e..4aee8ac 100644 --- a/app/msal-2/bookshopConfig.js +++ b/app/msal-2/bookshopConfig.js @@ -6,4 +6,9 @@ const bookshopConfig = { method: "POST", body: "{}", }, + readProductsFromSAPGraphEndpoint: { + path: "/odata/v4/catalog/readProductsFromSAPGraph", + method: "POST", + body: "{}", + }, }; diff --git a/app/msal-2/index.html b/app/msal-2/index.html index 8584596..cb9ce6d 100644 --- a/app/msal-2/index.html +++ b/app/msal-2/index.html @@ -84,6 +84,13 @@
> get BTP JWT + diff --git a/package.json b/package.json index a31b4a3..9693365 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,13 @@ "max": 10 } }, + "SAPGraph": { + "kind": "rest", + "credentials": { + "destination": "SAPGraph", + "requestTimeout": 30000 + } + }, "uaa": { "kind": "xsuaa" }, diff --git a/srv/catalog-service.cds b/srv/catalog-service.cds index 1d7a201..2a14136 100644 --- a/srv/catalog-service.cds +++ b/srv/catalog-service.cds @@ -8,6 +8,7 @@ service CatalogService @(requires: 'authenticated-user') { @readonly entity Products as projection on external.SEPMRA_C_PD_Product; - action getBTPJWT() returns String; - action readSAPLogonTicket() returns String; + action getBTPJWT() returns String; + action readProductsFromSAPGraph() returns array of String; + action readSAPLogonTicket() returns String; } diff --git a/srv/catalog-service.js b/srv/catalog-service.js index fd63a68..da76495 100644 --- a/srv/catalog-service.js +++ b/srv/catalog-service.js @@ -1,6 +1,8 @@ const cds = require("@sap/cds"); const LOG = cds.log("catalog-service"); const { AuthClient } = require("./AuthClient"); +const axios = require("axios"); +const { getDestination } = require("@sap-cloud-sdk/connectivity"); function getAuthToken(req) { const authHeader = req._.req.headers["authorization"]; @@ -12,13 +14,7 @@ module.exports = async function (srv) { const biscuitService = await cds.connect.to("biscuit"); const productService = await cds.connect.to("SEPMRA_PROD_MAN"); - srv.on("READ", "Products", async (req) => { - const token = getAuthToken(req); - LOG.debug("Token: " + token); - return productService.run(req.query); - }); - - srv.on("getBTPJWT", async (req) => { + async function getBTPJWT(req) { const token = getAuthToken(req); const authClient = new AuthClient(); LOG.debug("Token: " + token); @@ -28,6 +24,45 @@ module.exports = async function (srv) { ); LOG.debug("BTP Access Token: " + btpAccessToken); return btpAccessToken; + } + + srv.on("READ", "Products", async (req) => { + const token = getAuthToken(req); + LOG.debug("Token: " + token); + return productService.run(req.query); + }); + + srv.on("getBTPJWT", async (req) => { + return getBTPJWT(req); + }); + + srv.on("readProductsFromSAPGraph", async (req) => { + const sapGraph = await cds.connect.to("SAPGraph"); + const btpAccessToken = await getBTPJWT(req); + try { + const path = + "/api/s4hc/sap.s4/A_Product?$top=10&$select=Product,Brand,CountryOfOrigin&$count=true"; + /* + // CAP request does not work due to authentication issue + const products = await sapGraph.send({ + event: "get", + path, + headers: { + Authorization: `Bearer ${btpAccessToken}`, + }, + }); + */ + const graphDest = await getDestination({ destinationName: "SAPGraph" }); + const graphUrl = graphDest.url + path; + let products = await axios.get(graphUrl, { + headers: { + Authorization: `Bearer ${btpAccessToken}`, + }, + }); + return products?.data?.value; + } catch (error) { + LOG.error("Error Message: " + error.message); + } }); srv.on("readSAPLogonTicket", async (req) => {