2017-05-20 20:55:37 +00:00
|
|
|
|
|
|
|
|
|
import sinon = require("sinon");
|
|
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
|
import assert = require("assert");
|
|
|
|
|
import winston = require("winston");
|
|
|
|
|
|
2017-05-25 13:09:29 +00:00
|
|
|
|
import FirstFactorPost = require("../../../../src/server/lib/routes/firstfactor/post");
|
|
|
|
|
import exceptions = require("../../../../src/server/lib/Exceptions");
|
|
|
|
|
import AuthenticationSession = require("../../../../src/server/lib/AuthenticationSession");
|
|
|
|
|
import Endpoints = require("../../../../src/server/endpoints");
|
|
|
|
|
|
|
|
|
|
import AuthenticationRegulatorMock = require("../../mocks/AuthenticationRegulator");
|
|
|
|
|
import AccessControllerMock = require("../../mocks/AccessController");
|
|
|
|
|
import { LdapClientMock } from "../../mocks/LdapClient";
|
|
|
|
|
import ExpressMock = require("../../mocks/express");
|
|
|
|
|
import ServerVariablesMock = require("../../mocks/ServerVariablesMock");
|
2017-05-20 20:55:37 +00:00
|
|
|
|
|
2017-05-21 21:32:09 +00:00
|
|
|
|
describe("test the first factor validation route", function () {
|
2017-05-21 10:14:59 +00:00
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-05-20 20:55:37 +00:00
|
|
|
|
let emails: string[];
|
|
|
|
|
let groups: string[];
|
|
|
|
|
let configuration;
|
2017-05-20 23:15:34 +00:00
|
|
|
|
let ldapMock: LdapClientMock;
|
2017-05-21 10:14:59 +00:00
|
|
|
|
let regulator: AuthenticationRegulatorMock.AuthenticationRegulatorMock;
|
|
|
|
|
let accessController: AccessControllerMock.AccessControllerMock;
|
2017-05-20 20:55:37 +00:00
|
|
|
|
|
2017-05-21 21:32:09 +00:00
|
|
|
|
beforeEach(function () {
|
2017-05-20 20:55:37 +00:00
|
|
|
|
configuration = {
|
|
|
|
|
ldap: {
|
|
|
|
|
base_dn: "ou=users,dc=example,dc=com",
|
|
|
|
|
user_name_attribute: "uid"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-21 21:32:09 +00:00
|
|
|
|
emails = ["test_ok@example.com"];
|
|
|
|
|
groups = ["group1", "group2" ];
|
2017-05-20 20:55:37 +00:00
|
|
|
|
|
2017-05-20 23:15:34 +00:00
|
|
|
|
ldapMock = LdapClientMock();
|
2017-05-20 20:55:37 +00:00
|
|
|
|
|
2017-05-21 10:14:59 +00:00
|
|
|
|
accessController = AccessControllerMock.AccessControllerMock();
|
2017-05-20 20:55:37 +00:00
|
|
|
|
accessController.isDomainAllowedForUser.returns(true);
|
|
|
|
|
|
2017-05-21 10:14:59 +00:00
|
|
|
|
regulator = AuthenticationRegulatorMock.AuthenticationRegulatorMock();
|
2017-05-20 20:55:37 +00:00
|
|
|
|
regulator.regulate.returns(BluebirdPromise.resolve());
|
|
|
|
|
regulator.mark.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
|
|
req = {
|
|
|
|
|
app: {
|
|
|
|
|
},
|
|
|
|
|
body: {
|
|
|
|
|
username: "username",
|
|
|
|
|
password: "password"
|
|
|
|
|
},
|
|
|
|
|
session: {
|
2017-05-21 21:32:09 +00:00
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
host: "home.example.com"
|
2017-05-20 20:55:37 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
|
|
|
|
|
|
const mocks = ServerVariablesMock.mock(req.app);
|
|
|
|
|
mocks.ldap = ldapMock;
|
|
|
|
|
mocks.config = configuration;
|
|
|
|
|
mocks.logger = winston;
|
|
|
|
|
mocks.regulator = regulator;
|
|
|
|
|
mocks.accessController = accessController;
|
|
|
|
|
|
2017-05-20 23:15:34 +00:00
|
|
|
|
res = ExpressMock.ResponseMock();
|
2017-05-20 20:55:37 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-05-25 13:09:29 +00:00
|
|
|
|
it("should redirect client to second factor page", function () {
|
2017-06-14 22:22:16 +00:00
|
|
|
|
ldapMock.checkPassword.withArgs("username").returns(BluebirdPromise.resolve());
|
|
|
|
|
ldapMock.retrieveEmails.returns(BluebirdPromise.resolve(emails));
|
2017-05-25 13:09:29 +00:00
|
|
|
|
const authSession = AuthenticationSession.get(req as any);
|
|
|
|
|
return FirstFactorPost.default(req as any, res as any)
|
|
|
|
|
.then(function () {
|
|
|
|
|
assert.equal("username", authSession.userid);
|
|
|
|
|
assert.equal(Endpoints.SECOND_FACTOR_GET, res.redirect.getCall(0).args[0]);
|
2017-05-20 20:55:37 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-14 22:22:16 +00:00
|
|
|
|
it("should retrieve email from LDAP", function () {
|
|
|
|
|
ldapMock.checkPassword.returns(BluebirdPromise.resolve());
|
|
|
|
|
ldapMock.retrieveEmails = sinon.stub().withArgs("username").returns(BluebirdPromise.resolve([{ mail: ["test@example.com"] }]));
|
|
|
|
|
return FirstFactorPost.default(req as any, res as any);
|
2017-05-20 20:55:37 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-06-14 22:22:16 +00:00
|
|
|
|
it("should set first email address as user session variable", function () {
|
2017-05-25 13:09:29 +00:00
|
|
|
|
const emails = ["test_ok@example.com"];
|
|
|
|
|
const authSession = AuthenticationSession.get(req as any);
|
2017-06-14 22:22:16 +00:00
|
|
|
|
ldapMock.checkPassword.returns(BluebirdPromise.resolve());
|
|
|
|
|
ldapMock.retrieveEmails.returns(BluebirdPromise.resolve(emails));
|
2017-05-25 13:09:29 +00:00
|
|
|
|
return FirstFactorPost.default(req as any, res as any)
|
|
|
|
|
.then(function () {
|
|
|
|
|
assert.equal("test_ok@example.com", authSession.email);
|
2017-05-20 20:55:37 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-14 22:22:16 +00:00
|
|
|
|
it("should return status code 401 when LDAP binding throws", function () {
|
|
|
|
|
ldapMock.checkPassword.returns(BluebirdPromise.reject(new exceptions.LdapBindError("Bad credentials")));
|
|
|
|
|
return FirstFactorPost.default(req as any, res as any)
|
|
|
|
|
.then(function () {
|
|
|
|
|
assert.equal(401, res.status.getCall(0).args[0]);
|
|
|
|
|
assert.equal(regulator.mark.getCall(0).args[0], "username");
|
|
|
|
|
});
|
2017-05-20 20:55:37 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-06-14 22:22:16 +00:00
|
|
|
|
it("should return status code 500 when LDAP search throws", function () {
|
|
|
|
|
ldapMock.checkPassword.returns(BluebirdPromise.resolve());
|
|
|
|
|
ldapMock.retrieveEmails.returns(BluebirdPromise.reject(new exceptions.LdapSearchError("error while retrieving emails")));
|
|
|
|
|
return FirstFactorPost.default(req as any, res as any)
|
|
|
|
|
.then(function () {
|
|
|
|
|
assert.equal(500, res.status.getCall(0).args[0]);
|
|
|
|
|
});
|
2017-05-20 20:55:37 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-06-14 22:22:16 +00:00
|
|
|
|
it("should return status code 403 when regulator rejects authentication", function () {
|
2017-05-20 20:55:37 +00:00
|
|
|
|
const err = new exceptions.AuthenticationRegulationError("Authentication regulation...");
|
|
|
|
|
regulator.regulate.returns(BluebirdPromise.reject(err));
|
2017-06-14 22:22:16 +00:00
|
|
|
|
return FirstFactorPost.default(req as any, res as any)
|
|
|
|
|
.then(function () {
|
|
|
|
|
assert.equal(403, res.status.getCall(0).args[0]);
|
|
|
|
|
assert.equal(1, res.send.callCount);
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-05-20 20:55:37 +00:00
|
|
|
|
|
2017-06-14 22:22:16 +00:00
|
|
|
|
it("should fail when admin user does not have rights to retrieve attribute mail", function () {
|
|
|
|
|
ldapMock.checkPassword.returns(BluebirdPromise.resolve());
|
|
|
|
|
ldapMock.retrieveEmails = sinon.stub().withArgs("username").returns(BluebirdPromise.resolve([]));
|
|
|
|
|
ldapMock.retrieveGroups = sinon.stub().withArgs("username").returns(BluebirdPromise.resolve(["group1"]));
|
|
|
|
|
return FirstFactorPost.default(req as any, res as any)
|
|
|
|
|
.then(function () {
|
|
|
|
|
assert.equal(500, res.status.getCall(0).args[0]);
|
|
|
|
|
assert.equal(1, res.send.callCount);
|
|
|
|
|
});
|
2017-05-20 20:55:37 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|