@sap-cloud-sdk/connectivity
Version:
SAP Cloud SDK for JavaScript connectivity
48 lines • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setForwardedAuthTokenIfNeeded = setForwardedAuthTokenIfNeeded;
const util_1 = require("@sap-cloud-sdk/util");
const jwt_1 = require("../jwt");
const logger = (0, util_1.createLogger)({
package: 'connectivity',
messageContext: 'forward-auth-token'
});
/**
* Transform a token to the format given by the destination service.
* @param token - Token to transform.
* @returns The transformed token.
*/
function buildDestinationAuthToken(token) {
const decodedJwt = (0, jwt_1.decodeJwt)(token);
return [
{
value: token,
expiresIn: decodedJwt.exp?.toString(),
error: null,
http_header: { key: 'Authorization', value: `Bearer ${token}` },
type: 'Bearer'
}
];
}
function validateToken(token) {
if (!token) {
logger.warn("Option 'forwardAuthToken' was set on destination but no token was provided to forward. This is most likely unintended and will lead to an authorization error on request execution.");
}
return !!token;
}
/**
* @internal
* Set forwarded auth token, if needed.
* @param destination - Destination to set the token on, if needed.
* @param token - Token to forward, if needed.
*/
function setForwardedAuthTokenIfNeeded(destination, token) {
if (destination.forwardAuthToken) {
if (validateToken(token)) {
logger.debug("Option 'forwardAuthToken' enabled on destination. Using the given token for the destination.");
destination.authTokens = buildDestinationAuthToken(token);
}
}
return destination;
}
//# sourceMappingURL=forward-auth-token.js.map