Skip to content
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

Merged
merged 4 commits into from
Nov 14, 2018
Merged

Conversation

kazuki229
Copy link
Contributor

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.

Copy link
Contributor

@ziluvatar ziluvatar left a 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();
})
});
Copy link
Contributor

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)

Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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'},
]

README.md Outdated Show resolved Hide resolved
@MitMaro
Copy link
Contributor

MitMaro commented Nov 2, 2018

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 .

Copy link
Contributor

@MitMaro MitMaro left a 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) {
Copy link
Contributor

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) {
Copy link
Contributor

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'},
]

});
[
{
description: 'should work with string',
Copy link
Contributor

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?

{},
{ foo: 'bar' },
].forEach((nonce) => {
let tokenhoge = jwt.sign({ foo: 'bar' }, undefined, { algorithm: 'none' });
Copy link
Contributor

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();
})
});
Copy link
Contributor

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!

ziluvatar and others added 3 commits November 10, 2018 23:12
@kazuki229
Copy link
Contributor Author

@ziluvatar @MitMaro
I'm sorry too late.
I fixed the points pointed out, So please review again.:bow:

@ziluvatar ziluvatar merged commit e7938f0 into auth0:master Nov 14, 2018
@ziluvatar
Copy link
Contributor

Thanks @kazuki229 !! Good job 👏 I'll create a minor version with this and some other patch changes that are on master.

@ziluvatar
Copy link
Contributor

Released on v8.4.0 🎉

@kazuki229
Copy link
Contributor Author

@ziluvatar
Thank you for your quick response! 🎉🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants