From 18aab16f815da1a9ffb0d34a0def3d182bfe03f1 Mon Sep 17 00:00:00 2001 From: Rashmi Date: Sun, 26 May 2024 15:46:44 +0530 Subject: [PATCH] changes --- lib/sdm.js | 4 ++-- lib/util/index.js | 59 +++++++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/lib/sdm.js b/lib/sdm.js index 7506212..86dcb6b 100644 --- a/lib/sdm.js +++ b/lib/sdm.js @@ -29,7 +29,7 @@ module.exports = class SDMAttachmentsService extends ( async get(attachments, keys) { const response = await getURLFromAttachments(keys, attachments); - const token = await fetchAccessToken(this.creds); + const token = await fetchAccessToken(this.creds,req.user.tokeninfo.getTokenValue()); try { const Key = response?.url; const content = await readAttachment(Key, token, this.creds); @@ -264,4 +264,4 @@ module.exports = class SDMAttachmentsService extends ( ); return; } -}; +}; \ No newline at end of file diff --git a/lib/util/index.js b/lib/util/index.js index 1048ddd..15dfa26 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -3,31 +3,46 @@ const cds = require("@sap/cds"); const requests = xssec.requests; const NodeCache = require("node-cache"); const cache = new NodeCache(); - -function fetchAccessToken(credentials) { - const access_token = cache.get("SDM_ACCESS_TOKEN"); // to check if token exists +async function fetchAccessToken(credentials,jwt) { + let access_token = cache.get("SDM_ACCESS_TOKEN"); // to check if token exists if (access_token === undefined) { - return new Promise(function (resolve, reject) { - requests.requestClientCredentialsToken( - null, - credentials.uaa, - null, - (error, response) => { - if (error) { - console.error( - `Response error while fetching access token ${response.statusCode}` - ); - reject(err); - } else { - cache.set("SDM_ACCESS_TOKEN", response, 11); //expires after 11 hours - resolve(response); - } - } - ); - }); + access_token = await generateSDMBearerToken(credentials,jwt); + cache.set("SDM_ACCESS_TOKEN", access_token, 11); //expires after 11 hours } else { - return access_token; + if(isTokenExpired(access_token)){ +access_token = generateSDMBearerToken(credentials,jwt); +cache.set("SDM_ACCESS_TOKEN", access_token, 11); //expires after 11 hours + } + } + return access_token; +} +async function generateSDMBearerToken(credentials,jwt){ +return new Promise(function (resolve, reject) { + requests.requestUserToken( + jwt, + credentials.uaa, + null, null, null, null, (error, response)=>{ + if (error) { + console.error( + `Response error while fetching access token ${response.statusCode}` + ); + reject(err); + } else { + console.log("TOKEN EXCHANGE "+response); + resolve(response); + } + } + ); + }); +} +function isTokenExpired(jwtEncoded){ + const jwtBase64Encoded = jwtEncoded.split('.')[1] + const jwtDecodedAsString = Buffer.from(jwtBase64Encoded, 'base64').toString('ascii') + const jwtDecodedJson = JSON.parse(jwtDecodedAsString) + var expiry = new Date(jwtDecodedJson.exp * 1000); + var now = new Date(); + return now>expiry; } function getConfigurations() {