|
1 |
| -@page "~/jwt-decoder" |
| 1 | +@page "~/jwt-decoder" |
2 | 2 | @model IdentityServerHost.Pages.Home.JwtDecoder
|
3 | 3 |
|
4 | 4 | <div class="jwt-decoder-page">
|
|
332 | 332 | });
|
333 | 333 |
|
334 | 334 | const jwtInput = $('#jwt-input');
|
335 |
| - jwtInput.on('input', async function() { |
336 |
| - decodedJwt = { |
337 |
| - header: null, |
338 |
| - payload: null, |
339 |
| - signature: null |
340 |
| - }; |
341 |
| - |
342 |
| - const jwt = $(this).text(); |
343 |
| - if (jwt) { |
344 |
| - try { |
345 |
| - const parts = jwt.split('.'); |
346 |
| - if (parts.length === 3) { |
347 |
| - const header = JSON.parse(atob(parts[0])); |
348 |
| - const payload = JSON.parse(atob(parts[1])); |
349 |
| - const signature = parts[2]; |
350 |
| -
|
351 |
| - decodedJwt = { |
352 |
| - header: header, |
353 |
| - payload: payload, |
354 |
| - signature: signature |
355 |
| - }; |
356 |
| - await showDecodedJwt(parts, header, payload, signature); |
357 |
| - colorJwtInput($(this), parts); |
358 |
| - } else { |
359 |
| - showError('Invalid JWT format. A JWT should have three parts separated by dots.'); |
360 |
| - } |
361 |
| - } catch (e) { |
362 |
| - showError('Error decoding JWT: ' + e.message); |
363 |
| - } |
364 |
| - } else { |
365 |
| - await showDecodedJwt(null, null, null, ' '); |
366 |
| - } |
| 335 | + let debounceTimeout; |
| 336 | + jwtInput.on('input', function() { |
| 337 | + clearTimeout(debounceTimeout); |
| 338 | + debounceTimeout = setTimeout(async () => await parseJwt.call(this), 300); // 300ms debounce |
367 | 339 | });
|
368 | 340 |
|
369 | 341 | @if (Model.View?.Token != null)
|
|
376 | 348 | }
|
377 | 349 | }
|
378 | 350 |
|
| 351 | + async function parseJwt() { |
| 352 | + decodedJwt = { |
| 353 | + header: null, |
| 354 | + payload: null, |
| 355 | + signature: null |
| 356 | + }; |
| 357 | +
|
| 358 | + await showDecodedJwt(null, null, null, ' '); |
| 359 | + const jwt = $(this).text(); |
| 360 | + if (jwt) { |
| 361 | + try { |
| 362 | + const parts = jwt.split('.'); |
| 363 | + if (parts.length === 3) { |
| 364 | + const header = JSON.parse(decodeBase64UrlSafe(parts[0])); |
| 365 | + const payload = JSON.parse(decodeBase64UrlSafe(parts[1])); |
| 366 | + const signature = parts[2]; |
| 367 | +
|
| 368 | + decodedJwt = { |
| 369 | + header: header, |
| 370 | + payload: payload, |
| 371 | + signature: signature |
| 372 | + }; |
| 373 | + await showDecodedJwt(parts, header, payload, signature); |
| 374 | + colorJwtInput($(this), parts); |
| 375 | + } else { |
| 376 | + showError('Invalid JWT format. A JWT should have three parts separated by dots.'); |
| 377 | + } |
| 378 | + } catch (e) { |
| 379 | + showError('Error decoding JWT: ' + e.message); |
| 380 | + } |
| 381 | + } else { |
| 382 | + await showDecodedJwt(null, null, null, ' '); |
| 383 | + } |
| 384 | + } |
| 385 | +
|
379 | 386 | function colorJwtInput(target, parts) {
|
380 | 387 | let html = '';
|
381 | 388 | if (parts.length > 0) {
|
|
0 commit comments