-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
317 lines (276 loc) · 9.05 KB
/
index.html
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SSO</title>
{{ if ne (len .Scopes) 0 }}
<meta name="oauth2:scopes" content="{{ .Scopes }}">
{{ end }}
{{ if ne (len .ClientName) 0 }}
<meta name="oauth2:client" content="{{ .ClientName }}">
{{ end }}
{{ if ne (len .GrantNonce) 0 }}
<meta name="oauth2:grant_nonce" content="{{ .GrantNonce }}">
{{ end }}
{{ if .RequireSignIn }}
<meta name="oauth2:require_sign_in" content="true">
{{ else }}
<meta name="oauth2:require_sign_in" content="false">
{{ end }}
<meta name="description" content="SSO">
<meta name="author" content="GamaOps">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<script>
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
function onGrantScopes() {
var nonce = $('meta[name="oauth2:grant_nonce"]').attr('content');
var subject = getCookie('SSO_SUB');
$.ajax({
url: '/sign-in/authorize',
type: 'post',
dataType: 'json',
contentType: 'application/json',
error: function (jqXHR,textStatus,errorThrown) {
putError(jqXHR.responseJSON);
},
success: function (data) {
resetError();
// Reload so server can generate authorization code/tokens usign the granted session
window.location.reload();
},
data: JSON.stringify({
nonce,
subject,
granted: true
})
});
}
function onDenyScopes() {
var redirectUri = getUrlParameter('redirect_uri')
var state = getUrlParameter('state')
window.location.replace(redirectUri+`#state=${state}&error=access_denied&details=resource owner denied scopes grant`);
}
function showGrantScreen() {
var elGrantNonce = $('meta[name="oauth2:grant_nonce"]');
if (elGrantNonce.length === 0) {
return
}
$("#grant").show();
var requireSignIn = $('meta[name="oauth2:require_sign_in"]').attr('content') === 'true';
var clientName = $('meta[name="oauth2:client"]').attr('content');
var scopes = $('meta[name="oauth2:scopes"]').attr('content');
if (requireSignIn) {
$("#signin-helper").html(`You must sign in before granting scopes to: ${clientName}`);
return
}
$("#grant-app").html(clientName);
$("#grant-scopes").html(scopes);
}
function loadAccountSwitch() {
var accountSwitch = $("#account-switch");
accountSwitch.html('');
var cookies = getCookiesByRegexp(/^SUBSESS_.*/);
var currentSubject = getCookie('SSO_SUB');
var currentSession = null;
for (var key in cookies) {
cookie = cookies[key];
var session = JSON.parse(window.atob(cookie));
var subject = key.substring('SUBSESS_'.length)
accountSwitch.append(`<p><b>Account: ${session.name} (activated: ${session.activated})</b> <button onclick="onSwitchAccount('${subject}')">Switch To</button> <button onclick="onSignOut('${subject}')">Sign Out</button></p>`);
}
if (currentSubject === null) {
return;
}
var currentSession = getCookie('SUBSESS_'+currentSubject);
if (currentSession === null) {
return;
}
currentSession = JSON.parse(window.atob(currentSession));
accountSwitch.prepend(`<h2>Current Session: ${currentSession.name}</h2>`);
}
function onSignOut(subject) {
var cookies = getCookiesByRegexp(/^SUBSESS_.*/);
var currentSubject = getCookie('SSO_SUB');
var subjectKey = 'SUBSESS_'+subject;
if (subjectKey in cookies) {
eraseCookie(subjectKey);
delete cookies[subjectKey]
}
if (currentSubject === subject) {
if (Object.keys(cookies).length === 0) {
eraseCookie('SSO_SUB');
loadAccountSwitch();
return;
}
for (var key in cookies) {
subject = key.substring('SUBSESS_'.length);
setCookie('SSO_SUB', subject)
break;
}
}
loadAccountSwitch();
}
function onSwitchAccount(subject) {
setCookie("SSO_SUB", subject);
window.location.reload();
}
$(document).ready(function () {
loadAccountSwitch();
showGrantScreen();
});
function setCookie(name,value,expiresAt) {
var expires = "";
if (expiresAt) {
var date = new Date(expiresAt*1000);
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function getCookiesByRegexp(regexp) {
var cookies = {};
var ca = document.cookie.split(';');
for (var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
var [nameEQ] = c.split('=')
if (regexp.test(nameEQ))
cookies[nameEQ] = c.substring(nameEQ.length+1,c.length);
}
return cookies;
}
function eraseCookie(name) {
document.cookie = name+'=; Max-Age=-99999999;';
}
function putError(error) {
$("#error").html(JSON.stringify(error, null, ' '));
}
function resetError() {
$("#error").html('');
}
function onSubmitSignIn(token) {
var data = $("#signin-form").serializeArray();
var payload = {};
for (item of data) {
if (item.name !== 'g-recaptcha-response') {
payload[item.name] = item.value;
continue;
}
payload.recaptcha_response = item.value
}
$.ajax({
url: '/sign-in/authenticate',
type: 'post',
dataType: 'json',
contentType: 'application/json',
error: function (jqXHR,textStatus,errorThrown) {
putError(jqXHR.responseJSON);
},
success: function (data) {
resetError();
// Stores subject cookie with the name and set expiration from response
// If activation_method == 0 we're already authenticated otherwise we need to go to mfa flow
var currentSession = getCookie("SUBSESS_"+data.subject);
if (currentSession !== null) {
currentSession = JSON.parse(window.atob(currentSession));
} else {
currentSession = {name: data.name, activated: false}
}
if (data.activation_method === 0) {
currentSession.activated = true;
}
if (!currentSession.activated) {
$("#signin").hide()
$("#activate [name='subject']").val(data.subject);
// Only for demonstration purpose, we shouldn't put the challenge on DOM, instead store only in-memory
$("#activate [name='challenge']").val(data.challenge);
$("#activate").show();
}
setCookie("SUBSESS_"+data.subject, window.btoa(JSON.stringify(currentSession)), data.expiration);
if (currentSession.activated) {
window.location.reload();
return;
}
loadAccountSwitch();
},
data: JSON.stringify(payload)
});
}
function onSubmitActivation() {
var data = $("#activate-form").serializeArray();
var payload = {};
for (item of data) {
payload[item.name] = item.value;
}
$.ajax({
url: '/sign-in/activate',
type: 'post',
dataType: 'json',
contentType: 'application/json',
error: function (jqXHR,textStatus,errorThrown) {
putError(jqXHR.responseJSON);
},
success: function (data) {
resetError();
var subject = $("#activate-form [name='subject']").val();
var currentSession = JSON.parse(window.atob(getCookie("SUBSESS_"+subject)));
currentSession.activated = true;
$("#activate").hide();
setCookie("SUBSESS_"+subject, window.btoa(JSON.stringify(currentSession)), data.expiration);
setCookie("SSO_SUB", subject);
window.location.reload();
},
data: JSON.stringify(payload)
});
}
</script>
<div id="error">
</div>
<div id="account-switch">
</div>
<div id="signin">
<form id="signin-form">
<h3>SignIn</h3>
<p id="signin-helper"></p>
<input type="text" name="identifier" placeholder="Identifier" /><br>
<input type="password" name="password" placeholder="Password" /><br>
<button class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
data-callback="onSubmitSignIn">Submit</button>
</form>
</div>
</div>
<div id="activate" style="display:none">
<form id="activate-form">
<h3>Activate Session (MFA)</h3>
<input type="text" name="activation_code" placeholder="Code" /><br>
<input type="hidden" name="subject" />
<input type="hidden" name="challenge" />
<button onclick="onSubmitActivation()" type="button">Submit</button>
</form>
</div>
<div id="grant" style="display:none">
<h3>Grant Scopes To App: <span id="grant-app"></span></h3>
<p>This app is requiring the following scopes: <span id="grant-scopes"></span></p>
<button onclick="onGrantScopes()" type="button">Grant</button>
<button onclick="onDenyScopes()" type="button">Deny</button>
</div>
</body>
</html>