diff --git a/lib/sdm.js b/lib/sdm.js index 86dcb6b..6f99513 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,req.user.tokeninfo.getTokenValue()); + const token = await fetchAccessToken(this.creds,req.user.tokenInfo.getTokenValue()); try { const Key = response?.url; const content = await readAttachment(Key, token, this.creds); @@ -50,7 +50,7 @@ module.exports = class SDMAttachmentsService extends ( if (duplicateDraftFilesErrMsg != "") { req.reject(409, duplicateDraftFileErr(duplicateDraftFilesErrMsg)); } - const token = await fetchAccessToken(this.creds); + const token = await fetchAccessToken(this.creds,req.user.tokenInfo.getTokenValue()); console.log("Token: ", token); const folderIds = await getFolderIdForEntity(attachments, req); let parentId = ""; @@ -146,7 +146,7 @@ module.exports = class SDMAttachmentsService extends ( cds.model.definitions[req.query.target.name + ".attachments"]; if (req?.attachmentsToDelete?.length > 0) { - const token = await fetchAccessToken(this.creds); + const token = await fetchAccessToken(this.creds,req.user.tokenInfo.getTokenValue()); if (req?.parentId) { await deleteFolderWithAttachments(this.creds, token, req.parentId); } else { diff --git a/lib/util/index.js b/lib/util/index.js index 15dfa26..bc75390 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -7,11 +7,11 @@ async function fetchAccessToken(credentials,jwt) { let access_token = cache.get("SDM_ACCESS_TOKEN"); // to check if token exists if (access_token === undefined) { access_token = await generateSDMBearerToken(credentials,jwt); - cache.set("SDM_ACCESS_TOKEN", access_token, 11); //expires after 11 hours + cache.set("SDM_ACCESS_TOKEN", access_token, 11 * 3600); //expires after 11 hours } else { if(isTokenExpired(access_token)){ access_token = generateSDMBearerToken(credentials,jwt); -cache.set("SDM_ACCESS_TOKEN", access_token, 11); //expires after 11 hours +cache.set("SDM_ACCESS_TOKEN", access_token, 11 * 3600); //expires after 11 hours } } @@ -29,17 +29,17 @@ return new Promise(function (resolve, reject) { ); reject(err); } else { - console.log("TOKEN EXCHANGE "+response); resolve(response); + return response; } } ); }); } function isTokenExpired(jwtEncoded){ - const jwtBase64Encoded = jwtEncoded.split('.')[1] - const jwtDecodedAsString = Buffer.from(jwtBase64Encoded, 'base64').toString('ascii') - const jwtDecodedJson = JSON.parse(jwtDecodedAsString) + 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;