2016-12-17 01:06:40 +00:00
|
|
|
|
|
2016-12-17 23:07:56 +00:00
|
|
|
|
var server = require('../../src/lib/server');
|
|
|
|
|
|
2016-12-17 01:06:40 +00:00
|
|
|
|
var request = require('request');
|
|
|
|
|
var assert = require('assert');
|
|
|
|
|
var speakeasy = require('speakeasy');
|
|
|
|
|
var sinon = require('sinon');
|
2017-01-19 00:01:37 +00:00
|
|
|
|
var Promise = require('bluebird');
|
|
|
|
|
|
|
|
|
|
var request = Promise.promisifyAll(request);
|
2016-12-17 01:06:40 +00:00
|
|
|
|
|
2016-12-17 23:07:56 +00:00
|
|
|
|
var BASE_URL = 'http://localhost:8090';
|
|
|
|
|
|
2016-12-17 01:06:40 +00:00
|
|
|
|
describe('test the server', function() {
|
2017-01-21 16:41:06 +00:00
|
|
|
|
var _server
|
|
|
|
|
var u2f;
|
2016-12-17 01:06:40 +00:00
|
|
|
|
var ldap_client = {
|
2016-12-17 18:36:41 +00:00
|
|
|
|
bind: sinon.stub()
|
2016-12-17 01:06:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-21 16:41:06 +00:00
|
|
|
|
beforeEach(function(done) {
|
2016-12-17 01:06:40 +00:00
|
|
|
|
var config = {
|
2016-12-17 23:07:56 +00:00
|
|
|
|
port: 8090,
|
2016-12-17 01:06:40 +00:00
|
|
|
|
totp_secret: 'totp_secret',
|
|
|
|
|
ldap_url: 'ldap://127.0.0.1:389',
|
|
|
|
|
ldap_users_dn: 'ou=users,dc=example,dc=com',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
session_secret: 'session_secret',
|
|
|
|
|
session_max_age: 50000
|
2016-12-17 01:06:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-21 16:41:06 +00:00
|
|
|
|
u2f = {};
|
|
|
|
|
u2f.startRegistration = sinon.stub();
|
|
|
|
|
u2f.finishRegistration = sinon.stub();
|
|
|
|
|
u2f.startAuthentication = sinon.stub();
|
|
|
|
|
u2f.finishAuthentication = sinon.stub();
|
|
|
|
|
|
2016-12-17 01:06:40 +00:00
|
|
|
|
ldap_client.bind.withArgs('cn=test_ok,ou=users,dc=example,dc=com',
|
|
|
|
|
'password').yields(undefined);
|
2016-12-17 18:36:41 +00:00
|
|
|
|
ldap_client.bind.withArgs('cn=test_nok,ou=users,dc=example,dc=com',
|
|
|
|
|
'password').yields('error');
|
2017-01-21 16:41:06 +00:00
|
|
|
|
_server = server.run(config, ldap_client, u2f, function() {
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-12-17 01:06:40 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-01-21 16:41:06 +00:00
|
|
|
|
afterEach(function() {
|
|
|
|
|
_server.close();
|
|
|
|
|
});
|
2016-12-17 18:36:41 +00:00
|
|
|
|
|
|
|
|
|
describe('test GET /login', function() {
|
|
|
|
|
test_login()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('test GET /logout', function() {
|
|
|
|
|
test_logout()
|
|
|
|
|
});
|
|
|
|
|
|
2017-01-21 16:41:06 +00:00
|
|
|
|
describe('test authentication and verification', function() {
|
|
|
|
|
test_authentication();
|
2016-12-17 01:06:40 +00:00
|
|
|
|
});
|
2017-01-19 00:44:24 +00:00
|
|
|
|
|
2017-01-21 16:41:06 +00:00
|
|
|
|
function test_login() {
|
|
|
|
|
it('should serve the login page', function(done) {
|
|
|
|
|
request.getAsync(BASE_URL + '/login')
|
|
|
|
|
.then(function(response) {
|
|
|
|
|
assert.equal(response.statusCode, 200);
|
2017-01-19 00:44:24 +00:00
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-01-21 16:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function test_logout() {
|
|
|
|
|
it('should logout and redirect to /', function(done) {
|
|
|
|
|
request.getAsync(BASE_URL + '/logout')
|
|
|
|
|
.then(function(response) {
|
|
|
|
|
assert.equal(response.req.path, '/');
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-12-17 18:36:41 +00:00
|
|
|
|
});
|
2017-01-21 16:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function test_authentication() {
|
|
|
|
|
it('should return status code 401 when user is not authenticated', function() {
|
2017-01-21 19:33:55 +00:00
|
|
|
|
return request.getAsync({ url: BASE_URL + '/verify' })
|
2017-01-21 16:41:06 +00:00
|
|
|
|
.then(function(response) {
|
|
|
|
|
assert.equal(response.statusCode, 401);
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return status code 204 when user is authenticated using totp', function() {
|
|
|
|
|
var real_token = speakeasy.totp({
|
|
|
|
|
secret: 'totp_secret',
|
|
|
|
|
encoding: 'base32'
|
|
|
|
|
});
|
|
|
|
|
var j = request.jar();
|
|
|
|
|
return request.getAsync({ url: BASE_URL + '/login', jar: j })
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 200, 'get login page failed');
|
|
|
|
|
return request.postAsync({
|
2017-01-21 19:33:55 +00:00
|
|
|
|
url: BASE_URL + '/1stfactor',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
jar: j,
|
|
|
|
|
form: {
|
|
|
|
|
username: 'test_ok',
|
|
|
|
|
password: 'password'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 204, 'first factor failed');
|
|
|
|
|
return request.postAsync({
|
2017-01-21 19:33:55 +00:00
|
|
|
|
url: BASE_URL + '/2ndfactor/totp',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
jar: j,
|
|
|
|
|
form: {
|
|
|
|
|
token: real_token
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 204, 'second factor failed');
|
2017-01-21 19:33:55 +00:00
|
|
|
|
return request.getAsync({ url: BASE_URL + '/verify', jar: j })
|
2017-01-21 16:41:06 +00:00
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 204, 'verify failed');
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return status code 204 when user is authenticated using u2f', function() {
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
var j = request.jar();
|
|
|
|
|
return request.getAsync({ url: BASE_URL + '/login', jar: j })
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 200, 'get login page failed');
|
|
|
|
|
return request.postAsync({
|
2017-01-21 19:33:55 +00:00
|
|
|
|
url: BASE_URL + '/1stfactor',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
jar: j,
|
|
|
|
|
form: {
|
|
|
|
|
username: 'test_ok',
|
|
|
|
|
password: 'password'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 204, 'first factor failed');
|
|
|
|
|
return request.getAsync({
|
2017-01-21 19:33:55 +00:00
|
|
|
|
url: BASE_URL + '/2ndfactor/u2f/register_request',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
jar: j
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 200, 'second factor, start register failed');
|
|
|
|
|
return request.postAsync({
|
2017-01-21 19:33:55 +00:00
|
|
|
|
url: BASE_URL + '/2ndfactor/u2f/register',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
jar: j,
|
|
|
|
|
form: {
|
|
|
|
|
s: 'test'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 204, 'second factor, finish register failed');
|
|
|
|
|
return request.getAsync({
|
2017-01-21 19:33:55 +00:00
|
|
|
|
url: BASE_URL + '/2ndfactor/u2f/sign_request',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
jar: j
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 200, 'second factor, start sign failed');
|
|
|
|
|
return request.postAsync({
|
2017-01-21 19:33:55 +00:00
|
|
|
|
url: BASE_URL + '/2ndfactor/u2f/sign',
|
2017-01-21 16:41:06 +00:00
|
|
|
|
jar: j,
|
|
|
|
|
form: {
|
|
|
|
|
s: 'test'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 204, 'second factor, finish sign failed');
|
2017-01-21 19:33:55 +00:00
|
|
|
|
return request.getAsync({ url: BASE_URL + '/verify', jar: j })
|
2017-01-21 16:41:06 +00:00
|
|
|
|
})
|
|
|
|
|
.then(function(res) {
|
|
|
|
|
assert.equal(res.statusCode, 204, 'verify failed');
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|