Skip to content

Commit

Permalink
Cache implementation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rashmiangadi11 committed May 27, 2024
1 parent 6b22322 commit 6453eb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Binary file added cap-js-attachments-sdm-1.0.0.tgz
Binary file not shown.
22 changes: 14 additions & 8 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ 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 * 3600); //expires after 11 hours
let user = decodeAccessToken(access_token).email;
cache.set(user, access_token, 11 * 3600); //expires after 11 hours
} else {
if(isTokenExpired(access_token)){
let decoded_token = decodeAccessToken(jwtEncoded);
if(isTokenExpired(decoded_token.exp)){
access_token = generateSDMBearerToken(credentials,jwt);
cache.set("SDM_ACCESS_TOKEN", access_token, 11 * 3600); //expires after 11 hours
cache.del(decoded_token.email);
cache.set(decoded_token.email, access_token, 11 * 3600); //expires after 11 hours
}

}
Expand All @@ -36,14 +39,17 @@ return new Promise(function (resolve, reject) {
);
});
}
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);
function isTokenExpired(exp){

var expiry = new Date(exp * 1000);
var now = new Date();
return now>expiry;
}
function decodeAccessToken(jwtEncoded){
const jwtBase64Encoded = jwtEncoded.split('.')[1];
const jwtDecodedAsString = Buffer.from(jwtBase64Encoded, 'base64').toString('ascii');
return JSON.parse(jwtDecodedAsString);
}

function getConfigurations() {
return cds.env.requires?.["attachments-sdm"]?.settings || {};
Expand Down

0 comments on commit 6453eb7

Please sign in to comment.