Skip to content

Commit

Permalink
Add a new mutatePayload option (#446)
Browse files Browse the repository at this point in the history
This option allows you to keep a reference to the raw token payload after claims have been applied to it but before it has been encoded.
  • Loading branch information
jondubois authored and ziluvatar committed Mar 2, 2018
1 parent 7b0a010 commit d6d7c5e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -42,6 +42,7 @@ encoded private key for RSA and ECDSA. In case of a private key with passphrase
* `noTimestamp`
* `header`
* `keyid`
* `mutatePayload`: if true, the sign function will modify the payload object directly. This is useful if you need a raw reference to the payload after claims have been applied to it but before it has been encoded into a token.

If `payload` is not a buffer or a string, it will be coerced into a string using `JSON.stringify`.

Expand Down
7 changes: 5 additions & 2 deletions sign.js
Expand Up @@ -20,7 +20,8 @@ var sign_options_schema = {
subject: { isValid: isString, message: '"subject" must be a string' },
jwtid: { isValid: isString, message: '"jwtid" must be a string' },
noTimestamp: { isValid: isBoolean, message: '"noTimestamp" must be a boolean' },
keyid: { isValid: isString, message: '"keyid" must be a string' }
keyid: { isValid: isString, message: '"keyid" must be a string' },
mutatePayload: { isValid: isBoolean, message: '"mutatePayload" must be a boolean' }
};

var registered_claims_schema = {
Expand Down Expand Up @@ -110,7 +111,9 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
catch (error) {
return failure(error);
}
payload = xtend(payload);
if (!options.mutatePayload) {
payload = xtend(payload);
}
} else {
var invalid_options = options_for_objects.filter(function (opt) {
return typeof options[opt] !== 'undefined';
Expand Down
24 changes: 24 additions & 0 deletions test/async_sign.tests.js
Expand Up @@ -82,6 +82,30 @@ describe('signing a token asynchronously', function() {
});
});

describe('when mutatePayload is not set', function() {
it('should not apply claims to the original payload object (mutatePayload defaults to false)', function(done) {
var originalPayload = { foo: 'bar' };
jwt.sign(originalPayload, 'secret', { notBefore: 60, expiresIn: 600 }, function (err) {
if (err) { return done(err); }
expect(originalPayload).to.not.have.property('nbf');
expect(originalPayload).to.not.have.property('exp');
done();
});
});
});

describe('when mutatePayload is set to true', function() {
it('should apply claims directly to the original payload object', function(done) {
var originalPayload = { foo: 'bar' };
jwt.sign(originalPayload, 'secret', { notBefore: 60, expiresIn: 600, mutatePayload: true }, function (err) {
if (err) { return done(err); }
expect(originalPayload).to.have.property('nbf').that.is.a('number');
expect(originalPayload).to.have.property('exp').that.is.a('number');
done();
});
});
});

describe('secret must have a value', function(){
[undefined, '', 0].forEach(function(secret){
it('should return an error if the secret is falsy and algorithm is not set to none: ' + (typeof secret === 'string' ? '(empty string)' : secret), function(done) {
Expand Down

0 comments on commit d6d7c5e

Please sign in to comment.