Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rashmiangadi11 committed May 26, 2024
1 parent e338cd0 commit 18aab16
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/sdm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -264,4 +264,4 @@ module.exports = class SDMAttachmentsService extends (
);
return;
}
};
};
59 changes: 37 additions & 22 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 18aab16

Please sign in to comment.