Skip to content

Commit a9bb362

Browse files
Merge pull request jsonwebtoken#211 from FadyMak/master
Added support for access_token parsing from URL hash
2 parents 1c93a62 + 11e9591 commit a9bb362

File tree

4 files changed

+88
-40
lines changed

4 files changed

+88
-40
lines changed

js/app.js

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,11 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
330330

331331
var algorithmRadios = $('input[name="algorithm"]'),
332332
lastRestoredToken;
333+
var tokenRadios = $('input[name="token-type"]');
333334

334335
function setJSONEditorContent(jsonEditor, decodedJSON, selector) {
335336
jsonEditor.off('change', refreshTokenEditor);
336337

337-
338-
339338
if (decodedJSON.result !== null && decodedJSON.result !== undefined) {
340339
jsonEditor.setValue(decodedJSON.result);
341340
} else {
@@ -385,23 +384,18 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
385384
if (window.matchMedia('(min-width: 768px)').matches) {
386385
autoHeightInput();
387386
}
388-
389387
}
390388

391389
function selectDetectedAlgorithm(alg){
392390
var $algRadio = $('.algorithm input[value="'+alg+'"]');
393391
$algRadio.prop('checked', true);
394392

395393
fireEvent($algRadio.get(0));
396-
397-
398394
}
399395

400396
function saveToStorage(jwt) {
401397
// Save last valid jwt value for refresh
402398
safeLocalStorageSetItem("jwtValue", jwt);
403-
404-
405399
}
406400

407401
function loadFromStorage(cb) {
@@ -452,8 +446,6 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
452446
}
453447
tokenEditor.on('change', tokenEditorOnChangeListener);
454448
fireEvent(secretElement);
455-
456-
457449
}
458450

459451
function getFirstElementByClassName(selector) {
@@ -502,8 +494,6 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
502494
isBase64
503495
);
504496

505-
506-
507497
var error = result.error;
508498
result = result.result;
509499
if (!error && result) {
@@ -515,8 +505,6 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
515505
$(signatureElement).addClass('invalid-token');
516506
signatureElement.innerHTML = '<i class="icon-budicon-501"></i> invalid signature';
517507
}
518-
519-
520508
}
521509

522510
function getKey(algorithm, action) {
@@ -529,8 +517,6 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
529517
} else {
530518
return action === 'sign' ? privateKeyElement.val() : publicKeyElement.val();
531519
}
532-
533-
534520
}
535521

536522
function getAlgorithm() {
@@ -547,30 +533,43 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
547533
.filter('.' + algorithm)
548534
.show();
549535

550-
if(getTrimmedValue(tokenEditor) === DEFAULT_HS_TOKEN &&
536+
if(getTokenType() === 'id_token' && getTrimmedValue(tokenEditor) === DEFAULT_HS_TOKEN &&
551537
algorithm === 'RS256'){
552538
setDefaultsForRSA();
553-
}else if(getTrimmedValue(tokenEditor) === DEFAULT_RS_TOKEN &&
539+
}else if(getTokenType() === 'id_token' && getTrimmedValue(tokenEditor) === DEFAULT_RS_TOKEN &&
554540
algorithm === 'HS256'){
555541
setDefaultsForHMAC();
556542
}
557-
558-
559543
}
560544

561545
function setDefaultsForRSA() {
562546
tokenEditor.setValue(DEFAULT_RS_TOKEN);
563547

564548
$('.jwt-signature textarea[name=public-key]').val(DEFAULT_PUBLIC_RSA);
565549
$('.jwt-signature textarea[name=private-key]').val(DEFAULT_PRIVATE_RSA);
566-
567-
568550
}
569551

570552
function setDefaultsForHMAC(){
571553
tokenEditor.setValue(DEFAULT_HS_TOKEN);
554+
}
572555

556+
function updateToken() {
557+
var tokenType = getTokenType();
558+
if (document.location.hash) {
559+
var qs = document.location.hash.slice(1);
560+
var d = {};
561+
qs = qs.split('&');
562+
qs.forEach(function (kv) { kv = kv.split('='); d[kv[0]] = kv[1]; });
573563

564+
if (d[tokenType]) {
565+
tokenEditor.setValue(decodeURIComponent(d[tokenType]));
566+
return;
567+
}
568+
}
569+
}
570+
571+
function getTokenType() {
572+
return tokenRadios.filter(':checked').val();
574573
}
575574

576575
function validateKey() {
@@ -588,17 +587,19 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
588587
} else {
589588
$textarea.addClass('error');
590589
}
591-
592-
593590
}
594591

595592
updateAlgorithm();
596593

597594
algorithmRadios.on('change', function(){
598595
updateAlgorithm();
599596
updateSignature();
597+
});
600598

601-
599+
tokenRadios.on('change', function(){
600+
updateToken();
601+
updateAlgorithm();
602+
updateSignature();
602603
});
603604

604605
$('.jwt-signature textarea[name="public-key"]').on('input', updateSignature);
@@ -629,10 +630,21 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
629630
var d = {};
630631
qs = qs.split('&');
631632
qs.forEach(function (kv) { kv = kv.split('='); d[kv[0]] = kv[1]; });
633+
634+
if (d.access_token && d.id_token) {
635+
// show token-type selector
636+
$('.jwt-playground .selections .token-type').show();
637+
}
638+
632639
if (d.id_token) {
633640
tokenEditor.setValue(decodeURIComponent(d.id_token));
634641
return;
635642
}
643+
644+
if (d.access_token) {
645+
tokenEditor.setValue(decodeURIComponent(d.access_token));
646+
return;
647+
}
636648
}
637649

638650
loadFromStorage(function (jwt) {
@@ -669,6 +681,14 @@ $(".debugger-jwt .algorithm select").change(function() {
669681
$(".debugger-jwt .algorithm select").change(function(){var a=$('.debugger-jwt .algorithm input[value="'+$(this).val()+'"]');a.prop("checked",!0)})
670682
// end 07012015
671683

684+
$(".debugger-jwt .token-type select").change(function() {
685+
$('.debugger-jwt .token-type input[value="'+$(this).val()+'"]').parent().trigger("click");
686+
$('.debugger-jwt .token-type input[value="'+$(this).val()+'"]').change();
687+
});
688+
689+
$(".debugger-jwt .token-type select").change(function(){var a=$('.debugger-jwt .token-type input[value="'+$(this).val()+'"]');a.prop("checked",!0)})
690+
691+
672692
// Fetch stargazers count for each repo from GitHub's API
673693
$('.stars').each(function(idx, element){
674694
var $el = $(element);

server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
var express = require('express');
22
var http = require('http');
33
var enforce = require('express-sslify');
4-
4+
55
var app = express();
66

7-
// use HTTPS(true) in case you are behind a load balancer (e.g. Heroku)
7+
// use HTTPS(true) in case you are behind a load balancer (e.g. Heroku)
88
if (process.env.NODE_ENV === 'production') {
99
console.log('redirecting to ssl');
1010
app.use(enforce.HTTPS({ trustProtoHeader: true }))
1111
}
1212

1313
app.use('/', express.static(__dirname));
14-
14+
1515
http.createServer(app).listen(process.env.PORT || 3000, function() {
1616
console.log('started');
17-
});
17+
});

stylus/app.styl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ body
402402
position relative
403403
select
404404
background white
405-
width 100px
405+
width 112px
406406
height 38px
407407
font-size 12px
408408
padding 0 10px
@@ -425,6 +425,10 @@ body
425425
right 10px
426426

427427
.jwt-playground
428+
.selections
429+
display flex
430+
justify-content center
431+
428432
.algorithm
429433
text-align center
430434
span
@@ -435,6 +439,18 @@ body
435439
.jwt-select
436440
margin-left 10px
437441

442+
.token-type
443+
display none
444+
text-align center
445+
span
446+
font-weight bold
447+
text-transform uppercase
448+
font-size 12px
449+
vertical-align middle
450+
.jwt-select
451+
margin-left 10px
452+
margin-right 20px
453+
438454
.algorithm-code
439455
margin-top 35px
440456
.tab-nav

views/index.jade

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,30 @@ block content
6464
h1 Debugger
6565

6666
.jwt-playground
67+
.selections
68+
.token-type
69+
span Token
70+
.hide
71+
label
72+
input(type='radio', name='token-type', value='id_token', checked='')
73+
label
74+
input(type='radio', name='token-type', value='access_token')
75+
.jwt-select
76+
select
77+
option(name='token-type',value='id_token', selected='') id_token
78+
option(name='token-type',value='access_token') access_token
6779

68-
.algorithm
69-
span Algorithm
70-
.hide
71-
label
72-
input(type='radio', name='algorithm', value='HS256', checked='')
73-
label
74-
input(type='radio', name='algorithm', value='RS256')
75-
.jwt-select
76-
select
77-
option(name='algorithm',value='HS256', selected='') HS256
78-
option(name='algorithm',value='RS256') RS256
80+
.algorithm
81+
span Algorithm
82+
.hide
83+
label
84+
input(type='radio', name='algorithm', value='HS256', checked='')
85+
label
86+
input(type='radio', name='algorithm', value='RS256')
87+
.jwt-select
88+
select
89+
option(name='algorithm',value='HS256', selected='') HS256
90+
option(name='algorithm',value='RS256') RS256
7991

8092
.algorithm-code
8193
.tab-nav

0 commit comments

Comments
 (0)