Skip to content

Commit e7938f0

Browse files
kazuki229ziluvatar
authored andcommittedNov 14, 2018
Add verify option for nonce validation (#540)
* Add verify option for nonce validation * Update README.md Co-Authored-By: kazuki229 <tsuzuku.k@gmail.com> * Refactor option-nonce test * Add nonce option validation
1 parent 0268813 commit e7938f0

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ As mentioned in [this comment](https://github.com/auth0/node-jsonwebtoken/issues
146146
* `maxAge`: the maximum allowed age for tokens to still be valid. It is expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms).
147147
> Eg: `1000`, `"2 days"`, `"10h"`, `"7d"`. A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default (`"120"` is equal to `"120ms"`).
148148
* `clockTimestamp`: the time in seconds that should be used as the current time for all necessary comparisons.
149+
* `nonce`: if you want to check `nonce` claim, provide a string value here. It is used on Open ID for the ID Tokens. ([Open ID implementation notes](https://openid.net/specs/openid-connect-core-1_0.html#NonceNotes))
149150

150151

151152
```js

‎test/option-nonce.test.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const jwt = require('../');
4+
const expect = require('chai').expect;
5+
const util = require('util');
6+
const testUtils = require('./test-utils')
7+
8+
describe('nonce option', function () {
9+
let token;
10+
11+
beforeEach(function () {
12+
token = jwt.sign({ nonce: 'abcde' }, undefined, { algorithm: 'none' });
13+
});
14+
[
15+
{
16+
description: 'should work with a string',
17+
nonce: 'abcde',
18+
},
19+
].forEach((testCase) => {
20+
it(testCase.description, function (done) {
21+
testUtils.verifyJWTHelper(token, undefined, { nonce: testCase.nonce }, (err, decoded) => {
22+
testUtils.asyncCheck(done, () => {
23+
expect(err).to.be.null;
24+
expect(decoded).to.have.property('nonce', 'abcde');
25+
});
26+
});
27+
});
28+
});
29+
[
30+
true,
31+
false,
32+
null,
33+
-1,
34+
0,
35+
1,
36+
-1.1,
37+
1.1,
38+
-Infinity,
39+
Infinity,
40+
NaN,
41+
'',
42+
' ',
43+
[],
44+
['foo'],
45+
{},
46+
{ foo: 'bar' },
47+
].forEach((nonce) => {
48+
it(`should error with value ${util.inspect(nonce)}`, function (done) {
49+
testUtils.verifyJWTHelper(token, undefined, { nonce }, (err) => {
50+
testUtils.asyncCheck(done, () => {
51+
expect(err).to.be.instanceOf(jwt.JsonWebTokenError);
52+
expect(err).to.have.property('message', 'nonce must be a non-empty string')
53+
});
54+
});
55+
});
56+
});
57+
});

‎verify.js

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
3333
return done(new JsonWebTokenError('clockTimestamp must be a number'));
3434
}
3535

36+
if (options.nonce !== undefined && (typeof options.nonce !== 'string' || options.nonce.trim() === '')) {
37+
return done(new JsonWebTokenError('nonce must be a non-empty string'));
38+
}
39+
3640
var clockTimestamp = options.clockTimestamp || Math.floor(Date.now() / 1000);
3741

3842
if (!jwtString){
@@ -179,6 +183,12 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
179183
}
180184
}
181185

186+
if (options.nonce) {
187+
if (payload.nonce !== options.nonce) {
188+
return done(new JsonWebTokenError('jwt nonce invalid. expected: ' + options.nonce));
189+
}
190+
}
191+
182192
if (options.maxAge) {
183193
if (typeof payload.iat !== 'number') {
184194
return done(new JsonWebTokenError('iat required when maxAge is specified'));

0 commit comments

Comments
 (0)
Please sign in to comment.