-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcb.html
More file actions
385 lines (322 loc) · 16.9 KB
/
cb.html
File metadata and controls
385 lines (322 loc) · 16.9 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OAuth2 Playground</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="icon" href="favicon.ico">
<link rel="stylesheet" href="assets/css/styles.css">
</head>
<body class="bg-slate-50 text-slate-700 font-mono min-h-screen flex items-center justify-center p-4">
<div class="w-full max-w-6xl mx-auto bg-white border border-slate-200 shadow-lg p-6">
<div class="border-b border-slate-200 pb-4 mb-6">
<h1 class="text-2xl font-bold text-slate-800 flex items-center">
<span class="mr-2 text-blue-500"></span>
OAuth2 Playground
</h1>
<p class="text-slate-600 text-sm mt-1">
Test OAuth2 flows interactively without data tracking nor credentials storing
</p>
</div>
<div class="grid gap-6 h-[68vh]">
<div class="border border-slate-200 bg-white form-container flex flex-col">
<div class="bg-blue-50 border-b border-slate-200 p-3 flex-shrink-0">
<h2 class="text-lg font-semibold text-slate-800 flex items-center">
<span class="mr-2 text-blue-500">></span>
Callback Configuration
</h2>
</div>
<div class="form-content p-6 flex-1 overflow-y-auto">
<div class="mb-6">
<h3 class="text-sm font-semibold text-slate-700 mb-3 uppercase tracking-wide border-b border-slate-200 pb-2">
OAuth Configuration
</h3>
<div class="space-y-3">
<div class="flex gap-4">
<div class="flex-1">
<label for="clientId" class="block text-slate-700 text-xs font-semibold mb-1">
Client ID
</label>
<input type="text" id="clientId" readonly
class="w-full px-3 py-1 bg-gray-100 border border-slate-300 text-slate-600 font-mono select-none focus:outline-none h-[34px] rounded-sm"
placeholder="your-client-id">
</div>
<div class="flex-1">
<div class="hidden">
<label for="clientSecret" class="block text-slate-700 text-xs font-semibold mb-1">
Client Secret
</label>
<!-- <input type="text" id="clientSecret" readonly-->
<!-- class="w-full px-3 py-1 bg-gray-100 border border-slate-300 text-slate-600 font-mono select-none focus:outline-none h-[34px] rounded-sm"-->
<!-- placeholder="your-client-secret">-->
<input type="text" id="clientSecret"
class="w-full px-3 py-1 bg-white border border-slate-300 text-slate-700 font-mono focus:outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400 h-[34px] rounded-sm"
placeholder="your-client-secret">
</div>
<div class="">
<label for="codeVerifier" class="block text-slate-700 text-xs font-semibold mb-1">
Code Verifier
</label>
<input type="text" id="codeVerifier" readonly
class="w-full px-3 py-1 bg-gray-100 border border-slate-300 text-slate-600 font-mono select-none focus:outline-none h-[34px] rounded-sm"
placeholder="randomly-generated-code">
</div>
</div>
</div>
<div class="">
<label for="tokenUrl" class="block text-slate-700 text-xs font-semibold mb-1">
Token URL
</label>
<input type="url" id="tokenUrl"
class="w-full px-3 py-1 bg-white border border-slate-300 text-slate-700 font-mono focus:outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400 rounded-sm"
placeholder="https://auth.example.com/oauth2/token">
</div>
<div class="">
<label for="redirectUri" class="block text-slate-700 text-xs font-semibold mb-1">
Redirect URI
</label>
<input type="url" id="redirectUri"
class="w-full px-3 py-1 bg-white border border-slate-300 text-slate-700 font-mono focus:outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400 rounded-sm"
placeholder="https://myapp.prod.com/auth/callback">
</div>
</div>
</div>
<div id="authCodeSection" class="mb-6">
<h3 class="text-sm font-semibold text-slate-700 mb-3 uppercase tracking-wide border-b border-slate-200 pb-2">
Authorization Code
</h3>
<div class="relative">
<input type="text" id="authCodeInput" readonly
class="w-full px-3 py-1 bg-gray-100 border border-slate-300 text-slate-600 font-mono text-sm select-none focus:outline-none rounded-sm"
placeholder="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">
</div>
</div>
<div class="mt-auto pt-6 border-t border-slate-200">
<div class="flex justify-between items-center">
<button id="returnBtn"
class="px-6 py-2 bg-slate-500 hover:bg-slate-600 text-white text-sm font-semibold rounded-sm transition-colors duration-200 flex items-center">
<svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
</svg>
Return
</button>
<button id="exchangeCodeBtn"
class="px-6 py-2 bg-emerald-500 hover:bg-emerald-600 text-white text-sm font-semibold rounded-sm transition-colors duration-200 flex items-center">
Exchange Code
<svg class="w-4 h-4 ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7l5 5m0 0l-5 5m5-5H6"/>
</svg>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="confirmModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
<div class="bg-white border border-slate-300 shadow-xl max-w-md w-full mx-4">
<div class="p-6">
<div class="flex items-center mb-4">
<span class="mr-3 text-xl">⚠</span>
<h3 class="text-lg font-semibold text-slate-800">Confirm Action</h3>
</div>
<p id="modalMessage" class="text-slate-600 mb-6 font-mono">Are you sure you want to proceed?</p>
<div class="flex justify-end space-x-2">
<button id="modalCancel"
class="px-4 py-1 text-slate-600 bg-white border border-slate-300 hover:bg-slate-50 font-semibold transition-colors h-[34px]">
Cancel
</button>
<button id="modalConfirm"
class="px-4 py-1 bg-red-500 hover:bg-red-600 text-white border border-red-500 font-semibold transition-colors h-[34px]">
Confirm
</button>
</div>
</div>
</div>
</div>
<div id="notifications" class="fixed top-4 right-4 space-y-2 z-40 font-mono"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
<script src="assets/js/utils.js"></script>
<script src="assets/js/storage-service.js"></script>
<script src="assets/js/ui-service.js"></script>
<script src="assets/js/oauth2-service.js"></script>
<!--<script src="assets/js/callback-service.js"></script>-->
<!--<script src="assets/js/main.js"></script>-->
<script>
//logout ccallback
(() => {
const params = new URLSearchParams(window.location.search);
const st = params.get('state');
if (st && st.endsWith('logoutcb')) {
document.documentElement.style.backgroundColor = '#ffffff';
document.body.innerHTML = '';
try {
if (typeof OAuth2Service === 'object' && OAuth2Service.isValidTheState(st)) {
window.location.replace('index.html');
} else {
window.location.replace('index.html');
}
} catch (_) {
window.location.replace('index.html');
}
}
})();
const grantType = sessionStorage.getItem("flow_grant_type");
if (grantType !== "authorization_code" && grantType !== "authorization_code_pkce" && grantType !== "implicit") {
window.location.href = 'index.html';
}
if (grantType === "implicit") {
const hashParams = new URLSearchParams(window.location.hash.substring(1));
const tokenResponse = {
access_token: hashParams.get("access_token"),
state: hashParams.get("state"),
expires_in: hashParams.get("expires_in"),
tokenType: hashParams.get("token_type"),
scope: hashParams.get("scope").replace(/\+/g, " ")
}
if (OAuth2Service.isValidTheState(tokenResponse.state)){
sessionStorage.setItem("token_response", JSON.stringify(tokenResponse));
} else {
UIService.showNotification(`The received state isn't valid`, "error");
Utils.delay(2000).then(() => returnToIndex());
}
window.location.href = 'results.html';
}
const isPKCE = grantType === "authorization_code_pkce";
const clientId = sessionStorage.getItem("flow_client_id");
const clientSecret = !isPKCE ? sessionStorage.getItem("flow_client_secret") : undefined;
const codeVerifier = isPKCE ? sessionStorage.getItem("flow_code_verifier") : undefined;
const tokenUrl = sessionStorage.getItem("flow_token_url") || '';
const redirectUri = sessionStorage.getItem("flow_redirect_uri") || '';
const toggleVisibilityBetweenClientSecretAndCodeVerifier = (isPKCE) => {
if (isPKCE) {
$('#clientSecret').closest('div').addClass('hidden');
$('#codeVerifier').closest('div').removeClass('hidden');
} else {
$('#codeVerifier').closest('div').addClass('hidden');
$('#clientSecret').closest('div').removeClass('hidden');
}
};
const returnToIndex = () => {
window.location.href = 'index.html' ;
};
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
const state = urlParams.get('state');
const error = urlParams.get('error');
const errorDescription = urlParams.get('error_description');
// if (state && OAuth2Service.isValidTheState(state)) {
// if (state.endsWith("logoutcb")) {
// returnToIndex()
// }
// }
if (!code || !state) {
UIService.showNotification("No authorization code or state found in the URL", "error");
Utils.delay(2000).then(() => returnToIndex()); //todo; activar
}
if (error) {
const errorMsg = `Error: ${errorDescription || 'something occurred during authorization'}`;
UIService.showNotification(errorMsg, "error");
Utils.delay(2000).then(() => returnToIndex());
}
// if (code) {
const $authCodeInput = $('#authCodeInput');
// UIService.showNotification(`Authorization code received: ${code}`, "success");
if (state && OAuth2Service.isValidTheState(state)) {
// console.log(state);
// console.log(state)
$authCodeInput.val(code);
const cleanUrl = window.location.origin + window.location.pathname;
// window.history.replaceState({}, document.title, cleanUrl);
// UIService.showNotification(`The received state isn't valid`, "error");
$('#clientId').val(clientId);
isPKCE? $('#codeVerifier').val(codeVerifier) : $('#clientSecret').val(clientSecret);
$('#tokenUrl').val(tokenUrl);
$('#redirectUri').val(redirectUri);
// const clientId = sessionStorage.getItem("flow_client_id");
// const clientSecret = isPKCE? sessionStorage.getItem("flow_client_secret") : undefined;
// const codeVerifier = !isPKCE? sessionStorage.getItem("flow_code_verifier"): undefined;
// const tokenUrl = sessionStorage.getItem("flow_token_url") || '';
// const redirectUri = sessionStorage.getItem("flow_redirect_uri") || '';
// UIService.updateVisualization();
} else {
UIService.showNotification(`The received state isn't valid`, "error");
Utils.delay(2000).then(() => returnToIndex());
}
function toggleExchangeButton(isPKCE) {
const tokenUrl = $('#tokenUrl').val().trim();
const redirectUri = $('#redirectUri').val().trim();
const authCode = $authCodeInput.val().trim();
const clientSecret = $('#clientSecret').val().trim();
const $exchangeBtn = $('#exchangeCodeBtn');
if (tokenUrl && redirectUri && authCode && (isPKCE || clientSecret)) {
// console.log("enabling exchange button");
$exchangeBtn
.prop('disabled', false)
.removeClass('bg-gray-300 text-gray-500 cursor-not-allowed')
.addClass('bg-emerald-500 hover:bg-emerald-600 text-white cursor-pointer');
} else {
// console.log("Disabling exchange button");
$exchangeBtn
.prop('disabled', true)
.removeClass('bg-emerald-500 hover:bg-emerald-600 text-white cursor-pointer')
.addClass('bg-gray-300 text-gray-500 cursor-not-allowed');
}
}
$(document).ready(() => {
toggleExchangeButton(isPKCE);
toggleVisibilityBetweenClientSecretAndCodeVerifier(isPKCE);
});
$('#tokenUrl, #redirectUri, #authCodeInput #clientSecret').on('input keyup change paste', () => toggleExchangeButton(isPKCE));
$('#returnBtn').on('click', () => returnToIndex());
$('#exchangeCodeBtn').on('click', () => {
const authCode = $authCodeInput.val().trim();
UIService.showLoading('exchangeCodeBtn');
const payloadCommons = {
grant_type: 'authorization_code',
code: authCode,
redirect_uri: redirectUri,
};
const payload = isPKCE? {
...payloadCommons,
client_id: clientId,
code_verifier: codeVerifier
} : {
...payloadCommons
// client_secret: clientSecret
};
const headers = isPKCE ? {
'Content-Type': 'application/x-www-form-urlencoded'
} : {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa(`${clientId}:${clientSecret}`)
};
$.ajax({
url: tokenUrl,
method: 'POST',
headers: headers,
data: $.param(payload),
success: (res) => {
// console.log(res);
// this.displayTokenResponse(res.access_toke)
UIService.showNotification('Code Exchanged Successfully', 'success');
sessionStorage.setItem('token_response', JSON.stringify(res));
// Utils.delay(200).then(() => {
window.location.href = 'results.html';
// });
},
error: (err) => {
console.error(err);
const errorMsg = err.responseJSON?.error_description || err.statusText || 'unknown error';
UIService.showNotification(`Error exchanging code: ${errorMsg}`, 'error');
},
complete: () => {
UIService.hideLoading('exchangeCodeBtn', 'Exchange Code');
}
});
});
</script>
</body>
</html>