diff --git a/packages/oidc-client/README.md b/packages/oidc-client/README.md index a17f2b93f..b958d2bc0 100644 --- a/packages/oidc-client/README.md +++ b/packages/oidc-client/README.md @@ -194,9 +194,10 @@ const configuration = { refresh_time_before_tokens_expiration_in_second: Number, // default is 120 seconds service_worker_relative_url: String, service_worker_keep_alive_path: String, // default is "/" - service_worker_only: Boolean, // default false - service_worker_activate: () => boolean, // you can take the control of the service worker default activation which use user agent string + service_worker_only: Boolean, // default false, if true, the user will not be able to login if the service worker is not available on its browser + service_worker_activate: () => boolean, // you can take the control of the service worker default activation which use user agent string, if return false, the service worker mode will not be used service_worker_update_require_callback: (registration:any, stopKeepAlive:Function) => Promise, // callback called when service worker need to be updated, you can take the control of the update process + service_worker_register: (url: string) => Promise, // Optional, you can take the control of the service worker registration extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that is sent to the OIDC server (more info: https://github.com/openid/AppAuth-JS) token_request_extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that is sent to the OIDC server during token request (more info: https://github.com/openid/AppAuth-JS) authority_time_cache_wellknowurl_in_second: 60 * 60, // Time to cache in seconds of the openid well-known URL, default is 1 hour diff --git a/packages/oidc-client/src/initWorker.ts b/packages/oidc-client/src/initWorker.ts index 229a4471f..959bc0a75 100644 --- a/packages/oidc-client/src/initWorker.ts +++ b/packages/oidc-client/src/initWorker.ts @@ -6,7 +6,7 @@ import {ILOidcLocation} from "./location"; let keepAliveServiceWorkerTimeoutId = null; let keepAliveController; -export const sleepAsync = (milliseconds) => { +export const sleepAsync = ({milliseconds}: { milliseconds: any }) => { return new Promise(resolve => timer.setTimeout(resolve, milliseconds)); }; @@ -16,7 +16,7 @@ const keepAlive = (service_worker_keep_alive_path='/') => { keepAliveController = new AbortController(); const promise = fetch(`${service_worker_keep_alive_path}OidcKeepAliveServiceWorker.json?minSleepSeconds=${minSleepSeconds}`, { signal: keepAliveController.signal }); promise.catch(error => { console.log(error); }); - sleepAsync(minSleepSeconds * 1000).then(keepAlive); + sleepAsync({milliseconds: minSleepSeconds * 1000}).then(keepAlive); } catch (error) { console.log(error); } }; @@ -41,7 +41,7 @@ export const defaultServiceWorkerUpdateRequireCallback = (location:ILOidcLocatio await registration.update(); const isSuccess = await registration.unregister(); console.log(`Service worker unregistering ${isSuccess}`) - await sleepAsync(2000); + await sleepAsync({milliseconds: 2000}); location.reload(); } @@ -72,7 +72,12 @@ export const initWorkerAsync = async(configuration, configurationName) => { return null; } - const registration = await navigator.serviceWorker.register(serviceWorkerRelativeUrl); + let registration = null; + if(configuration.register) { + registration = await configuration.service_worker_register(serviceWorkerRelativeUrl); + } else { + registration = await navigator.serviceWorker.register(serviceWorkerRelativeUrl); + } try { await navigator.serviceWorker.ready; diff --git a/packages/oidc-client/src/oidc.ts b/packages/oidc-client/src/oidc.ts index 0d53d90cf..b0e023a6a 100644 --- a/packages/oidc-client/src/oidc.ts +++ b/packages/oidc-client/src/oidc.ts @@ -352,18 +352,18 @@ Please checkout that you are using OIDC hook inside a 0) { - await sleepAsync(1000); + await sleepAsync({milliseconds: 1000}); numberTryOnline--; this.publishEvent(eventNames.refreshTokensAsync, { message: `wait because navigator is offline try ${numberTryOnline}` }); } let numberTryHidden = Math.floor(Math.random() * 15) + 10; while (document.hidden && numberTryHidden > 0) { - await sleepAsync(1000); + await sleepAsync({milliseconds: 1000}); numberTryHidden--; this.publishEvent(eventNames.refreshTokensAsync, { message: `wait because navigator is hidden try ${numberTryHidden}` }); } diff --git a/packages/oidc-client/src/parseTokens.ts b/packages/oidc-client/src/parseTokens.ts index c57816ca9..b1e6f83df 100644 --- a/packages/oidc-client/src/parseTokens.ts +++ b/packages/oidc-client/src/parseTokens.ts @@ -173,7 +173,7 @@ export const getValidTokenAsync = async (oidc: OidcToken, waitMs = 200, numberWa return null; } while (!isTokensValid(oidc.tokens) && numberWaitTemp > 0) { - await sleepAsync(waitMs); + await sleepAsync({milliseconds: waitMs}); numberWaitTemp = numberWaitTemp - 1; } const isValid = isTokensValid(oidc.tokens); diff --git a/packages/oidc-client/src/types.ts b/packages/oidc-client/src/types.ts index 89e4e7a44..54180c818 100644 --- a/packages/oidc-client/src/types.ts +++ b/packages/oidc-client/src/types.ts @@ -3,6 +3,7 @@ export type Fetch = typeof window.fetch; export type LogoutToken = 'access_token' | 'refresh_token'; export type ServiceWorkerUpdateRequireCallback = (registration:any, stopKeepAlive:Function) => Promise; +export type ServiceWorkerRegister = (serviceWorkerRelativeUrl:string) => Promise; export type ServiceWorkerActivate = () => boolean; export type OidcConfiguration = { @@ -19,6 +20,7 @@ export type OidcConfiguration = { refresh_time_before_tokens_expiration_in_second?: number; token_request_timeout?: number; service_worker_relative_url?:string; + service_worker_register?:ServiceWorkerRegister; service_worker_keep_alive_path?:string; service_worker_activate?:ServiceWorkerActivate; service_worker_only?:boolean; diff --git a/packages/oidc-client/src/user.ts b/packages/oidc-client/src/user.ts index ebc1d0600..5068c6435 100644 --- a/packages/oidc-client/src/user.ts +++ b/packages/oidc-client/src/user.ts @@ -8,7 +8,7 @@ export const userInfoAsync = (oidc) => async (noCache = false) => { // We wait the synchronisation before making a request while (oidc.tokens && !isTokensValid(oidc.tokens)) { - await sleepAsync(200); + await sleepAsync({milliseconds: 200}); } if (!oidc.tokens) { diff --git a/packages/react-oidc/README.md b/packages/react-oidc/README.md index 638da1918..8abaddeb2 100644 --- a/packages/react-oidc/README.md +++ b/packages/react-oidc/README.md @@ -191,6 +191,7 @@ const configuration = { service_worker_only: Boolean, // default false service_worker_activate: () => boolean, // you can take the control of the service worker default activation which use user agent string service_worker_update_require_callback: (registration:any, stopKeepAlive:Function) => Promise, // callback called when service worker need to be updated, you can take the control of the update process + service_worker_register: (url: string) => Promise, // Optional, you can take the control of the service worker registration extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that is sent to the OIDC server (more info: https://github.com/openid/AppAuth-JS) token_request_extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that is sent to the OIDC server during token request (more info: https://github.com/openid/AppAuth-JS) withCustomHistory: Function, // Override history modification, return an instance with replaceState(url, stateHistory) implemented (like History.replaceState())