Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: auth0/node-jsonwebtoken
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.2.0
Choose a base ref
...
head repository: auth0/node-jsonwebtoken
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.0.0
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on May 3, 2016

  1. Copy the full SHA
    65aadb4 View commit details

Commits on May 19, 2016

  1. Copy the full SHA
    1e46c5a View commit details
  2. 7.0.0

    jfromaniello committed May 19, 2016
    Copy the full SHA
    a79ac9a View commit details
Showing with 43 additions and 11 deletions.
  1. +9 −0 CHANGELOG.md
  2. +1 −1 package.json
  3. +16 −9 sign.js
  4. +17 −1 test/async_sign.tests.js
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,15 @@
All notable changes to this project will be documented in this file starting from version **v4.0.0**.
This project adheres to [Semantic Versioning](http://semver.org/).

## 6.2.0 - 2016-04-29

- add support for `options.clockTolerance` to `jwt.verify` ([65ddea934f226bf06bc9d6a55be9587515cfc38d](https://github.com/auth0/node-jsonwebtoken/commit/65ddea934f226bf06bc9d6a55be9587515cfc38d))

## 6.1.2 - 2016-04-29

- fix sign method for node.js 0.12. closes #193 ([9c38374142d3929be3c9314b5e9bc5d963c5955f](https://github.com/auth0/node-jsonwebtoken/commit/9c38374142d3929be3c9314b5e9bc5d963c5955f)), closes [#193](https://github.com/auth0/node-jsonwebtoken/issues/193)
- improve async test ([7b0981380ddc40a5f1208df520631785b5ffb85a](https://github.com/auth0/node-jsonwebtoken/commit/7b0981380ddc40a5f1208df520631785b5ffb85a))

## 6.1.0 - 2016-04-27

- verify unsigned tokens ([ec880791c10ed5ef7c8df7bf28ebb95c810479ed](https://github.com/auth0/node-jsonwebtoken/commit/ec880791c10ed5ef7c8df7bf28ebb95c810479ed))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonwebtoken",
"version": "6.2.0",
"version": "7.0.0",
"description": "JSON Web Token implementation (symmetric and asymmetric)",
"main": "index.js",
"scripts": {
25 changes: 16 additions & 9 deletions sign.js
Original file line number Diff line number Diff line change
@@ -48,13 +48,20 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {
typ: typeof payload === 'object' ? 'JWT' : undefined
}, options.header);

function failure (err) {
if (callback) {
return callback(err);
}
throw err;
}

if (typeof payload === 'undefined') {
throw new Error('payload is required');
return failure(new Error('payload is required'));
} else if (typeof payload === 'object') {
var payload_validation_result = registered_claims_schema.validate(payload);

if (payload_validation_result.error) {
throw payload_validation_result.error;
return failure(payload_validation_result.error);
}

payload = xtend(payload);
@@ -64,22 +71,22 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {
});

if (invalid_options.length > 0) {
throw new Error('invalid ' + invalid_options.join(',') + ' option for ' + (typeof payload ) + ' payload' );
return failure(new Error('invalid ' + invalid_options.join(',') + ' option for ' + (typeof payload ) + ' payload' ));
}
}

if (typeof payload.exp !== 'undefined' && typeof options.expiresIn !== 'undefined') {
throw new Error('Bad "options.expiresIn" option the payload already has an "exp" property.');
return failure(new Error('Bad "options.expiresIn" option the payload already has an "exp" property.'));
}

if (typeof payload.nbf !== 'undefined' && typeof options.notBefore !== 'undefined') {
throw new Error('Bad "options.notBefore" option the payload already has an "nbf" property.');
return failure(new Error('Bad "options.notBefore" option the payload already has an "nbf" property.'));
}

var validation_result = sign_options_schema.validate(options);

if (validation_result.error) {
throw validation_result.error;
return failure(validation_result.error);
}

var timestamp = payload.iat || Math.floor(Date.now() / 1000);
@@ -93,22 +100,22 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {
if (typeof options.notBefore !== 'undefined') {
payload.nbf = timespan(options.notBefore);
if (typeof payload.nbf === 'undefined') {
throw new Error('"notBefore" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60');
return failure(new Error('"notBefore" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60'));
}
}

if (typeof options.expiresIn !== 'undefined' && typeof payload === 'object') {
payload.exp = timespan(options.expiresIn);
if (typeof payload.exp === 'undefined') {
throw new Error('"expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60');
return failure(new Error('"expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60'));
}
}

Object.keys(options_to_payload).forEach(function (key) {
var claim = options_to_payload[key];
if (typeof options[key] !== 'undefined') {
if (typeof payload[claim] !== 'undefined') {
throw new Error('Bad "options.' + key + '" option. The payload already has an "' + claim + '" property.');
return failure(new Error('Bad "options.' + key + '" option. The payload already has an "' + claim + '" property.'));
}
payload[claim] = options[key];
}
18 changes: 17 additions & 1 deletion test/async_sign.tests.js
Original file line number Diff line number Diff line change
@@ -17,12 +17,28 @@ describe('signing a token asynchronously', function() {
});
});

it('should throw error', function(done) {
it('should return error when secret is not a cert for RS256', function(done) {
//this throw an error because the secret is not a cert and RS256 requires a cert.
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'RS256' }, function (err) {
expect(err).to.be.ok();
done();
});
});

it('should return error on wrong arguments', function(done) {
//this throw an error because the secret is not a cert and RS256 requires a cert.
jwt.sign({ foo: 'bar' }, secret, { notBefore: {} }, function (err) {
expect(err).to.be.ok();
done();
});
});

it('should return error on wrong arguments (2)', function(done) {
jwt.sign('string', 'secret', {noTimestamp: true}, function (err) {
expect(err).to.be.ok();
expect(err).to.be.instanceof(Error);
done();
});
});
});
});