2017-01-22 02:24:35 +07:00
|
|
|
|
|
|
|
var server = require('../../src/lib/server');
|
|
|
|
|
2017-01-27 07:20:03 +07:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var request = Promise.promisifyAll(require('request'));
|
2017-01-22 02:24:35 +07:00
|
|
|
var assert = require('assert');
|
|
|
|
var speakeasy = require('speakeasy');
|
|
|
|
var sinon = require('sinon');
|
|
|
|
var tmp = require('tmp');
|
2017-01-27 07:20:03 +07:00
|
|
|
var nedb = require('nedb');
|
2017-03-16 05:07:57 +07:00
|
|
|
var session = require('express-session');
|
2017-03-25 21:17:21 +07:00
|
|
|
var winston = require('winston');
|
2017-01-22 02:24:35 +07:00
|
|
|
|
|
|
|
var PORT = 8050;
|
2017-01-27 07:20:03 +07:00
|
|
|
var requests = require('./requests')(PORT);
|
|
|
|
|
|
|
|
|
2017-01-22 02:24:35 +07:00
|
|
|
describe('test data persistence', function() {
|
|
|
|
var u2f;
|
|
|
|
var tmpDir;
|
|
|
|
var ldap_client = {
|
2017-01-22 23:54:45 +07:00
|
|
|
bind: sinon.stub(),
|
2017-03-25 21:17:21 +07:00
|
|
|
search: sinon.stub(),
|
|
|
|
on: sinon.spy()
|
2017-01-22 02:24:35 +07:00
|
|
|
};
|
2017-03-25 21:17:21 +07:00
|
|
|
var ldap = {
|
|
|
|
createClient: sinon.spy(function() {
|
|
|
|
return ldap_client;
|
|
|
|
})
|
|
|
|
}
|
2017-01-22 02:24:35 +07: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 23:54:45 +07: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-22 02:24:35 +07: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 23:54:45 +07:00
|
|
|
ldap_client.search.yields(undefined, search_res);
|
|
|
|
|
2017-01-22 02:24:35 +07:00
|
|
|
tmpDir = tmp.dirSync({ unsafeCleanup: true });
|
|
|
|
config = {
|
|
|
|
port: PORT,
|
|
|
|
totp_secret: 'totp_secret',
|
2017-03-22 02:57:03 +07:00
|
|
|
ldap: {
|
|
|
|
url: 'ldap://127.0.0.1:389',
|
2017-03-25 21:17:21 +07:00
|
|
|
base_dn: 'ou=users,dc=example,dc=com',
|
2017-03-22 02:57:03 +07:00
|
|
|
},
|
|
|
|
session: {
|
|
|
|
secret: 'session_secret',
|
|
|
|
expiration: 50000,
|
|
|
|
},
|
2017-01-22 23:54:45 +07:00
|
|
|
store_directory: tmpDir.name,
|
2017-01-29 01:59:15 +07:00
|
|
|
notifier: { gmail: { user: 'user@example.com', pass: 'password' } }
|
2017-01-22 02:24:35 +07: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 07:20:03 +07: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-16 05:07:57 +07:00
|
|
|
deps.session = session;
|
2017-03-25 21:17:21 +07:00
|
|
|
deps.winston = winston;
|
|
|
|
deps.ldapjs = ldap;
|
2017-01-27 07:20:03 +07:00
|
|
|
|
2017-01-22 02:24:35 +07:00
|
|
|
var j1 = request.jar();
|
|
|
|
var j2 = request.jar();
|
2017-01-27 07:20:03 +07:00
|
|
|
|
2017-03-25 21:17:21 +07:00
|
|
|
return start_server(config, deps)
|
2017-01-22 02:24:35 +07:00
|
|
|
.then(function(s) {
|
|
|
|
server = s;
|
2017-01-27 07:20:03 +07:00
|
|
|
return requests.login(j1);
|
2017-01-22 02:24:35 +07:00
|
|
|
})
|
|
|
|
.then(function(res) {
|
2017-01-27 07:20:03 +07:00
|
|
|
return requests.first_factor(j1);
|
2017-01-22 02:24:35 +07:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 07:20:03 +07:00
|
|
|
return requests.u2f_registration(j1, transporter);
|
2017-01-22 02:24:35 +07:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 07:20:03 +07:00
|
|
|
return requests.u2f_authentication(j1);
|
2017-01-22 02:24:35 +07:00
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
return stop_server(server);
|
|
|
|
})
|
|
|
|
.then(function() {
|
2017-03-25 21:17:21 +07:00
|
|
|
return start_server(config, deps)
|
2017-01-22 02:24:35 +07:00
|
|
|
})
|
|
|
|
.then(function(s) {
|
|
|
|
server = s;
|
2017-01-27 07:20:03 +07:00
|
|
|
return requests.login(j2);
|
2017-01-22 02:24:35 +07:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 07:20:03 +07:00
|
|
|
return requests.first_factor(j2);
|
2017-01-22 02:24:35 +07:00
|
|
|
})
|
|
|
|
.then(function() {
|
2017-01-27 07:20:03 +07:00
|
|
|
return requests.u2f_authentication(j2);
|
2017-01-22 02:24:35 +07: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-03-25 21:17:21 +07:00
|
|
|
function start_server(config, deps) {
|
2017-01-22 02:24:35 +07:00
|
|
|
return new Promise(function(resolve, reject) {
|
2017-03-25 21:17:21 +07:00
|
|
|
var s = server.run(config, deps);
|
2017-01-22 02:24:35 +07:00
|
|
|
resolve(s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop_server(s) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
s.close();
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|