Skip to content

Commit

Permalink
don't throw an error when decode a bad token or header
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimserafim committed Jun 14, 2016
1 parent c94c63b commit e1b6fb3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_js:
- 4
- 5
- 6
branches:
only:
- master
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const crypto = require('crypto')
const b64url = require('base64-url')
const inherits = require('util').inherits
const parse = require('json-parse-safe')

//
// supported algorithms
Expand Down Expand Up @@ -70,8 +71,8 @@ function decode (key, token, cb) {
}

// base64 decode and parse JSON
var header = JSON.parse(b64url.decode(parts[0]))
var payload = JSON.parse(b64url.decode(parts[1]))
var header = JSONParse(b64url.decode(parts[0]))
var payload = JSONParse(b64url.decode(parts[1]))

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

function JSONParse (str) {
var res = parse(str)

return res.error && '' || res.value
}

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-web-token",
"version": "2.0.2",
"version": "2.0.3",
"description": "JSON Web Token (JWT) is a compact token format intended for space constrained environments such as HTTP Authorization headers and URI query parameters.",
"main": "index.js",
"scripts": {
Expand All @@ -9,7 +9,7 @@
"style": "jscs -p google index.js test/test.js",
"coverage": "istanbul cover tape test/test.js && istanbul check-coverage",
"coverage:open": "open reports/coverage/index.html",
"complexity": "plato -r -t 'jenkins-client code report' -l .jshintrc -x 'node_modules|reports|test' -d reports/plato .",
"complexity": "plato -r -t 'jenkins-client code report' -l .jshintrc -x 'node_modules|reports|test|bench' -d reports/plato .",
"complexity:open": "open reports/plato/index.html",
"security": "nsp check",
"bench": "echo 'installing dependencies first ...' && sleep 1 && npm i --save-dev benchmark microtime && echo '' && node bench && npm uninstall --save-dev benchmark microtime"
Expand Down Expand Up @@ -38,15 +38,16 @@
},
"homepage": "https://github.com/joaquimserafim/json-web-token",
"dependencies": {
"base64-url": "^1.2.2"
"base64-url": "^1.2.2",
"json-parse-safe": "^1.0.3"
},
"devDependencies": {
"istanbul": "^0.4.3",
"jscs": "^2.11.0",
"jshint": "^2.9.1",
"nsp": "^2.3.0",
"jshint": "^2.9.2",
"nsp": "^2.4.0",
"plato": "^1.5.0",
"pre-commit": "^1.1.2",
"pre-commit": "^1.1.3",
"tap-spec": "^4.1.1",
"tape": "^4.5.1"
},
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ test('jwt - decode with callback / bad algorithm', function(assert) {
})
})

test('jwt - decode with callback / bad token', function(assert) {
var badToken = theToken.split('.')
badToken[1] = 'bad token hash'
jwt.decode(secret, badToken.join('.'), function(err) {
assert.equal(err.name, 'JWTError')
assert.equal(err.message, 'Invalid key!')
assert.end()
})
})

test('jwt - decode with callback / invalid key', function(assert) {
jwt.decode('wow', theToken, function(err) {
assert.equal(err.name, 'JWTError')
Expand Down

0 comments on commit e1b6fb3

Please sign in to comment.