2017-07-16 22:37:13 +07:00
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
import exceptions = require("../Exceptions");
|
|
|
|
import ldapjs = require("ldapjs");
|
2017-09-03 03:38:26 +07:00
|
|
|
import { IClient } from "./IClient";
|
|
|
|
import { IClientFactory } from "./IClientFactory";
|
|
|
|
import { GroupsAndEmails } from "./IClient";
|
2017-07-16 22:37:13 +07:00
|
|
|
|
2017-09-03 03:38:26 +07:00
|
|
|
import { IAuthenticator } from "./IAuthenticator";
|
2017-07-20 02:06:12 +07:00
|
|
|
import { LdapConfiguration } from "../configuration/Configuration";
|
2017-10-07 18:46:19 +07:00
|
|
|
import { EmailsAndGroupsRetriever } from "./EmailsAndGroupsRetriever";
|
2017-07-16 22:37:13 +07:00
|
|
|
|
|
|
|
|
2017-09-03 03:38:26 +07:00
|
|
|
export class Authenticator implements IAuthenticator {
|
2017-07-16 22:37:13 +07:00
|
|
|
private options: LdapConfiguration;
|
2017-09-03 03:38:26 +07:00
|
|
|
private clientFactory: IClientFactory;
|
2017-07-16 22:37:13 +07:00
|
|
|
|
2017-09-03 03:38:26 +07:00
|
|
|
constructor(options: LdapConfiguration, clientFactory: IClientFactory) {
|
2017-07-16 22:37:13 +07:00
|
|
|
this.options = options;
|
2017-09-03 03:38:26 +07:00
|
|
|
this.clientFactory = clientFactory;
|
2017-07-16 22:37:13 +07:00
|
|
|
}
|
|
|
|
|
2017-09-03 03:38:26 +07:00
|
|
|
authenticate(username: string, password: string): BluebirdPromise<GroupsAndEmails> {
|
|
|
|
const that = this;
|
|
|
|
let userClient: IClient;
|
|
|
|
const adminClient = this.clientFactory.create(this.options.user, this.options.password);
|
2017-10-07 18:46:19 +07:00
|
|
|
const emailsAndGroupsRetriever = new EmailsAndGroupsRetriever(this.options, this.clientFactory);
|
2017-07-16 22:37:13 +07:00
|
|
|
|
2017-09-03 03:38:26 +07:00
|
|
|
return adminClient.open()
|
|
|
|
.then(function () {
|
|
|
|
return adminClient.searchUserDn(username);
|
|
|
|
})
|
|
|
|
.then(function (userDN: string) {
|
|
|
|
userClient = that.clientFactory.create(userDN, password);
|
|
|
|
return userClient.open();
|
|
|
|
})
|
2017-07-16 22:37:13 +07:00
|
|
|
.then(function () {
|
|
|
|
return userClient.close();
|
|
|
|
})
|
|
|
|
.then(function () {
|
2017-10-07 18:46:19 +07:00
|
|
|
return emailsAndGroupsRetriever.retrieve(username);
|
2017-07-16 22:37:13 +07:00
|
|
|
})
|
2017-10-07 18:46:19 +07:00
|
|
|
.then(function (groupsAndEmails: GroupsAndEmails) {
|
2017-09-03 03:38:26 +07:00
|
|
|
return BluebirdPromise.resolve(groupsAndEmails);
|
2017-07-16 22:37:13 +07:00
|
|
|
})
|
|
|
|
.error(function (err: Error) {
|
|
|
|
return BluebirdPromise.reject(new exceptions.LdapError(err.message));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|