Use promises in jwt component

This commit is contained in:
Clement Michaud 2017-01-19 01:44:24 +01:00
parent d21164af58
commit 8c743228bf
7 changed files with 50 additions and 41 deletions

View File

@ -1,11 +1,9 @@
module.exports = { module.exports = {
'verify': verify_authentication verify: verify_authentication
} }
var objectPath = require('object-path'); var objectPath = require('object-path');
var totp_checker = require('./totp_checker');
var replies = require('./replies');
var utils = require('./utils'); var utils = require('./utils');
function verify_authentication(req, res) { function verify_authentication(req, res) {

View File

@ -3,27 +3,30 @@ module.exports = Jwt;
var jwt = require('jsonwebtoken'); var jwt = require('jsonwebtoken');
var utils = require('./utils'); var utils = require('./utils');
var Q = require('q'); var Promise = require('bluebird');
function Jwt(secret) { function Jwt(secret) {
var _secret;
this._secret = secret; this._secret = secret;
} }
Jwt.prototype.sign = function(data, expiration_time) { Jwt.prototype.sign = function(data, expiration_time) {
return jwt.sign(data, this._secret, { expiresIn: expiration_time }); var that = this;
return new Promise(function(resolve, reject) {
var token = jwt.sign(data, that._secret, { expiresIn: expiration_time })
resolve(token);
});
} }
Jwt.prototype.verify = function(token) { Jwt.prototype.verify = function(token) {
var defer = Q.defer(); var that = this;
return new Promise(function(resolve, reject) {
try { try {
var decoded = jwt.verify(token, this._secret); var decoded = jwt.verify(token, that._secret);
defer.resolve(decoded); resolve(decoded);
} }
catch(err) { catch(err) {
defer.reject(err); reject(err.message);
} }
return defer.promise; });
} }

View File

@ -20,7 +20,7 @@ function serveAuthGet(req, res) {
.then(function(user) { .then(function(user) {
replies.already_authenticated(res, user); replies.already_authenticated(res, user);
}) })
.fail(function(err) { .catch(function(err) {
replies.authentication_failed(res); replies.authentication_failed(res);
console.error(err); console.error(err);
}); });

View File

@ -1,33 +1,39 @@
var Jwt = require('../../src/lib/jwt'); var Jwt = require('../../src/lib/jwt');
var sinon = require('sinon'); var sinon = require('sinon');
var sinonPromise = require('sinon-promise');
sinonPromise(sinon);
var autoResolving = sinon.promise().resolves();
describe('test jwt', function() { describe('test jwt', function() {
it('should sign and verify the token', function() { it('should sign and verify the token', function() {
var data = {user: 'user'}; var data = {user: 'user'};
var secret = 'secret'; var secret = 'secret';
var jwt = new Jwt(secret); var jwt = new Jwt(secret);
var token = jwt.sign(data, '1m'); return jwt.sign(data, '1m')
.then(function(token) {
return jwt.verify(token); return jwt.verify(token);
}); });
});
it('should verify and fail on wrong token', function() { it('should verify and fail on wrong token', function() {
var jwt = new Jwt('secret'); var jwt = new Jwt('secret');
return jwt.verify('wrong token').fail(autoResolving); var token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoidXNlciIsImlhdCI6MTQ4NDc4NTExMywiZXhwIjoaNDg0Nzg1MTczfQ.yZOZEaMDyOn0tSDiDSPYl4ZP2oL3FQ-Vrzds7hYcNio';
return jwt.verify(token).catch(function() {
return Promise.resolve();
});
}); });
it('should fail after expiry', function(done) { it('should fail after expiry', function() {
var clock = sinon.useFakeTimers(0); var clock = sinon.useFakeTimers(0);
var data = {user: 'user'}; var data = { user: 'user' };
var jwt = new Jwt('secret'); var jwt = new Jwt('secret');
var token = jwt.sign(data, '1m'); return jwt.sign(data, '1m')
.then(function(token) {
clock.tick(1000 * 61); // 61 seconds clock.tick(1000 * 61); // 61 seconds
jwt.verify(token).fail(function() { done(); }); return jwt.verify(token);
})
.catch(function() {
clock.restore(); clock.restore();
return Promise.resolve();
});
}); });
}); });

View File

@ -86,7 +86,8 @@ function test_get_auth(jwt) {
it('should return status code 204 when user is authenticated', function(done) { it('should return status code 204 when user is authenticated', function(done) {
var j = request.jar(); var j = request.jar();
var r = request.defaults({jar: j}); var r = request.defaults({jar: j});
var token = jwt.sign({ user: 'test' }, '1h'); jwt.sign({ user: 'test' }, '1h')
.then(function(token) {
var cookie = r.cookie('access_token=' + token); var cookie = r.cookie('access_token=' + token);
j.setCookie(cookie, BASE_URL + '/_auth'); j.setCookie(cookie, BASE_URL + '/_auth');
@ -94,7 +95,8 @@ function test_get_auth(jwt) {
.on('response', function(response) { .on('response', function(response) {
assert.equal(response.statusCode, 204); assert.equal(response.statusCode, 204);
done(); done();
}) });
});
}); });
} }

View File

@ -1,5 +1,5 @@
var totp_checker = require('../../src/lib/totp_checker'); var totp = require('../../src/lib/totp');
var sinon = require('sinon'); var sinon = require('sinon');
var sinonPromise = require('sinon-promise'); var sinonPromise = require('sinon-promise');
sinonPromise(sinon); sinonPromise(sinon);
@ -15,7 +15,7 @@ describe('test TOTP checker', function() {
var speakeasy_mock = { var speakeasy_mock = {
totp: totp_mock totp: totp_mock
} }
return totp_checker.validate(speakeasy_mock, token, totp_secret); return totp.validate(speakeasy_mock, token, totp_secret);
}); });
it('should not validate a wrong TOTP token', function() { it('should not validate a wrong TOTP token', function() {
@ -26,7 +26,7 @@ describe('test TOTP checker', function() {
var speakeasy_mock = { var speakeasy_mock = {
totp: totp_mock totp: totp_mock
} }
return totp_checker.validate(speakeasy_mock, token, totp_secret).fail(autoResolving); return totp.validate(speakeasy_mock, token, totp_secret).fail(autoResolving);
}); });
}); });