2017-07-16 15:37:13 +00:00
|
|
|
|
2017-10-06 22:09:42 +00:00
|
|
|
import { Authenticator } from "../../src/lib/ldap/Authenticator";
|
|
|
|
import { LdapConfiguration } from "../../src/lib/configuration/Configuration";
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
import Sinon = require("sinon");
|
2017-07-16 15:37:13 +00:00
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-09-02 20:38:26 +00:00
|
|
|
import Assert = require("assert");
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
import { ClientFactoryStub } from "../mocks/ldap/ClientFactoryStub";
|
|
|
|
import { ClientStub } from "../mocks/ldap/ClientStub";
|
2017-07-16 15:37:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
describe("test ldap authentication", function () {
|
2017-09-02 20:38:26 +00:00
|
|
|
const USERNAME = "username";
|
|
|
|
const PASSWORD = "password";
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
const ADMIN_USER_DN = "cn=admin,dc=example,dc=com";
|
|
|
|
const ADMIN_PASSWORD = "admin_password";
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
let clientFactoryStub: ClientFactoryStub;
|
|
|
|
let adminClientStub: ClientStub;
|
|
|
|
let userClientStub: ClientStub;
|
2017-07-16 15:37:13 +00:00
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
let authenticator: Authenticator;
|
|
|
|
let ldapConfig: LdapConfiguration;
|
2017-07-16 15:37:13 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-09-02 20:38:26 +00:00
|
|
|
clientFactoryStub = new ClientFactoryStub();
|
|
|
|
adminClientStub = new ClientStub();
|
|
|
|
userClientStub = new ClientStub();
|
2017-07-16 15:37:13 +00:00
|
|
|
|
|
|
|
ldapConfig = {
|
|
|
|
url: "http://localhost:324",
|
2017-09-02 20:38:26 +00:00
|
|
|
users_dn: "ou=users,dc=example,dc=com",
|
|
|
|
users_filter: "cn={0}",
|
|
|
|
groups_dn: "ou=groups,dc=example,dc=com",
|
|
|
|
groups_filter: "member={0}",
|
|
|
|
mail_attribute: "mail",
|
|
|
|
group_name_attribute: "cn",
|
|
|
|
user: ADMIN_USER_DN,
|
|
|
|
password: ADMIN_PASSWORD
|
2017-07-16 15:37:13 +00:00
|
|
|
};
|
|
|
|
|
2017-09-02 20:38:26 +00:00
|
|
|
authenticator = new Authenticator(ldapConfig, clientFactoryStub);
|
2017-07-16 15:37:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("success", function () {
|
|
|
|
it("should bind the user if good credentials provided", function () {
|
2017-09-02 20:38:26 +00:00
|
|
|
clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD)
|
|
|
|
.returns(adminClientStub);
|
|
|
|
clientFactoryStub.createStub.withArgs("cn=" + USERNAME + ",ou=users,dc=example,dc=com", PASSWORD)
|
|
|
|
.returns(userClientStub);
|
|
|
|
|
|
|
|
// admin connects successfully
|
|
|
|
adminClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
adminClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
// admin search for user dn of user
|
|
|
|
adminClientStub.searchUserDnStub.withArgs(USERNAME)
|
|
|
|
.returns(BluebirdPromise.resolve("cn=" + USERNAME + ",ou=users,dc=example,dc=com"));
|
|
|
|
|
|
|
|
// user connects successfully
|
|
|
|
userClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
userClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
// admin retrieves emails and groups of user
|
2017-10-07 11:46:19 +00:00
|
|
|
adminClientStub.searchEmailsStub.returns(BluebirdPromise.resolve(["group1"]));
|
|
|
|
adminClientStub.searchGroupsStub.returns(BluebirdPromise.resolve(["user@example.com"]));
|
2017-09-02 20:38:26 +00:00
|
|
|
|
|
|
|
return authenticator.authenticate(USERNAME, PASSWORD);
|
2017-07-16 15:37:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("failure", function () {
|
|
|
|
it("should not bind the user if wrong credentials provided", function () {
|
2017-09-02 20:38:26 +00:00
|
|
|
clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD)
|
|
|
|
.returns(adminClientStub);
|
|
|
|
clientFactoryStub.createStub.withArgs("cn=" + USERNAME + ",ou=users,dc=example,dc=com", PASSWORD)
|
|
|
|
.returns(userClientStub);
|
|
|
|
|
|
|
|
// admin connects successfully
|
|
|
|
adminClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
adminClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
// admin search for user dn of user
|
|
|
|
adminClientStub.searchUserDnStub.withArgs(USERNAME)
|
|
|
|
.returns(BluebirdPromise.resolve("cn=" + USERNAME + ",ou=users,dc=example,dc=com"));
|
|
|
|
|
|
|
|
// user connects successfully
|
|
|
|
userClientStub.openStub.returns(BluebirdPromise.reject(new Error("Error while binding")));
|
|
|
|
userClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
return authenticator.authenticate(USERNAME, PASSWORD)
|
|
|
|
.then(function () { return BluebirdPromise.reject("Should not be here!"); })
|
2017-07-16 15:37:13 +00:00
|
|
|
.catch(function () {
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not bind the user if search of emails or group fails", function () {
|
2017-09-02 20:38:26 +00:00
|
|
|
clientFactoryStub.createStub.withArgs(ADMIN_USER_DN, ADMIN_PASSWORD)
|
|
|
|
.returns(adminClientStub);
|
|
|
|
clientFactoryStub.createStub.withArgs("cn=" + USERNAME + ",ou=users,dc=example,dc=com", PASSWORD)
|
|
|
|
.returns(userClientStub);
|
|
|
|
|
|
|
|
// admin connects successfully
|
|
|
|
adminClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
adminClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
// admin search for user dn of user
|
|
|
|
adminClientStub.searchUserDnStub.withArgs(USERNAME)
|
|
|
|
.returns(BluebirdPromise.resolve("cn=" + USERNAME + ",ou=users,dc=example,dc=com"));
|
|
|
|
|
|
|
|
// user connects successfully
|
|
|
|
userClientStub.openStub.returns(BluebirdPromise.resolve());
|
|
|
|
userClientStub.closeStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
2017-10-07 11:46:19 +00:00
|
|
|
adminClientStub.searchEmailsStub.returns(BluebirdPromise.resolve(["group1"]));
|
2017-09-02 20:38:26 +00:00
|
|
|
// admin retrieves emails and groups of user
|
2017-10-07 11:46:19 +00:00
|
|
|
adminClientStub.searchGroupsStub
|
2017-09-02 20:38:26 +00:00
|
|
|
.returns(BluebirdPromise.reject(new Error("Error while retrieving emails and groups")));
|
|
|
|
|
|
|
|
return authenticator.authenticate(USERNAME, PASSWORD)
|
|
|
|
.then(function () { return BluebirdPromise.reject("Should not be here!"); })
|
2017-07-16 15:37:13 +00:00
|
|
|
.catch(function () {
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|