Skip to content

Commit e1b6fb3

Browse files
don't throw an error when decode a bad token or header
1 parent c94c63b commit e1b6fb3

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
node_js:
33
- 4
44
- 5
5+
- 6
56
branches:
67
only:
78
- master

index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const crypto = require('crypto')
44
const b64url = require('base64-url')
55
const inherits = require('util').inherits
6+
const parse = require('json-parse-safe')
67

78
//
89
// supported algorithms
@@ -70,8 +71,8 @@ function decode (key, token, cb) {
7071
}
7172

7273
// base64 decode and parse JSON
73-
var header = JSON.parse(b64url.decode(parts[0]))
74-
var payload = JSON.parse(b64url.decode(parts[1]))
74+
var header = JSONParse(b64url.decode(parts[0]))
75+
var payload = JSONParse(b64url.decode(parts[1]))
7576

7677
// get algorithm hash and type and check if is valid
7778
var algorithm = algorithms[header.alg]
@@ -150,3 +151,9 @@ function paramsAreFalsy (param1, param2) {
150151
return !param1 || !param2
151152
}
152153

154+
function JSONParse (str) {
155+
var res = parse(str)
156+
157+
return res.error && '' || res.value
158+
}
159+

package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-web-token",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "JSON Web Token (JWT) is a compact token format intended for space constrained environments such as HTTP Authorization headers and URI query parameters.",
55
"main": "index.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
"style": "jscs -p google index.js test/test.js",
1010
"coverage": "istanbul cover tape test/test.js && istanbul check-coverage",
1111
"coverage:open": "open reports/coverage/index.html",
12-
"complexity": "plato -r -t 'jenkins-client code report' -l .jshintrc -x 'node_modules|reports|test' -d reports/plato .",
12+
"complexity": "plato -r -t 'jenkins-client code report' -l .jshintrc -x 'node_modules|reports|test|bench' -d reports/plato .",
1313
"complexity:open": "open reports/plato/index.html",
1414
"security": "nsp check",
1515
"bench": "echo 'installing dependencies first ...' && sleep 1 && npm i --save-dev benchmark microtime && echo '' && node bench && npm uninstall --save-dev benchmark microtime"
@@ -38,15 +38,16 @@
3838
},
3939
"homepage": "https://github.com/joaquimserafim/json-web-token",
4040
"dependencies": {
41-
"base64-url": "^1.2.2"
41+
"base64-url": "^1.2.2",
42+
"json-parse-safe": "^1.0.3"
4243
},
4344
"devDependencies": {
4445
"istanbul": "^0.4.3",
4546
"jscs": "^2.11.0",
46-
"jshint": "^2.9.1",
47-
"nsp": "^2.3.0",
47+
"jshint": "^2.9.2",
48+
"nsp": "^2.4.0",
4849
"plato": "^1.5.0",
49-
"pre-commit": "^1.1.2",
50+
"pre-commit": "^1.1.3",
5051
"tap-spec": "^4.1.1",
5152
"tape": "^4.5.1"
5253
},

test/test.js

+10
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ test('jwt - decode with callback / bad algorithm', function(assert) {
9292
})
9393
})
9494

95+
test('jwt - decode with callback / bad token', function(assert) {
96+
var badToken = theToken.split('.')
97+
badToken[1] = 'bad token hash'
98+
jwt.decode(secret, badToken.join('.'), function(err) {
99+
assert.equal(err.name, 'JWTError')
100+
assert.equal(err.message, 'Invalid key!')
101+
assert.end()
102+
})
103+
})
104+
95105
test('jwt - decode with callback / invalid key', function(assert) {
96106
jwt.decode('wow', theToken, function(err) {
97107
assert.equal(err.name, 'JWTError')

0 commit comments

Comments
 (0)