node-ldapauth ReferenceError:未定义err

时间:2014-02-12 04:17:08

标签: javascript node.js express

我正在尝试在我们正在开发的内部网应用程序上对LDAP身份验证进行原型设计,这里有一些示例代码,我已经编写过它以证明它是否可行。但是,我收到了错误,我无法在网上找到任何解决方案。

以下是我得到的完整错误:

ReferenceError: err is not defined at /Users/tdeshpande/Dropbox/NodeAuth/app.js:32:54 at 
Object.handle (/Users/tdeshpande/node_modules/express/node_modules/connect/lib/middleware
/basicAuth.js:89:7) at next (/Users/tdeshpande/node_modules/express/node_modules/connect
/lib/proto.js:193:15) at Object.expressInit [as handle] (/Users/tdeshpande/node_modules
/express/lib/middleware.js:30:5) at next (/Users/tdeshpande/node_modules/express
/node_modules/connect/lib/proto.js:193:15) at Object.query [as handle] (/Users/tdeshpande
/node_modules/express/node_modules/connect/lib/middleware/query.js:45:5) at next (/Users
/tdeshpande/node_modules/express/node_modules/connect/lib/proto.js:193:15) at 
Function.app.handle (/Users/tdeshpande/node_modules/express/node_modules/connect
/lib/proto.js:201:3) at Server.app (/Users/tdeshpande/node_modules/express/node_modules
/connect/lib/connect.js:65:37) at Server.EventEmitter.emit (events.js:98:17)

这是我的代码

var connect = require('connect');
var LdapAuth = require('ldapauth');
var express = require('express');
var app = express();

// Config from a .json or .ini file or whatever.
var config = {

    url: 'ldap://intranetldapserver:389',
    adminDn: 'userName',
    adminPassword: 'secret',
    searchBase: 'dc=corp,dc=company,dc=com, ou=People',
    searchFilter: '(&(objectcategory=person)(samaccountname=*))',
    searchAttributes: ['samaccountname']

};

var ldap = new LdapAuth(config);

var LdapAuthfunc = function(err, user) {
    if (err) {
        console.log("LDAP Auth error: %s", err);
        callback(err);
    } 
    else {
        callback(err, user);
    }
}

//Authenticator
app.use(express.basicAuth(function (username, password, callback) {
  ldap.authenticate(username, password, LdapAuthfunc(err, user));
  }));


// Basic Authenticator
// app.use(express.basicAuth('testUser', 'testPassword'));



app.get('/', function(req, res) {
 res.send('Test Completed');
});

app.listen(8080);

有人能告诉我我做错了吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

app.use(express.basicAuth(function (username, password, callback) {
  ldap.authenticate(username, password, LdapAuthfunc(err, user));
}));

你看到范围err了吗? (或者user就此而言?)

我想你可能只想要:

ldap.authenticate(username, password, LdapAuthfunc);

然后,在此之后,callback不在范围内。你可能想绑定那个或那个:

function ldapAuth(callback, err, user) {
    if (err) {
        console.log("LDAP Auth error: %s", err);
        callback(err);
    } 
    else {
        callback(err, user);
    }
}

// Authenticator
app.use(express.basicAuth(function (username, password, callback) {
    ldap.authenticate(username, password, ldapAuth.bind(null, callback));
}));

选择jsHint

<子> P.S。仅适用于构造函数的PascalCase。