@sap-cloud-sdk/connectivity
Version:
SAP Cloud SDK for JavaScript connectivity
79 lines • 4.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDestinationFromServiceBinding = getDestinationFromServiceBinding;
exports.searchServiceBindingForDestination = searchServiceBindingForDestination;
const util_1 = require("@sap-cloud-sdk/util");
const jwt_1 = require("../jwt");
const environment_accessor_1 = require("../environment-accessor");
const http_proxy_util_1 = require("./http-proxy-util");
const destination_service_types_1 = require("./destination-service-types");
const service_binding_to_destination_1 = require("./service-binding-to-destination");
const forward_auth_token_1 = require("./forward-auth-token");
const logger = (0, util_1.createLogger)({
package: 'connectivity',
messageContext: 'destination-accessor-vcap'
});
/**
* Tries to build a destination from a service binding with the given name.
* Throws an error if no services are bound at all, no service with the given name can be found, or the service type is not supported.
* The last error can be circumvent by using the second parameter to provide a custom function that transforms a service binding to a destination.
* @param options - Options to customize the behavior of this function.
* @returns A destination.
*/
async function getDestinationFromServiceBinding(options) {
const decodedJwt = options.iss
? { iss: options.iss }
: options.jwt
? (0, jwt_1.decodeJwt)(options.jwt)
: undefined;
// If using business user authentication with IAS and no assertion provided, use the JWT from options
let iasOptions = options.iasOptions;
if (iasOptions?.authenticationType === 'OAuth2JWTBearer' &&
options.jwt &&
!iasOptions.assertion) {
iasOptions = { ...iasOptions, assertion: options.jwt };
}
const retrievalOptions = { ...options, jwt: decodedJwt, iasOptions };
let destination = (await retrieveDestination(retrievalOptions));
destination = (0, forward_auth_token_1.addForwardedAuthTokenIfNeeded)(destination, options.jwt);
return (0, destination_service_types_1.isHttpDestination)(destination) &&
['internet', 'private-link'].includes((0, http_proxy_util_1.proxyStrategy)(destination))
? (0, http_proxy_util_1.addProxyConfigurationInternet)(destination)
: destination;
}
async function retrieveDestination({ useCache, jwt, destinationName, iasOptions, service: serviceBinding, serviceBindingTransformFn }) {
const service = serviceBinding || (0, environment_accessor_1.getServiceBindingByInstanceName)(destinationName);
const destination = await (serviceBindingTransformFn || transform)(service, {
useCache,
jwt,
...(iasOptions ? { iasOptions } : {})
});
const name = (serviceBinding && service.name) || destinationName;
return { name, ...destination };
}
async function transform(service, options) {
if (!service_binding_to_destination_1.serviceToDestinationTransformers[service.label]) {
throw serviceTypeNotSupportedError(service);
}
return service_binding_to_destination_1.serviceToDestinationTransformers[service.label](service, options);
}
function serviceTypeNotSupportedError(service) {
return Error(`The service "${service.name}" is of type "${service.label}" which is not supported! Consider providing your own transformation function when calling \`getDestinationFromServiceBinding()\`, like this:
destinationServiceForBinding(yourServiceName, { serviceBindingToDestination: yourTransformationFunction });`);
}
/**
* @internal
*/
async function searchServiceBindingForDestination(options) {
logger.debug('Attempting to retrieve destination from service binding.');
try {
const destination = await getDestinationFromServiceBinding(options);
logger.info('Successfully retrieved destination from service binding.');
return destination;
}
catch (error) {
logger.debug(`Could not retrieve destination from service binding. If you are not using SAP Extension Factory, this information probably does not concern you. ${error.message}`);
}
return null;
}
//# sourceMappingURL=destination-from-vcap.js.map