@webex/webex-core 3.11.0 → 3.12.0-auth-prejoin-fetch.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +5 -2
  2. package/dist/config.js +24 -0
  3. package/dist/config.js.map +1 -1
  4. package/dist/credentials-config.js +12 -0
  5. package/dist/credentials-config.js.map +1 -1
  6. package/dist/interceptors/redirect.js +1 -1
  7. package/dist/interceptors/redirect.js.map +1 -1
  8. package/dist/lib/batcher.js +23 -7
  9. package/dist/lib/batcher.js.map +1 -1
  10. package/dist/lib/credentials/credentials.js +48 -4
  11. package/dist/lib/credentials/credentials.js.map +1 -1
  12. package/dist/lib/credentials/token.js +1 -1
  13. package/dist/lib/domains.js +90 -0
  14. package/dist/lib/domains.js.map +1 -0
  15. package/dist/lib/services/service-catalog.js +6 -10
  16. package/dist/lib/services/service-catalog.js.map +1 -1
  17. package/dist/lib/services/services.js +253 -37
  18. package/dist/lib/services/services.js.map +1 -1
  19. package/dist/lib/services-v2/service-catalog.js +6 -12
  20. package/dist/lib/services-v2/service-catalog.js.map +1 -1
  21. package/dist/lib/services-v2/services-v2.js +257 -36
  22. package/dist/lib/services-v2/services-v2.js.map +1 -1
  23. package/dist/plugins/logger.js +1 -1
  24. package/dist/webex-core.js +2 -2
  25. package/dist/webex-core.js.map +1 -1
  26. package/package.json +13 -13
  27. package/src/config.js +27 -0
  28. package/src/credentials-config.js +13 -0
  29. package/src/interceptors/redirect.js +4 -1
  30. package/src/lib/batcher.js +25 -10
  31. package/src/lib/credentials/credentials.js +50 -3
  32. package/src/lib/domains.ts +94 -0
  33. package/src/lib/services/service-catalog.js +6 -10
  34. package/src/lib/services/services.js +230 -20
  35. package/src/lib/services-v2/service-catalog.ts +6 -11
  36. package/src/lib/services-v2/services-v2.ts +234 -20
  37. package/test/fixtures/activation-email.ts +22 -0
  38. package/test/integration/spec/services/services.js +183 -112
  39. package/test/integration/spec/services-v2/services-v2.js +169 -98
  40. package/test/unit/spec/credentials/credentials.js +133 -2
  41. package/test/unit/spec/interceptors/auth.js +56 -0
  42. package/test/unit/spec/lib/batcher.js +56 -0
  43. package/test/unit/spec/services/service-catalog.js +93 -11
  44. package/test/unit/spec/services/services.js +491 -2
  45. package/test/unit/spec/services-v2/service-catalog.ts +93 -11
  46. package/test/unit/spec/services-v2/services-v2.ts +498 -0
@@ -73,6 +73,21 @@ var Services = _webexPlugin.default.extend({
73
73
  validateDomains: ['boolean', false, true],
74
74
  initFailed: ['boolean', false, false]
75
75
  },
76
+ session: {
77
+ /**
78
+ * Becomes `true` once the initial catalog collection has completed
79
+ * (successfully or otherwise) and any in-flight credentials refresh has
80
+ * settled. Blocks `webex.ready` so consumers can rely on `webex.ready`
81
+ * implying "catalogs populated AND credential state stable".
82
+ * @instance
83
+ * @memberof Services
84
+ * @type {boolean}
85
+ */
86
+ ready: {
87
+ default: false,
88
+ type: 'boolean'
89
+ }
90
+ },
76
91
  _catalogs: new _weakMap.default(),
77
92
  _serviceUrls: null,
78
93
  _hostCatalog: null,
@@ -243,6 +258,24 @@ var Services = _webexPlugin.default.extend({
243
258
  var hostCatalog = this._hostCatalog || {};
244
259
  return !!((_hostCatalog$host = hostCatalog[host]) !== null && _hostCatalog$host !== void 0 && _hostCatalog$host.length);
245
260
  },
261
+ /**
262
+ * Checks if the current environment is an integration (INT) environment
263
+ * by examining the u2c discovery URL from webex config.
264
+ * INT environments use discovery URLs containing 'intb' (e.g., u2c-intb.ciscospark.com).
265
+ * @returns {boolean} True if INT environment, false otherwise
266
+ */
267
+ isIntegrationEnvironment: function isIntegrationEnvironment() {
268
+ try {
269
+ var _this$webex, _this$webex$config, _this$webex$config$se, _this$webex$config$se2;
270
+ var u2cUrl = ((_this$webex = this.webex) === null || _this$webex === void 0 ? void 0 : (_this$webex$config = _this$webex.config) === null || _this$webex$config === void 0 ? void 0 : (_this$webex$config$se = _this$webex$config.services) === null || _this$webex$config$se === void 0 ? void 0 : (_this$webex$config$se2 = _this$webex$config$se.discovery) === null || _this$webex$config$se2 === void 0 ? void 0 : _this$webex$config$se2.u2c) || '';
271
+ var isInt = u2cUrl.includes('intb');
272
+ this.logger.info("services: isIntegrationEnvironment: ".concat(isInt));
273
+ return isInt;
274
+ } catch (error) {
275
+ this.logger.error('services: failed to determine integration environment', error);
276
+ return false;
277
+ }
278
+ },
246
279
  /**
247
280
  * Merge provided active cluster mappings into current state.
248
281
  * @param {Record<string,string>} activeServices
@@ -1042,9 +1075,11 @@ var Services = _webexPlugin.default.extend({
1042
1075
  authorization: token
1043
1076
  };
1044
1077
  }
1045
- return this.webex.internal.newMetrics.callDiagnosticLatencies.measureLatency(function () {
1078
+ var sendRequest = function sendRequest() {
1046
1079
  return _this7.request(requestObject);
1047
- }, 'internal.get.u2c.time').then(function (_ref12) {
1080
+ };
1081
+ var responsePromise = this.webex.internal.newMetrics ? this.webex.internal.newMetrics.callDiagnosticLatencies.measureLatency(sendRequest, 'internal.get.u2c.time') : sendRequest();
1082
+ return responsePromise.then(function (_ref12) {
1048
1083
  var body = _ref12.body;
1049
1084
  return body;
1050
1085
  });
@@ -1271,9 +1306,9 @@ var Services = _webexPlugin.default.extend({
1271
1306
  initConfig: function initConfig() {
1272
1307
  // Get the catalog and destructure the services config.
1273
1308
  var catalog = this._getCatalog();
1274
- var _this$webex$config = this.webex.config,
1275
- services = _this$webex$config.services,
1276
- fedramp = _this$webex$config.fedramp;
1309
+ var _this$webex$config2 = this.webex.config,
1310
+ services = _this$webex$config2.services,
1311
+ fedramp = _this$webex$config2.fedramp;
1277
1312
 
1278
1313
  // Validate that the services configuration exists.
1279
1314
  if (services) {
@@ -1332,6 +1367,7 @@ var Services = _webexPlugin.default.extend({
1332
1367
 
1333
1368
  // Destructure the credentials plugin.
1334
1369
  var credentials = this.webex.credentials;
1370
+ var catalog = this._getCatalog();
1335
1371
 
1336
1372
  // Init a promise chain. Must be done as a Promise.resolve() to allow
1337
1373
  // credentials#getOrgId() to properly throw.
@@ -1348,9 +1384,13 @@ var Services = _webexPlugin.default.extend({
1348
1384
  }).then(function () {
1349
1385
  // Validate if the token is authorized.
1350
1386
  if (credentials.canAuthorize) {
1351
- // Attempt to collect the postauth catalog.
1352
-
1353
- return _this0.updateServices().catch(function () {
1387
+ // Attempt to collect the postauth catalog, then mark the catalog
1388
+ // ready. Setting `isReady` here - rather than only in the init
1389
+ // callers - means a slow postauth fetch that loses the gated-init
1390
+ // timeout race still marks the catalog ready once it completes.
1391
+ return _this0.updateServices().then(function () {
1392
+ catalog.isReady = true;
1393
+ }).catch(function () {
1354
1394
  _this0.initFailed = true;
1355
1395
  _this0.logger.warn('services: cannot retrieve postauth catalog');
1356
1396
  });
@@ -1360,6 +1400,62 @@ var Services = _webexPlugin.default.extend({
1360
1400
  return _promise.default.resolve();
1361
1401
  });
1362
1402
  },
1403
+ /**
1404
+ * Await any in-flight credentials refresh, then flip `services.ready` so
1405
+ * `webex.ready` can fire. Closes the parallel-refresh window: if a credential
1406
+ * refresh is in flight when initial catalog collection settles, we must not
1407
+ * signal ready until the refresh has resolved - otherwise downstream
1408
+ * consumers may observe `canAuthorize`/token state that is about to change
1409
+ * under them.
1410
+ *
1411
+ * @private
1412
+ * @returns {Promise<void>}
1413
+ */
1414
+ _finalizeReady: function _finalizeReady() {
1415
+ var _this1 = this;
1416
+ return (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee4() {
1417
+ var credentials;
1418
+ return _regenerator.default.wrap(function (_context4) {
1419
+ while (1) switch (_context4.prev = _context4.next) {
1420
+ case 0:
1421
+ credentials = _this1.webex.credentials;
1422
+ if (!(credentials && credentials.isRefreshing)) {
1423
+ _context4.next = 1;
1424
+ break;
1425
+ }
1426
+ _context4.next = 1;
1427
+ return new _promise.default(function (resolve) {
1428
+ credentials.once('change:isRefreshing', resolve);
1429
+ });
1430
+ case 1:
1431
+ _this1.ready = true;
1432
+ case 2:
1433
+ case "end":
1434
+ return _context4.stop();
1435
+ }
1436
+ }, _callee4);
1437
+ }))();
1438
+ },
1439
+ /**
1440
+ * Build a promise that rejects once the catalog init timeout elapses. Race
1441
+ * this against catalog collection so a hung request never leaves
1442
+ * `services.ready` false forever - that would stall `webex.ready` and leave
1443
+ * consumers waiting on it indefinitely. Timeout is configurable via
1444
+ * `config.services.catalogInitTimeout` (defaults to 15s in config). Created
1445
+ * lazily so paths that skip catalog collection never schedule a stray timer.
1446
+ *
1447
+ * @private
1448
+ * @returns {Promise<never>}
1449
+ */
1450
+ _makeInitTimeout: function _makeInitTimeout() {
1451
+ var _this$webex$config3, _this$webex$config3$s;
1452
+ var initTimeoutMs = (_this$webex$config3 = this.webex.config) === null || _this$webex$config3 === void 0 ? void 0 : (_this$webex$config3$s = _this$webex$config3.services) === null || _this$webex$config3$s === void 0 ? void 0 : _this$webex$config3$s.catalogInitTimeout;
1453
+ return new _promise.default(function (_, reject) {
1454
+ setTimeout(function () {
1455
+ return reject(new Error("services: init timed out after ".concat(initTimeoutMs, "ms")));
1456
+ }, initTimeoutMs);
1457
+ });
1458
+ },
1363
1459
  /**
1364
1460
  * Initializer
1365
1461
  *
@@ -1368,7 +1464,7 @@ var Services = _webexPlugin.default.extend({
1368
1464
  * @returns {Services}
1369
1465
  */
1370
1466
  initialize: function initialize() {
1371
- var _this1 = this;
1467
+ var _this10 = this;
1372
1468
  var catalog = new _serviceCatalog.default();
1373
1469
  var registry = new _serviceRegistry.default();
1374
1470
  var state = new _serviceState.default();
@@ -1376,56 +1472,176 @@ var Services = _webexPlugin.default.extend({
1376
1472
  this.registries.set(this.webex, registry);
1377
1473
  this.states.set(this.webex, state);
1378
1474
 
1379
- // Listen for configuration changes once.
1475
+ // Listen for configuration changes once. The config is not populated on the
1476
+ // webex instance until the `change:config` event fires, so any decision that
1477
+ // depends on config values (such as the gated-vs-ungated init below) must be
1478
+ // made from within this handler rather than synchronously in `initialize()`.
1380
1479
  this.listenToOnce(this.webex, 'change:config', function () {
1381
- _this1.initConfig();
1382
- });
1480
+ var _this10$webex$config, _this10$webex$config$;
1481
+ _this10.initConfig();
1383
1482
 
1483
+ // Feature flag: when enabled, `webex.ready` is blocked until the initial
1484
+ // catalog collection has settled AND any in-flight credentials refresh has
1485
+ // completed. When disabled (the default), preserves the pre-existing
1486
+ // behavior where `webex.ready` fires as soon as `webex.loaded` does and
1487
+ // the catalog is collected out-of-band.
1488
+ var waitForCatalogInit = ((_this10$webex$config = _this10.webex.config) === null || _this10$webex$config === void 0 ? void 0 : (_this10$webex$config$ = _this10$webex$config.services) === null || _this10$webex$config$ === void 0 ? void 0 : _this10$webex$config$.waitForCatalogInit) === true;
1489
+ if (waitForCatalogInit) {
1490
+ _this10._initializeCatalogsGated(catalog);
1491
+ } else {
1492
+ // Not gating - immediately mark ready so we do not block webex.ready.
1493
+ _this10.ready = true;
1494
+ _this10._initializeCatalogsUngated(catalog);
1495
+ }
1496
+ });
1497
+ },
1498
+ /**
1499
+ * Original (pre-verified-ready) initialization path. Runs on `webex.ready`
1500
+ * and collects catalogs opportunistically without blocking anything.
1501
+ *
1502
+ * @private
1503
+ * @param {ServiceCatalog} catalog
1504
+ * @returns {void}
1505
+ */
1506
+ _initializeCatalogsUngated: function _initializeCatalogsUngated(catalog) {
1507
+ var _this11 = this;
1384
1508
  // wait for webex instance to be ready before attempting
1385
1509
  // to update the service catalogs
1386
1510
  // this can cause a race condition because credentials may
1387
1511
  // not be valid when services is initialized
1388
- this.listenToOnce(this.webex, 'ready', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee4() {
1389
- var cachedCatalog, supertoken, email;
1390
- return _regenerator.default.wrap(function (_context4) {
1391
- while (1) switch (_context4.prev = _context4.next) {
1512
+ this.listenToOnce(this.webex, 'ready', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee5() {
1513
+ var cachedCatalog, supertoken, _this11$webex$config, _this11$webex$config$, email;
1514
+ return _regenerator.default.wrap(function (_context5) {
1515
+ while (1) switch (_context5.prev = _context5.next) {
1392
1516
  case 0:
1393
- _context4.next = 1;
1394
- return _this1._loadCatalogFromCache();
1517
+ _context5.next = 1;
1518
+ return _this11._loadCatalogFromCache();
1519
+ case 1:
1520
+ cachedCatalog = _context5.sent;
1521
+ if (!cachedCatalog) {
1522
+ _context5.next = 2;
1523
+ break;
1524
+ }
1525
+ catalog.isReady = true;
1526
+ return _context5.abrupt("return");
1527
+ case 2:
1528
+ supertoken = _this11.webex.credentials.supertoken; // Validate if the supertoken exists.
1529
+ if (!(supertoken && supertoken.access_token)) {
1530
+ _context5.next = 3;
1531
+ break;
1532
+ }
1533
+ // `initServiceCatalogs` marks the catalog ready internally once the
1534
+ // postauth catalog is collected.
1535
+ _this11.initServiceCatalogs().catch(function (error) {
1536
+ _this11.initFailed = true;
1537
+ _this11.logger.error("services: failed to init initial services when credentials available, ".concat(error === null || error === void 0 ? void 0 : error.message));
1538
+ });
1539
+ _context5.next = 5;
1540
+ break;
1541
+ case 3:
1542
+ email = _this11.webex.config.email;
1543
+ if (!(((_this11$webex$config = _this11.webex.config) === null || _this11$webex$config === void 0 ? void 0 : (_this11$webex$config$ = _this11$webex$config.services) === null || _this11$webex$config$ === void 0 ? void 0 : _this11$webex$config$.skipPreauthCatalogOnUnauthenticated) === true)) {
1544
+ _context5.next = 4;
1545
+ break;
1546
+ }
1547
+ _this11.logger.info('services: skipping preauth catalog collection while unauthenticated as per the config');
1548
+ return _context5.abrupt("return");
1549
+ case 4:
1550
+ _this11.collectPreauthCatalog(email ? {
1551
+ email: email
1552
+ } : undefined).catch(function (error) {
1553
+ _this11.initFailed = true;
1554
+ _this11.logger.error("services: failed to init initial services when no credentials available, ".concat(error === null || error === void 0 ? void 0 : error.message));
1555
+ });
1556
+ case 5:
1557
+ case "end":
1558
+ return _context5.stop();
1559
+ }
1560
+ }, _callee5);
1561
+ })));
1562
+ },
1563
+ /**
1564
+ * Verified-ready initialization path. Blocks `webex.ready` until the initial
1565
+ * catalog fetch has settled (or timed out) AND any in-flight credentials
1566
+ * refresh has completed. Also handles the fresh-login case where OAuth
1567
+ * completes after `loaded` fires.
1568
+ *
1569
+ * @private
1570
+ * @param {ServiceCatalog} catalog
1571
+ * @returns {void}
1572
+ */
1573
+ _initializeCatalogsGated: function _initializeCatalogsGated(catalog) {
1574
+ var _this12 = this;
1575
+ // Wait for storage to be loaded before attempting to update the service
1576
+ // catalogs. We listen for 'loaded' instead of 'ready' because `services.ready`
1577
+ // now blocks `webex.ready` - listening to 'ready' would deadlock.
1578
+ this.listenToOnce(this.webex, 'loaded', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee6() {
1579
+ var cachedCatalog, supertoken, _this12$webex$config, _this12$webex$config$, email;
1580
+ return _regenerator.default.wrap(function (_context6) {
1581
+ while (1) switch (_context6.prev = _context6.next) {
1582
+ case 0:
1583
+ _context6.next = 1;
1584
+ return _this12._loadCatalogFromCache();
1395
1585
  case 1:
1396
- cachedCatalog = _context4.sent;
1586
+ cachedCatalog = _context6.sent;
1397
1587
  if (!cachedCatalog) {
1398
- _context4.next = 2;
1588
+ _context6.next = 3;
1399
1589
  break;
1400
1590
  }
1401
1591
  catalog.isReady = true;
1402
- return _context4.abrupt("return");
1592
+ _context6.next = 2;
1593
+ return _this12._finalizeReady();
1403
1594
  case 2:
1404
- supertoken = _this1.webex.credentials.supertoken; // Validate if the supertoken exists.
1595
+ return _context6.abrupt("return");
1596
+ case 3:
1597
+ supertoken = _this12.webex.credentials.supertoken; // Validate if the supertoken exists.
1405
1598
  if (supertoken && supertoken.access_token) {
1406
- _this1.initServiceCatalogs().then(function () {
1407
- catalog.isReady = true;
1408
- }).catch(function (error) {
1409
- _this1.initFailed = true;
1410
- _this1.logger.error("services: failed to init initial services when credentials available, ".concat(error === null || error === void 0 ? void 0 : error.message));
1599
+ // `initServiceCatalogs` marks the catalog ready internally once the
1600
+ // postauth catalog is collected - even if it loses the timeout race
1601
+ // below, so a slow fetch still eventually flips `catalog.isReady`.
1602
+ _promise.default.race([_this12.initServiceCatalogs(), _this12._makeInitTimeout()]).catch(function (error) {
1603
+ _this12.initFailed = true;
1604
+ _this12.logger.error("services: failed to init initial services when credentials available, ".concat(error === null || error === void 0 ? void 0 : error.message));
1605
+ }).finally(function () {
1606
+ return _this12._finalizeReady();
1411
1607
  });
1412
1608
  } else {
1413
- email = _this1.webex.config.email;
1414
- _this1.collectPreauthCatalog(email ? {
1415
- email: email
1416
- } : undefined).catch(function (error) {
1417
- _this1.initFailed = true;
1418
- _this1.logger.error("services: failed to init initial services when no credentials available, ".concat(error === null || error === void 0 ? void 0 : error.message));
1609
+ email = _this12.webex.config.email; // Handle fresh login: 'loaded' fires before OAuth completes, so listen
1610
+ // for `canAuthorize` flipping true and then collect the postauth catalog.
1611
+ _this12.listenToOnce(_this12.webex, 'change:canAuthorize', function () {
1612
+ if (_this12.webex.canAuthorize && !catalog.status.postauth.ready) {
1613
+ // `initServiceCatalogs` marks the catalog ready internally.
1614
+ _this12.initServiceCatalogs().catch(function (error) {
1615
+ _this12.logger.error("services: failed to init service catalogs after auth, ".concat(error === null || error === void 0 ? void 0 : error.message));
1616
+ });
1617
+ }
1419
1618
  });
1619
+ if (((_this12$webex$config = _this12.webex.config) === null || _this12$webex$config === void 0 ? void 0 : (_this12$webex$config$ = _this12$webex$config.services) === null || _this12$webex$config$ === void 0 ? void 0 : _this12$webex$config$.skipPreauthCatalogOnUnauthenticated) === true) {
1620
+ // Skip the preauth catalog fetch (it will be collected manually
1621
+ // later), but still finalize `services.ready` so `webex.ready` is not
1622
+ // stalled while unauthenticated. No timeout is created here so there
1623
+ // is no stray timer or unhandled rejection.
1624
+ _this12.logger.info('services: skipping preauth catalog collection while unauthenticated as per the config');
1625
+ _this12._finalizeReady();
1626
+ } else {
1627
+ _promise.default.race([_this12.collectPreauthCatalog(email ? {
1628
+ email: email
1629
+ } : undefined), _this12._makeInitTimeout()]).catch(function (error) {
1630
+ _this12.initFailed = true;
1631
+ _this12.logger.error("services: failed to init initial services when no credentials available, ".concat(error === null || error === void 0 ? void 0 : error.message));
1632
+ }).finally(function () {
1633
+ return _this12._finalizeReady();
1634
+ });
1635
+ }
1420
1636
  }
1421
- case 3:
1637
+ case 4:
1422
1638
  case "end":
1423
- return _context4.stop();
1639
+ return _context6.stop();
1424
1640
  }
1425
- }, _callee4);
1641
+ }, _callee6);
1426
1642
  })));
1427
1643
  },
1428
- version: "0.0.0"
1644
+ version: "3.12.0-auth-prejoin-fetch.1"
1429
1645
  });
1430
1646
  /* eslint-enable no-underscore-dangle */
1431
1647
  var _default = exports.default = Services;