All files / node-activedirectory/lib/services service.getRootDSE.js

11.63% Statements 5/43
0% Branches 0/28
0% Functions 0/7
12.2% Lines 5/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 781x 1x 1x 1x                                                                                                                                                   1x
const _                                 = require('underscore');
const createClient                      = require('./internal/service.createClient');
const hasEvents                         = require('./internal/service.hasEvents');
const log                               = require('./internal/service.log');
/**
 * Retrieves the root DSE for the specified url
 *
 * @public
 * @param {String} url The url to retrieve the root DSE for.
 * @param {Array} [attributes] The optional list of attributes to retrieve. Returns all if not specified.
 * @param {Function} callback The callback to execute when the getRootDSE is completed. callback(err: {Object}, result: {Object})
 */
function getRootDSE(url, attributes, callback) {
    var self = this;
    return new Promise((resolve, reject) => {
        if (typeof (attributes) === 'function') {
            callback = attributes;
            attributes = undefined;
        }
        if (typeof (url) === 'function') {
            callback = url;
            url = self.url || self.opts.url;
            attributes = undefined;
        }
        if (!url) throw new Error('No url specified for the root DSE. Please specify an ldap url in the following format: "ldap://yourdomain.com:389".');
        log.trace('getRootDSE(%s,%j)', url, attributes || ['*']);
    
        /**
         * Inline function handle connection and result errors.
         *
         * @private
         **/
        function onClientError(err) {
            // Ignore ECONNRESET errors
            if ((err || {}).errno !== 'ECONNRESET') {
                log.error('An unhandled error occured when searching for the root DSE at "%s". Error: %j', url, err);
                if (hasEvents.call(self, 'error')) self.emit('error', err);
                reject(err);
            }
        }
    
        var client = createClient.call(this, url);
        client.on('error', onClientError);
        // Anonymous bind
        client.bind('', '', function (err) {
            if (err) {
                log.error('Anonymous bind to "%s" failed. Error: %s', url, err);
                if(callback){
                    callback(err, false);
                }
                return reject(err);
            }
    
            client.search('', { scope: 'base', attributes: attributes || ['*'], filter: '(objectClass=*)' }, function (err, result) {
                if (err) {
                    log.error('Root DSE search failed for "%s". Error: %s', url, err);
                    if(callback){
                        callback(err);
                    }
                    return reject(err);
                }
    
                result.on('error', onClientError);
                result.on('end', function (result) {
                    client.unbind();
                });
                result.on('searchEntry', function (entry) {
                    if(callback){
                        callback(null, _.omit(entry.object, 'controls'));
                    }
                    return resolve(_.omit(entry.object, 'controls'));
                });
            });
        });
    });    
};
 
module.exports = getRootDSE;