使用Node.js进行LDAP认证

时间:2018-09-01 22:51:19

标签: angularjs node.js authentication authorization ldapjs

对于我的angularJs应用,我想通过使用nodeJs连接到我公司的LDAP来创建身份验证,以管理用户授权。 这是我当前的代码,出现此错误:

绑定失败 状态:0 结果:{“ messageID”:2,“ protocolOp”:“ LDAPResult”,“状态”:0,“ matchedDN”:“”,“ errorMessage”:“”,“引荐”:[],“控件”:[] }

使用简单的javascript绑定是否有可能? 请问有人样品好吗? 预先谢谢你!

'use strict';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var ldap = require('ldapjs');
const assert = require('assert');

var creds = {
  url: "ldap://*****************",
};

var baseDN = "OU=****,OU=Utilisateurs,OU=****,OU=****,OU=****,DC=***,DC=***,DC=local";
var user = 'LASTNAME Firstname';
var password = 'password';
var fullDN = 'uid=' + user + ',' + baseDN;

var opts = {
  filter: "(&(objectClass=user)(email=*****@***))",
  scope: "sub",
  password: "password",
  client: "email"
};

function authDN(user, baseDN, password, cb) {
	console.log('binding done');
  client.bind(baseDN,password, function (err) {
    client.unbind();
    cb(err === null, err);
    console.log('binding failed');
  });
}

function output(res, err) {				
  if (res) {
    console.log('success');
    console.log(res);
  } else {
    console.log(['Error',err.code, err.dn, err.message ]);
  }
}

var client = ldap.createClient(creds);
authDN(client, 'server instance name', 'password', output);

  //serach method 
  client.search('CN=LASTNAME Firstname,OU=****,OU=Utilisateurs,OU=****,OU=****,OU=****,DC=***,DC=***,DC=local', opts, function(err, res) {
  assert.ifError(err);

  res.on('searchEntry', function(entry) {
    console.log('entry: ' + JSON.stringify(entry.object));
  });
  res.on('searchReference', function(referral) {
    console.log('referral: ' + referral.uris.join());
  });
  //res.put(Context.REFERRAL,"follow");
  res.on('error', function(err) {
    console.error('error: ' + err.message);
  });
  res.on('end', function(result) {
    console.log('status: ' + result.status);
    console.log('result: ' + result);
    process.exit(1);
  });

});

1 个答案:

答案 0 :(得分:0)

好吧,我已将其修复,并将其发布在此处,可能会对某人有所帮助=) 下面的代码绑定并验证。

'use strict';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var ldap = require('ldapjs');
const assert = require('assert');

var creds = {
  url: "ldap://************.local:389",
  bindDN: "DC=***,DC=***,DC=local"
};

var opts = {
  filter: "(&(objectClass=user))",
  scope: "sub",
  client: "*"
};
//binding
function authDN(user, baseDN, password, cb) {
  client.bind(baseDN, password, function (err) {
    client.unbind();
    cb(err === null, err);
  });
}

function output(res, err) {				
  if (res) {
    console.log('success :' + res);
  } else {
    console.log(['Error :',err.code, err.dn, err.message ]);
  }
}

var client = ldap.createClient(creds);
authDN(client, 'username', 'password', output);

//search
  client.search('CN=*** ,OU=****,...,OU=****,DC=***,DC=***,DC=local', opts, function(err, res) {
  assert.ifError(err);

  res.on('searchEntry', function(entry) {
    console.log('entry: ' + JSON.stringify(entry.object));
  });
  res.on('searchReference', function(referral) {
    console.log('referral: ' + referral.uris.join());
  });
  res.on('error', function(err) {
    console.error('error: ' + err.message);
  });
  res.on('end', function(result) {
    console.log('status: ' + result.status);
    console.log('result: ' + result);
    process.exit(1);
  });

});