Skip to content

Commit

Permalink
SDMEXT-401:Implementing token Exchange
Browse files Browse the repository at this point in the history
instead of clientCredentialsToken, requestUserToken of node js library @sap/xssec is used which excahnges the token from approuter with sdm xsuaa token.
  • Loading branch information
rashmiangadi11 committed May 27, 2024
1 parent 18aab16 commit 6b22322
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 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,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);
Expand All @@ -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 = "";
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Expand All @@ -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;
Expand Down

0 comments on commit 6b22322

Please sign in to comment.