From 2148d421cd62e009d2b3f36957b1550bb52c93eb Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Thu, 15 Oct 2020 13:23:22 +0200 Subject: [PATCH] Load the keycloak-js libary from the keycloak server instead of bundling it This makes sure the library version works with the server version. Ideally we'd also want to check the version so we can error out if a too old keycloak server is on the other side, but I don't see any public API changes that would allow us to know if the library is too old. Fixes #32 --- packages/auth/package.json | 1 - packages/auth/src/keycloak.js | 20 +++++++++++++++++++- packages/location-select/package.json | 1 - packages/person-select/package.json | 1 - yarn.lock | 15 +-------------- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/auth/package.json b/packages/auth/package.json index 40271f8a..8e11571f 100644 --- a/packages/auth/package.json +++ b/packages/auth/package.json @@ -33,7 +33,6 @@ "@open-wc/scoped-elements": "^1.1.1", "dbp-common": "^1.0.0", "event-target-shim": "^5.0.1", - "keycloak-js": "^10.0", "lit-element": "^2.3.1" }, "scripts": { diff --git a/packages/auth/src/keycloak.js b/packages/auth/src/keycloak.js index 59138222..d13c7279 100644 --- a/packages/auth/src/keycloak.js +++ b/packages/auth/src/keycloak.js @@ -1,5 +1,21 @@ import {EventTarget} from "event-target-shim"; // Because EventTarget() doesn't exist on Safari -import Keycloak from "keycloak-js"; + + +/** + * Imports the keycloak JS API as if it was a module. + * + * @param baseUrl {string} + */ +async function importKeycloak(baseUrl) { + const keycloakSrc = baseUrl + '/js/keycloak.min.js'; + // Importing will write it to window so we take it from there + await import(keycloakSrc); + if (importKeycloak._keycloakMod !== undefined) + return importKeycloak._keycloakMod; + importKeycloak._keycloakMod = window.Keycloak; + delete window.Keycloak; + return importKeycloak._keycloakMod; +} const promiseTimeout = function(ms, promise) { @@ -88,6 +104,8 @@ export class KeycloakWrapper extends EventTarget { if (this._keycloak !== null) return; + const Keycloak = await importKeycloak(this._baseURL); + this._keycloak = Keycloak({ url: this._baseURL, realm: this._realm, diff --git a/packages/location-select/package.json b/packages/location-select/package.json index 39d6240e..8dccd2fc 100644 --- a/packages/location-select/package.json +++ b/packages/location-select/package.json @@ -30,7 +30,6 @@ "dbp-auth": "^1.0.0", "dbp-common": "^1.0.0", "jquery": "^3.4.1", - "keycloak-js": "^10.0", "lit-element": "^2.3.1", "select2": "^4.0.10" }, diff --git a/packages/person-select/package.json b/packages/person-select/package.json index 294cdafd..4d058df1 100644 --- a/packages/person-select/package.json +++ b/packages/person-select/package.json @@ -30,7 +30,6 @@ "dbp-auth": "^1.0.0", "dbp-common": "^1.0.0", "jquery": "^3.4.1", - "keycloak-js": "^10.0", "lit-element": "^2.3.1", "select2": "^4.0.10" }, diff --git a/yarn.lock b/yarn.lock index 0932724a..3f36f1b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1677,7 +1677,7 @@ base64-arraybuffer@0.1.5: resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz#73926771923b5a19747ad666aa5cd4bf9c6e9ce8" integrity sha1-c5JncZI7Whl0etZmqlzUv5xunOg= -base64-js@1.3.1, base64-js@^1.0.2: +base64-js@^1.0.2: version "1.3.1" resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.3.1.tgz#58ece8cb75dd07e71ed08c736abc5fac4dbf8df1" integrity sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g== @@ -4960,11 +4960,6 @@ jquery@>=1.7, jquery@^3.4.1: resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.5.1.tgz#d7b4d08e1bfdb86ad2f1a3d039ea17304717abb5" integrity sha512-XwIBPqcMn57FxfT+Go5pzySnm4KWkT1Tv7gjrpT1srtf8Weynl6R273VJ5GjkRb51IzMp5nbaPjJXMWeju2MKg== -js-sha256@0.9.0: - version "0.9.0" - resolved "https://registry.yarnpkg.com/js-sha256/-/js-sha256-0.9.0.tgz#0b89ac166583e91ef9123644bd3c5334ce9d0966" - integrity sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA== - js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -5117,14 +5112,6 @@ karma@^5.0.1, karma@^5.1.0: ua-parser-js "0.7.22" yargs "^15.3.1" -keycloak-js@^10.0: - version "10.0.2" - resolved "https://registry.yarnpkg.com/keycloak-js/-/keycloak-js-10.0.2.tgz#f0cf5b942627c5221f1466552c40e4624503b77b" - integrity sha512-7nkg4Ob1khHGcNbuK36AMndKUEuIQFpNlWU9ygWs7nSBPCI9VZ8dJjjXfKJHm0ewgcqLFGPIJ6bxxRlfcQ6sLg== - dependencies: - base64-js "1.3.1" - js-sha256 "0.9.0" - kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0: version "3.2.2" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64" -- GitLab