2014-02-12 42 views
0

處定義我正在嘗試在我們正在開發的Intranet應用程序上創建LDAP身份驗證的原型,這裏是我編寫的一些示例代碼,用於演示它是否可行。但是,我遇到了錯誤,我無法在任何地方在線找到解決方案。node-ldapauth ReferenceError:err未在

以下是完整的錯誤,我得到:

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
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僅適用於構造函數,請。

+0

我不知道。我應該如何處理登錄失敗呢? – Tejas

+0

@Tejas:見編輯。 – Ryan

+0

此外,我使用http://stackoverflow.com/questions/20525070/why-is-node-ldapauth-so-slow-even-when-cached作爲編寫此原型的參考之一。 – Tejas