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 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
14 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
15 | return new (P || (P = Promise))(function (resolve, reject) {
|
16 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
17 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
18 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
19 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
20 | });
|
21 | };
|
22 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
23 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
24 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
25 | function verb(n) { return function (v) { return step([n, v]); }; }
|
26 | function step(op) {
|
27 | if (f) throw new TypeError("Generator is already executing.");
|
28 | while (_) try {
|
29 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
30 | if (y = 0, t) op = [op[0] & 2, t.value];
|
31 | switch (op[0]) {
|
32 | case 0: case 1: t = op; break;
|
33 | case 4: _.label++; return { value: op[1], done: false };
|
34 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
35 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
36 | default:
|
37 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
38 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
39 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
40 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
41 | if (t[2]) _.ops.pop();
|
42 | _.trys.pop(); continue;
|
43 | }
|
44 | op = body.call(thisArg, _);
|
45 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
46 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
47 | }
|
48 | };
|
49 | var __rest = (this && this.__rest) || function (s, e) {
|
50 | var t = {};
|
51 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
52 | t[p] = s[p];
|
53 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
54 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
55 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
56 | t[p[i]] = s[p[i]];
|
57 | }
|
58 | return t;
|
59 | };
|
60 | Object.defineProperty(exports, "__esModule", { value: true });
|
61 | exports.getDestinationFromDestinationService = void 0;
|
62 | var util_1 = require("@sap-cloud-sdk/util");
|
63 | var jwt_1 = require("../jwt");
|
64 | var cache_1 = require("../cache");
|
65 | var token_accessor_1 = require("../token-accessor");
|
66 | var connectivity_service_1 = require("../connectivity-service");
|
67 | var environment_accessor_1 = require("../environment-accessor");
|
68 | var tenant_1 = require("../tenant");
|
69 | var xsuaa_service_1 = require("../xsuaa-service");
|
70 | var http_agent_1 = require("../../../http-agent");
|
71 | var destination_selection_strategies_1 = require("./destination-selection-strategies");
|
72 | var destination_service_1 = require("./destination-service");
|
73 | var destination_cache_1 = require("./destination-cache");
|
74 | var logger = (0, util_1.createLogger)({
|
75 | package: 'connectivity',
|
76 | messageContext: 'destination-accessor-service'
|
77 | });
|
78 | var emptyDestinationByType = {
|
79 | instance: [],
|
80 | subaccount: []
|
81 | };
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function getDestinationFromDestinationService(name, options) {
|
94 | return __awaiter(this, void 0, void 0, function () {
|
95 | return __generator(this, function (_a) {
|
96 | logger.info('Attempting to retrieve destination from destination service.');
|
97 | return [2 , DestinationFromServiceRetriever.getDestinationFromDestinationService(name, options)];
|
98 | });
|
99 | });
|
100 | }
|
101 | exports.getDestinationFromDestinationService = getDestinationFromDestinationService;
|
102 |
|
103 |
|
104 |
|
105 | var DestinationFromServiceRetriever = (function () {
|
106 | function DestinationFromServiceRetriever(name, options, subscriberToken, providerServiceToken) {
|
107 | this.name = name;
|
108 | this.subscriberToken = subscriberToken;
|
109 | this.providerServiceToken = providerServiceToken;
|
110 | var defaultOptions = __assign({ isolationStrategy: cache_1.IsolationStrategy.Tenant_User, selectionStrategy: destination_selection_strategies_1.subscriberFirst, useCache: false }, options);
|
111 | this.options = __assign(__assign({}, defaultOptions), options);
|
112 | }
|
113 | DestinationFromServiceRetriever.getDestinationFromDestinationService = function (name, options) {
|
114 | return __awaiter(this, void 0, void 0, function () {
|
115 | var subscriberToken, xsuaaCredentials, providerToken, da, destinationResult, destination, withProxySetting;
|
116 | return __generator(this, function (_a) {
|
117 | switch (_a.label) {
|
118 | case 0: return [4 , DestinationFromServiceRetriever.getSubscriberToken(options)];
|
119 | case 1:
|
120 | subscriberToken = _a.sent();
|
121 | xsuaaCredentials = (0, environment_accessor_1.getXsuaaServiceCredentials)(options.userJwt);
|
122 | return [4 , DestinationFromServiceRetriever.getProviderServiceToken(xsuaaCredentials, options)];
|
123 | case 2:
|
124 | providerToken = _a.sent();
|
125 | da = new DestinationFromServiceRetriever(name, __assign(__assign({}, options), { xsuaaCredentials: xsuaaCredentials }), subscriberToken, providerToken);
|
126 | return [4 , da.searchDestinationWithSelectionStrategyAndCache()];
|
127 | case 3:
|
128 | destinationResult = _a.sent();
|
129 | if (!destinationResult) {
|
130 | return [2 , null];
|
131 | }
|
132 | if (destinationResult.fromCache) {
|
133 | return [2 , destinationResult.destination];
|
134 | }
|
135 | destination = destinationResult.destination;
|
136 | if (!(destination.authentication === 'OAuth2UserTokenExchange' ||
|
137 | destination.authentication === 'OAuth2JWTBearer' ||
|
138 | (destination.authentication === 'OAuth2SAMLBearerAssertion' &&
|
139 | !da.usesSystemUser(destination)))) return [3 , 5];
|
140 | return [4 , da.fetchDestinationWithUserExchangeFlows(destinationResult)];
|
141 | case 4:
|
142 | destination = _a.sent();
|
143 | _a.label = 5;
|
144 | case 5:
|
145 | if (!(destination.authentication === 'OAuth2Password' ||
|
146 | destination.authentication === 'ClientCertificateAuthentication' ||
|
147 | destination.authentication === 'OAuth2ClientCredentials' ||
|
148 | da.usesSystemUser(destination))) return [3 , 7];
|
149 | return [4 , da.fetchDestinationWithNonUserExchangeFlows(destinationResult)];
|
150 | case 6:
|
151 | destination = _a.sent();
|
152 | _a.label = 7;
|
153 | case 7: return [4 , da.addProxyConfiguration(destination)];
|
154 | case 8:
|
155 | withProxySetting = _a.sent();
|
156 | da.updateDestinationCache(withProxySetting, destinationResult.origin);
|
157 | return [2 , withProxySetting];
|
158 | }
|
159 | });
|
160 | });
|
161 | };
|
162 | DestinationFromServiceRetriever.getSubscriberToken = function (options) {
|
163 | return __awaiter(this, void 0, void 0, function () {
|
164 | var encoded, payload, encoded, clientCertJwt;
|
165 | var _a, _b;
|
166 | return __generator(this, function (_c) {
|
167 | switch (_c.label) {
|
168 | case 0:
|
169 | if (!options.userJwt) return [3 , 3];
|
170 | if (options.iss) {
|
171 | logger.warn('You have provided the `userJwt` and `iss` options to fetch the destination. This is most likely unintentional. Ignoring `iss`.');
|
172 | }
|
173 | return [4 , (0, token_accessor_1.serviceToken)('destination', __assign({}, options))];
|
174 | case 1:
|
175 | encoded = _c.sent();
|
176 | _a = {};
|
177 | _b = {};
|
178 | return [4 , (0, jwt_1.verifyJwt)(options.userJwt, options)];
|
179 | case 2: return [2 , (_a.userJwt = (_b.decoded = _c.sent(),
|
180 | _b.encoded = options.userJwt,
|
181 | _b),
|
182 | _a.serviceJwt = { encoded: encoded, decoded: (0, jwt_1.decodeJwt)(encoded) },
|
183 | _a)];
|
184 | case 3:
|
185 | if (!options.iss) return [3 , 5];
|
186 | logger.info('Using `iss` option to fetch a destination instead of a full JWT. No validation is performed.');
|
187 | payload = { iss: options.iss };
|
188 | return [4 , (0, token_accessor_1.serviceToken)('destination', __assign(__assign({}, options), { userJwt: payload }))];
|
189 | case 4:
|
190 | encoded = _c.sent();
|
191 | clientCertJwt = { encoded: encoded, decoded: (0, jwt_1.decodeJwt)(encoded) };
|
192 | return [2 , { serviceJwt: clientCertJwt }];
|
193 | case 5: return [2 ];
|
194 | }
|
195 | });
|
196 | });
|
197 | };
|
198 | DestinationFromServiceRetriever.getProviderServiceToken = function (xsuaaCredentials, options) {
|
199 | return __awaiter(this, void 0, void 0, function () {
|
200 | var userJwt, optionsWithoutJwt, encoded;
|
201 | return __generator(this, function (_a) {
|
202 | switch (_a.label) {
|
203 | case 0:
|
204 | userJwt = options.userJwt, optionsWithoutJwt = __rest(options, ["userJwt"]);
|
205 | return [4 , (0, token_accessor_1.serviceToken)('destination', __assign(__assign({}, optionsWithoutJwt), { xsuaaCredentials: xsuaaCredentials }))];
|
206 | case 1:
|
207 | encoded = _a.sent();
|
208 | return [2 , { encoded: encoded, decoded: (0, jwt_1.decodeJwt)(encoded) }];
|
209 | }
|
210 | });
|
211 | });
|
212 | };
|
213 | DestinationFromServiceRetriever.prototype.searchDestinationWithSelectionStrategyAndCache = function () {
|
214 | return __awaiter(this, void 0, void 0, function () {
|
215 | var destinationSearchResult;
|
216 | return __generator(this, function (_a) {
|
217 | switch (_a.label) {
|
218 | case 0:
|
219 | if (!this.isSubscriberNeeded()) return [3 , 2];
|
220 | return [4 , this.searchSubscriberAccountForDestination()];
|
221 | case 1:
|
222 | destinationSearchResult =
|
223 | _a.sent();
|
224 | _a.label = 2;
|
225 | case 2:
|
226 | if (!this.isProviderNeeded(destinationSearchResult)) return [3 , 4];
|
227 | return [4 , this.searchProviderAccountForDestination()];
|
228 | case 3:
|
229 | destinationSearchResult =
|
230 | _a.sent();
|
231 | _a.label = 4;
|
232 | case 4:
|
233 | if (destinationSearchResult && !destinationSearchResult.fromCache) {
|
234 | logger.debug('Successfully retrieved destination from destination service.');
|
235 | }
|
236 | if (destinationSearchResult && destinationSearchResult.fromCache) {
|
237 | logger.debug("Successfully retrieved destination from destination service cache for ".concat(destinationSearchResult.origin, " destinations."));
|
238 | }
|
239 | if (!destinationSearchResult) {
|
240 | logger.info('Could not retrieve destination from destination service.');
|
241 | }
|
242 | return [2 , destinationSearchResult];
|
243 | }
|
244 | });
|
245 | });
|
246 | };
|
247 | DestinationFromServiceRetriever.prototype.getInstanceAndSubaccountDestinations = function (accessToken) {
|
248 | return __awaiter(this, void 0, void 0, function () {
|
249 | var _a, instance, subaccount;
|
250 | return __generator(this, function (_b) {
|
251 | switch (_b.label) {
|
252 | case 0: return [4 , Promise.all([
|
253 | (0, destination_service_1.fetchInstanceDestinations)(this.destinationServiceCredentials.uri, accessToken, this.options),
|
254 | (0, destination_service_1.fetchSubaccountDestinations)(this.destinationServiceCredentials.uri, accessToken, this.options)
|
255 | ])];
|
256 | case 1:
|
257 | _a = _b.sent(), instance = _a[0], subaccount = _a[1];
|
258 | return [2 , {
|
259 | instance: instance,
|
260 | subaccount: subaccount
|
261 | }];
|
262 | }
|
263 | });
|
264 | });
|
265 | };
|
266 | Object.defineProperty(DestinationFromServiceRetriever.prototype, "destinationServiceCredentials", {
|
267 | get: function () {
|
268 | var credentials = (0, environment_accessor_1.getDestinationServiceCredentialsList)();
|
269 | if (!credentials || credentials.length === 0) {
|
270 | throw Error('No binding to a destination service instance found. Please bind a destination service instance to your application.');
|
271 | }
|
272 | if (credentials.length > 1) {
|
273 | logger.info('Found more than one destination service instance. Using the first one.');
|
274 | }
|
275 | return credentials[0];
|
276 | },
|
277 | enumerable: false,
|
278 | configurable: true
|
279 | });
|
280 | DestinationFromServiceRetriever.prototype.fetchDestinationByToken = function (jwt) {
|
281 | return __awaiter(this, void 0, void 0, function () {
|
282 | return __generator(this, function (_a) {
|
283 | return [2 , (0, destination_service_1.fetchDestination)(this.destinationServiceCredentials.uri, jwt, this.name, this.options)];
|
284 | });
|
285 | });
|
286 | };
|
287 | DestinationFromServiceRetriever.prototype.getExchangeTenant = function (destination) {
|
288 | var _a, _b, _c;
|
289 | if (destination.authentication !== 'OAuth2ClientCredentials') {
|
290 | return undefined;
|
291 | }
|
292 | if (destination.originalProperties['tokenServiceURLType'] !== 'Common') {
|
293 | return undefined;
|
294 | }
|
295 | var subdomainSubscriber = (0, xsuaa_service_1.getSubdomainAndZoneId)((_b = (_a = this.subscriberToken) === null || _a === void 0 ? void 0 : _a.userJwt) === null || _b === void 0 ? void 0 : _b.encoded).subdomain;
|
296 | var subdomainProvider = (0, xsuaa_service_1.getSubdomainAndZoneId)((_c = this.providerServiceToken) === null || _c === void 0 ? void 0 : _c.encoded).subdomain;
|
297 | return subdomainSubscriber || subdomainProvider || undefined;
|
298 | };
|
299 | DestinationFromServiceRetriever.prototype.getAuthTokenForOAuth2ClientCrendentials = function (destinationResult) {
|
300 | return __awaiter(this, void 0, void 0, function () {
|
301 | var destination, origin, exchangeTenant, clientGrant;
|
302 | return __generator(this, function (_a) {
|
303 | switch (_a.label) {
|
304 | case 0:
|
305 | destination = destinationResult.destination, origin = destinationResult.origin;
|
306 | exchangeTenant = this.getExchangeTenant(destination);
|
307 | return [4 , (0, token_accessor_1.serviceToken)('destination', {
|
308 | userJwt: origin === 'subscriber'
|
309 | ? this.subscriberToken.serviceJwt.decoded
|
310 | : this.providerServiceToken.decoded
|
311 | })];
|
312 | case 1:
|
313 | clientGrant = _a.sent();
|
314 | return [2 , { authHeaderJwt: clientGrant, exchangeTenant: exchangeTenant }];
|
315 | }
|
316 | });
|
317 | });
|
318 | };
|
319 |
|
320 | DestinationFromServiceRetriever.prototype.usesSystemUser = function (destination) {
|
321 |
|
322 | if (destination.systemUser &&
|
323 | destination.authentication === 'OAuth2SAMLBearerAssertion') {
|
324 | logger.debug("System user found on destination: \"".concat(destination.name, "\". \nThe property SystemUser has been deprecated. \nIt is highly recommended that you stop using it.\nPossible alternatives for such technical user authentication are BasicAuthentication, OAuth2ClientCredentials, or ClientCertificateAuthentication"));
|
325 | return true;
|
326 | }
|
327 | return false;
|
328 | };
|
329 | DestinationFromServiceRetriever.prototype.getAuthTokenForOAuth2UserBasedTokenExchanges = function (destinationResult) {
|
330 | return __awaiter(this, void 0, void 0, function () {
|
331 | var destination, origin, serviceJwt;
|
332 | var _a;
|
333 | return __generator(this, function (_b) {
|
334 | switch (_b.label) {
|
335 | case 0:
|
336 | destination = destinationResult.destination, origin = destinationResult.origin;
|
337 | if (!this.subscriberToken || !(0, jwt_1.isUserToken)(this.subscriberToken.userJwt)) {
|
338 | throw Error("No user token (JWT) has been provided. This is strictly necessary for '".concat(destination.authentication, "'."));
|
339 | }
|
340 | if (!this.isProviderAndSubscriberSameTenant()) return [3 , 2];
|
341 | logger.debug("UserExchange flow started without user exchange token for destination ".concat(this.name, " of the provider account."));
|
342 | _a = {};
|
343 | return [4 , (0, token_accessor_1.jwtBearerToken)(this.subscriberToken.userJwt.encoded, (0, environment_accessor_1.getDestinationService)(), this.options)];
|
344 | case 1: return [2 , (_a.authHeaderJwt = _b.sent(),
|
345 | _a)];
|
346 | case 2:
|
347 | serviceJwt = origin === 'subscriber'
|
348 | ? this.subscriberToken.serviceJwt
|
349 | : this.providerServiceToken;
|
350 | logger.debug("UserExchange flow started for destination ".concat(this.name, " of the ").concat(origin, " account."));
|
351 | return [2 , {
|
352 | authHeaderJwt: serviceJwt.encoded,
|
353 | exchangeHeaderJwt: this.subscriberToken.userJwt.encoded
|
354 | }];
|
355 | }
|
356 | });
|
357 | });
|
358 | };
|
359 | |
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 | DestinationFromServiceRetriever.prototype.fetchDestinationWithNonUserExchangeFlows = function (destinationResult) {
|
367 | return __awaiter(this, void 0, void 0, function () {
|
368 | var token;
|
369 | return __generator(this, function (_a) {
|
370 | switch (_a.label) {
|
371 | case 0: return [4 , this.getAuthTokenForOAuth2ClientCrendentials(destinationResult)];
|
372 | case 1:
|
373 | token = _a.sent();
|
374 | return [2 , this.fetchDestinationByToken(token)];
|
375 | }
|
376 | });
|
377 | });
|
378 | };
|
379 | DestinationFromServiceRetriever.prototype.fetchDestinationWithUserExchangeFlows = function (destinationResult) {
|
380 | return __awaiter(this, void 0, void 0, function () {
|
381 | var token;
|
382 | return __generator(this, function (_a) {
|
383 | switch (_a.label) {
|
384 | case 0: return [4 , this.getAuthTokenForOAuth2UserBasedTokenExchanges(destinationResult)];
|
385 | case 1:
|
386 | token = _a.sent();
|
387 | return [2 , this.fetchDestinationByToken(token)];
|
388 | }
|
389 | });
|
390 | });
|
391 | };
|
392 | DestinationFromServiceRetriever.prototype.addProxyConfiguration = function (destination) {
|
393 | var _a;
|
394 | return __awaiter(this, void 0, void 0, function () {
|
395 | return __generator(this, function (_b) {
|
396 | switch ((0, http_agent_1.proxyStrategy)(destination)) {
|
397 | case http_agent_1.ProxyStrategy.ON_PREMISE_PROXY:
|
398 | return [2 , (0, connectivity_service_1.addProxyConfigurationOnPrem)(destination, (_a = this.subscriberToken) === null || _a === void 0 ? void 0 : _a.userJwt)];
|
399 | case http_agent_1.ProxyStrategy.INTERNET_PROXY:
|
400 | case http_agent_1.ProxyStrategy.PRIVATELINK_PROXY:
|
401 | return [2 , (0, http_agent_1.addProxyConfigurationInternet)(destination)];
|
402 | case http_agent_1.ProxyStrategy.NO_PROXY:
|
403 | return [2 , destination];
|
404 | default:
|
405 | throw new Error('Illegal argument: No valid proxy configuration found in the destination input to be aded.');
|
406 | }
|
407 | return [2 ];
|
408 | });
|
409 | });
|
410 | };
|
411 |
|
412 | DestinationFromServiceRetriever.prototype.selectSubscriberJwt = function () {
|
413 | var _a;
|
414 | if (!this.subscriberToken) {
|
415 | throw new Error('Try to get subscriber token but value is undefined.');
|
416 | }
|
417 | return (((_a = this.subscriberToken.userJwt) === null || _a === void 0 ? void 0 : _a.decoded) ||
|
418 | this.subscriberToken.serviceJwt.decoded);
|
419 | };
|
420 | DestinationFromServiceRetriever.prototype.updateDestinationCache = function (destination, destinationOrigin) {
|
421 | if (!this.options.useCache) {
|
422 | return destination;
|
423 | }
|
424 | destination_cache_1.destinationCache.cacheRetrievedDestination(destinationOrigin === 'subscriber'
|
425 | ? this.selectSubscriberJwt()
|
426 | : this.providerServiceToken.decoded, destination, this.options.isolationStrategy);
|
427 | };
|
428 | DestinationFromServiceRetriever.prototype.getProviderDestinationService = function () {
|
429 | return __awaiter(this, void 0, void 0, function () {
|
430 | var provider, destination;
|
431 | return __generator(this, function (_a) {
|
432 | switch (_a.label) {
|
433 | case 0: return [4 , this.getInstanceAndSubaccountDestinations(this.providerServiceToken.encoded)];
|
434 | case 1:
|
435 | provider = _a.sent();
|
436 | destination = this.options.selectionStrategy({
|
437 | subscriber: emptyDestinationByType,
|
438 | provider: provider
|
439 | }, this.name);
|
440 | if (destination) {
|
441 | return [2 , {
|
442 | destination: destination,
|
443 | fromCache: false,
|
444 | origin: 'provider'
|
445 | }];
|
446 | }
|
447 | return [2 ];
|
448 | }
|
449 | });
|
450 | });
|
451 | };
|
452 | DestinationFromServiceRetriever.prototype.getProviderDestinationCache = function () {
|
453 | var destination = destination_cache_1.destinationCache.retrieveDestinationFromCache(this.providerServiceToken.decoded, this.name, this.options.isolationStrategy);
|
454 | if (destination) {
|
455 | return { destination: destination, fromCache: true, origin: 'subscriber' };
|
456 | }
|
457 | };
|
458 | DestinationFromServiceRetriever.prototype.getSubscriberDestinationService = function () {
|
459 | return __awaiter(this, void 0, void 0, function () {
|
460 | var subscriber, destination;
|
461 | return __generator(this, function (_a) {
|
462 | switch (_a.label) {
|
463 | case 0:
|
464 | if (!this.subscriberToken) {
|
465 | throw new Error('Try to get destinations from subscriber account but user JWT was not set.');
|
466 | }
|
467 | return [4 , this.getInstanceAndSubaccountDestinations(this.subscriberToken.serviceJwt.encoded)];
|
468 | case 1:
|
469 | subscriber = _a.sent();
|
470 | destination = this.options.selectionStrategy({
|
471 | subscriber: subscriber,
|
472 | provider: emptyDestinationByType
|
473 | }, this.name);
|
474 | if (destination) {
|
475 | return [2 , { destination: destination, fromCache: false, origin: 'subscriber' }];
|
476 | }
|
477 | return [2 ];
|
478 | }
|
479 | });
|
480 | });
|
481 | };
|
482 | DestinationFromServiceRetriever.prototype.getSubscriberDestinationCache = function () {
|
483 | var destination = destination_cache_1.destinationCache.retrieveDestinationFromCache(this.selectSubscriberJwt(), this.name, this.options.isolationStrategy);
|
484 | if (destination) {
|
485 | return { destination: destination, fromCache: true, origin: 'subscriber' };
|
486 | }
|
487 | };
|
488 | DestinationFromServiceRetriever.prototype.isProviderAndSubscriberSameTenant = function () {
|
489 | return (this.subscriberToken &&
|
490 | (0, tenant_1.isIdenticalTenant)(this.subscriberToken.serviceJwt.decoded, this.providerServiceToken.decoded));
|
491 | };
|
492 | DestinationFromServiceRetriever.prototype.isProviderNeeded = function (resultFromSubscriber) {
|
493 | if (this.options.selectionStrategy === destination_selection_strategies_1.alwaysSubscriber) {
|
494 | return false;
|
495 | }
|
496 | if (this.options.selectionStrategy === destination_selection_strategies_1.subscriberFirst &&
|
497 | resultFromSubscriber) {
|
498 | return false;
|
499 | }
|
500 | return true;
|
501 | };
|
502 | DestinationFromServiceRetriever.prototype.isSubscriberNeeded = function () {
|
503 | if (!this.subscriberToken) {
|
504 | return false;
|
505 | }
|
506 | if (this.options.selectionStrategy === destination_selection_strategies_1.alwaysProvider) {
|
507 | return false;
|
508 | }
|
509 | return true;
|
510 | };
|
511 | DestinationFromServiceRetriever.prototype.searchProviderAccountForDestination = function () {
|
512 | return __awaiter(this, void 0, void 0, function () {
|
513 | return __generator(this, function (_a) {
|
514 | return [2 , ((this.options.useCache && this.getProviderDestinationCache()) ||
|
515 | this.getProviderDestinationService())];
|
516 | });
|
517 | });
|
518 | };
|
519 | DestinationFromServiceRetriever.prototype.searchSubscriberAccountForDestination = function () {
|
520 | return __awaiter(this, void 0, void 0, function () {
|
521 | return __generator(this, function (_a) {
|
522 | return [2 , ((this.options.useCache && this.getSubscriberDestinationCache()) ||
|
523 | this.getSubscriberDestinationService())];
|
524 | });
|
525 | });
|
526 | };
|
527 | return DestinationFromServiceRetriever;
|
528 | }());
|
529 |
|
\ | No newline at end of file |