Use promises in jwt component

pull/7/head
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 = {
'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) {

View File

@ -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);
}
});
}

View File

@ -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);
});

View File

@ -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();
});
});
});

View File

@ -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();
});
});
});
}

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 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);
});
});