Skip to content

Add complete option in jwt.verify #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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);
});
};