@auth0/auth0-spa-js 2.0.0 → 2.0.2

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.
@@ -1,5 +1,5 @@
1
1
  import { ICache } from './cache';
2
- import { Auth0ClientOptions, AuthorizationParams, AuthorizeOptions } from './global';
2
+ import { Auth0ClientOptions, AuthorizationParams, AuthorizeOptions, LogoutOptions } from './global';
3
3
  /**
4
4
  * @ignore
5
5
  */
@@ -26,3 +26,9 @@ export declare const cacheFactory: (location: string) => () => ICache;
26
26
  export declare const getAuthorizeParams: (clientOptions: Auth0ClientOptions & {
27
27
  authorizationParams: AuthorizationParams;
28
28
  }, scope: string, authorizationParams: AuthorizationParams, state: string, nonce: string, code_challenge: string, redirect_uri: string | undefined, response_mode: string | undefined) => AuthorizeOptions;
29
+ /**
30
+ * @ignore
31
+ *
32
+ * Function used to provide support for the deprecated onRedirect through openUrl.
33
+ */
34
+ export declare const patchOpenUrlWithOnRedirect: <T extends Pick<LogoutOptions, "openUrl" | "onRedirect">>(options: T) => T;
@@ -44,6 +44,9 @@ export declare class MfaRequiredError extends GenericError {
44
44
  mfa_token: string;
45
45
  constructor(error: string, error_description: string, mfa_token: string);
46
46
  }
47
+ /**
48
+ * Error thrown when there is no refresh token to use
49
+ */
47
50
  export declare class MissingRefreshTokenError extends GenericError {
48
51
  audience: string;
49
52
  scope: string;
@@ -264,8 +264,20 @@ export interface RedirectLoginOptions<TAppState = any> extends BaseLoginOptions
264
264
  * window.location.replace(url);
265
265
  * }
266
266
  * });
267
+ * @deprecated since v2.0.1, use `openUrl` instead.
267
268
  */
268
269
  onRedirect?: (url: string) => Promise<void>;
270
+ /**
271
+ * Used to control the redirect and not rely on the SDK to do the actual redirect.
272
+ *
273
+ * @example
274
+ * const client = new Auth0Client({
275
+ * async openUrl(url) {
276
+ * window.location.replace(url);
277
+ * }
278
+ * });
279
+ */
280
+ openUrl?: (url: string) => Promise<void>;
269
281
  }
270
282
  export interface RedirectLoginResult<TAppState = any> {
271
283
  /**
@@ -395,8 +407,22 @@ export interface LogoutOptions extends LogoutUrlOptions {
395
407
  * window.location.replace(url);
396
408
  * }
397
409
  * });
410
+ * @deprecated since v2.0.1, use `openUrl` instead.
398
411
  */
399
412
  onRedirect?: (url: string) => Promise<void>;
413
+ /**
414
+ * Used to control the redirect and not rely on the SDK to do the actual redirect.
415
+ *
416
+ * Set to `false` to disable the redirect, or provide a function to handle the actual redirect yourself.
417
+ *
418
+ * @example
419
+ * await auth0.logout({
420
+ * async openUrl(url) {
421
+ * window.location.replace(url);
422
+ * }
423
+ * });
424
+ */
425
+ openUrl?: false | ((url: string) => Promise<void>);
400
426
  }
401
427
  /**
402
428
  * @ignore
@@ -6,12 +6,12 @@ export * from './global';
6
6
  * Asynchronously creates the Auth0Client instance and calls `checkSession`.
7
7
  *
8
8
  * **Note:** There are caveats to using this in a private browser tab, which may not silently authenticae
9
- * a user on page refresh. Please see [the checkSession docs](https://auth0.github.io/auth0-spa-js/classes/auth0client.html#checksession) for more info.
9
+ * a user on page refresh. Please see [the checkSession docs](https://auth0.github.io/auth0-spa-js/classes/Auth0Client.html#checksession) for more info.
10
10
  *
11
11
  * @param options The client options
12
12
  * @returns An instance of Auth0Client
13
13
  */
14
14
  export declare function createAuth0Client(options: Auth0ClientOptions): Promise<Auth0Client>;
15
15
  export { Auth0Client };
16
- export { GenericError, AuthenticationError, TimeoutError, PopupTimeoutError, PopupCancelledError, MfaRequiredError } from './errors';
16
+ export { GenericError, AuthenticationError, TimeoutError, PopupTimeoutError, PopupCancelledError, MfaRequiredError, MissingRefreshTokenError } from './errors';
17
17
  export { ICache, LocalStorageCache, InMemoryCache, Cacheable, DecodedToken, CacheEntry, WrappedCacheEntry, KeyManifestEntry, MaybePromise, CacheKey, CacheKeyData } from './cache';
@@ -1,5 +1,5 @@
1
1
  import { AuthenticationResult, PopupConfigOptions } from './global';
2
- export declare const parseQueryResult: (queryString: string) => AuthenticationResult;
2
+ export declare const parseAuthenticationResult: (queryString: string) => AuthenticationResult;
3
3
  export declare const runIframe: (authorizeUrl: string, eventOrigin: string, timeoutInSeconds?: number) => Promise<AuthenticationResult>;
4
4
  export declare const openPopup: (url: string) => Window | null;
5
5
  export declare const runPopup: (config: PopupConfigOptions) => Promise<AuthenticationResult>;
@@ -1,2 +1,2 @@
1
- declare const _default: "2.0.0";
1
+ declare const _default: "2.0.2";
2
2
  export default _default;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "name": "@auth0/auth0-spa-js",
4
4
  "description": "Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE",
5
5
  "license": "MIT",
6
- "version": "2.0.0",
6
+ "version": "2.0.2",
7
7
  "main": "dist/lib/auth0-spa-js.cjs.js",
8
8
  "types": "dist/typings/index.d.ts",
9
9
  "module": "dist/auth0-spa-js.production.esm.js",
@@ -41,14 +41,15 @@
41
41
  "@typescript-eslint/eslint-plugin-tslint": "^5.33.1",
42
42
  "@typescript-eslint/parser": "^5.33.1",
43
43
  "babel-jest": "^28.1.3",
44
- "browserstack-cypress-cli": "1.8.1",
45
44
  "browser-tabs-lock": "^1.2.15",
45
+ "browserstack-cypress-cli": "1.8.1",
46
46
  "cli-table": "^0.3.6",
47
47
  "concurrently": "^7.3.0",
48
48
  "cypress": "7.2.0",
49
49
  "es-check": "^7.0.1",
50
50
  "es-cookie": "~1.3.2",
51
51
  "eslint": "^8.22.0",
52
+ "eslint-plugin-security": "^1.5.0",
52
53
  "gzip-size": "^7.0.0",
53
54
  "husky": "^7.0.4",
54
55
  "idtoken-verifier": "^2.2.2",
@@ -57,9 +58,9 @@
57
58
  "jest-fetch-mock": "^3.0.3",
58
59
  "jest-junit": "^14.0.0",
59
60
  "jest-localstorage-mock": "^2.4.22",
60
- "jsonwebtoken": "^8.5.1",
61
+ "jsonwebtoken": "^9.0.0",
61
62
  "node-fetch": "^3.2.10",
62
- "oidc-provider": "^7.11.5",
63
+ "oidc-provider": "^7.14.0",
63
64
  "prettier": "^2.7.1",
64
65
  "pretty-quick": "^3.1.2",
65
66
  "qss": "^2.0.3",
@@ -78,8 +79,6 @@
78
79
  "serve": "^14.0.1",
79
80
  "ts-jest": "^28.0.8",
80
81
  "tslib": "^2.4.0",
81
- "tslint": "^6.1.3",
82
- "tslint-config-security": "^1.16.0",
83
82
  "typedoc": "^0.23.10",
84
83
  "typescript": "^4.7.4",
85
84
  "wait-on": "^6.0.0"
@@ -3,7 +3,7 @@ import Lock from 'browser-tabs-lock';
3
3
  import {
4
4
  createQueryParams,
5
5
  runPopup,
6
- parseQueryResult,
6
+ parseAuthenticationResult,
7
7
  encode,
8
8
  createRandomString,
9
9
  runIframe,
@@ -89,7 +89,8 @@ import {
89
89
  cacheFactory,
90
90
  getAuthorizeParams,
91
91
  GET_TOKEN_SILENTLY_LOCK_KEY,
92
- OLD_IS_AUTHENTICATED_COOKIE_NAME
92
+ OLD_IS_AUTHENTICATED_COOKIE_NAME,
93
+ patchOpenUrlWithOnRedirect
93
94
  } from './Auth0Client.utils';
94
95
 
95
96
  /**
@@ -444,7 +445,8 @@ export class Auth0Client {
444
445
  public async loginWithRedirect<TAppState = any>(
445
446
  options: RedirectLoginOptions<TAppState> = {}
446
447
  ) {
447
- const { onRedirect, fragment, appState, ...urlOptions } = options;
448
+ const { openUrl, fragment, appState, ...urlOptions } =
449
+ patchOpenUrlWithOnRedirect(options);
448
450
 
449
451
  const organizationId =
450
452
  urlOptions.authorizationParams?.organization ||
@@ -462,8 +464,8 @@ export class Auth0Client {
462
464
 
463
465
  const urlWithFragment = fragment ? `${url}#${fragment}` : url;
464
466
 
465
- if (onRedirect) {
466
- await onRedirect(urlWithFragment);
467
+ if (openUrl) {
468
+ await openUrl(urlWithFragment);
467
469
  } else {
468
470
  window.location.assign(urlWithFragment);
469
471
  }
@@ -484,7 +486,7 @@ export class Auth0Client {
484
486
  throw new Error('There are no query params available for parsing.');
485
487
  }
486
488
 
487
- const { state, code, error, error_description } = parseQueryResult(
489
+ const { state, code, error, error_description } = parseAuthenticationResult(
488
490
  queryStringFragments.join('')
489
491
  );
490
492
 
@@ -824,7 +826,7 @@ export class Auth0Client {
824
826
  * @param options
825
827
  */
826
828
  public async logout(options: LogoutOptions = {}): Promise<void> {
827
- const { onRedirect, ...logoutOptions } = options;
829
+ const { openUrl, ...logoutOptions } = patchOpenUrlWithOnRedirect(options);
828
830
 
829
831
  await this.cacheManager.clear();
830
832
 
@@ -838,9 +840,9 @@ export class Auth0Client {
838
840
 
839
841
  const url = this._buildLogoutUrl(logoutOptions);
840
842
 
841
- if (onRedirect) {
842
- await onRedirect(url);
843
- } else {
843
+ if (openUrl) {
844
+ await openUrl(url);
845
+ } else if (openUrl !== false) {
844
846
  window.location.assign(url);
845
847
  }
846
848
  }
@@ -918,7 +920,7 @@ export class Auth0Client {
918
920
  } catch (e) {
919
921
  if (e.error === 'login_required') {
920
922
  this.logout({
921
- onRedirect: async () => {}
923
+ openUrl: false
922
924
  });
923
925
  }
924
926
  throw e;
@@ -2,7 +2,8 @@ import { ICache, InMemoryCache, LocalStorageCache } from './cache';
2
2
  import {
3
3
  Auth0ClientOptions,
4
4
  AuthorizationParams,
5
- AuthorizeOptions
5
+ AuthorizeOptions,
6
+ LogoutOptions
6
7
  } from './global';
7
8
  import { getUniqueScopes } from './scope';
8
9
 
@@ -73,3 +74,23 @@ export const getAuthorizeParams = (
73
74
  code_challenge_method: 'S256'
74
75
  };
75
76
  };
77
+
78
+ /**
79
+ * @ignore
80
+ *
81
+ * Function used to provide support for the deprecated onRedirect through openUrl.
82
+ */
83
+ export const patchOpenUrlWithOnRedirect = <
84
+ T extends Pick<LogoutOptions, 'openUrl' | 'onRedirect'>
85
+ >(
86
+ options: T
87
+ ) => {
88
+ const { openUrl, onRedirect, ...originalOptions } = options;
89
+
90
+ const result = {
91
+ ...originalOptions,
92
+ openUrl: openUrl === false || openUrl ? openUrl : onRedirect
93
+ };
94
+
95
+ return result as T;
96
+ };
package/src/errors.ts CHANGED
@@ -81,6 +81,9 @@ export class MfaRequiredError extends GenericError {
81
81
  }
82
82
  }
83
83
 
84
+ /**
85
+ * Error thrown when there is no refresh token to use
86
+ */
84
87
  export class MissingRefreshTokenError extends GenericError {
85
88
  constructor(public audience: string, public scope: string) {
86
89
  super(
package/src/global.ts CHANGED
@@ -296,8 +296,21 @@ export interface RedirectLoginOptions<TAppState = any>
296
296
  * window.location.replace(url);
297
297
  * }
298
298
  * });
299
+ * @deprecated since v2.0.1, use `openUrl` instead.
299
300
  */
300
301
  onRedirect?: (url: string) => Promise<void>;
302
+
303
+ /**
304
+ * Used to control the redirect and not rely on the SDK to do the actual redirect.
305
+ *
306
+ * @example
307
+ * const client = new Auth0Client({
308
+ * async openUrl(url) {
309
+ * window.location.replace(url);
310
+ * }
311
+ * });
312
+ */
313
+ openUrl?: (url: string) => Promise<void>;
301
314
  }
302
315
 
303
316
  export interface RedirectLoginResult<TAppState = any> {
@@ -442,8 +455,23 @@ export interface LogoutOptions extends LogoutUrlOptions {
442
455
  * window.location.replace(url);
443
456
  * }
444
457
  * });
458
+ * @deprecated since v2.0.1, use `openUrl` instead.
445
459
  */
446
460
  onRedirect?: (url: string) => Promise<void>;
461
+
462
+ /**
463
+ * Used to control the redirect and not rely on the SDK to do the actual redirect.
464
+ *
465
+ * Set to `false` to disable the redirect, or provide a function to handle the actual redirect yourself.
466
+ *
467
+ * @example
468
+ * await auth0.logout({
469
+ * async openUrl(url) {
470
+ * window.location.replace(url);
471
+ * }
472
+ * });
473
+ */
474
+ openUrl?: false | ((url: string) => Promise<void>);
447
475
  }
448
476
 
449
477
  /**
package/src/index.ts CHANGED
@@ -9,7 +9,7 @@ export * from './global';
9
9
  * Asynchronously creates the Auth0Client instance and calls `checkSession`.
10
10
  *
11
11
  * **Note:** There are caveats to using this in a private browser tab, which may not silently authenticae
12
- * a user on page refresh. Please see [the checkSession docs](https://auth0.github.io/auth0-spa-js/classes/auth0client.html#checksession) for more info.
12
+ * a user on page refresh. Please see [the checkSession docs](https://auth0.github.io/auth0-spa-js/classes/Auth0Client.html#checksession) for more info.
13
13
  *
14
14
  * @param options The client options
15
15
  * @returns An instance of Auth0Client
@@ -28,7 +28,8 @@ export {
28
28
  TimeoutError,
29
29
  PopupTimeoutError,
30
30
  PopupCancelledError,
31
- MfaRequiredError
31
+ MfaRequiredError,
32
+ MissingRefreshTokenError
32
33
  } from './errors';
33
34
 
34
35
  export {
package/src/utils.ts CHANGED
@@ -12,24 +12,21 @@ import {
12
12
  PopupCancelledError
13
13
  } from './errors';
14
14
 
15
- export const parseQueryResult = (queryString: string): AuthenticationResult => {
15
+ export const parseAuthenticationResult = (
16
+ queryString: string
17
+ ): AuthenticationResult => {
16
18
  if (queryString.indexOf('#') > -1) {
17
- queryString = queryString.substr(0, queryString.indexOf('#'));
19
+ queryString = queryString.substring(0, queryString.indexOf('#'));
18
20
  }
19
21
 
20
- const queryParams = queryString.split('&');
21
- const parsedQuery: Record<string, any> = {};
22
+ const searchParams = new URLSearchParams(queryString);
22
23
 
23
- queryParams.forEach(qp => {
24
- const [key, val] = qp.split('=');
25
- parsedQuery[key] = decodeURIComponent(val);
26
- });
27
-
28
- if (parsedQuery.expires_in) {
29
- parsedQuery.expires_in = parseInt(parsedQuery.expires_in);
30
- }
31
-
32
- return parsedQuery as AuthenticationResult;
24
+ return {
25
+ state: searchParams.get('state')!,
26
+ code: searchParams.get('code') || undefined,
27
+ error: searchParams.get('error') || undefined,
28
+ error_description: searchParams.get('error_description') || undefined
29
+ };
33
30
  };
34
31
 
35
32
  export const runIframe = (
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export default '2.0.0';
1
+ export default '2.0.2';