@sap-cloud-sdk/connectivity
Version:
SAP Cloud SDK for JavaScript connectivity
59 lines • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildIasDestination = buildIasDestination;
exports.buildDestination = buildDestination;
const jwt_1 = require("../jwt");
/**
* Builds destination based on supplied IAS options.
* Uses `targetUrl` as the destination URL if supplied and adds `mtlsKeyPair` if available.
* @param accessToken - The JWT token to access the service.
* @param service - Service binding for identity service.
* @param iasOptions - IAS options to build the destination.
* @returns A destination object.
* @internal
*/
function buildIasDestination(accessToken, service, iasOptions) {
const destination = buildDestination(accessToken, iasOptions?.targetUrl ?? service.credentials.url, service.name, iasOptions.authenticationType || 'OAuth2ClientCredentials');
// Add mTLS key pair if available
if (service.credentials.certificate && service.credentials.key) {
destination.mtlsKeyPair = {
cert: service.credentials.certificate,
key: service.credentials.key
};
}
return destination;
}
/**
* Builds a destination object with a token, name, and url.
* If no authentication type is provided, 'OAuth2ClientCredentials' is used by default.
* @internal
* @param token - The access token for the destination.
* @param url - The URL of the destination.
* @param name - The name of the destination.
* @param authentication - The authentication type for the destination. Defaults to 'OAuth2ClientCredentials'.
* @returns A destination object.
*/
function buildDestination(token, url, name, authentication = 'OAuth2ClientCredentials') {
const expirationTime = (0, jwt_1.decodeJwt)(token).exp;
const expirationTimeMs = expirationTime ? expirationTime * 1000 : undefined;
const now = Date.now();
const expiresIn = expirationTimeMs
? // Calculate time delta (ms) and convert to seconds (string)
Math.floor((expirationTimeMs - now) / 1000).toString()
: undefined;
return {
url,
name,
authentication,
authTokens: [
{
value: token,
type: 'bearer',
expiresIn,
http_header: { key: 'Authorization', value: `Bearer ${token}` },
error: null
}
]
};
}
//# sourceMappingURL=build-ias-destination.js.map