@webex/webex-core 3.12.0 → 3.13.0-next.10

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 (92) hide show
  1. package/.repo-context.json +653 -0
  2. package/.sdd/manifest.json +615 -0
  3. package/AGENTS.md +209 -0
  4. package/README.md +6 -3
  5. package/dist/config.js +46 -1
  6. package/dist/config.js.map +1 -1
  7. package/dist/credentials-config.js +21 -1
  8. package/dist/credentials-config.js.map +1 -1
  9. package/dist/index.js +14 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/interceptors/auth.js +10 -1
  12. package/dist/interceptors/auth.js.map +1 -1
  13. package/dist/interceptors/catalog-url.js +87 -0
  14. package/dist/interceptors/catalog-url.js.map +1 -0
  15. package/dist/interceptors/payload-transformer.js +25 -4
  16. package/dist/interceptors/payload-transformer.js.map +1 -1
  17. package/dist/lib/batcher.js +23 -7
  18. package/dist/lib/batcher.js.map +1 -1
  19. package/dist/lib/credentials/credentials.js +117 -5
  20. package/dist/lib/credentials/credentials.js.map +1 -1
  21. package/dist/lib/credentials/token.js +23 -9
  22. package/dist/lib/credentials/token.js.map +1 -1
  23. package/dist/lib/domains.js +90 -0
  24. package/dist/lib/domains.js.map +1 -0
  25. package/dist/lib/interceptors/service.js +5 -1
  26. package/dist/lib/interceptors/service.js.map +1 -1
  27. package/dist/lib/services/catalog-auth.js +114 -0
  28. package/dist/lib/services/catalog-auth.js.map +1 -0
  29. package/dist/lib/services/service-catalog.js +108 -18
  30. package/dist/lib/services/service-catalog.js.map +1 -1
  31. package/dist/lib/services/services.js +287 -40
  32. package/dist/lib/services/services.js.map +1 -1
  33. package/dist/lib/services-v2/service-catalog.js +14 -13
  34. package/dist/lib/services-v2/service-catalog.js.map +1 -1
  35. package/dist/lib/services-v2/services-v2.js +294 -44
  36. package/dist/lib/services-v2/services-v2.js.map +1 -1
  37. package/dist/lib/static-asset-loader.js +153 -0
  38. package/dist/lib/static-asset-loader.js.map +1 -0
  39. package/dist/plugins/logger.js +1 -1
  40. package/dist/webex-core.js +11 -3
  41. package/dist/webex-core.js.map +1 -1
  42. package/docs/adr/index.md +32 -0
  43. package/docs/architecture.md +298 -0
  44. package/docs/getting-started.md +96 -0
  45. package/docs/index.md +55 -0
  46. package/docs/specs/README.md +76 -0
  47. package/package.json +13 -13
  48. package/schemas/repo-context.schema.json +796 -0
  49. package/src/config.js +50 -1
  50. package/src/credentials-config.js +22 -0
  51. package/src/docs/README.md +732 -0
  52. package/src/index.js +2 -0
  53. package/src/interceptors/auth.js +14 -1
  54. package/src/interceptors/catalog-url.js +66 -0
  55. package/src/interceptors/docs/README.md +658 -0
  56. package/src/interceptors/payload-transformer.js +23 -1
  57. package/src/lib/batcher.js +25 -10
  58. package/src/lib/credentials/credentials.js +106 -4
  59. package/src/lib/credentials/docs/README.md +847 -0
  60. package/src/lib/credentials/token.js +15 -8
  61. package/src/lib/domains.ts +94 -0
  62. package/src/lib/interceptors/service.js +11 -3
  63. package/src/lib/services/catalog-auth.js +112 -0
  64. package/src/lib/services/docs/README.md +737 -0
  65. package/src/lib/services/service-catalog.js +112 -16
  66. package/src/lib/services/services.js +274 -24
  67. package/src/lib/services-v2/docs/README.md +751 -0
  68. package/src/lib/services-v2/service-catalog.ts +16 -12
  69. package/src/lib/services-v2/services-v2.ts +282 -26
  70. package/src/lib/static-asset-loader.ts +128 -0
  71. package/src/lib/storage/docs/README.md +646 -0
  72. package/src/webex-core.js +8 -0
  73. package/test/fixtures/activation-email.ts +22 -0
  74. package/test/integration/spec/credentials/credentials.js +3 -1
  75. package/test/integration/spec/credentials/token.js +8 -0
  76. package/test/integration/spec/services/service-catalog.js +5 -0
  77. package/test/integration/spec/services/services.js +183 -112
  78. package/test/integration/spec/services-v2/services-v2.js +169 -98
  79. package/test/integration/spec/unit-browser/token.js +7 -2
  80. package/test/unit/spec/credentials/credentials.js +326 -3
  81. package/test/unit/spec/credentials/token.js +259 -6
  82. package/test/unit/spec/interceptors/auth.js +92 -1
  83. package/test/unit/spec/interceptors/catalog-url.js +224 -0
  84. package/test/unit/spec/interceptors/payload-transformer.js +119 -1
  85. package/test/unit/spec/lib/batcher.js +56 -0
  86. package/test/unit/spec/services/interceptors/service.js +35 -1
  87. package/test/unit/spec/services/service-catalog.js +368 -11
  88. package/test/unit/spec/services/services.js +783 -10
  89. package/test/unit/spec/services-v2/service-catalog.ts +134 -11
  90. package/test/unit/spec/services-v2/services-v2.ts +825 -10
  91. package/test/unit/spec/static-asset-loader.ts +224 -0
  92. package/test/unit/spec/webex-core.js +77 -1
@@ -259,11 +259,11 @@ const Token = WebexPlugin.extend({
259
259
  return Promise.reject(new Error('cannot downscope access token'));
260
260
  }
261
261
 
262
- if (diffScopes(scope, this.config.scope) !== '') {
262
+ const availableScope = this.scope || this.config.scope;
263
+
264
+ if (diffScopes(scope, availableScope) !== '') {
263
265
  return Promise.reject(
264
- new Error(
265
- `new scope (${scope}) is not subset of the available scopes (${this.config.scope})`
266
- )
266
+ new Error(`new scope (${scope}) is not subset of the available scopes (${availableScope})`)
267
267
  );
268
268
  }
269
269
 
@@ -276,7 +276,7 @@ const Token = WebexPlugin.extend({
276
276
  // Ideally, we could depend on the service to communicate this error, but
277
277
  // all we get is "invalid scope", which, to the lay person, implies
278
278
  // something wrong with *one* of the scopes, not the whole thing.
279
- if (scope === sortScope(this.config.scope)) {
279
+ if (scope === sortScope(availableScope)) {
280
280
  return Promise.reject(new Error('token: scope reduction requires a reduced scope'));
281
281
  }
282
282
 
@@ -339,9 +339,12 @@ const Token = WebexPlugin.extend({
339
339
  * {@link config.credentials.refreshCallback()}
340
340
  * @instance
341
341
  * @memberof Token
342
+ * @param {Object} [options={}]
343
+ * @param {string} [options.scope] Scope to request on refresh, if same or narrower than existing scope
344
+ * @param {string} [options.upgradeScope] Scope to request on refresh, if broader than existing scope
342
345
  * @returns {Promise<Token>}
343
346
  */
344
- refresh() {
347
+ refresh({scope, upgradeScope} = {}) {
345
348
  if (!this.canRefresh) {
346
349
  throw new Error('Not enough information available to refresh this access token');
347
350
  }
@@ -353,7 +356,9 @@ const Token = WebexPlugin.extend({
353
356
  throw new Error('Cannot refresh access token without refreshCallback');
354
357
  }
355
358
 
356
- promise = Promise.resolve(this.config.refreshCallback(this.webex, this));
359
+ promise = Promise.resolve(
360
+ this.config.refreshCallback(this.webex, this, {scope, upgradeScope})
361
+ );
357
362
  }
358
363
 
359
364
  return (
@@ -366,6 +371,8 @@ const Token = WebexPlugin.extend({
366
371
  grant_type: 'refresh_token',
367
372
  redirect_uri: this.config.redirect_uri,
368
373
  refresh_token: this.refresh_token,
374
+ ...(scope && {scope}),
375
+ ...(upgradeScope && {upgrade_scope: upgradeScope}),
369
376
  },
370
377
  auth: {
371
378
  user: this.config.client_id,
@@ -409,7 +416,7 @@ const Token = WebexPlugin.extend({
409
416
  }
410
417
 
411
418
  obj.previousToken = this;
412
- obj.scope = this.scope;
419
+ obj.scope = obj.scope || scope || this.scope;
413
420
 
414
421
  return new Token(obj, {parent: this.parent});
415
422
  })
@@ -0,0 +1,94 @@
1
+ import Url from 'url';
2
+
3
+ import {uniq} from 'lodash';
4
+
5
+ // Canonicalise a hostname for comparison: lowercase, drop the brackets around
6
+ // an IPv6 literal, and drop leading/trailing dots. DNS treats `Example.com`,
7
+ // `example.com.` and `example.com` as the same name.
8
+ //
9
+ // Node's `url.domainToASCII` looks like the standard way to do this, but the
10
+ // `url` polyfill this package bundles for the browser does not implement it,
11
+ // so it cannot be used here. It also leaves trailing dots in place.
12
+ const normalizeHostname = (value: string): string =>
13
+ typeof value === 'string'
14
+ ? value
15
+ .toLowerCase()
16
+ .replace(/^\[|\]$/g, '')
17
+ .replace(/^\.+/, '')
18
+ .replace(/\.+$/, '')
19
+ : '';
20
+
21
+ /**
22
+ * Canonicalise a list of configured allowed domains, discarding any entry that
23
+ * is not a usable hostname. Callers normalise on the way in so the stored list
24
+ * is already canonical, rather than re-deriving it on every request.
25
+ *
26
+ * @param {Array<string>} allowedDomains - The configured allowed domains.
27
+ * @returns {Array<string>} - Normalized, de-duplicated, non-empty entries.
28
+ */
29
+ export const normalizeAllowedDomains = (allowedDomains: Array<string>): Array<string> =>
30
+ uniq(
31
+ (Array.isArray(allowedDomains) ? allowedDomains : []).map(normalizeHostname).filter(Boolean)
32
+ );
33
+
34
+ /**
35
+ * Determine if a hostname is covered by an allowed domain, matching only on DNS
36
+ * label boundaries, so that a hostname is allowed only when it is the domain
37
+ * itself or a subdomain of it. Matching on a substring instead would treat
38
+ * unrelated hostnames that merely contain the domain as allowed.
39
+ *
40
+ * @param {string} hostname - Hostname to test. Must not include a port.
41
+ * @param {string} allowedDomain - The configured allowed domain.
42
+ * @returns {boolean} - True when the hostname is the domain or a subdomain of it.
43
+ */
44
+ const hostnameMatchesDomain = (hostname: string, allowedDomain: string): boolean => {
45
+ // The stored list is normalized on write, but `allowedDomains` is a public
46
+ // property, so normalize again here rather than trust it.
47
+ const host = normalizeHostname(hostname);
48
+ const domain = normalizeHostname(allowedDomain);
49
+
50
+ return !!host && !!domain && (host === domain || host.endsWith(`.${domain}`));
51
+ };
52
+
53
+ /**
54
+ * Find the allowed domain covering a url, or `undefined` if there is none.
55
+ *
56
+ * Parsing lives here rather than in the callers, and deliberately uses both url
57
+ * parsers, because this check gates an `Authorization` header. The two
58
+ * transports behind `@webex/http-core` do not use the same url parser: the
59
+ * browser transport parses per WHATWG, the node transport uses Node's legacy
60
+ * `Url.parse`, and for some inputs the two resolve different hosts.
61
+ *
62
+ * Rather than picking one, require both to agree and fail closed when they do
63
+ * not, so this check can never authorize a host that differs from the one a
64
+ * transport would actually connect to. Do not narrow this to a single parser.
65
+ *
66
+ * @param {string} url - The url to match the allowed domains against.
67
+ * @param {Array<string>} allowedDomains - The configured allowed domains.
68
+ * @returns {string} - The matching allowed domain, or undefined if there is none.
69
+ */
70
+ export const matchAllowedDomain = (
71
+ url: string,
72
+ allowedDomains: Array<string>
73
+ ): string | undefined => {
74
+ let hostname: string;
75
+ let legacyHostname: string;
76
+
77
+ try {
78
+ ({hostname} = new URL(url));
79
+ ({hostname: legacyHostname} = Url.parse(url));
80
+ } catch {
81
+ // Not a parsable absolute url, so it cannot belong to an allowed domain.
82
+ return undefined;
83
+ }
84
+
85
+ if (normalizeHostname(hostname) !== normalizeHostname(legacyHostname)) {
86
+ return undefined;
87
+ }
88
+
89
+ return (allowedDomains || []).find((allowedDomain) =>
90
+ hostnameMatchesDomain(hostname, allowedDomain)
91
+ );
92
+ };
93
+
94
+ export default matchAllowedDomain;
@@ -4,6 +4,8 @@
4
4
 
5
5
  import {Interceptor} from '@webex/http-core';
6
6
 
7
+ import {isCatalogAuthReason} from '../services/catalog-auth';
8
+
7
9
  const trailingSlashes = /(?:^\/)|(?:\/$)/;
8
10
 
9
11
  /**
@@ -47,9 +49,15 @@ export default class ServiceInterceptor extends Interceptor {
47
49
 
48
50
  return options;
49
51
  })
50
- .catch(() =>
51
- Promise.reject(new Error(`service-interceptor: '${service}' is not a known service`))
52
- );
52
+ .catch((error) => {
53
+ if (error && isCatalogAuthReason(error.reason)) {
54
+ return Promise.reject(error);
55
+ }
56
+
57
+ return Promise.reject(
58
+ new Error(`service-interceptor: '${service}' is not a known service`)
59
+ );
60
+ });
53
61
  }
54
62
 
55
63
  /* eslint-disable class-methods-use-this */
@@ -0,0 +1,112 @@
1
+ import {OAuthError} from '../credentials/grant-errors';
2
+
3
+ export const INVALID_ACCESS_TOKEN = 'invalid_access_token';
4
+ export const ACCESS_DENIED = 'access_denied';
5
+
6
+ const catalogAuthFailures = new WeakMap();
7
+ const catalogAuthReplayLimit = /^Failed after \d+ replay attempts$/;
8
+ const reasonByStatus = {
9
+ 401: INVALID_ACCESS_TOKEN,
10
+ 403: ACCESS_DENIED,
11
+ };
12
+
13
+ /**
14
+ * A catalog 401 can arrive as an OAuth refresh failure or the auth replay limit.
15
+ * @param {Error} error
16
+ * @returns {boolean}
17
+ */
18
+ export function isWrappedCatalogAuthFailure(error) {
19
+ return (
20
+ error instanceof OAuthError ||
21
+ (typeof error?.message === 'string' && catalogAuthReplayLimit.test(error.message))
22
+ );
23
+ }
24
+
25
+ /**
26
+ * @param {string} reason
27
+ * @returns {boolean}
28
+ */
29
+ export function isCatalogAuthReason(reason) {
30
+ return reason === INVALID_ACCESS_TOKEN || reason === ACCESS_DENIED;
31
+ }
32
+
33
+ /**
34
+ * Classify a postauth catalog failure.
35
+ * A result is stored. Null means any previously stored failure should be cleared.
36
+ * Network errors and HTTP 5xx stay null.
37
+ * @param {Error} error
38
+ * @returns {{status: number, reason: string}|null}
39
+ */
40
+ export function classifyCatalogAuthFailure(error) {
41
+ const status = error && (error.catalogAuthStatus || error.statusCode || error.status);
42
+ const reason = reasonByStatus[status];
43
+
44
+ if (reason) {
45
+ return {
46
+ status,
47
+ reason,
48
+ };
49
+ }
50
+
51
+ if (isWrappedCatalogAuthFailure(error)) {
52
+ return {
53
+ status: 401,
54
+ reason: INVALID_ACCESS_TOKEN,
55
+ };
56
+ }
57
+
58
+ return null;
59
+ }
60
+
61
+ /**
62
+ * @param {object} services
63
+ * @param {Error} error
64
+ * @returns {void}
65
+ */
66
+ export function recordCatalogAuthFailure(services, error) {
67
+ const authFailure = classifyCatalogAuthFailure(error);
68
+
69
+ if (authFailure) {
70
+ catalogAuthFailures.set(services, authFailure);
71
+
72
+ return;
73
+ }
74
+
75
+ catalogAuthFailures.delete(services);
76
+ }
77
+
78
+ /**
79
+ * @param {object} services
80
+ * @returns {void}
81
+ */
82
+ export function clearCatalogAuthFailure(services) {
83
+ catalogAuthFailures.delete(services);
84
+ }
85
+
86
+ /**
87
+ * @param {object} services
88
+ * @returns {{status: number, reason: string}|null}
89
+ */
90
+ export function getCatalogAuthFailure(services) {
91
+ return catalogAuthFailures.get(services) || null;
92
+ }
93
+
94
+ /**
95
+ * @param {object} services
96
+ * @param {string} fallbackMessage
97
+ * @returns {Error}
98
+ */
99
+ export function serviceLookupError(services, fallbackMessage) {
100
+ const authFailure = getCatalogAuthFailure(services);
101
+
102
+ if (authFailure && isCatalogAuthReason(authFailure.reason)) {
103
+ const authError = new Error(authFailure.reason);
104
+
105
+ authError.reason = authFailure.reason;
106
+ authError.statusCode = authFailure.status;
107
+
108
+ return authError;
109
+ }
110
+
111
+ return new Error(fallbackMessage);
112
+ }