Update ldap.js

This commit is contained in:
Thiago Fernando 2019-05-10 14:56:07 -03:00 committed by GitHub
parent ce0473480b
commit fd1af07e43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ import util from 'util';
import Bunyan from 'bunyan'; import Bunyan from 'bunyan';
import {log_debug, log_info, log_warn, log_error} from './logger'; import {log_debug, log_info, log_warn, log_error} from './logger';
export default class LDAP { export default class LDAP {
constructor() { constructor() {
this.ldapjs = ldapjs; this.ldapjs = ldapjs;
@ -25,6 +26,8 @@ export default class LDAP {
Authentication_Fallback : this.constructor.settings_get('LDAP_LOGIN_FALLBACK'), Authentication_Fallback : this.constructor.settings_get('LDAP_LOGIN_FALLBACK'),
BaseDN : this.constructor.settings_get('LDAP_BASEDN'), BaseDN : this.constructor.settings_get('LDAP_BASEDN'),
Internal_Log_Level : this.constructor.settings_get('INTERNAL_LOG_LEVEL'), Internal_Log_Level : this.constructor.settings_get('INTERNAL_LOG_LEVEL'),
User_Authentication : this.constructor.settings_get('LDAP_USER_AUTHENTICATION'),
User_Attributes : this.constructor.settings_get('LDAP_USER_ATTRIBUTES'),
User_Search_Filter : this.constructor.settings_get('LDAP_USER_SEARCH_FILTER'), User_Search_Filter : this.constructor.settings_get('LDAP_USER_SEARCH_FILTER'),
User_Search_Scope : this.constructor.settings_get('LDAP_USER_SEARCH_SCOPE'), User_Search_Scope : this.constructor.settings_get('LDAP_USER_SEARCH_SCOPE'),
User_Search_Field : this.constructor.settings_get('LDAP_USER_SEARCH_FIELD'), User_Search_Field : this.constructor.settings_get('LDAP_USER_SEARCH_FIELD'),
@ -52,6 +55,7 @@ export default class LDAP {
log_warn(`Lookup for unset variable: ${name}`); log_warn(`Lookup for unset variable: ${name}`);
} }
} }
connectSync(...args) { connectSync(...args) {
if (!this._connectSync) { if (!this._connectSync) {
this._connectSync = Meteor.wrapAsync(this.connectAsync, this); this._connectSync = Meteor.wrapAsync(this.connectAsync, this);
@ -60,6 +64,7 @@ export default class LDAP {
} }
searchAllSync(...args) { searchAllSync(...args) {
if (!this._searchAllSync) { if (!this._searchAllSync) {
this._searchAllSync = Meteor.wrapAsync(this.searchAllAsync, this); this._searchAllSync = Meteor.wrapAsync(this.searchAllAsync, this);
} }
@ -208,6 +213,25 @@ export default class LDAP {
return `(&${filter.join('')})`; return `(&${filter.join('')})`;
} }
bindUserIfNecessary(username, password) {
if (this.domainBinded === true) {
return;
}
if (!this.options.User_Authentication) {
return;
}
if (!this.options.BaseDN) throw new Error('BaseDN is not provided');
const userDn = `uid=${username},${this.options.BaseDN}`;
this.bindSync(userDn, password);
this.domainBinded = true;
}
bindIfNecessary() { bindIfNecessary() {
if (this.domainBinded === true) { if (this.domainBinded === true) {
return; return;
@ -218,19 +242,21 @@ export default class LDAP {
} }
log_info('Binding UserDN', this.options.Authentication_UserDN); log_info('Binding UserDN', this.options.Authentication_UserDN);
this.bindSync(this.options.Authentication_UserDN, this.options.Authentication_Password); this.bindSync(this.options.Authentication_UserDN, this.options.Authentication_Password);
this.domainBinded = true; this.domainBinded = true;
} }
searchUsersSync(username, page) { searchUsersSync(username, page) {
this.bindIfNecessary(); this.bindIfNecessary();
const searchOptions = { const searchOptions = {
filter : this.getUserFilter(username), filter : this.getUserFilter(username),
scope : this.options.User_Search_Scope || 'sub', scope : this.options.User_Search_Scope || 'sub',
sizeLimit: this.options.Search_Size_Limit, sizeLimit: this.options.Search_Size_Limit,
}; };
if (!!this.options.User_Attributes) searchOptions.attributes = this.options.User_Attributes.split(',');
if (this.options.Search_Page_Size > 0) { if (this.options.Search_Page_Size > 0) {
searchOptions.paged = { searchOptions.paged = {
pageSize : this.options.Search_Page_Size, pageSize : this.options.Search_Page_Size,
@ -430,11 +456,13 @@ export default class LDAP {
log_info(title); log_info(title);
// Force LDAP idle to wait the record processing // Force LDAP idle to wait the record processing
this.client._updateIdle(true); this.client._updateIdle(true);
page(null, entries, {end, next: () => { page(null, entries, {
end, next: () => {
// Reset idle timer // Reset idle timer
this.client._updateIdle(); this.client._updateIdle();
next && next(); next && next();
}}); }
});
}; };
this.client.search(BaseDN, options, (error, res) => { this.client.search(BaseDN, options, (error, res) => {