@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,7 +18,6 @@
18
18
 
19
19
  import {
20
20
  AsgardeoAuthClient,
21
- AsgardeoAuthException,
22
21
  AuthClientConfig,
23
22
  AuthorizationURLParams,
24
23
  BasicUserInfo,
@@ -28,15 +27,12 @@ import {
28
27
  OIDCEndpoints,
29
28
  SESSION_STATE,
30
29
  STATE,
31
- Store,
32
- TokenResponse
30
+ Store
33
31
  } from "@asgardeo/auth-js";
34
- import { CUSTOM_GRANT_CONFIG } from "../constants";
35
- import { SPAHelper } from "../helpers";
32
+ import { AuthenticationHelper, SPAHelper } from "../helpers";
36
33
  import { HttpClient, HttpClientInstance } from "../http-client";
37
34
  import {
38
35
  AuthorizationResponse,
39
- HttpError,
40
36
  HttpRequestConfig,
41
37
  HttpResponse,
42
38
  WebWorkerClientConfig,
@@ -46,7 +42,11 @@ import { MemoryStore } from "../stores";
46
42
  import { SPACryptoUtils } from "../utils/crypto-utils";
47
43
 
48
44
  export const WebWorkerCore = async (
49
- config: AuthClientConfig<WebWorkerClientConfig>
45
+ config: AuthClientConfig<WebWorkerClientConfig>,
46
+ getAuthHelper: (
47
+ authClient: AsgardeoAuthClient<WebWorkerClientConfig>,
48
+ spaHelper: SPAHelper<WebWorkerClientConfig>
49
+ ) => AuthenticationHelper<WebWorkerClientConfig>
50
50
  ): Promise<WebWorkerCoreInterface> => {
51
51
  const _store: Store = new MemoryStore();
52
52
  const _cryptoUtils: SPACryptoUtils = new SPACryptoUtils();
@@ -54,186 +54,53 @@ export const WebWorkerCore = async (
54
54
  await _authenticationClient.initialize(config);
55
55
 
56
56
  const _spaHelper = new SPAHelper<WebWorkerClientConfig>(_authenticationClient);
57
+
58
+ const _authenticationHelper: AuthenticationHelper<WebWorkerClientConfig> =
59
+ getAuthHelper(_authenticationClient, _spaHelper);
60
+
57
61
  const _dataLayer = _authenticationClient.getDataLayer();
58
62
 
59
63
  const _httpClient: HttpClientInstance = HttpClient.getInstance();
60
64
 
61
- const attachToken = async (request: HttpRequestConfig): Promise<void> => {
62
- const requestConfig = { attachToken: true, ...request };
63
- if (requestConfig.attachToken) {
64
- request.headers = {
65
- ...request.headers,
66
- Authorization: `Bearer ${ await _authenticationClient.getAccessToken() }`
67
- };
68
- }
69
- };
70
-
71
- _httpClient?.init && (await _httpClient.init(true, attachToken));
65
+ _httpClient?.init && (await _httpClient.init(true, _authenticationHelper.attachToken));
72
66
 
73
67
  const setHttpRequestStartCallback = (callback: () => void): void => {
74
- _httpClient?.setHttpRequestStartCallback && _httpClient.setHttpRequestStartCallback(callback);
68
+ _authenticationHelper.setHttpRequestStartCallback(_httpClient, callback);
75
69
  };
76
70
 
77
- const setHttpRequestSuccessCallback = (callback: (response: HttpResponse) => void): void => {
78
- _httpClient?.setHttpRequestSuccessCallback && _httpClient.setHttpRequestSuccessCallback(callback);
71
+ const setHttpRequestSuccessCallback = (
72
+ callback: (response: HttpResponse) => void
73
+ ): void => {
74
+ _authenticationHelper.setHttpRequestSuccessCallback(_httpClient, callback);
79
75
  };
80
76
 
81
77
  const setHttpRequestFinishCallback = (callback: () => void): void => {
82
- _httpClient?.setHttpRequestFinishCallback && _httpClient.setHttpRequestFinishCallback(callback);
83
- };
84
-
85
- const httpRequest = async (requestConfig: HttpRequestConfig): Promise<HttpResponse> => {
86
- let matches = false;
87
-
88
- for (const baseUrl of [
89
- ...((await _dataLayer.getConfigData())?.resourceServerURLs ?? []),
90
- (config as any).baseUrl
91
- ]) {
92
- if (baseUrl && requestConfig?.url?.startsWith(baseUrl)) {
93
- matches = true;
94
-
95
- break;
96
- }
97
- }
98
-
99
- if (matches) {
100
- return _httpClient
101
- .request(requestConfig)
102
- .then((response: HttpResponse) => {
103
- return Promise.resolve(response);
104
- })
105
- .catch(async (error: HttpError) => {
106
- if (error?.response?.status === 401 || !error?.response) {
107
- let refreshAccessTokenResponse: BasicUserInfo;
108
- try {
109
- refreshAccessTokenResponse = await refreshAccessToken();
110
- } catch (refreshError: any) {
111
- throw new AsgardeoAuthException(
112
- "SPA-WORKER_CORE-HR-SE01",
113
- refreshError?.name ?? "Refresh token request failed.",
114
- refreshError?.message ??
115
- "An error occurred while trying to refresh the " +
116
- "access token following a 401 response from the server."
117
- );
118
- }
119
-
120
- if (refreshAccessTokenResponse) {
121
- return _httpClient
122
- .request(requestConfig)
123
- .then((response) => {
124
- return Promise.resolve(response);
125
- })
126
- .catch((error) => {
127
- return Promise.reject(error);
128
- });
129
- }
130
- }
131
-
132
- return Promise.reject(error);
133
- });
134
- } else {
135
- return Promise.reject(
136
- new AsgardeoAuthException(
137
- "SPA-WORKER_CORE-HR-IV02",
138
- "Request to the provided endpoint is prohibited.",
139
- "Requests can only be sent to resource servers specified by the `resourceServerURLs`" +
140
- " attribute while initializing the SDK. The specified endpoint in this request " +
141
- "cannot be found among the `resourceServerURLs`"
142
- )
143
- );
144
- }
78
+ _httpClient?.setHttpRequestFinishCallback &&
79
+ _httpClient.setHttpRequestFinishCallback(callback);
80
+ };
81
+
82
+ const httpRequest = async (
83
+ requestConfig: HttpRequestConfig
84
+ ): Promise<HttpResponse> => {
85
+ return await _authenticationHelper.httpRequest(
86
+ _httpClient,
87
+ requestConfig
88
+ );
145
89
  };
146
90
 
147
91
  const httpRequestAll = async (requestConfigs: HttpRequestConfig[]): Promise<HttpResponse[] | undefined> => {
148
- let matches = true;
149
-
150
- for (const requestConfig of requestConfigs) {
151
- let urlMatches = false;
152
-
153
- for (const baseUrl of [
154
- ...((await _dataLayer.getConfigData())?.resourceServerURLs ?? []),
155
- (config as any).baseUrl
156
- ]) {
157
- if (baseUrl && requestConfig.url?.startsWith(baseUrl)) {
158
- urlMatches = true;
159
-
160
- break;
161
- }
162
- }
163
-
164
- if (!urlMatches) {
165
- matches = false;
166
-
167
- break;
168
- }
169
- }
170
-
171
- const requests: Promise<HttpResponse<any>>[] = [];
172
-
173
- if (matches) {
174
- requestConfigs.forEach((request) => {
175
- requests.push(_httpClient.request(request));
176
- });
177
-
178
- return (
179
- _httpClient?.all &&
180
- _httpClient
181
- .all(requests)
182
- .then((responses: HttpResponse[]) => {
183
- return Promise.resolve(responses);
184
- })
185
- .catch(async (error: HttpError) => {
186
- if (error?.response?.status === 401) {
187
- let refreshAccessTokenResponse: BasicUserInfo;
188
- try {
189
- refreshAccessTokenResponse = await refreshAccessToken();
190
- } catch (refreshError: any) {
191
- throw new AsgardeoAuthException(
192
- "SPA-WORKER_CORE-HRA-SE01",
193
- refreshError?.name ?? "Refresh token request failed.",
194
- refreshError?.message ??
195
- "An error occurred while trying to refresh the " +
196
- "access token following a 401 response from the server."
197
- );
198
- }
199
-
200
- if (refreshAccessTokenResponse) {
201
- return (
202
- _httpClient.all &&
203
- _httpClient
204
- .all(requests)
205
- .then((response) => {
206
- return Promise.resolve(response);
207
- })
208
- .catch((error) => {
209
- return Promise.reject(error);
210
- })
211
- );
212
- }
213
- }
214
-
215
- return Promise.reject(error);
216
- })
217
- );
218
- } else {
219
- return Promise.reject(
220
- new AsgardeoAuthException(
221
- "SPA-WORKER_CORE-HRA-IV02",
222
- "Request to the provided endpoint is prohibited.",
223
- "Requests can only be sent to resource servers specified by the `resourceServerURLs`" +
224
- " attribute while initializing the SDK. The specified endpoint in this request " +
225
- "cannot be found among the `resourceServerURLs`"
226
- )
227
- );
228
- }
92
+ return await _authenticationHelper.httpRequestAll(
93
+ requestConfigs,
94
+ _httpClient
95
+ );
229
96
  };
230
97
 
231
98
  const enableHttpHandler = (): void => {
232
- _httpClient.enableHandler && _httpClient.enableHandler();
99
+ _authenticationHelper.enableHttpHandler(_httpClient);
233
100
  };
234
101
 
235
102
  const disableHttpHandler = (): void => {
236
- _httpClient.disableHandler && _httpClient.disableHandler();
103
+ _authenticationHelper.disableHttpHandler(_httpClient);
237
104
  };
238
105
 
239
106
  const getAuthorizationURL = async (params?: AuthorizationURLParams): Promise<AuthorizationResponse> => {
@@ -262,31 +129,12 @@ export const WebWorkerCore = async (
262
129
  pkce?: string,
263
130
  state?: string
264
131
  ): Promise<BasicUserInfo> => {
265
- const config = await _dataLayer.getConfigData();
266
-
267
- if (pkce && config.enablePKCE) {
268
- await _authenticationClient.setPKCECode(pkce, state ?? "");
269
- }
270
-
271
- if (authorizationCode) {
272
- return _authenticationClient
273
- .requestAccessToken(authorizationCode, sessionState ?? "", state ?? "'")
274
- .then(() => {
275
- _spaHelper.refreshAccessTokenAutomatically();
276
-
277
- return _authenticationClient.getBasicUserInfo();
278
- })
279
- .catch((error) => {
280
- return Promise.reject(error);
281
- });
282
- }
283
-
284
- return Promise.reject(
285
- new AsgardeoAuthException(
286
- "SPA-WORKER_CORE-RAT1-NF01",
287
- "No authorization code.",
288
- "No authorization code was found."
289
- )
132
+ return await _authenticationHelper.requestAccessToken(
133
+ authorizationCode,
134
+ sessionState,
135
+ undefined,
136
+ pkce,
137
+ state
290
138
  );
291
139
  };
292
140
 
@@ -301,64 +149,12 @@ export const WebWorkerCore = async (
301
149
  };
302
150
 
303
151
  const requestCustomGrant = async (config: CustomGrantConfig): Promise<BasicUserInfo | FetchResponse> => {
304
- let useDefaultEndpoint = true;
305
- let matches = false;
306
-
307
- // If the config does not contains a token endpoint, default token endpoint will be used.
308
- if (config?.tokenEndpoint) {
309
- useDefaultEndpoint = false;
310
- for (const baseUrl of [
311
- ...((await _dataLayer.getConfigData())?.resourceServerURLs ?? []),
312
- (config as any).baseUrl
313
- ]) {
314
- if (baseUrl && config.tokenEndpoint?.startsWith(baseUrl)) {
315
- matches = true;
316
- break;
317
- }
318
- }
319
- }
320
-
321
- if (config.shouldReplayAfterRefresh) {
322
- _dataLayer.setTemporaryDataParameter(CUSTOM_GRANT_CONFIG, JSON.stringify(config));
323
- }
324
- if (useDefaultEndpoint || matches) {
325
- return _authenticationClient
326
- .requestCustomGrant(config)
327
- .then(async (response: FetchResponse | TokenResponse) => {
328
- if (config.returnsSession) {
329
- _spaHelper.refreshAccessTokenAutomatically();
330
-
331
- return _authenticationClient.getBasicUserInfo();
332
- } else {
333
- return response as FetchResponse;
334
- }
335
- })
336
- .catch((error) => {
337
- return Promise.reject(error);
338
- });
339
- } else {
340
- return Promise.reject(
341
- new AsgardeoAuthException(
342
- "SPA-WORKER_CORE-RCG-IV01",
343
- "Request to the provided endpoint is prohibited.",
344
- "Requests can only be sent to resource servers specified by the `resourceServerURLs`" +
345
- " attribute while initializing the SDK. The specified token endpoint in this request " +
346
- "cannot be found among the `resourceServerURLs`"
347
- )
348
- );
349
- }
152
+ return await _authenticationHelper.requestCustomGrant(config);
350
153
  };
351
154
 
352
155
  const refreshAccessToken = async (): Promise<BasicUserInfo> => {
353
156
  try {
354
- await _authenticationClient.refreshAccessToken();
355
- const customGrantConfig = await getCustomGrantConfigData();
356
- if (customGrantConfig) {
357
- await requestCustomGrant(customGrantConfig);
358
- }
359
- _spaHelper.refreshAccessTokenAutomatically();
360
-
361
- return _authenticationClient.getBasicUserInfo();
157
+ return await _authenticationHelper.refreshAccessToken();
362
158
  } catch (error) {
363
159
  return Promise.reject(error);
364
160
  }
@@ -376,26 +172,26 @@ export const WebWorkerCore = async (
376
172
  };
377
173
 
378
174
  const getBasicUserInfo = async (): Promise<BasicUserInfo> => {
379
- return _authenticationClient.getBasicUserInfo();
175
+ return _authenticationHelper.getBasicUserInfo();
380
176
  };
381
177
 
382
178
  const getDecodedIDToken = async (): Promise<DecodedIDTokenPayload> => {
383
- return _authenticationClient.getDecodedIDToken();
179
+ return _authenticationHelper.getDecodedIDToken();
384
180
  };
385
181
 
386
182
  const getIDToken = async (): Promise<string> => {
387
- return _authenticationClient.getIDToken();
183
+ return _authenticationHelper.getIDToken();
388
184
  };
389
185
  const getOIDCServiceEndpoints = async (): Promise<OIDCEndpoints> => {
390
- return _authenticationClient.getOIDCServiceEndpoints();
186
+ return _authenticationHelper.getOIDCServiceEndpoints();
391
187
  };
392
188
 
393
189
  const getAccessToken = (): Promise<string> => {
394
- return _authenticationClient.getAccessToken();
190
+ return _authenticationHelper.getAccessToken();
395
191
  };
396
192
 
397
193
  const isAuthenticated = (): Promise<boolean> => {
398
- return _authenticationClient.isAuthenticated();
194
+ return _authenticationHelper.isAuthenticated();
399
195
  };
400
196
 
401
197
  const setSessionState = async (sessionState: string): Promise<void> => {
@@ -414,15 +210,6 @@ export const WebWorkerCore = async (
414
210
  return _dataLayer.getConfigData();
415
211
  };
416
212
 
417
- const getCustomGrantConfigData = async (): Promise<AuthClientConfig<CustomGrantConfig> | null> => {
418
- const configString = await _dataLayer.getTemporaryDataParameter(CUSTOM_GRANT_CONFIG);
419
- if (configString) {
420
- return JSON.parse(configString as string);
421
- } else {
422
- return null;
423
- }
424
- };
425
-
426
213
  return {
427
214
  disableHttpHandler,
428
215
  enableHttpHandler,