mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
Use promises in jwt component
This commit is contained in:
parent
d21164af58
commit
8c743228bf
|
@ -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) {
|
||||||
|
|
|
@ -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;
|
||||||
try {
|
return new Promise(function(resolve, reject) {
|
||||||
var decoded = jwt.verify(token, this._secret);
|
try {
|
||||||
defer.resolve(decoded);
|
var decoded = jwt.verify(token, that._secret);
|
||||||
}
|
resolve(decoded);
|
||||||
catch(err) {
|
}
|
||||||
defer.reject(err);
|
catch(err) {
|
||||||
}
|
reject(err.message);
|
||||||
return defer.promise;
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
});
|
});
|
||||||
|
|
|
@ -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')
|
||||||
return jwt.verify(token);
|
.then(function(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')
|
||||||
clock.tick(1000 * 61); // 61 seconds
|
.then(function(token) {
|
||||||
jwt.verify(token).fail(function() { done(); });
|
clock.tick(1000 * 61); // 61 seconds
|
||||||
clock.restore();
|
return jwt.verify(token);
|
||||||
|
})
|
||||||
|
.catch(function() {
|
||||||
|
clock.restore();
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -86,15 +86,17 @@ 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')
|
||||||
var cookie = r.cookie('access_token=' + token);
|
.then(function(token) {
|
||||||
j.setCookie(cookie, BASE_URL + '/_auth');
|
var cookie = r.cookie('access_token=' + token);
|
||||||
|
j.setCookie(cookie, BASE_URL + '/_auth');
|
||||||
|
|
||||||
r.get(BASE_URL + '/_auth')
|
r.get(BASE_URL + '/_auth')
|
||||||
.on('response', function(response) {
|
.on('response', function(response) {
|
||||||
assert.equal(response.statusCode, 204);
|
assert.equal(response.statusCode, 204);
|
||||||
done();
|
done();
|
||||||
})
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user