Skip to content

Commit 8737789

Browse files
javespiziluvatar
authored andcommitted
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
1 parent 7b60c12 commit 8737789

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ As mentioned in [this comment](https://github.com/auth0/node-jsonwebtoken/issues
138138
* `algorithms`: List of strings with the names of the allowed algorithms. For instance, `["HS256", "HS384"]`.
139139
* `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.
140140
> Eg: `"urn:foo"`, `/urn:f[o]{2}/`, `[/urn:f[o]{2}/, "urn:bar"]`
141+
* `complete`: return an object with the decoded `{ payload, header, signature }` instead of only the usual content of the payload.
141142
* `issuer` (optional): string or array of strings of valid values for the `iss` field.
142143
* `ignoreExpiration`: if `true` do not validate the expiration of the token.
143144
* `ignoreNotBefore`...

test/option-complete.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const jws = require('jws');
4+
const expect = require('chai').expect;
5+
const path = require('path');
6+
const fs = require('fs');
7+
const testUtils = require('./test-utils')
8+
9+
describe('complete option', function () {
10+
const secret = fs.readFileSync(path.join(__dirname, 'priv.pem'));
11+
const pub = fs.readFileSync(path.join(__dirname, 'pub.pem'));
12+
13+
const header = { alg: 'RS256' };
14+
const payload = { iat: Math.floor(Date.now() / 1000 ) };
15+
const signed = jws.sign({ header, payload, secret, encoding: 'utf8' });
16+
const signature = jws.decode(signed).signature;
17+
18+
[
19+
{
20+
description: 'should return header, payload and signature',
21+
complete: true,
22+
},
23+
].forEach((testCase) => {
24+
it(testCase.description, function (done) {
25+
testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => {
26+
testUtils.asyncCheck(done, () => {
27+
expect(err).to.be.null;
28+
expect(decoded.header).to.have.property('alg', header.alg);
29+
expect(decoded.payload).to.have.property('iat', payload.iat);
30+
expect(decoded).to.have.property('signature', signature);
31+
});
32+
});
33+
});
34+
});
35+
[
36+
{
37+
description: 'should return payload',
38+
complete: false,
39+
},
40+
].forEach((testCase) => {
41+
it(testCase.description, function (done) {
42+
testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => {
43+
testUtils.asyncCheck(done, () => {
44+
expect(err).to.be.null;
45+
expect(decoded.header).to.be.undefined;
46+
expect(decoded.payload).to.be.undefined;
47+
expect(decoded.signature).to.be.undefined;
48+
expect(decoded).to.have.property('iat', payload.iat);
49+
});
50+
});
51+
});
52+
});
53+
});

verify.js

+10
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
203203
}
204204
}
205205

206+
if (options.complete === true) {
207+
var signature = decodedToken.signature;
208+
209+
return done(null, {
210+
header: header,
211+
payload: payload,
212+
signature: signature
213+
});
214+
}
215+
206216
return done(null, payload);
207217
});
208218
};

0 commit comments

Comments
 (0)