-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (83 loc) · 2.84 KB
/
index.js
File metadata and controls
98 lines (83 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var ldap = require('ldapjs');
var ldap_port = parseInt(process.env.STACKATO_HARBOR_LDAP_PORT) || 1389;
var ldap_host = '0.0.0.0';
var server = ldap.createServer();
var SUFFIX = 'dc=example, dc=com';
var userdb = {
'stackato' : { samaccountname: 'stackato', email: '[email protected]', password: 'stackato' },
'testuser1' : { samaccountname: 'testuser1', email: '[email protected]', password: 'stackato' },
'testuser2' : { samaccountname: 'testuser2', email: '[email protected]', password: 'stackato' },
'testuser3' : { samaccountname: 'testuser3', email: '[email protected]', password: 'stackato' },
};
var groupdb = [
{cn:'stackato-admin', objectclass: 'posixgroup', memberuid:'stackato' },
{cn:'stackato-user', objectclass: 'posixgroup', memberuid:'testuser1' },
{cn:'stackato-user', objectclass: 'posixgroup', memberuid:'testuser2' },
{cn:'some-other-group', objectclass: 'posixgroup', memberuid:'testuser2' },
{cn:'some-other-group', objectclass: 'posixgroup', memberuid:'testuser3' }
];
var user = {};
server.search(SUFFIX, function(req, res, next) {
user.dn = req.dn.toString();
// check if this is a user query
Object.keys(userdb).forEach( function(uid){
if (req.filter.matches(userdb[uid])){
user.attributes = userdb[uid];
console.log('found user ' + uid);
res.send(user);
res.end();
return next();
}
});
// check if this is a group query
groupdb.forEach( function(group) {
if (req.filter.matches( group ) ) {
console.log("Group query matched " + group.cn );
var group_resp = {}
group_resp.dn = req.dn.toString();
group_resp.attributes = group;
res.send(group_resp);
res.end();
return next();
}
});
res.end();
return next();
});
server.bind(SUFFIX, function(req, res, next) {
if(req.credentials !== user.attributes.password)
return next(new ldap.InvalidCredentialsError());
console.log( user.attributes.samaccountname + ' logged in');
user = {};
res.end();
return next();
});
var service_info;
if(process.env.STACKATO_SERVICES){
var services = JSON.parse(process.env.STACKATO_SERVICES);
service_info = services['ldap-port']['protocol'][0] +
"://" + services['ldap-port']['hostname'] +
":" + services['ldap-port']['port'];
}
server.listen(ldap_port, ldap_host, function() {
console.log('LDAP server listening at:');
if(service_info){
console.log(service_info);
} else {
console.log('LDAP server listening at %s', server.url);
}
});
// for Stackato
if(process.env.PORT){
var http_port = parseInt(process.env.PORT);
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200);
res.write('LDAP Server OK\n');
if(service_info){
res.write("LDAP Server:");
res.write(service_info);
}
res.end();
}).listen(http_port);
}