我是nodejs的新手,只是为了好玩而进行概念验证。
背景: 我有一个用户信息的云目录(如用户名,密码和其他信息)。此云目录可用于仅通过restful API对用户进行身份验证(即,不使用LDAP或JDBC等直接连接)。
目标: 为此云目录构建LDAP接口。首先,我只对身份验证(LDAP绑定)感兴趣。
预期流程:
LDAPClient启动标准LDAP简单BIND请求: 主机:我的nodejs应用程序将运行的主机 端口:1389(我的nodejs app将绑定的端口) 用户名:来自云目录的用户 密码:用户密码
我的NodeJS应用程序收到此请求(我正在使用ldapjs模块)。
// process ldap bind operation
myLdapServer.bind(searchBase, function (bindReq, bindRes, next) {
// bind creds
var userDn = req.dn.toString();
var userPw = req.credentials;
console.log('bind DN: ' + req.dn.toString());
...
...
}
在上面的回调中,我必须使用http.request向云目录发送一个restful API(POST),其中包含我从BIND请求中收到的详细信息(即用户名,密码)。
如果restful api响应状态为200(auth success),那么我必须将成功返回给LDAPClient,否则我必须返回无效凭证错误。
成功:
bindRes.end();
return next();
失败:
Console.log("returning error");
return next(new ldap.InvalidCredentialsError());
问题:
使用NodeJS可以吗?因为上面显示的嵌套请求(在回调中调用REST API)。此外,由于这是一个身份验证操作,这意味着阻塞操作(?)
谢谢, Jatin
更新:
谢谢Klvs,我的解决方案或多或少与您发布的解决方案类似。请查看下面的代码段:
// do the POST call from within callback
var postRequest = https.request(postOptions, function(postResponse) {
console.log("statusCode: ", postResponse.statusCode);
if(postResponse.statusCode!=200) {
console.log("cloud authentication failed: "+postResponse.statusCode);
return next(ldapModule.InvalidCredentialsError());
} else {
postResponse.on('data', function(d) {
console.info('POST result:\n');
process.stdout.write(d);
console.info('\n\nPOST completed');
});
res.end();
return next();
}
});
// write json data
postRequest.write(postData);
postRequest.end();
postRequest.on('error', function(e) {
console.error("postRequest error occured: "+e);
});
成功的身份验证工作正常,但是,身份验证失败根本不会将任何响应发送回LDAPClient。我的客户只是超时而不是显示身份验证失败错误。我确实看到"云身份验证失败了:"节点控制台上的日志消息,这意味着以下语句没有按照我的意图执行:
return next(ldapModule.InvalidCredentialsError());
请注意,当我删除其余的调用等时,上述语句仍然有效,只是将错误返回给客户端。
我错过了什么吗?
谢谢, Jatin
答案 0 :(得分:0)
当然可以在nodejs中使用。如果我了解您想要向服务器发出身份验证请求并使其失败或成功。
const request = require('request')
// process ldap bind operation
myLdapServer.bind(searchBase, function (bindReq, bindRes, next) {
// bind creds
var userDn = req.dn.toString();
var userPw = req.credentials;
console.log('bind DN: ' + req.dn.toString());
request.post({username: userDn, password: userPw}, (err, res, body)=>{
if(err) {
console.log("returning error");
next(new ldap.InvalidCredentialsError());
} else {
bindRes.end();
next();
}
})
}
这就是你要找的东西吗?如果是这样,你只需要习惯于回调。