-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add verify option for nonce validation #540
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kazuki229 !! 👏
@MitMaro not sure if you have any other feedback.
expect(err).to.be.null; | ||
done(); | ||
}) | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you do the tests using the helpers that @MitMaro prepared?
It will internally assert sync and async behavior (like you did well here, just for consistency across tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kazuki229 Some direction that hopefully will make things a little easier.
Add const testUtils = require('./test-utils');
to get the test utilities.
To use the test helpers for verify
:
testUtils.verifyJWTHelper(token, undefined, {nonce: testCase.nonce}, (err, decoded) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('nonce', 'abcde');
});
});
To check for an error you would do something along the lines of:
testUtils.verifyJWTHelper(token, undefined, {nonce}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(jwt.JsonWebTokenError);
expect(err).to.have.property('message', 'nonce must be a non-empty string')
});
});
Ping me if you need any other assistance and I will gladly help!
@@ -179,6 +179,12 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) { | |||
} | |||
} | |||
|
|||
if (options.nonce) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you return error if options.nonce
is not a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The verify option really could use some validation of the options, but that is currently not the case for any option besides for clockTimestamp
.
Perhaps something like adding some validation in the same area as the clockTimestamp
validation?
Something like (note: untested):
if (options.nonce !== undefined && (typeof options.nonce !== 'string' || options.nonce.trim() === '')) {
return done(new JsonWebTokenError('nonce must be a non-empty string'));
}
Ideally, we would handle the validation in a similar matter to the validation in verify, but that would be adding a lot of additional weight to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the above validation is added, then we should test that the following values result in the error being returned:
[
true,
false,
null,
-1,
0,
1,
-1.1,
1.1,
-Infinity,
Infinity,
NaN,
'',
' ',
[],
['foo'],
{},
{foo: 'bar'},
]
Kudos to @kazuki229 for adding tests! I think there are a few additional tests that could be easily added. I will take a look this weekend and provide some direction to @kazuki229 . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kazuki229 I left some notes that should assist you. Ping me if you need any other assistance and I will gladly help!
@@ -179,6 +179,12 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) { | |||
} | |||
} | |||
|
|||
if (options.nonce) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The verify option really could use some validation of the options, but that is currently not the case for any option besides for clockTimestamp
.
Perhaps something like adding some validation in the same area as the clockTimestamp
validation?
Something like (note: untested):
if (options.nonce !== undefined && (typeof options.nonce !== 'string' || options.nonce.trim() === '')) {
return done(new JsonWebTokenError('nonce must be a non-empty string'));
}
Ideally, we would handle the validation in a similar matter to the validation in verify, but that would be adding a lot of additional weight to this PR.
@@ -179,6 +179,12 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) { | |||
} | |||
} | |||
|
|||
if (options.nonce) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the above validation is added, then we should test that the following values result in the error being returned:
[
true,
false,
null,
-1,
0,
1,
-1.1,
1.1,
-Infinity,
Infinity,
NaN,
'',
' ',
[],
['foo'],
{},
{foo: 'bar'},
]
test/option-nonce.test.js
Outdated
}); | ||
[ | ||
{ | ||
description: 'should work with string', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor item: 'should work with a string'
instead?
test/option-nonce.test.js
Outdated
{}, | ||
{ foo: 'bar' }, | ||
].forEach((nonce) => { | ||
let tokenhoge = jwt.sign({ foo: 'bar' }, undefined, { algorithm: 'none' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to remove this line and reuse the token
generated in the beforeEach
above.
expect(err).to.be.null; | ||
done(); | ||
}) | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kazuki229 Some direction that hopefully will make things a little easier.
Add const testUtils = require('./test-utils');
to get the test utilities.
To use the test helpers for verify
:
testUtils.verifyJWTHelper(token, undefined, {nonce: testCase.nonce}, (err, decoded) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('nonce', 'abcde');
});
});
To check for an error you would do something along the lines of:
testUtils.verifyJWTHelper(token, undefined, {nonce}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(jwt.JsonWebTokenError);
expect(err).to.have.property('message', 'nonce must be a non-empty string')
});
});
Ping me if you need any other assistance and I will gladly help!
Co-Authored-By: kazuki229 <tsuzuku.k@gmail.com>
@ziluvatar @MitMaro |
Thanks @kazuki229 !! Good job 👏 I'll create a minor version with this and some other patch changes that are on master. |
Released on v8.4.0 🎉 |
@ziluvatar |
I want to verify nonce value in jwt.
Then I found the following pull request.
#344
But because tests and README have not been added, it has not yet been merged.
So I did add tests and README.