Added Keycloak integration

This commit is contained in:
2023-10-03 01:34:57 +02:00
parent 872541150f
commit eb92b96b46
2 changed files with 55 additions and 0 deletions

48
keycloak.js Normal file
View File

@@ -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 = '<i class="fas fa-sign-out-alt"></i> [' + keycloak.idTokenParsed.preferred_username + "] ausloggen";
document.getElementById("keycloak-footer").href = createLogoutUrl();
} else {
keycloak.loadUserProfile(
function () {
document.getElementById("keycloak-footer").innerHTML = '<i class="fas fa-sign-out-alt"></i> [' + 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 = '<i class="fas fa-sign-in-alt"></i> 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);

7
silent-check-sso.html Normal file
View File

@@ -0,0 +1,7 @@
<html>
<body>
<script>
parent.postMessage(location.href, location.origin)
</script>
</body>
</html>