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

41.18% Statements 14/34
18.18% Branches 4/22
50% Functions 2/4
42.42% Lines 14/33

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    1x 1x 1x 1x 1x                     2x 2x 2x     2x                       2x                       2x 2x 2x                                     1x
 
 
const util                              = require('util');
const createClient                      = require('./internal/service.createClient');
const hasEvents                         = require('./internal/service.hasEvents');
const log                               = require('./internal/service.log');
const isPasswordLoggingEnabled          = false;
 
/**
 * Attempts to authenticate the specified username / password combination.
 *
 * @public
 * @param {String} username The username to authenticate.
 * @param {String} password The password to use for authentication.	
 * @param {Function} callback The callback to execute when the authenication is completed. callback(err: {Object}, authenticated: {Boolean})
 */
function authenticate(username, password, callback) {
    var self = this;
    return new Promise((resolve, reject) => {
        log.trace('authenticate(%j,%s)', username, isPasswordLoggingEnabled ? password : '********');
 
        // Skip authentication if an empty username or password is provided.
        Iif ((!username) || (!password)) {
            var err = {
                'code': 0x31,
                'errno': 'LDAP_INVALID_CREDENTIALS',
                'description': 'The supplied credential is invalid'
            };
            if(callback){
                callback(err, false);
            }
            return reject(err);
        }
 
        var errorHandled = false;
        function handleError(err) {
            if (!errorHandled) {
                errorHandled = true;
                if (hasEvents.call(self, 'error')) self.emit('error', err);
                if(callback){
                    callback(err, null);
                }
                return reject(err);
            }
        }
 
        var client = createClient.call(self);
        client.on('error', handleError);
        client.bind(username, password, function (err) {
            client.unbind();
            var message = util.format('Authentication %s for "%s" as "%s" (password: "%s")',
                err ? 'failed' : 'succeeded',
                self.opts.url, username, isPasswordLoggingEnabled ? password : '********');
            if (err) {
                log.warn('%s. Error: %s', message, err);
                return (handleError(err));
            }
 
            log.info(message);
            if(callback){
                callback(null, true);
            }
            return resolve(true);
        });
    });    
};
 
module.exports = authenticate;