This repository was archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.html
More file actions
105 lines (95 loc) · 5.05 KB
/
App.html
File metadata and controls
105 lines (95 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web Client</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<link rel="icon" type="image/png" href="favicon.png">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"
referrerpolicy="no-referrer">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"
referrerpolicy="no-referrer"></script>
<!-- See https://www.keycloak.org/docs/latest/securing_apps/#_javascript_adapter -->
<script src="http://localhost:8080/auth/js/keycloak.js"></script>
<script>
let keycloak = null;
let cyberchefUrlBase = "https://gchq.github.io/CyberChef/#recipe=JWT_Decode()&input=";
//Credits: https://jsfiddle.net/magikMaker/7bjaT/
function base64EncodeUrl(str) {
return window.btoa(str).replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
}
function initKC() {
keycloak = new Keycloak({
url: "http://localhost:8080/auth",
realm: "demo",
clientId: "demo"
});
keycloak.init().then(function (authenticated) {
//See https://www.keycloak.org/docs/latest/securing_apps/#javascript-adapter-reference
let loginStateDiv = document.getElementById("loginState");
let decodeAccessTokenLink = document.getElementById("decodeAccessTokenLink");
let decodeIdentityTokenLink = document.getElementById("decodeIdentityTokenLink");
loginStateDiv.innerText = "Anonymous";
loginStateDiv.className = authenticated ? "alert alert-success" : "alert alert-secondary";
if (authenticated) {
loginStateDiv.innerText = "Loading user profile...";
keycloak.loadUserProfile()
.then(function (profile) {
loginStateDiv.innerText = `Welcome user ${profile.username} !`;
decodeAccessTokenLink.href = cyberchefUrlBase + base64EncodeUrl(keycloak.token);
decodeIdentityTokenLink.href = cyberchefUrlBase + base64EncodeUrl(keycloak
.idToken);
console.info("USER PROFILE:")
console.info(profile);
}).catch(function (er) {
alert("Failed to load user profile: " + er.error);
console.error(er);
});
console.info("TOKENS:")
console.info(keycloak);
}
}).catch(function (err) {
alert("Failed to initialize: " + err.error);
console.error(err);
});
//See https://getbootstrap.com/docs/5.0/components/tooltips/#example-enable-tooltips-everywhere
let tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
let tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
}
function login() {
//See https://www.keycloak.org/docs/latest/securing_apps/#login-options
let options = {
scope: "openid profile api",
prompt: "consent"
}
keycloak.login(options);
}
function logout() {
//See https://www.keycloak.org/docs/latest/securing_apps/#logout-options
keycloak.logout();
}
</script>
</head>
<body onload="initKC()">
<div class="container-lg">
<div class="alert alert-primary" role="alert" id="loginState"></div>
<button type="button" class="btn btn-primary" onclick="login()" data-bs-toggle="tooltip"
title="Login user on Keycloak. Open developer Console to see tokens and profile information."
data-bs-placement="top">Login</button>
<button type="button" class="btn btn-primary" onclick="logout()" title="Logout user from Keycloak."
data-bs-toggle="tooltip" data-bs-placement="top">Logout</button>
<br />
💻 <a target="_blank" id="decodeAccessTokenLink"
href="https://gchq.github.io/CyberChef/#recipe=JWT_Decode()&input=">Decode Access Token</a><br />
💻 <a target="_blank" id="decodeIdentityTokenLink"
href="https://gchq.github.io/CyberChef/#recipe=JWT_Decode()&input=">Decode Identity Token</a>
</div>
</body>
</html>