{"size": 848, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "NathanCong/verdaccio-ldap-node", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "NathanCong/verdaccio-ldap-node", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "NathanCong/verdaccio-ldap-node", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.3157894737, "max_line_length": 79, "alphanum_fraction": 0.6391509434}
/**
 * verdaccio-ldap-node
 */
"use strict";

const LdapClient = require("node-ldap");

function Auth(config, stuff) {
  const _this = Object.create(Auth.prototype);
  _this._users = {};
  // config for this module
  _this._config = config;
  // verdaccio logger
  _this._logger = stuff.logger;
  return _this;
}

Auth.prototype.authenticate = function (username, password, callback) {
  const _this = this;
  // initial LDAP client
  const client = new LdapClient(_this._config);
  // start auth
  client
    .auth(`${username}@xdf.cn`, password)
    .then(function () {
      callback(null, [username]);
    })
    .catch(function (error) {
      _this._logger.warn({ username, error }, `verdaccio-ldap error ${error}`);
      callback(null, false);
    })
    .finally(function () {
      client.disconnect();
    });
};

module.exports = Auth;
