@asgardeo/auth-spa 0.3.8 → 0.3.9

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 (68) hide show
  1. package/dist/asgardeo-spa.production.esm.js +13 -13
  2. package/dist/asgardeo-spa.production.esm.js.map +1 -1
  3. package/dist/asgardeo-spa.production.js +13 -13
  4. package/dist/asgardeo-spa.production.js.map +1 -1
  5. package/dist/asgardeo-spa.production.min.js +1 -1
  6. package/dist/asgardeo-spa.production.min.js.map +1 -1
  7. package/dist/polyfilled/asgardeo-spa.production.esm.js +42 -40
  8. package/dist/polyfilled/asgardeo-spa.production.esm.js.map +1 -1
  9. package/dist/polyfilled/asgardeo-spa.production.js +42 -40
  10. package/dist/polyfilled/asgardeo-spa.production.js.map +1 -1
  11. package/dist/polyfilled/asgardeo-spa.production.min.js +1 -1
  12. package/dist/polyfilled/asgardeo-spa.production.min.js.map +1 -1
  13. package/dist/src/client.d.ts +21 -16
  14. package/dist/src/client.d.ts.map +1 -1
  15. package/dist/src/client.js +29 -3
  16. package/dist/src/client.js.map +1 -1
  17. package/dist/src/clients/main-thread-client.d.ts +3 -2
  18. package/dist/src/clients/main-thread-client.d.ts.map +1 -1
  19. package/dist/src/clients/main-thread-client.js +83 -385
  20. package/dist/src/clients/main-thread-client.js.map +1 -1
  21. package/dist/src/clients/web-worker-client.d.ts +3 -2
  22. package/dist/src/clients/web-worker-client.d.ts.map +1 -1
  23. package/dist/src/clients/web-worker-client.js +98 -138
  24. package/dist/src/clients/web-worker-client.js.map +1 -1
  25. package/dist/src/helpers/authentication-helper.d.ts +50 -0
  26. package/dist/src/helpers/authentication-helper.d.ts.map +1 -0
  27. package/dist/src/helpers/authentication-helper.js +482 -0
  28. package/dist/src/helpers/authentication-helper.js.map +1 -0
  29. package/dist/src/helpers/index.d.ts +1 -0
  30. package/dist/src/helpers/index.d.ts.map +1 -1
  31. package/dist/src/helpers/index.js +1 -0
  32. package/dist/src/helpers/index.js.map +1 -1
  33. package/dist/src/public-api.d.ts +3 -0
  34. package/dist/src/public-api.d.ts.map +1 -1
  35. package/dist/src/public-api.js +5 -0
  36. package/dist/src/public-api.js.map +1 -1
  37. package/dist/src/worker/index.d.ts +1 -1
  38. package/dist/src/worker/index.d.ts.map +1 -1
  39. package/dist/src/worker/index.js +1 -1
  40. package/dist/src/worker/index.js.map +1 -1
  41. package/dist/src/worker/worker-core.d.ts +3 -2
  42. package/dist/src/worker/worker-core.d.ts.map +1 -1
  43. package/dist/src/worker/worker-core.js +23 -202
  44. package/dist/src/worker/worker-core.js.map +1 -1
  45. package/dist/src/worker/worker-receiver.d.ts +22 -0
  46. package/dist/src/worker/worker-receiver.d.ts.map +1 -0
  47. package/dist/src/worker/worker-receiver.js +233 -0
  48. package/dist/src/worker/worker-receiver.js.map +1 -0
  49. package/dist/src/{worker/client.worker.d.ts → worker.d.ts} +2 -2
  50. package/dist/src/worker.d.ts.map +1 -0
  51. package/dist/src/worker.js +24 -0
  52. package/dist/src/worker.js.map +1 -0
  53. package/dist/tsconfig.tsbuildinfo +1 -1
  54. package/package.json +1 -1
  55. package/src/client.ts +65 -19
  56. package/src/clients/main-thread-client.ts +137 -486
  57. package/src/clients/web-worker-client.ts +128 -169
  58. package/src/helpers/authentication-helper.ts +661 -0
  59. package/src/helpers/index.ts +1 -0
  60. package/src/public-api.ts +7 -0
  61. package/src/worker/index.ts +1 -1
  62. package/src/worker/worker-core.ts +48 -261
  63. package/src/worker/worker-receiver.ts +297 -0
  64. package/src/worker.ts +30 -0
  65. package/dist/src/worker/client.worker.d.ts.map +0 -1
  66. package/dist/src/worker/client.worker.js +0 -232
  67. package/dist/src/worker/client.worker.js.map +0 -1
  68. package/src/worker/client.worker.ts +0 -286
@@ -18,6 +18,7 @@
18
18
 
19
19
  import {
20
20
  AUTHORIZATION_CODE,
21
+ AsgardeoAuthClient,
21
22
  AsgardeoAuthException,
22
23
  AuthClientConfig,
23
24
  AuthenticationUtils,
@@ -29,16 +30,12 @@ import {
29
30
  OIDCEndpoints,
30
31
  ResponseMode,
31
32
  SESSION_STATE,
32
- STATE
33
+ STATE,
34
+ Store
33
35
  } from "@asgardeo/auth-js";
34
- import WorkerFile from "web-worker:../worker/client.worker.ts";
35
36
  import {
36
- CHECK_SESSION_SIGNED_IN,
37
- CHECK_SESSION_SIGNED_OUT,
38
37
  DISABLE_HTTP_HANDLER,
39
38
  ENABLE_HTTP_HANDLER,
40
- ERROR,
41
- ERROR_DESCRIPTION,
42
39
  GET_AUTH_URL,
43
40
  GET_BASIC_USER_INFO,
44
41
  GET_CONFIG_DATA,
@@ -50,7 +47,6 @@ import {
50
47
  HTTP_REQUEST_ALL,
51
48
  INIT,
52
49
  IS_AUTHENTICATED,
53
- PROMPT_NONE_IFRAME,
54
50
  REFRESH_ACCESS_TOKEN,
55
51
  REQUEST_ACCESS_TOKEN,
56
52
  REQUEST_CUSTOM_GRANT,
@@ -58,14 +54,14 @@ import {
58
54
  REQUEST_START,
59
55
  REQUEST_SUCCESS,
60
56
  REVOKE_ACCESS_TOKEN,
61
- RP_IFRAME,
62
57
  SET_SESSION_STATE,
63
58
  SIGN_OUT,
64
59
  SILENT_SIGN_IN_STATE,
65
60
  START_AUTO_REFRESH_TOKEN,
61
+ Storage,
66
62
  UPDATE_CONFIG
67
63
  } from "../constants";
68
- import { SessionManagementHelper } from "../helpers";
64
+ import { AuthenticationHelper, SPAHelper, SessionManagementHelper } from "../helpers";
69
65
  import {
70
66
  AuthorizationInfo,
71
67
  AuthorizationResponse,
@@ -79,10 +75,30 @@ import {
79
75
  WebWorkerClientInterface
80
76
  } from "../models";
81
77
  import { SPACustomGrantConfig } from "../models/request-custom-grant";
78
+ import { LocalStore, MemoryStore, SessionStore } from "../stores";
82
79
  import { SPAUtils } from "../utils";
80
+ import { SPACryptoUtils } from "../utils/crypto-utils";
81
+
82
+ const initiateStore = (store: Storage | undefined): Store => {
83
+ switch (store) {
84
+ case Storage.LocalStorage:
85
+ return new LocalStore();
86
+ case Storage.SessionStorage:
87
+ return new SessionStore();
88
+ case Storage.BrowserMemory:
89
+ return new MemoryStore();
90
+ default:
91
+ return new SessionStore();
92
+ }
93
+ };
83
94
 
84
95
  export const WebWorkerClient = async (
85
- config: AuthClientConfig<WebWorkerClientConfig>
96
+ config: AuthClientConfig<WebWorkerClientConfig>,
97
+ webWorker: new () => Worker,
98
+ getAuthHelper: (
99
+ authClient: AsgardeoAuthClient<WebWorkerClientConfig>,
100
+ spaHelper: SPAHelper<WebWorkerClientConfig>
101
+ ) => AuthenticationHelper<WebWorkerClientConfig>
86
102
  ): Promise<WebWorkerClientInterface> => {
87
103
  /**
88
104
  * HttpClient handlers
@@ -94,7 +110,13 @@ export const WebWorkerClient = async (
94
110
  const _requestTimeout: number = config?.requestTimeout ?? 60000;
95
111
  let _isHttpHandlerEnabled: boolean = true;
96
112
  let _getSignOutURLFromSessionStorage: boolean = false;
97
-
113
+
114
+ const _store: Store = initiateStore(config.storage);
115
+ const _cryptoUtils: SPACryptoUtils = new SPACryptoUtils();
116
+ const _authenticationClient = new AsgardeoAuthClient<WebWorkerClientConfig>(_store, _cryptoUtils);
117
+ await _authenticationClient.initialize(config);
118
+ const _spaHelper = new SPAHelper<WebWorkerClientConfig>(_authenticationClient);
119
+
98
120
  const _sessionManagementHelper = await SessionManagementHelper(
99
121
  async () => {
100
122
  const message: Message<string> = {
@@ -113,7 +135,10 @@ export const WebWorkerClient = async (
113
135
  (sessionState: string) => setSessionState(sessionState)
114
136
  );
115
137
 
116
- const worker: Worker = new WorkerFile();
138
+ const _authenticationHelper: AuthenticationHelper<WebWorkerClientConfig> =
139
+ getAuthHelper(_authenticationClient, _spaHelper);
140
+
141
+ const worker: Worker = new webWorker();
117
142
 
118
143
  const communicate = <T, R>(message: Message<T>): Promise<R> => {
119
144
  const channel = new MessageChannel();
@@ -340,49 +365,17 @@ export const WebWorkerClient = async (
340
365
  const oidcEndpoints: OIDCEndpoints = await getOIDCServiceEndpoints();
341
366
  const config: AuthClientConfig<WebWorkerClientConfig> = await getConfigData();
342
367
 
343
- _sessionManagementHelper.initialize(
344
- config.clientID,
345
- oidcEndpoints.checkSessionIframe ?? "",
368
+ _authenticationHelper.initializeSessionManger(
369
+ config,
370
+ oidcEndpoints,
346
371
  async () => (await getBasicUserInfo()).sessionState,
347
- config.checkSessionInterval ?? 3,
348
- config.sessionRefreshInterval ?? 300,
349
- config.signInRedirectURL,
350
- async (params?: GetAuthURLConfig): Promise<string> => (await getAuthorizationURL(params)).authorizationURL
372
+ async (params?: GetAuthURLConfig): Promise<string> => (await getAuthorizationURL(params)).authorizationURL,
373
+ _sessionManagementHelper
351
374
  );
352
375
  };
353
376
 
354
- /**
355
- * This method checks if there is an active user session in the server by sending a prompt none request.
356
- * If the user is signed in, this method sends a token request. Returns false otherwise.
357
- *
358
- * @return {Promise<BasicUserInfo|boolean} Returns a Promise that resolves with the BasicUserInfo
359
- * if the user is signed in or with `false` if there is no active user session in the server.
360
- */
361
- const trySignInSilently = async (): Promise<BasicUserInfo | boolean> => {
377
+ const constructSilentSignInUrl = async (): Promise<string> => {
362
378
  const config: AuthClientConfig<WebWorkerClientConfig> = await getConfigData();
363
-
364
- // This block is executed by the iFrame when the server redirects with the authorization code.
365
- if (SPAUtils.isInitializedSilentSignIn()) {
366
- await _sessionManagementHelper.receivePromptNoneResponse();
367
-
368
- return Promise.resolve({
369
- allowedScopes: "",
370
- displayName: "",
371
- email: "",
372
- sessionState: "",
373
- sub: "",
374
- tenantDomain: "",
375
- username: ""
376
- });
377
- }
378
-
379
- // This gets executed in the main thread and sends the prompt none request.
380
- const rpIFrame = document.getElementById(RP_IFRAME) as HTMLIFrameElement;
381
-
382
- const promptNoneIFrame: HTMLIFrameElement = rpIFrame?.contentDocument?.getElementById(
383
- PROMPT_NONE_IFRAME
384
- ) as HTMLIFrameElement;
385
-
386
379
  const message: Message<GetAuthURLConfig> = {
387
380
  data: {
388
381
  prompt: "none",
@@ -391,59 +384,37 @@ export const WebWorkerClient = async (
391
384
  type: GET_AUTH_URL
392
385
  };
393
386
 
394
- try {
395
- const response: AuthorizationResponse = await communicate<GetAuthURLConfig, AuthorizationResponse>(message);
387
+ const response: AuthorizationResponse = await communicate<GetAuthURLConfig, AuthorizationResponse>(message);
396
388
 
397
- const pkceKey: string = AuthenticationUtils.extractPKCEKeyFromStateParam(
398
- new URL(response.authorizationURL).searchParams.get(STATE) ?? ""
399
- );
400
-
401
- response.pkce && config.enablePKCE && SPAUtils.setPKCE(pkceKey, response.pkce);
402
-
403
- const urlString: string = response.authorizationURL;
404
-
405
- // Replace form_post with query
406
- const urlObject = new URL(urlString);
407
- urlObject.searchParams.set("response_mode", "query");
408
- const url: string = urlObject.toString();
409
-
410
- promptNoneIFrame.src = url;
411
- } catch (error) {
412
- return Promise.reject(error);
413
- }
389
+ const pkceKey: string = AuthenticationUtils.extractPKCEKeyFromStateParam(
390
+ new URL(response.authorizationURL).searchParams.get(STATE) ?? ""
391
+ );
414
392
 
415
- return new Promise((resolve, reject) => {
416
- const timer = setTimeout(() => {
417
- resolve(false);
418
- }, 10000);
393
+ response.pkce && config.enablePKCE && SPAUtils.setPKCE(pkceKey, response.pkce);
419
394
 
420
- const listenToPromptNoneIFrame = async (e: MessageEvent) => {
421
- const data: Message<AuthorizationInfo | null> = e.data;
395
+ const urlString: string = response.authorizationURL;
422
396
 
423
- if (data?.type == CHECK_SESSION_SIGNED_OUT) {
424
- window.removeEventListener("message", listenToPromptNoneIFrame);
425
- clearTimeout(timer);
426
- resolve(false);
427
- }
397
+ // Replace form_post with query
398
+ const urlObject = new URL(urlString);
399
+ urlObject.searchParams.set("response_mode", "query");
400
+ const url: string = urlObject.toString();
428
401
 
429
- if (data?.type == CHECK_SESSION_SIGNED_IN && data?.data?.code) {
430
- requestAccessToken(data?.data?.code, data?.data?.sessionState, data?.data?.state)
431
- .then((response: BasicUserInfo) => {
432
- window.removeEventListener("message", listenToPromptNoneIFrame);
433
- resolve(response);
434
- })
435
- .catch((error) => {
436
- window.removeEventListener("message", listenToPromptNoneIFrame);
437
- reject(error);
438
- })
439
- .finally(() => {
440
- clearTimeout(timer);
441
- });
442
- }
443
- };
402
+ return url;
403
+ }
444
404
 
445
- window.addEventListener("message", listenToPromptNoneIFrame);
446
- });
405
+ /**
406
+ * This method checks if there is an active user session in the server by sending a prompt none request.
407
+ * If the user is signed in, this method sends a token request. Returns false otherwise.
408
+ *
409
+ * @return {Promise<BasicUserInfo|boolean} Returns a Promise that resolves with the BasicUserInfo
410
+ * if the user is signed in or with `false` if there is no active user session in the server.
411
+ */
412
+ const trySignInSilently = async (): Promise<BasicUserInfo | boolean> => {
413
+ return await _authenticationHelper.trySignInSilently(
414
+ constructSilentSignInUrl,
415
+ requestAccessToken,
416
+ _sessionManagementHelper
417
+ );
447
418
  };
448
419
 
449
420
  /**
@@ -523,50 +494,15 @@ export const WebWorkerClient = async (
523
494
  });
524
495
  };
525
496
 
526
- /**
527
- * Initiates the authentication flow.
528
- *
529
- * @returns {Promise<UserInfo>} A promise that resolves when authentication is successful.
530
- */
531
- const signIn = async (
532
- params?: GetAuthURLConfig,
533
- authorizationCode?: string,
534
- sessionState?: string,
535
- state?: string
536
- ): Promise<BasicUserInfo> => {
537
- const config: AuthClientConfig<WebWorkerClientConfig> = await getConfigData();
538
-
539
- const shouldStopContinue: boolean = await _sessionManagementHelper.receivePromptNoneResponse(
497
+ const shouldStopAuthn = async (): Promise<boolean> => {
498
+ return await _sessionManagementHelper.receivePromptNoneResponse(
540
499
  async (sessionState: string | null) => {
541
500
  return setSessionState(sessionState);
542
501
  }
543
502
  );
503
+ }
544
504
 
545
- if (shouldStopContinue) {
546
- return Promise.resolve({
547
- allowedScopes: "",
548
- displayName: "",
549
- email: "",
550
- sessionState: "",
551
- sub: "",
552
- tenantDomain: "",
553
- username: ""
554
- });
555
- }
556
-
557
- const error = new URL(window.location.href).searchParams.get(ERROR);
558
- const errorDescription = new URL(window.location.href).searchParams.get(ERROR_DESCRIPTION);
559
-
560
- if (error) {
561
- const url = new URL(window.location.href);
562
- url.searchParams.delete(ERROR);
563
- url.searchParams.delete(ERROR_DESCRIPTION);
564
-
565
- history.pushState(null, document.title, url.toString());
566
-
567
- throw new AsgardeoAuthException("SPA-WEB_WORKER_CLIENT-SI-SE01", error, errorDescription ?? "");
568
- }
569
-
505
+ const tryRetrievingUserInfo = async (): Promise<BasicUserInfo | undefined> => {
570
506
  if (await isAuthenticated()) {
571
507
  await startAutoRefreshToken();
572
508
 
@@ -577,46 +513,69 @@ export const WebWorkerClient = async (
577
513
 
578
514
  return getBasicUserInfo();
579
515
  }
516
+ }
517
+
518
+ /**
519
+ * Initiates the authentication flow.
520
+ *
521
+ * @returns {Promise<UserInfo>} A promise that resolves when authentication is successful.
522
+ */
523
+ const signIn = async (
524
+ params?: GetAuthURLConfig,
525
+ authorizationCode?: string,
526
+ sessionState?: string,
527
+ state?: string
528
+ ): Promise<BasicUserInfo> => {
580
529
 
581
- let resolvedAuthorizationCode: string;
582
- let resolvedSessionState: string;
583
- let resolvedState: string;
530
+ const basicUserInfo = await _authenticationHelper.handleSignIn(
531
+ shouldStopAuthn,
532
+ checkSession,
533
+ tryRetrievingUserInfo
534
+ );
584
535
 
585
- if (config?.responseMode === ResponseMode.formPost && authorizationCode) {
586
- resolvedAuthorizationCode = authorizationCode;
587
- resolvedSessionState = sessionState ?? "";
588
- resolvedState = state ?? "";
536
+ if(basicUserInfo) {
537
+ return basicUserInfo;
589
538
  } else {
590
- resolvedAuthorizationCode = new URL(window.location.href).searchParams.get(AUTHORIZATION_CODE) ?? "";
591
- resolvedSessionState = new URL(window.location.href).searchParams.get(SESSION_STATE) ?? "";
592
- resolvedState = new URL(window.location.href).searchParams.get(STATE) ?? "";
593
-
594
- SPAUtils.removeAuthorizationCode();
595
- }
596
-
597
- if (resolvedAuthorizationCode && resolvedState) {
598
- return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState, resolvedState);
599
- }
539
+ let resolvedAuthorizationCode: string;
540
+ let resolvedSessionState: string;
541
+ let resolvedState: string;
542
+
543
+ if (config?.responseMode === ResponseMode.formPost && authorizationCode) {
544
+ resolvedAuthorizationCode = authorizationCode;
545
+ resolvedSessionState = sessionState ?? "";
546
+ resolvedState = state ?? "";
547
+ } else {
548
+ resolvedAuthorizationCode = new URL(window.location.href).searchParams.get(AUTHORIZATION_CODE) ?? "";
549
+ resolvedSessionState = new URL(window.location.href).searchParams.get(SESSION_STATE) ?? "";
550
+ resolvedState = new URL(window.location.href).searchParams.get(STATE) ?? "";
551
+
552
+ SPAUtils.removeAuthorizationCode();
553
+ }
600
554
 
601
- return getAuthorizationURL(params)
602
- .then(async (response: AuthorizationResponse) => {
603
- location.href = response.authorizationURL;
555
+ if (resolvedAuthorizationCode && resolvedState) {
556
+ return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState, resolvedState);
557
+ }
558
+
559
+ return getAuthorizationURL(params)
560
+ .then(async (response: AuthorizationResponse) => {
561
+ location.href = response.authorizationURL;
604
562
 
605
- await SPAUtils.waitTillPageRedirect();
563
+ await SPAUtils.waitTillPageRedirect();
606
564
 
607
- return Promise.resolve({
608
- allowedScopes: "",
609
- displayName: "",
610
- email: "",
611
- sessionState: "",
612
- sub: "",
613
- tenantDomain: "",
614
- username: ""
565
+ return Promise.resolve({
566
+ allowedScopes: "",
567
+ displayName: "",
568
+ email: "",
569
+ sessionState: "",
570
+ sub: "",
571
+ tenantDomain: "",
572
+ username: ""
573
+ });
574
+ })
575
+ .catch((error) => {
576
+ return Promise.reject(error);
615
577
  });
616
- })
617
- .catch((error) => {
618
- return Promise.reject(error);
619
- });
578
+ }
620
579
  };
621
580
 
622
581
  /**