2017-01-21 19:24:35 +00:00
|
|
|
|
|
|
|
var server = require('../../src/lib/server');
|
|
|
|
|
2017-01-27 00:20:03 +00:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var request = Promise.promisifyAll(require('request'));
|
2017-01-21 19:24:35 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var speakeasy = require('speakeasy');
|
|
|
|
var sinon = require('sinon');
|
|
|
|
var tmp = require('tmp');
|
2017-01-27 00:20:03 +00:00
|
|
|
var nedb = require('nedb');
|
2017-03-15 22:07:57 +00:00
|
|
|
var session = require('express-session');
|
2017-01-21 19:24:35 +00:00
|
|
|
|
|
|
|
var PORT = 8050;
|
|
|
|
var BASE_URL = 'http://localhost:' + PORT;
|
|
|
|
|
2017-01-27 00:20:03 +00:00
|
|
|
var requests = require('./requests')(PORT);
|
|
|
|
|
|
|
|
|
2017-01-21 19:24:35 +00:00
|
|
|
describe('test data persistence', function() {
|
|
|
|
var u2f;
|
|
|
|
var tmpDir;
|
|
|
|
var ldap_client = {
|
2017-01-22 16:54:45 +00:00
|
|
|
bind: sinon.stub(),
|
|
|
|
search: sinon.stub()
|
2017-01-21 19:24:35 +00:00
|
|
|
};
|
|
|
|
var config;
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
u2f = {};
|
|
|
|
u2f.startRegistration = sinon.stub();
|
|
|
|
u2f.finishRegistration = sinon.stub();
|
|
|
|
u2f.startAuthentication = sinon.stub();
|
|
|
|
u2f.finishAuthentication = sinon.stub();
|
|
|
|
|
2017-01-22 16:54:45 +00:00
|
|
|
var search_doc = {
|
|
|
|
object: {
|
|
|
|
mail: 'test_ok@example.com'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var search_res = {};
|
|
|
|
search_res.on = sinon.spy(function(event, fn) {
|
|
|
|
if(event != 'error') fn(search_doc);
|
|
|
|
});
|
|
|
|
|
2017-01-21 19:24:35 +00:00
|
|
|
ldap_client.bind.withArgs('cn=test_ok,ou=users,dc=example,dc=com',
|
|
|
|
'password').yields(undefined);
|
|
|
|
ldap_client.bind.withArgs('cn=test_nok,ou=users,dc=example,dc=com',
|
|
|
|
'password').yields('error');
|
2017-01-22 16:54:45 +00:00
|
|
|
ldap_client.search.yields(undefined, search_res);
|
|
|
|
|
2017-01-21 19:24:35 +00:00
|
|
|
tmpDir = tmp.dirSync({ unsafeCleanup: true });
|
|
|
|
config = {
|
|
|
|
port: PORT,
|
|
|
|
totp_secret: 'totp_secret',
|
|
|
|
ldap_url: 'ldap://127.0.0.1:389',
|
|
|
|
ldap_users_dn: 'ou=users,dc=example,dc=com',
|
|
|
|
session_secret: 'session_secret',
|
|
|
|
session_max_age: 50000,
|
2017-01-22 16:54:45 +00:00
|
|
|
store_directory: tmpDir.name,
|
2017-01-28 18:59:15 +00:00
|
|
|
notifier: { gmail: { user: 'user@example.com', pass: 'password' } }
|
2017-01-21 19:24:35 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
tmpDir.removeCallback();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should save a u2f meta and reload it after a restart of the server', function() {
|
|
|
|
var server;
|
|
|
|
var sign_request = {};
|
|
|
|
var sign_status = {};
|
|
|
|
var registration_request = {};
|
|
|
|
var registration_status = {};
|
|
|
|
u2f.startRegistration.returns(Promise.resolve(sign_request));
|
|
|
|
u2f.finishRegistration.returns(Promise.resolve(sign_status));
|
|
|
|
u2f.startAuthentication.returns(Promise.resolve(registration_request));
|
|
|
|
u2f.finishAuthentication.returns(Promise.resolve(registration_status));
|
2017-01-27 00:20:03 +00:00
|
|
|
|
|
|
|
var nodemailer = {};
|
|
|
|
var transporter = {
|
|
|
|
sendMail: sinon.stub().yields()
|
|
|
|
};
|
|
|
|
nodemailer.createTransport = sinon.spy(function() {
|
|
|
|
return transporter;
|
|
|
|
});
|
|
|
|
|
|
|
|
var deps = {};
|
|
|
|
deps.u2f = u2f;
|
|
|
|
deps.nedb = nedb;
|
|
|
|
deps.nodemailer = nodemailer;
|
2017-03-15 22:07:57 +00:00
|
|
|
deps.session = session;
|
2017-01-27 00:20:03 +00:00
|
|
|
|
2017-01-21 19:24:35 +00:00
|
|
|
var j1 = request.jar();
|
|
|
|
var j2 = request.jar();
|
2017-01-27 00:20:03 +00:00
|
|
|
|
|
|
|
return start_server(config, ldap_client, deps)
|
2017-01-21 19:24:35 +00:00
|
|
|
.then(function(s) {
|
|
|
|
server = s;
|
2017-01-27 00:20:03 +00:00
|
|
|
return requests.login(j1);
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function(res) {
|
2017-01-27 00:20:03 +00:00
|
|
|
return requests.first_factor(j1);
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 00:20:03 +00:00
|
|
|
return requests.u2f_registration(j1, transporter);
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 00:20:03 +00:00
|
|
|
return requests.u2f_authentication(j1);
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
return stop_server(server);
|
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 00:20:03 +00:00
|
|
|
return start_server(config, ldap_client, deps)
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function(s) {
|
|
|
|
server = s;
|
2017-01-27 00:20:03 +00:00
|
|
|
return requests.login(j2);
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 00:20:03 +00:00
|
|
|
return requests.first_factor(j2);
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 00:20:03 +00:00
|
|
|
return requests.u2f_authentication(j2);
|
2017-01-21 19:24:35 +00:00
|
|
|
})
|
|
|
|
.then(function(res) {
|
|
|
|
assert.equal(204, res.statusCode);
|
|
|
|
server.close();
|
|
|
|
return Promise.resolve();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.error(err);
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-27 00:20:03 +00:00
|
|
|
function start_server(config, ldap_client, deps) {
|
2017-01-21 19:24:35 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
2017-01-27 00:20:03 +00:00
|
|
|
var s = server.run(config, ldap_client, deps);
|
2017-01-21 19:24:35 +00:00
|
|
|
resolve(s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop_server(s) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
s.close();
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|