From 8737789dd330cf9e7870f4df97fd52479adbac22 Mon Sep 17 00:00:00 2001 From: Javier Espinosa Date: Wed, 20 Feb 2019 13:32:23 +0100 Subject: [PATCH] Add complete option in jwt.verify (#522) * Add complete option in verify * Remove comment * Update README.md Co-Authored-By: javespi * Move tests in a specific file --- README.md | 1 + test/option-complete.test.js | 53 ++++++++++++++++++++++++++++++++++++ verify.js | 10 +++++++ 3 files changed, 64 insertions(+) create mode 100644 test/option-complete.test.js diff --git a/README.md b/README.md index 8013221..442aa55 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ As mentioned in [this comment](https://github.com/auth0/node-jsonwebtoken/issues * `algorithms`: List of strings with the names of the allowed algorithms. For instance, `["HS256", "HS384"]`. * `audience`: if you want to check audience (`aud`), provide a value here. The audience can be checked against a string, a regular expression or a list of strings and/or regular expressions. > Eg: `"urn:foo"`, `/urn:f[o]{2}/`, `[/urn:f[o]{2}/, "urn:bar"]` +* `complete`: return an object with the decoded `{ payload, header, signature }` instead of only the usual content of the payload. * `issuer` (optional): string or array of strings of valid values for the `iss` field. * `ignoreExpiration`: if `true` do not validate the expiration of the token. * `ignoreNotBefore`... diff --git a/test/option-complete.test.js b/test/option-complete.test.js new file mode 100644 index 0000000..29320e8 --- /dev/null +++ b/test/option-complete.test.js @@ -0,0 +1,53 @@ +'use strict'; + +const jws = require('jws'); +const expect = require('chai').expect; +const path = require('path'); +const fs = require('fs'); +const testUtils = require('./test-utils') + +describe('complete option', function () { + const secret = fs.readFileSync(path.join(__dirname, 'priv.pem')); + const pub = fs.readFileSync(path.join(__dirname, 'pub.pem')); + + const header = { alg: 'RS256' }; + const payload = { iat: Math.floor(Date.now() / 1000 ) }; + const signed = jws.sign({ header, payload, secret, encoding: 'utf8' }); + const signature = jws.decode(signed).signature; + + [ + { + description: 'should return header, payload and signature', + complete: true, + }, + ].forEach((testCase) => { + it(testCase.description, function (done) { + testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => { + testUtils.asyncCheck(done, () => { + expect(err).to.be.null; + expect(decoded.header).to.have.property('alg', header.alg); + expect(decoded.payload).to.have.property('iat', payload.iat); + expect(decoded).to.have.property('signature', signature); + }); + }); + }); + }); + [ + { + description: 'should return payload', + complete: false, + }, + ].forEach((testCase) => { + it(testCase.description, function (done) { + testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => { + testUtils.asyncCheck(done, () => { + expect(err).to.be.null; + expect(decoded.header).to.be.undefined; + expect(decoded.payload).to.be.undefined; + expect(decoded.signature).to.be.undefined; + expect(decoded).to.have.property('iat', payload.iat); + }); + }); + }); + }); +}); diff --git a/verify.js b/verify.js index fa1339a..6b459c7 100644 --- a/verify.js +++ b/verify.js @@ -203,6 +203,16 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) { } } + if (options.complete === true) { + var signature = decodedToken.signature; + + return done(null, { + header: header, + payload: payload, + signature: signature + }); + } + return done(null, payload); }); };