@asgardeo/auth-spa 0.4.10 → 0.4.12

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 (33) hide show
  1. package/dist/asgardeo-spa.production.esm.js +2 -2
  2. package/dist/asgardeo-spa.production.esm.js.map +1 -1
  3. package/dist/asgardeo-spa.production.js +7 -7
  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 +4 -4
  8. package/dist/polyfilled/asgardeo-spa.production.esm.js.map +1 -1
  9. package/dist/polyfilled/asgardeo-spa.production.js +37 -37
  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.map +1 -1
  14. package/dist/src/client.js +1 -0
  15. package/dist/src/client.js.map +1 -1
  16. package/dist/src/helpers/authentication-helper.d.ts +3 -1
  17. package/dist/src/helpers/authentication-helper.d.ts.map +1 -1
  18. package/dist/src/helpers/authentication-helper.js +26 -0
  19. package/dist/src/helpers/authentication-helper.js.map +1 -1
  20. package/dist/src/helpers/spa-helper.d.ts.map +1 -1
  21. package/dist/src/helpers/spa-helper.js +14 -9
  22. package/dist/src/helpers/spa-helper.js.map +1 -1
  23. package/dist/src/models/client-config.d.ts +1 -0
  24. package/dist/src/models/client-config.d.ts.map +1 -1
  25. package/dist/src/models/http-client.d.ts +10 -1
  26. package/dist/src/models/http-client.d.ts.map +1 -1
  27. package/dist/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +1 -1
  29. package/src/client.ts +1 -0
  30. package/src/helpers/authentication-helper.ts +32 -1
  31. package/src/helpers/spa-helper.ts +15 -10
  32. package/src/models/client-config.ts +1 -0
  33. package/src/models/http-client.ts +11 -1
@@ -32,7 +32,7 @@ import {
32
32
  TokenResponse
33
33
  } from "@asgardeo/auth-js";
34
34
  import { SPAHelper } from "./spa-helper";
35
- import { Message, SPAUtils, SessionManagementHelperInterface } from "..";
35
+ import { HttpRequestInterface, Message, SPAUtils, SessionManagementHelperInterface } from "..";
36
36
  import {
37
37
  ACCESS_TOKEN_INVALID,
38
38
  CHECK_SESSION_SIGNED_IN,
@@ -61,6 +61,7 @@ export class AuthenticationHelper<
61
61
  protected _authenticationClient: AsgardeoAuthClient<T>;
62
62
  protected _dataLayer: DataLayer<T>;
63
63
  protected _spaHelper: SPAHelper<T>;
64
+ protected _isTokenRefreshing: boolean;
64
65
 
65
66
  public constructor(
66
67
  authClient: AsgardeoAuthClient<T>,
@@ -69,6 +70,7 @@ export class AuthenticationHelper<
69
70
  this._authenticationClient = authClient;
70
71
  this._dataLayer = this._authenticationClient.getDataLayer();
71
72
  this._spaHelper = spaHelper;
73
+ this._isTokenRefreshing = false;
72
74
  }
73
75
 
74
76
  public enableHttpHandler(httpClient: HttpClientInstance): void {
@@ -189,6 +191,22 @@ export class AuthenticationHelper<
189
191
  }
190
192
  }
191
193
 
194
+ protected async retryFailedRequests (failedRequest: HttpRequestInterface): Promise<HttpResponse> {
195
+ if (this._isTokenRefreshing) {
196
+ return new Promise(() => setTimeout(() => {
197
+ return this.retryFailedRequests(failedRequest);
198
+ }, 500));
199
+ } else {
200
+ return this.httpRequest(failedRequest.httpClient,
201
+ failedRequest.requestConfig,
202
+ failedRequest.isHttpHandlerEnabled,
203
+ failedRequest.httpErrorCallback,
204
+ failedRequest.httpFinishCallback,
205
+ failedRequest.enableRetrievingSignOutURLFromSession
206
+ );
207
+ }
208
+ }
209
+
192
210
  public async httpRequest(
193
211
  httpClient: HttpClientInstance,
194
212
  requestConfig: HttpRequestConfig,
@@ -219,13 +237,26 @@ export class AuthenticationHelper<
219
237
  })
220
238
  .catch(async (error: HttpError) => {
221
239
  if (error?.response?.status === 401 || !error?.response) {
240
+ if (this._isTokenRefreshing) {
241
+ return this.retryFailedRequests({
242
+ enableRetrievingSignOutURLFromSession,
243
+ httpClient,
244
+ httpErrorCallback,
245
+ httpFinishCallback,
246
+ isHttpHandlerEnabled,
247
+ requestConfig
248
+ });
249
+ }
250
+ this._isTokenRefreshing = true;
222
251
  // Try to refresh the token
223
252
  let refreshAccessTokenResponse: BasicUserInfo;
224
253
  try {
225
254
  refreshAccessTokenResponse = await this.refreshAccessToken(
226
255
  enableRetrievingSignOutURLFromSession
227
256
  );
257
+ this._isTokenRefreshing = false;
228
258
  } catch (refreshError: any) {
259
+ this._isTokenRefreshing = false;
229
260
  if (isHttpHandlerEnabled) {
230
261
  if (typeof httpErrorCallback === "function") {
231
262
  await httpErrorCallback({
@@ -33,17 +33,22 @@ export class SPAHelper<T extends MainThreadClientConfig | WebWorkerClientConfig>
33
33
  MainThreadClientConfig | WebWorkerClientConfig
34
34
  >
35
35
  ): Promise<void> {
36
- const sessionData = await this._dataLayer.getSessionData();
37
- if (sessionData.refresh_token) {
38
- // Refresh 10 seconds before the expiry time
39
- const expiryTime = parseInt(sessionData.expires_in);
40
- const time = expiryTime <= 10 ? expiryTime : expiryTime - 10;
36
+ const shouldRefreshAutomatically = (await this._dataLayer.getConfigData())?.periodicTokenRefresh;
41
37
 
42
- const timer = setTimeout(async () => {
43
- await authenticationHelper.refreshAccessToken();
44
- }, time * 1000);
45
-
46
- await this._dataLayer.setTemporaryDataParameter(REFRESH_TOKEN_TIMER, JSON.stringify(timer));
38
+ // Automatic Token Refresh is enabled
39
+ if (shouldRefreshAutomatically) {
40
+ const sessionData = await this._dataLayer.getSessionData();
41
+ if (sessionData.refresh_token) {
42
+ // Refresh 10 seconds before the expiry time
43
+ const expiryTime = parseInt(sessionData.expires_in);
44
+ const time = expiryTime <= 10 ? expiryTime : expiryTime - 10;
45
+
46
+ const timer = setTimeout(async () => {
47
+ await authenticationHelper.refreshAccessToken();
48
+ }, time * 1000);
49
+
50
+ await this._dataLayer.setTemporaryDataParameter(REFRESH_TOKEN_TIMER, JSON.stringify(timer));
51
+ }
47
52
  }
48
53
  }
49
54
 
@@ -30,6 +30,7 @@ export interface SPAConfig {
30
30
  checkSessionInterval?: number;
31
31
  sessionRefreshInterval?: number;
32
32
  resourceServerURLs?: string[];
33
+ periodicTokenRefresh?: boolean;
33
34
  }
34
35
 
35
36
  /**
@@ -17,7 +17,8 @@
17
17
  */
18
18
 
19
19
  import { AxiosRequestConfig } from "axios";
20
- import { HttpError, HttpResponse } from ".";
20
+ import { HttpClientInstance, HttpError, HttpResponse } from ".";
21
+ import { SPACustomGrantConfig } from "..";
21
22
 
22
23
  export interface HttpClient {
23
24
  requestStartCallback: () => void;
@@ -33,6 +34,15 @@ export interface HttpRequestConfig extends AxiosRequestConfig {
33
34
  startTimeInMs?: number
34
35
  }
35
36
 
37
+ export interface HttpRequestInterface {
38
+ httpClient: HttpClientInstance,
39
+ requestConfig: HttpRequestConfig,
40
+ isHttpHandlerEnabled?: boolean,
41
+ httpErrorCallback?: (error: HttpError) => void | Promise<void>,
42
+ httpFinishCallback?: () => void,
43
+ enableRetrievingSignOutURLFromSession?: (config: SPACustomGrantConfig) => void
44
+ }
45
+
36
46
  export {
37
47
  AxiosResponse as HttpResponse,
38
48
  Method as HttpMethod,