Skip to content

Commit

Permalink
Add complete option in jwt.verify (#522)
Browse files Browse the repository at this point in the history
* Add complete option in verify

* Remove comment

* Update README.md

Co-Authored-By: javespi <javespalf@gmail.com>

* Move tests in a specific file
  • Loading branch information
javespi authored and ziluvatar committed Feb 20, 2019
1 parent 7b60c12 commit 8737789
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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`...
Expand Down
53 changes: 53 additions & 0 deletions 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);
});
});
});
});
});
10 changes: 10 additions & 0 deletions verify.js
Expand Up @@ -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);
});
};

0 comments on commit 8737789

Please sign in to comment.