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: v7.0.1
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.1.0
Choose a head ref
  • 7 commits
  • 5 files changed
  • 5 contributors

Commits on May 17, 2016

  1. add dependencies badge on readme

    change build badge to link to master branch and changed it to svg (nicer on retina)
    a0viedo committed May 17, 2016
    Copy the full SHA
    659b399 View commit details

Commits on Jun 12, 2016

  1. Fixed broken link

    Link was going relative to the repo instead of to the external domain.
    ConnorMcF authored Jun 12, 2016
    Copy the full SHA
    4e69b31 View commit details

Commits on Jun 22, 2016

  1. Copy the full SHA
    757a16e View commit details

Commits on Jul 12, 2016

  1. Merge pull request #215 from ConnorMcF/patch-1

    Fixed broken link
    jfromaniello authored Jul 12, 2016
    Copy the full SHA
    0cb8702 View commit details
  2. Merge pull request #202 from a0viedo/patch-1

    add dependencies badge on readme
    jfromaniello authored Jul 12, 2016

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    164fdc1 View commit details
  3. Copy the full SHA
    df601b0 View commit details
  4. 7.1.0

    jfromaniello committed Jul 12, 2016
    Copy the full SHA
    4e1e581 View commit details
Showing with 35 additions and 33 deletions.
  1. +2 −2 README.md
  2. +3 −3 lib/timespan.js
  3. +1 −1 package.json
  4. +20 −20 sign.js
  5. +9 −7 test/iat.tests.js
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# jsonwebtoken [![Build Status](https://secure.travis-ci.org/auth0/node-jsonwebtoken.png)](http://travis-ci.org/auth0/node-jsonwebtoken)
# jsonwebtoken [![Build Status](https://secure.travis-ci.org/auth0/node-jsonwebtoken.svg?branch=master)](http://travis-ci.org/auth0/node-jsonwebtoken)[![Dependency Status](https://david-dm.org/auth0/node-jsonwebtoken.svg)](https://david-dm.org/auth0/node-jsonwebtoken)


An implementation of [JSON Web Tokens](https://tools.ietf.org/html/rfc7519).
@@ -253,7 +253,7 @@ If you have found a bug or if you have a feature request, please report them at

## Author

[Auth0](auth0.com)
[Auth0](https://auth0.com)

## License

6 changes: 3 additions & 3 deletions lib/timespan.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var ms = require('ms');

module.exports = function (time) {
var timestamp = Math.floor(Date.now() / 1000);
module.exports = function (time, iat) {
var timestamp = iat || Math.floor(Date.now() / 1000);

if (typeof time === 'string') {
var milliseconds = ms(time);
if (typeof milliseconds === 'undefined') {
return;
}
return Math.floor(timestamp + milliseconds / 1000);
} else if (typeof time === 'number' ) {
} else if (typeof time === 'number') {
return timestamp + time;
} else {
return;
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": "7.0.1",
"version": "7.1.0",
"description": "JSON Web Token implementation (symmetric and asymmetric)",
"main": "index.js",
"scripts": {
40 changes: 20 additions & 20 deletions sign.js
Original file line number Diff line number Diff line change
@@ -6,13 +6,13 @@ var jws = require('jws');
var sign_options_schema = Joi.object().keys({
expiresIn: [Joi.number().integer(), Joi.string()],
notBefore: [Joi.number().integer(), Joi.string()],
audience: [Joi.string(), Joi.array()],
algorithm: Joi.string().valid('RS256','RS384','RS512','ES256','ES384','ES512','HS256','HS384','HS512','none'),
header: Joi.object(),
encoding: Joi.string(),
issuer: Joi.string(),
subject: Joi.string(),
jwtid: Joi.string(),
audience: [Joi.string(), Joi.array()],
algorithm: Joi.string().valid('RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512', 'HS256', 'HS384', 'HS512', 'none'),
header: Joi.object(),
encoding: Joi.string(),
issuer: Joi.string(),
subject: Joi.string(),
jwtid: Joi.string(),
noTimestamp: Joi.boolean()
});

@@ -25,9 +25,9 @@ var registered_claims_schema = Joi.object().keys({

var options_to_payload = {
'audience': 'aud',
'issuer': 'iss',
'subject': 'sub',
'jwtid': 'jti'
'issuer': 'iss',
'subject': 'sub',
'jwtid': 'jti'
};

var options_for_objects = [
@@ -40,15 +40,15 @@ var options_for_objects = [
'jwtid',
];

module.exports = function(payload, secretOrPrivateKey, options, callback) {
module.exports = function (payload, secretOrPrivateKey, options, callback) {
options = options || {};

var header = xtend({
alg: options.algorithm || 'HS256',
typ: typeof payload === 'object' ? 'JWT' : undefined
}, options.header);

function failure (err) {
function failure(err) {
if (callback) {
return callback(err);
}
@@ -71,7 +71,7 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {
});

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

@@ -86,7 +86,7 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {
var validation_result = sign_options_schema.validate(options);

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

var timestamp = payload.iat || Math.floor(Date.now() / 1000);
@@ -105,7 +105,7 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {
}

if (typeof options.expiresIn !== 'undefined' && typeof payload === 'object') {
payload.exp = timespan(options.expiresIn);
payload.exp = timespan(options.expiresIn, timestamp);
if (typeof payload.exp === 'undefined') {
return failure(new Error('"expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60'));
}
@@ -123,17 +123,17 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {

var encoding = options.encoding || 'utf8';

if(typeof callback === 'function') {
if (typeof callback === 'function') {
jws.createSign({
header: header,
privateKey: secretOrPrivateKey,
payload: JSON.stringify(payload),
encoding: encoding
})
.once('error', callback)
.once('done', function(signature) {
callback(null, signature);
});
.once('error', callback)
.once('done', function (signature) {
callback(null, signature);
});
} else {
return jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding});
}
16 changes: 9 additions & 7 deletions test/iat.tests.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
var jwt = require('../index');
var expect = require('chai').expect;

describe('iat', function() {
describe('iat', function () {

it('should work with a numeric iat not changing the expiration date', function () {
var token = jwt.sign({foo: 123, iat: Math.floor(Date.now() / 1000) - 30}, '123', { expiresIn: 10 });
it('should work with a exp calculated based on numeric iat', function () {
var dateNow = Math.floor(Date.now() / 1000);
var iat = dateNow - 30;
var expiresIn = 50;
var token = jwt.sign({foo: 123, iat: iat}, '123', {expiresIn: expiresIn});
var result = jwt.verify(token, '123');
expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + 10, 0.2);
expect(result.exp).to.be.closeTo(iat + expiresIn, 0.2);
});


it('should throw if iat is not a number', function () {
expect(function () {
jwt.sign({foo: 123, iat:'hello'}, '123');
jwt.sign({foo: 123, iat: 'hello'}, '123');
}).to.throw(/"iat" must be a number/);
});


});
});