From 8c743228bff46608dad1b7dadb3f65bf9d74bb96 Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Thu, 19 Jan 2017 01:44:24 +0100 Subject: [PATCH] Use promises in jwt component --- src/lib/authentication.js | 4 +--- src/lib/jwt.js | 29 ++++++++++++++----------- src/lib/routes.js | 2 +- src/lib/{totp_checker.js => totp.js} | 0 test/unitary/test_jwt.js | 32 +++++++++++++++++----------- test/unitary/test_server.js | 18 +++++++++------- test/unitary/test_totp_checker.js | 6 +++--- 7 files changed, 50 insertions(+), 41 deletions(-) rename src/lib/{totp_checker.js => totp.js} (100%) diff --git a/src/lib/authentication.js b/src/lib/authentication.js index ee45da35..74993038 100644 --- a/src/lib/authentication.js +++ b/src/lib/authentication.js @@ -1,11 +1,9 @@ module.exports = { - 'verify': verify_authentication + verify: verify_authentication } var objectPath = require('object-path'); -var totp_checker = require('./totp_checker'); -var replies = require('./replies'); var utils = require('./utils'); function verify_authentication(req, res) { diff --git a/src/lib/jwt.js b/src/lib/jwt.js index 1fcba679..7cc00d00 100644 --- a/src/lib/jwt.js +++ b/src/lib/jwt.js @@ -3,27 +3,30 @@ module.exports = Jwt; var jwt = require('jsonwebtoken'); var utils = require('./utils'); -var Q = require('q'); +var Promise = require('bluebird'); function Jwt(secret) { - var _secret; - this._secret = secret; } 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) { - var defer = Q.defer(); - try { - var decoded = jwt.verify(token, this._secret); - defer.resolve(decoded); - } - catch(err) { - defer.reject(err); - } - return defer.promise; + var that = this; + return new Promise(function(resolve, reject) { + try { + var decoded = jwt.verify(token, that._secret); + resolve(decoded); + } + catch(err) { + reject(err.message); + } + }); } diff --git a/src/lib/routes.js b/src/lib/routes.js index ada664bf..507726e3 100644 --- a/src/lib/routes.js +++ b/src/lib/routes.js @@ -20,7 +20,7 @@ function serveAuthGet(req, res) { .then(function(user) { replies.already_authenticated(res, user); }) - .fail(function(err) { + .catch(function(err) { replies.authentication_failed(res); console.error(err); }); diff --git a/src/lib/totp_checker.js b/src/lib/totp.js similarity index 100% rename from src/lib/totp_checker.js rename to src/lib/totp.js diff --git a/test/unitary/test_jwt.js b/test/unitary/test_jwt.js index 2f37d500..0285fb48 100644 --- a/test/unitary/test_jwt.js +++ b/test/unitary/test_jwt.js @@ -1,33 +1,39 @@ var Jwt = require('../../src/lib/jwt'); var sinon = require('sinon'); -var sinonPromise = require('sinon-promise'); -sinonPromise(sinon); - -var autoResolving = sinon.promise().resolves(); describe('test jwt', function() { it('should sign and verify the token', function() { var data = {user: 'user'}; var secret = 'secret'; var jwt = new Jwt(secret); - var token = jwt.sign(data, '1m'); - return jwt.verify(token); + return jwt.sign(data, '1m') + .then(function(token) { + return jwt.verify(token); + }); }); it('should verify and fail on wrong token', function() { 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 data = {user: 'user'}; + var data = { user: 'user' }; var jwt = new Jwt('secret'); - var token = jwt.sign(data, '1m'); - clock.tick(1000 * 61); // 61 seconds - jwt.verify(token).fail(function() { done(); }); - clock.restore(); + return jwt.sign(data, '1m') + .then(function(token) { + clock.tick(1000 * 61); // 61 seconds + return jwt.verify(token); + }) + .catch(function() { + clock.restore(); + return Promise.resolve(); + }); }); }); diff --git a/test/unitary/test_server.js b/test/unitary/test_server.js index f2c4be57..5374073b 100644 --- a/test/unitary/test_server.js +++ b/test/unitary/test_server.js @@ -86,15 +86,17 @@ function test_get_auth(jwt) { it('should return status code 204 when user is authenticated', function(done) { var j = request.jar(); var r = request.defaults({jar: j}); - var token = jwt.sign({ user: 'test' }, '1h'); - var cookie = r.cookie('access_token=' + token); - j.setCookie(cookie, BASE_URL + '/_auth'); + jwt.sign({ user: 'test' }, '1h') + .then(function(token) { + var cookie = r.cookie('access_token=' + token); + j.setCookie(cookie, BASE_URL + '/_auth'); - r.get(BASE_URL + '/_auth') - .on('response', function(response) { - assert.equal(response.statusCode, 204); - done(); - }) + r.get(BASE_URL + '/_auth') + .on('response', function(response) { + assert.equal(response.statusCode, 204); + done(); + }); + }); }); } diff --git a/test/unitary/test_totp_checker.js b/test/unitary/test_totp_checker.js index c1582f51..948813b2 100644 --- a/test/unitary/test_totp_checker.js +++ b/test/unitary/test_totp_checker.js @@ -1,5 +1,5 @@ -var totp_checker = require('../../src/lib/totp_checker'); +var totp = require('../../src/lib/totp'); var sinon = require('sinon'); var sinonPromise = require('sinon-promise'); sinonPromise(sinon); @@ -15,7 +15,7 @@ describe('test TOTP checker', function() { var speakeasy_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() { @@ -26,7 +26,7 @@ describe('test TOTP checker', function() { var speakeasy_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); }); });