Skip to content

Commit b09b097

Browse files
committed
Grunt watch and general bugfixes.
1 parent 4ddd37a commit b09b097

File tree

7 files changed

+74
-22
lines changed

7 files changed

+74
-22
lines changed

Gruntfile.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = grunt => {
55
grunt.loadNpmTasks('grunt-contrib-stylus');
66
grunt.loadNpmTasks('grunt-contrib-pug');
77
grunt.loadNpmTasks('grunt-webpack');
8+
grunt.loadNpmTasks('grunt-contrib-watch');
89

910
grunt.initConfig({
1011
copy: {
@@ -44,9 +45,32 @@ module.exports = grunt => {
4445
webpack: {
4546
prod: websiteWebpackConfig,
4647
dev: websiteWebpackConfig
48+
},
49+
watch: {
50+
js: {
51+
files: 'src/**',
52+
tasks: 'webpack'
53+
},
54+
assets: {
55+
files: [
56+
'img/**',
57+
'fonts/**',
58+
'assets/**',
59+
'opensearch.xml',
60+
'node_modules/bootstrap/dist/css/bootstrap.min.css',
61+
'node_modules/codemirror/lib/codemirror.css',
62+
'node_modules/codemirror/addon/lint/lint.css',
63+
'css/budicon.css'
64+
],
65+
tasks: 'copy'
66+
},
67+
views: {
68+
files: ['stylus/**', 'views/**'],
69+
tasks: ['stylus', 'pug']
70+
}
4771
}
4872
});
4973

5074
grunt.registerTask('build', ['copy', 'stylus', 'pug', 'webpack']);
51-
grunt.registerTask('default', ['build']);
75+
grunt.registerTask('default', ['build', 'watch']);
5276
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"grunt-contrib-copy": "^1.0.0",
2020
"grunt-contrib-pug": "^1.0.0",
2121
"grunt-contrib-stylus": "^1.2.0",
22+
"grunt-contrib-watch": "^1.0.0",
2223
"grunt-webpack": "^3.0.2",
2324
"highlight.js": "^9.12.0",
2425
"isotope-layout": "^3.0.5",

src/jwt.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,39 @@ export function verify(jwt, secretOrPublicKeyString, base64Secret = false) {
3131
return false;
3232
}
3333

34-
if(decoded.header.alg.indexOf('HS') === 0) {
35-
return jws.JWS.verify(jwt,
36-
base64Secret ?
37-
b64utohex(secretOrPublicKeyString) :
38-
utf8tohex(secretOrPublicKeyString));
39-
} else {
40-
return jws.JWS.verify(jwt, secretOrPublicKeyString);
34+
try {
35+
if(decoded.header.alg.indexOf('HS') === 0) {
36+
return jws.JWS.verify(jwt,
37+
base64Secret ?
38+
b64utohex(secretOrPublicKeyString) :
39+
utf8tohex(secretOrPublicKeyString));
40+
} else {
41+
return jws.JWS.verify(jwt, secretOrPublicKeyString);
42+
}
43+
} catch(e) {
44+
console.error('Could not verify token, ' +
45+
'probably due to bad data in it or the keys: ', e);
46+
return false;
4147
}
4248
}
4349

4450
export function decode(jwt) {
4551
const split = jwt.split('.');
4652

47-
const result = {};
53+
const result = {
54+
errors: false
55+
};
4856
try {
4957
result.header = JSON.parse(b64utoutf8(split[0]));
5058
} catch(e) {
51-
result.header = '';
59+
result.header = {};
60+
result.errors = true;
5261
}
5362
try {
5463
result.payload = JSON.parse(b64utoutf8(split[1]));
5564
} catch(e) {
56-
result.payload = '';
65+
result.payload = {};
66+
result.errors = true;
5767
}
5868

5969
return result;

src/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@ export function isValidKey(key) {
100100
key: key
101101
};
102102
}
103+
104+
export function deferToNextLoop(func) {
105+
setTimeout(func, 1);
106+
}

src/website/editor/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { copyTextToClipboard } from '../utils.js';
2+
import { deferToNextLoop } from '../../utils.js';
23
import { downloadPublicKeyIfPossible } from '../../public-key-download.js';
34
import { tooltipHandler } from './tooltip.js';
45
import { tokenEditor, headerEditor, payloadEditor } from './instances.js';
@@ -73,7 +74,7 @@ function displaySecretOrKeys(algorithm) {
7374
keyEditorContainer.style.display = '';
7475
}
7576

76-
fixEditorHeight();
77+
deferToNextLoop(fixEditorHeight);
7778
}
7879

7980
function selectAlgorithm(algorithm) {
@@ -175,6 +176,8 @@ function markAsInvalidWithElement(element, clearTokenEditor = true) {
175176
}
176177

177178
function encodeToken() {
179+
deferToNextLoop(fixEditorHeight);
180+
178181
eventManager.withDisabledEvents(() => {
179182
let header;
180183
try {
@@ -218,23 +221,31 @@ function encodeToken() {
218221
}
219222

220223
function decodeToken() {
224+
deferToNextLoop(fixEditorHeight);
225+
221226
eventManager.withDisabledEvents(() => {
222227
try {
223228
const jwt = getTrimmedValue(tokenEditor);
224229
const decoded = decode(jwt);
225230

226231
selectAlgorithm(decoded.header.alg);
227-
downloadPublicKeyIfPossible(decoded).then(publicKey => {
228-
eventManager.withDisabledEvents(() => {
229-
publicKeyTextArea.value = publicKey;
230-
verifyToken();
232+
if(decoded.header.alg && decoded.header.alg.indexOf('HS') === -1) {
233+
downloadPublicKeyIfPossible(decoded).then(publicKey => {
234+
eventManager.withDisabledEvents(() => {
235+
publicKeyTextArea.value = publicKey;
236+
verifyToken();
237+
});
231238
});
232-
});
239+
}
233240

234241
headerEditor.setValue(stringify(decoded.header));
235242
payloadEditor.setValue(stringify(decoded.payload));
236243

237-
verifyToken();
244+
if(decoded.errors) {
245+
markAsInvalidWithElement(editorElement, false);
246+
} else {
247+
verifyToken();
248+
}
238249
} catch(e) {
239250
console.error('Failed to decode token: ', e);
240251
}

src/website/editor/instances.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const jsonEditorOptions = {
1616
instance.replaceSelection(' ', 'end');
1717
}
1818
},
19-
lint: true
19+
lint: true,
20+
scrollbarStyle: 'null'
2021
};
2122

2223
const tokenEditorOptions = Object.assign({}, jsonEditorOptions, {

stylus/app.styl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ body
518518
+breakpoint("tablet")
519519
grid(2, 30px, '.box-content')
520520

521+
.error .CodeMirror
522+
background-color #ffc0cb
523+
521524
.box-content
522525
height 0
523526
overflow hidden
@@ -576,9 +579,7 @@ body
576579
textarea
577580
width 100%
578581
max-width 266px
579-
resize none
580-
&.error
581-
background-color #ffc0cb
582+
resize none
582583

583584
.validation-status
584585
text-transform capitalize

0 commit comments

Comments
 (0)