-
Notifications
You must be signed in to change notification settings - Fork 1
/
arcade.js
140 lines (120 loc) · 4.35 KB
/
arcade.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import "@patternfly/pfe-cta";
import "@patternfly/pfe-navigation";
import "@patternfly/pfe-icon";
import "@patternfly/pfe-card";
import "@patternfly/pfe-band";
import "@patternfly/pfe-button";
const keycloak = new Keycloak();
let keycloakInitialized = false;
async function main() {
await initializeKeycloakWithRetry();
// if it worked, handle it in the UI
if (keycloakInitialized) {
activateLoginButtons();
authifyPlayButtons();
}
// if keycloak init didn't work, don't touch the UI, it's default state is
// to display nothing authentication-related.
}
function clearTokens() {
localStorage.clear();
}
async function initializeKeycloakWithRetry() {
try {
await initializeKeycloak();
} catch (e) {
// if an error occurs during keycloak init, it's probably because of
// corrupt saved tokens. clear them and try once again
clearTokens();
await initializeKeycloak();
}
}
async function initializeKeycloak() {
const keycloakInitOptions = {
promiseType: "native",
checkLoginIframe: false,
onLoad: location.search.includes("warhw") ? "login-required" : undefined,
token: localStorage.getItem("arcade-token") || undefined,
refreshToken: localStorage.getItem("refreshToken") || undefined,
};
const authenticated = await keycloak.init(keycloakInitOptions);
console.log('keycloak initialized', { authenticated });
keycloakInitialized = true;
if (authenticated) {
localStorage.setItem("arcade-token", keycloak.token);
localStorage.setItem("refreshToken", keycloak.refreshToken);
localStorage.setItem("arcade-username", getUsername());
}
}
function activateLoginButtons() {
document.addEventListener("pfe-button:click", ev => {
if (ev.target.getAttribute("arcade-action") === "login") {
console.log("logging in");
keycloak.login();
}
else if (ev.target.getAttribute("arcade-action") === "logout") {
console.log("logging out");
// remove warhw querystring from the post-logout landing page to
// prevent automatic re-login. if this ever needs to be more
// robust, use `new URL(location)` and remove the querystring from
// there.
const redirectUri = new URL(location);
redirectUri.search = "";
keycloak.logout({ redirectUri: redirectUri.href });
}
});
document.querySelector("#warhw-login").classList.remove("inactive");
if (keycloak.authenticated) {
let displayName;
try {
// try to get preferreed_username
displayName = keycloak.tokenParsed.preferred_username;
} catch (e) {
// if that fails, try to extract username from email
try {
displayName = keycloak.tokenParsed.email.split("@")[0];
} catch (e) {
// if that fails, assign a default
displayName = "Red Hat Associate";
}
}
document.querySelector("#warhw-logged-in").classList.remove("inactive");
document.querySelector("#warhw-login-id").textContent = displayName;
[...document.querySelectorAll(".warhw.eligible.inactive")].forEach(e => e .classList.remove("inactive"));
}
else {
document.querySelector("#warhw-login-prompt").classList.remove("inactive");
}
}
function getUsername() {
if (keycloak.authenticated) {
try {
// try to get preferreed_username
return keycloak.tokenParsed.preferred_username;
} catch (e) {
// if that fails, try to extract username from email
try {
return keycloak.tokenParsed.email.split("@")[0];
} catch (e) {
// if that fails, assign a default
return "Red Hat Associate";
}
}
}
return null;
}
function authifyPlayButtons() {
if (keycloak.authenticated) {
const playButtons = document.querySelectorAll("[arcade-play-link]");
[...playButtons].forEach(btn => {
const url = new URL(btn.href);
url.hash = keycloak.token;
console.log(`adding auth hash to ${btn.href}`);
btn.href = url.href;
});
}
else {
console.log('skipping autify play buttons');
}
}
main();