1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.noDestinationErrorMessage = exports.isDestinationJson = exports.isDestinationConfiguration = exports.toDestinationNameUrl = exports.parseDestination = exports.sanitizeDestination = void 0;
|
15 | var util_1 = require("@sap-cloud-sdk/util");
|
16 | var destination_service_types_1 = require("./destination-service-types");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function sanitizeDestination(destination) {
|
23 | validateDestinationInput(destination);
|
24 | var destAuthToken = parseAuthTokens(destination);
|
25 | var parsedDestination = parseCertificates(destAuthToken);
|
26 | parsedDestination = setDefaultAuthenticationFallback(parsedDestination);
|
27 | parsedDestination = setTrustAll(parsedDestination);
|
28 | parsedDestination = setOriginalProperties(parsedDestination);
|
29 | return parsedDestination;
|
30 | }
|
31 | exports.sanitizeDestination = sanitizeDestination;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function parseDestination(destinationJson) {
|
39 | var destinationConfig = getDestinationConfig(destinationJson);
|
40 | validateDestinationConfig(destinationConfig);
|
41 | var destination = Object.entries(destinationConfig).reduce(function (dest, _a) {
|
42 | var originalKey = _a[0], value = _a[1];
|
43 | if (originalKey in configMapping) {
|
44 | dest[configMapping[originalKey]] = value;
|
45 | }
|
46 | return dest;
|
47 | }, {
|
48 | originalProperties: destinationJson,
|
49 | authTokens: destinationJson['authTokens'] || [],
|
50 | certificates: destinationJson['certificates'] || []
|
51 | });
|
52 | var additionalHeadersAndQueryParameters = getAdditionalHeadersAndQueryParameters(destinationConfig);
|
53 | return sanitizeDestination(__assign(__assign({}, destination), additionalHeadersAndQueryParameters));
|
54 | }
|
55 | exports.parseDestination = parseDestination;
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function getAdditionalProperties(destinationConfig, originalKeyPrefix) {
|
63 | var relevantConfigEntries = Object.entries(destinationConfig).filter(function (_a) {
|
64 | var key = _a[0];
|
65 | return key.startsWith(originalKeyPrefix);
|
66 | });
|
67 | return relevantConfigEntries.reduce(function (additionalProperties, _a) {
|
68 | var originalKey = _a[0], value = _a[1];
|
69 | var headerKey = originalKey.replace(originalKeyPrefix, '');
|
70 | additionalProperties[headerKey] = value;
|
71 | return additionalProperties;
|
72 | }, {});
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function getAdditionalHeadersAndQueryParameters(destinationConfig) {
|
83 | var additionalHeaders = getAdditionalProperties(destinationConfig, 'URL.headers.');
|
84 | var additionalQueryParameters = getAdditionalProperties(destinationConfig, 'URL.queries.');
|
85 | var additionalProperties = {};
|
86 | if (Object.keys(additionalHeaders).length) {
|
87 | additionalProperties['headers'] = additionalHeaders;
|
88 | }
|
89 | if (Object.keys(additionalQueryParameters).length) {
|
90 | additionalProperties['queryParameters'] = additionalQueryParameters;
|
91 | }
|
92 | return additionalProperties;
|
93 | }
|
94 | function getDestinationConfig(destinationJson) {
|
95 | return isDestinationJson(destinationJson)
|
96 | ? destinationJson.destinationConfiguration
|
97 | : destinationJson;
|
98 | }
|
99 | function validateDestinationConfig(destinationConfig) {
|
100 | if (isHttpDestination(destinationConfig) &&
|
101 | typeof destinationConfig.URL === 'undefined') {
|
102 | throw Error("Property 'URL' of destination configuration must not be undefined.");
|
103 | }
|
104 | }
|
105 | function validateDestinationInput(destinationInput) {
|
106 | if (isHttpDestination(destinationInput) &&
|
107 | typeof destinationInput.url === 'undefined') {
|
108 | throw Error("Property 'url' of destination input must not be undefined.");
|
109 | }
|
110 | }
|
111 | function isHttpDestination(destinationInput) {
|
112 | return (destinationInput.Type === 'HTTP' ||
|
113 | destinationInput.type === 'HTTP' ||
|
114 | (typeof destinationInput.type === 'undefined' &&
|
115 | typeof destinationInput.Type === 'undefined'));
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function toDestinationNameUrl(destination) {
|
122 | return (0, destination_service_types_1.isDestinationNameAndJwt)(destination)
|
123 | ? "name: ".concat(destination.destinationName)
|
124 | : "name: ".concat(destination.name, ", url: ").concat(destination.url);
|
125 | }
|
126 | exports.toDestinationNameUrl = toDestinationNameUrl;
|
127 | function setOriginalProperties(destination) {
|
128 | var originalProperties = destination.originalProperties
|
129 | ? destination.originalProperties
|
130 | : destination;
|
131 | return (0, util_1.assoc)('originalProperties', originalProperties, destination);
|
132 | }
|
133 | function setDefaultAuthenticationFallback(destination) {
|
134 | return destination.authentication
|
135 | ? destination
|
136 | : (0, util_1.assoc)('authentication', getAuthenticationType(destination), destination);
|
137 | }
|
138 | function parseCertificate(certificate) {
|
139 | return {
|
140 | name: certificate.Name || certificate.name,
|
141 | content: certificate.Content || certificate.content,
|
142 | type: certificate.Type || certificate.type
|
143 | };
|
144 | }
|
145 | function parseCertificates(destination) {
|
146 | var certificates = destination.certificates
|
147 | ? destination.certificates.map(parseCertificate)
|
148 | : [];
|
149 | return (0, util_1.assoc)('certificates', certificates, destination);
|
150 | }
|
151 | function parseAuthToken(authToken) {
|
152 | return {
|
153 | type: authToken.type,
|
154 | value: authToken.value,
|
155 | expiresIn: authToken.expires_in,
|
156 | error: 'error' in authToken ? authToken.error : null,
|
157 | http_header: authToken.http_header
|
158 | };
|
159 | }
|
160 | function parseAuthTokens(destination) {
|
161 | var authTokens = destination.authTokens
|
162 | ? destination.authTokens.map(parseAuthToken)
|
163 | : [];
|
164 | return (0, util_1.assoc)('authTokens', authTokens, destination);
|
165 | }
|
166 | function setTrustAll(destination) {
|
167 | return (0, util_1.assoc)('isTrustingAllCertificates', parseTrustAll(destination.isTrustingAllCertificates), destination);
|
168 | }
|
169 | function parseTrustAll(isTrustingAllCertificates) {
|
170 | if (typeof isTrustingAllCertificates === 'string') {
|
171 | return isTrustingAllCertificates.toLowerCase() === 'true';
|
172 | }
|
173 | return !!isTrustingAllCertificates;
|
174 | }
|
175 | function getAuthenticationType(destination) {
|
176 | return destination.authentication ||
|
177 | (destination.username && destination.password)
|
178 | ? 'BasicAuthentication'
|
179 | : 'NoAuthentication';
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | function isDestinationConfiguration(destination) {
|
186 | return destination.URL !== undefined;
|
187 | }
|
188 | exports.isDestinationConfiguration = isDestinationConfiguration;
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | function isDestinationJson(destination) {
|
194 | return Object.keys(destination).includes('destinationConfiguration');
|
195 | }
|
196 | exports.isDestinationJson = isDestinationJson;
|
197 | var configMapping = {
|
198 | URL: 'url',
|
199 | Name: 'name',
|
200 | User: 'username',
|
201 | Password: 'password',
|
202 | ProxyType: 'proxyType',
|
203 | 'sap-client': 'sapClient',
|
204 | Authentication: 'authentication',
|
205 | TrustAll: 'isTrustingAllCertificates',
|
206 | Type: 'type',
|
207 | tokenServiceURL: 'tokenServiceUrl',
|
208 | clientId: 'clientId',
|
209 | clientSecret: 'clientSecret',
|
210 | tokenServiceUser: 'tokenServiceUser',
|
211 | tokenServicePassword: 'tokenServicePassword',
|
212 | CloudConnectorLocationId: 'cloudConnectorLocationId',
|
213 | certificates: 'certificates',
|
214 | KeyStoreLocation: 'keyStoreName',
|
215 | KeyStorePassword: 'keyStorePassword',
|
216 | SystemUser: 'systemUser'
|
217 | };
|
218 | function noDestinationErrorMessage(destination) {
|
219 | return (0, destination_service_types_1.isDestinationNameAndJwt)(destination)
|
220 | ? "Could not find a destination with name \"".concat(destination.destinationName, "\"! Unable to execute request.")
|
221 | : 'Could not find a destination to execute request against and no destination name has been provided (this should never happen)!';
|
222 | }
|
223 | exports.noDestinationErrorMessage = noDestinationErrorMessage;
|
224 |
|
\ | No newline at end of file |