-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
211 lines (180 loc) · 7.02 KB
/
app.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
window.addEventListener('load', function() {
var auth0 = newAuth0();
if (doesUserLogInLocally()) {
showThePage();
} else {
var isHandlingCallbackFromAuth0 = handleCallbackFromAuth0(toAuthResultFromLocationHash());
if (! isHandlingCallbackFromAuth0)
auth0.getSSOData(onSsoDataHandlingAuthentication);
}
setInterval(logoutIfLoggedOutGloballyByAnotherApp, 5000);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Helper function section
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function handleCallbackFromAuth0(authResult) {
if (authResult.idToken){
new Auth0Lock(AUTH0_CLIENT_ID, AUTH0_DOMAIN).getProfile(authResult.idToken, onUserProfile);
return true;
}
return false;
function onUserProfile(error, profile) {
if (! error) {
localStorage.setItem(KEY_LOCAL_STORAGE_USER_TOKEN, authResult.idToken);
localStorage.setItem(KEY_LOCAL_STORAGE_PROFILE, JSON.stringify(profile));
showThePage(authResult.state);
}
}
}
function onSsoDataHandlingAuthentication(err, data) {
var hasSsoSession = !err && data && data.sso;
if (hasSsoSession)
redirectToAuth0ForSso();
else
redirectTo(toMidasAccountsUrl('/sso'));
function redirectToAuth0ForSso() {
auth0.signin({
connection: data.lastUsedConnection.name,
state: window.location.href,
scope: 'openid name picture roles'
});
}
}
function logout() {
localStorage.removeItem(KEY_LOCAL_STORAGE_USER_TOKEN);
localStorage.removeItem(KEY_LOCAL_STORAGE_PROFILE);
redirectTo(toMidasAccountsUrl('/signoff', 'Logged off successfully'));
}
function logoutIfLoggedOutGloballyByAnotherApp() {
if (! doesUserLogInLocally()) return;
auth0.getSSOData(onSsoDataHandlingLogoutIfNoSsoSession);
function onSsoDataHandlingLogoutIfNoSsoSession(err, data) {
var hasSsoSession = !err && data && data.sso;
if (! hasSsoSession)
logout();
}
}
function newAuth0() {
return new Auth0({
domain: AUTH0_DOMAIN,
clientID: AUTH0_CLIENT_ID,
callbackURL: toAbsoluteUrl('/'),
callbackOnLocationHash: true
});
}
function toAbsoluteUrl(path) {
path = path || "";
return window.location.href.split(CONTEXT)[0] + CONTEXT + path;
}
function doesUserLogInLocally() {
return !! (localStorage.getItem(KEY_LOCAL_STORAGE_USER_TOKEN));
}
function showThePage(targetUrl) {
if (targetUrl)
redirectTo(targetUrl);
var locationPath = window.location.pathname;
document.body.style.display = 'inline';
routeByUserRole(locationPath.replace(CONTEXT, ''));
function routeByUserRole(route) {
var profile = JSON.parse(localStorage.getItem(KEY_LOCAL_STORAGE_PROFILE));
switch (route) {
case "":
case "/":
showLandingPageComponents();
break;
case ROUTE_USER:
secureRoute(ROLE_ISG_USER);
break;
case ROUTE_ADMIN:
secureRoute(ROLE_ISG_ADMIN);
break;
}
function showLandingPageComponents() {
var baseUrl = toAbsoluteUrl();
hide(document.getElementById('btn-login'));
showCommonComponentsInAllPages();
showProfile(profile);
if (hasRole(profile, ROLE_ISG_ADMIN)) {
rewireAndShowButtonById('btn-go-admin', baseUrl + ROUTE_ADMIN);
}
if (hasRole(profile, ROLE_ISG_USER)) {
rewireAndShowButtonById('btn-go-user', baseUrl + ROUTE_USER);
}
}
function secureRoute(requiredRole) {
if (!hasRole(profile, requiredRole)) {
if (profile)
redirectTo('unauthorized.html');
else
redirectTo(CONTEXT);
} else{
showCommonComponentsInAllPages();
}
}
function showCommonComponentsInAllPages() {
var buttonLogout = document.getElementById('btn-logout');
show(buttonLogout);
addClickListener(buttonLogout, logout);
show(document.querySelector('.container'));
document.getElementById('nickname').textContent = profile.nickname;
}
function rewireAndShowButtonById(id, route) {
var button = document.getElementById(id);
button.href = route;
show(button);
}
}
}
function showProfile(profile) {
var htmlNode,
i;
document.getElementById('profile-name').innerHTML = profile.name;
document.getElementById('profile-picture').src = profile.picture;
for(i = 0; i < profile.roles.length; i++) {
htmlNode = document.createElement("li");
htmlNode.innerHTML = profile.roles[i];
document.getElementById('profile-roles').appendChild(htmlNode);
}
show(document.getElementById("profile"));
}
function toAuthResultFromLocationHash() {
return {
idToken: getParameterByName('id_token'),
state: getParameterByName('state')
};
}
function toMidasAccountsUrl(endpoint, message, title) {
title = title || "JS Example";
message = message || "Please login to use the services";
return MIDAS_ACCOUNTS_URL + endpoint + '?returnToUrl='
+ encodeURIComponent(window.location) + '&title=' + title + '&message=' + message;
}
function hasRole(profile, role) {
return !!(profile &&
profile.app_metadata &&
profile.app_metadata.roles &&
profile.app_metadata.roles.indexOf(role) > -1);
}
function hide(element) {
element.style.display = "none";
}
function show(element) {
element.style.display = "inline-block";
}
function addClickListener(button, callback) {
if (button) {
button.addEventListener('click', callback);
}
}
function redirectTo(href) {
window.location.href = href;
}
function getParameterByName(name, url) {
name = name.replace(/[\[\]]/g, "\\$&");
url = url || window.location.href;
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
});