<commit_before>'use strict';

var _ = require('lodash');
var method = require('../../waterlock-ldap-auth');
var ldap = method.ldap;
var connection = method.connection;

/**
 * Login action
 */
module.exports = function(req, res) {
  
  var params = req.params.all();
  
  if (typeof params.username === 'undefined' || typeof params.password === 'undefined') {
    waterlock.cycle.loginFailure(req, res, null, {error: 'Invalid username or password'});
  } else {
    var auth = new ldap(connection);
    console.log("User attributes: %j", waterlock.User.attributes);
    auth.authenticate(params.username, params.password, function(err, user) {
      if (err) {
        waterlock.cycle.loginFailure(req, res, user, {error: 'Invalid username or password'});
      } else {
        var criteria = {
          username: params.username
        };
        var attr = {
          username: params.username,
          entryUUID: user.entryUUID,
          dn: user.dn
        };
        _.forOwn(method.attributes, function(fields, oid) {
          _.forOwn(fields, function(definition, name) {
            if (user.hasOwnProperty(oid)) {
              attr[name] = user[oid];
            }
          });
        });
        waterlock.engine.findOrCreateAuth(criteria, attr, function(err, user) {
          if (err) {
            waterlock.cycle.loginFailure(req, res, null, {error: 'user not found'});
          } else {
            waterlock.cycle.loginSuccess(req, res, user);
          }
        });
      }
    });
  }
};
<commit_msg>Remove debugging statement.<commit_after>'use strict';

var _ = require('lodash');
var method = require('../../waterlock-ldap-auth');
var ldap = method.ldap;
var connection = method.connection;

/**
 * Login action
 */
module.exports = function(req, res) {
  
  var params = req.params.all();
  
  if (typeof params.username === 'undefined' || typeof params.password === 'undefined') {
    waterlock.cycle.loginFailure(req, res, null, {error: 'Invalid username or password'});
  } else {
    var auth = new ldap(connection);
    auth.authenticate(params.username, params.password, function(err, user) {
      if (err) {
        waterlock.cycle.loginFailure(req, res, user, {error: 'Invalid username or password'});
      } else {
        var criteria = {
          username: params.username
        };
        var attr = {
          username: params.username,
          entryUUID: user.entryUUID,
          dn: user.dn
        };
        _.forOwn(method.attributes, function(fields, oid) {
          _.forOwn(fields, function(definition, name) {
            if (user.hasOwnProperty(oid)) {
              attr[name] = user[oid];
            }
          });
        });
        waterlock.engine.findOrCreateAuth(criteria, attr, function(err, user) {
          if (err) {
            waterlock.cycle.loginFailure(req, res, null, {error: 'user not found'});
          } else {
            waterlock.cycle.loginSuccess(req, res, user);
          }
        });
      }
    });
  }
};

{'fraction_non_alphanumeric': 0.14423396477234962, 'fraction_numerical': 0.0, 'mean_word_length': 2.2611050920910074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3723752', 'n_tokens_mistral': 877, 'n_tokens_neox': 875, 'n_words': 196}