我设计了一个用LDAP系统替换OAuth身份验证系统的解决方案。我正在撰写此博客,因为这对其他开发人员在使用强循环OAuth工具和其他身份验证系统时会有所帮助。
主要是,我对项目“loopback-component-oauth2”的库文件进行了更改,该文件位于强网关包中。
Location of file: loopback-component-oauth2\lib
file name: oauth2-loopback.js
Method: userLogin
Signature: req, res, callback
下面的代码解释了如何更改方法“userLogin”defination:
function userLogin(req, res, next) {
var OPTS = {
server: {
url: '<>',
bindDn: 'CN=<>,CN=<>,DC=<>,DC=<>,DC=<>',
bindCredentials: '<>',
searchBase: 'DC=<>,DC=<>,DC=<>',
searchFilter: '(<>={{<>}})'
}
};
var app = express();
passport.use(new LdapStrategy(OPTS));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(passport.initialize());
passport.authenticate('ldapauth', {
session: false
}, function(err, user, req) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (!user) {
return false;
} else {
return next(null, user);
}
})(req, res, next);
}