diff --git a/keycloak.js b/keycloak.js new file mode 100644 index 0000000..ee45a4d --- /dev/null +++ b/keycloak.js @@ -0,0 +1,48 @@ +console.log("Keycloak Script running!"); +const keycloak = new Keycloak({ + realm: "schmoekerei", + "auth-server-url": "https://auth.xn--schmkerei-37a.de", + "ssl-required": "external", + resource: "account", + "public-client": true, + "confidential-port": 0, + url: "https://auth.xn--schmkerei-37a.de", + clientId: "homer", + "enable-cors": true, +}); +// Creates logout URI based on updated requirements, see https://www.keycloak.org/2022/04/keycloak-1800-released +const createLogoutUrl = () => { + return keycloak.endpoints.logout() + + '?id_token_hint=' + keycloak.idToken + + '&post_logout_redirect_uri=' + encodeURIComponent(window.location.href); +} +const loadData = () => { + console.log(keycloak.subject); + if (keycloak.idToken) { + document.getElementById("keycloak-footer").innerHTML = ' [' + keycloak.idTokenParsed.preferred_username + "] ausloggen"; + document.getElementById("keycloak-footer").href = createLogoutUrl(); + } else { + keycloak.loadUserProfile( + function () { + document.getElementById("keycloak-footer").innerHTML = ' [' + keycloak.idTokenParsed.preferred_username + "] ausloggen"; + document.getElementById("keycloak-footer").href = createLogoutUrl(); + }, + function () { + console.log("Failed to retrieve user details. Please enable claims or account role"); + } + ); + } +}; +const loadFailure = () => { + console.log("Failed to load data. Check console log"); +}; +const reloadData = () => { + keycloak + .updateToken(10) + .then(loadData) + .catch(() => { + document.getElementById("keycloak-footer").innerHTML = ' Login/Registrierung'; + document.getElementById("keycloak-footer").href = "https://auth.xn--schmkerei-37a.de/realms/schmoekerei/protocol/openid-connect/auth?client_id=homer&response_type=code&redirect_uri=https://schmökerei.de"; + }); +}; +keycloak.init({ onLoad: "check-sso", silentCheckSsoRedirectUri: "https://xn--schmkerei-37a.de/assets/silent-check-sso.html", silentCheckSsoFallback: false }).then(reloadData); diff --git a/silent-check-sso.html b/silent-check-sso.html new file mode 100644 index 0000000..20ad209 --- /dev/null +++ b/silent-check-sso.html @@ -0,0 +1,7 @@ + + + + +