Add complete option in jwt.verify (#522)

* Add complete option in verify

* Remove comment

* Update README.md

Co-Authored-By: javespi <[email protected]>

* Move tests in a specific file
This commit is contained in:
Javier Espinosa
2019-02-20 13:32:23 +01:00
committed by Eduardo Diaz
parent f1f30a38a3
commit 1e880c8c19
3 changed files with 64 additions and 0 deletions
+1
View File
@@ -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`...
+53
View File
@@ -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
View File
@@ -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);
});
};