From e9df546e059e8d91f21ced8c8e5fbb04d04bd4a8 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Thu, 30 Apr 2020 16:59:01 +0200 Subject: [PATCH] Allow silentCheckSsoRedirectUri to be a relative/absolute path as well In some cases we want to set the path via a html attribute where we can't easily compute a URL relative to the bundle or page. Convert a path to an URL if parsing it as an URL doesn't work instead. --- packages/auth/README.md | 2 +- packages/auth/src/keycloak.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/auth/README.md b/packages/auth/README.md index 8d95b6e7..5367c0d4 100644 --- a/packages/auth/README.md +++ b/packages/auth/README.md @@ -28,7 +28,7 @@ - `url`: The base URL of the Keycloak server - `realm`: The Keycloak realm - `clientId`: The Keycloak client to use -- `silentCheckSsoRedirectUri`: URI to a separate page for checking the login session in an iframe, see https://www.keycloak.org/docs/latest/securing_apps/#_javascript_adapter +- `silentCheckSsoRedirectUri`: URI or path to a separate page for checking the login session in an iframe, see https://www.keycloak.org/docs/latest/securing_apps/#_javascript_adapter - `scope`: Space separated list of scopes to request. These scopes get added in addition to the default ones, assuming the scope is in the optional scopes list of the Keycloak client in use. ## Events to listen to diff --git a/packages/auth/src/keycloak.js b/packages/auth/src/keycloak.js index 831f9962..b46af737 100644 --- a/packages/auth/src/keycloak.js +++ b/packages/auth/src/keycloak.js @@ -17,6 +17,18 @@ const promiseTimeout = function(ms, promise) { }; +/** + * Returns a URL for a relative path or URL + */ +const ensureURL = function(urlOrPath) { + try { + return new URL(urlOrPath).href; + } catch (e) { + return new URL(urlOrPath, window.location.href).href; + } +} + + /** * Wraps the keycloak API to support async/await, adds auto token refreshing and consolidates all * events into one native "changed" event @@ -111,7 +123,7 @@ export class KeycloakWrapper extends EventTarget { if (this._silentCheckSsoUri) { options['onLoad'] = 'check-sso'; - options['silentCheckSsoRedirectUri'] = this._silentCheckSsoUri; + options['silentCheckSsoRedirectUri'] = ensureURL(this._silentCheckSsoUri); // When silent-sso-check is active but the iframe doesn't load/work we will // never return here, so add a timeout and emit a signal so the app can continue -- GitLab