-
Notifications
You must be signed in to change notification settings - Fork 20
/
ep_ldapauth.js
202 lines (183 loc) · 8.06 KB
/
ep_ldapauth.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2013 Andrew Grimberg <[email protected]>
//
// @License GPL-2.0 <http://spdx.org/licenses/GPL-2.0>
//var LdapAuth = require('ldapauth');
var MyLdapAuth = require('./lib/MyLdapAuth.js');
var util = require('util');
var fs = require('fs');
var ERR = require('async-stacktrace');
var settings = require('ep_etherpad-lite/node/utils/Settings');
var authorManager = require('ep_etherpad-lite/node/db/AuthorManager');
function ldapauthSetUsername(token, username) {
console.debug('ep_ldapauth.ldapauthSetUsername: getting authorid for token %s', token);
let author = authorManager.getAuthor4Token(token)
author.then(
function(author_id) {
console.debug('ep_ldapauth.ldapauthSetUsername: have authorid %s, setting username to "%s"', author_id, username);
authorManager.setAuthorName(author_id, username);
});
return;
}
exports.authenticate = function(hook_name, context, cb) {
console.debug('ep_ldapauth.authenticate');
// If auth headers are present use them to authenticate
if (context.req.headers.authorization && context.req.headers.authorization.search('Basic ') === 0) {
var userpass = new Buffer(context.req.headers.authorization.split(' ')[1], 'base64').toString().split(":");
var username = userpass.shift();
var password = userpass.join(':');
var express_sid = context.req.sessionID;
var myLdapAuthOpts = {
url: settings.users.ldapauth.url,
adminDn: settings.users.ldapauth.searchDN,
adminPassword: settings.users.ldapauth.searchPWD,
searchBase: settings.users.ldapauth.accountBase,
searchFilter: settings.users.ldapauth.accountPattern,
cache: true
};
if (typeof(settings.users.ldapauth.tls_ca_file) !== 'undefined') {
myLdapAuthOpts.tls_ca = fs.readFileSync(settings.users.ldapauth.tls_ca_file);
}
var authenticateLDAP = new MyLdapAuth(myLdapAuthOpts);
// Attempt to authenticate the user
authenticateLDAP.authenticate(username, password, function(err, user) {
if (err) {
console.error('ep_ldapauth.authenticate: LDAP auth error: %s', err);
authenticateLDAP.close(function (err) {
if (err) {
console.error('ep_ldapauth.authenticate: LDAP close error: %s', err);
}
});
authenticateLDAP = null;
return cb([false]);
}
// User authenticated, save off some information needed for authorization
context.req.session.user = { username: username };
if ('displayNameAttribute' in settings.users.ldapauth && settings.users.ldapauth.displayNameAttribute in user) {
context.req.session.user['displayName']=user[settings.users.ldapauth.displayNameAttribute];
}
else if ('cn' in user) {
context.req.session.user['displayName']=user.cn;
}
if (settings.users.ldapauth.groupAttributeIsDN) {
context.req.session.user.userDN = user.dn;
}
settings.globalUserName = username;
console.debug('ep_ldapauth.authenticate: deferring setting of username [%s] to CLIENT_READY for express_sid = %s', username, express_sid);
authenticateLDAP.close(function (err) {
if (err) {
console.error('ep_ldapauth.authenticate: LDAP close error: %s', err);
}
});
authenticateLDAP = null;
console.debug('ep_ldapauth.authenticate: successful authentication');
return cb([true]);
});
} else {
console.debug('ep_ldapauth.authenticate: failed authentication no auth headers');
return cb([false]);
}
};
exports.authorize = function(hook_name, context, cb) {
console.debug('ep_ldapauth.authorize');
if(settings.users.ldapauth.anonymousReadonly &&
/^\/(p\/r\..{16}|locales.json|static|javascripts|pluginfw|favicon.ico)/.test(context.resource)) {
console.debug('ep_ldapauth.authorize.anonymous: authorizing static path %s', context.resource);
return cb([true]);
}
userDN = null;
if (typeof(context.req.session.user) !== 'undefined' &&
typeof(context.req.session.user.username) !== 'undefined') {
username = context.req.session.user.username;
if (typeof(context.req.session.user.userDN) !== 'undefined') {
userDN = context.req.session.user.userDN;
}
} else {
console.debug('ep_ldapauth.authorize: no username in user object');
return cb([false]);
}
if (/^\/(static|javascripts|pluginfw|favicon.ico|api)/.test(context.resource)) {
console.debug('ep_ldapauth.authorize: authorizing static path %s', context.resource);
return cb([true]);
} else if (context.resource.match(/^\/admin/)) {
console.debug('ep_ldapauth.authorize: attempting to authorize along administrative path %s', context.resource);
var myLdapAuthOpts = {
url: settings.users.ldapauth.url,
adminDn: settings.users.ldapauth.searchDN,
adminPassword: settings.users.ldapauth.searchPWD,
searchBase: settings.users.ldapauth.accountBase,
searchFilter: settings.users.ldapauth.accountPattern,
groupSearchBase: settings.users.ldapauth.groupSearchBase,
groupAttribute: settings.users.ldapauth.groupAttribute,
groupAttributeIsDN: settings.users.ldapauth.groupAttributeIsDN,
searchScope: settings.users.ldapauth.searchScope,
groupSearch: settings.users.ldapauth.groupSearch,
cache: true
};
if (typeof(settings.users.ldapauth.tls_ca_file) !== 'undefined') {
myLdapAuthOpts.tls_ca = fs.readFileSync(settings.users.ldapauth.tls_ca_file);
}
var authorizeLDAP = new MyLdapAuth(myLdapAuthOpts);
authorizeLDAP.groupsearch(username, userDN, function(err, groups) {
if (err) {
console.error('ep_ldapauth.authorize: LDAP groupsearch error: %s', err);
authorizeLDAP.close(function (err) {
if (err) {
console.error('ep_ldapauth.authorize: LDAP close error: %s', err);
}
});
authorizeLDAP = null;
return cb([false]);
}
// We've recieved back group(s) that the user matches
// Given our current auth scheme (only checking on admin) we'll auth
if (groups) {
context.req.session.user.is_admin = true;
authorizeLDAP.close(function (err) {
if (err) {
console.error('ep_ldapauth.authorize: LDAP close error: %s', err);
}
});
authorizeLDAP = null;
console.debug('ep_ldapauth.authorize: successful authorization');
return cb([true]);
} else {
context.req.session.user.is_admin = false;
authorizeLDAP.close(function (err) {
if (err) {
console.error('ep_ldapauth.authorize: LDAP close error: %s', err);
}
});
authorizeLDAP = null;
console.debug('ep_ldapauth.authorize: failed authorization');
return cb([false]);
}
});
} else {
console.debug('ep_ldapauth.authorize: passing authorize along for path %s', context.resource);
return cb([false]);
}
};
exports.handleMessage = function(hook_name, context, cb) {
console.debug("ep_ldapauth.handleMessage");
if ( context.message.type == "CLIENT_READY" ) {
if (!context.message.token) {
console.debug('ep_ldapauth.handleMessage: intercepted CLIENT_READY message has no token!');
} else {
var client_id = context.client.id;
if ('user' in context.client.client.request.session) {
var displayName = context.client.client.request.session.user.displayName;
if(settings.users.ldapauth.anonymousReadonly && !displayName) displayName = 'guest';
console.debug('ep_ldapauth.handleMessage: intercepted CLIENT_READY message for client_id = %s, setting username for token %s to %s', client_id, context.message.token, displayName);
ldapauthSetUsername(context.message.token, displayName);
}
else {
console.debug('ep_ldapauth.handleMessage: intercepted CLIENT_READY but user does have displayName !');
}
}
} else if ( context.message.type == "COLLABROOM" && context.message.data.type == "USERINFO_UPDATE" ) {
console.debug('ep_ldapauth.handleMessage: intercepted USERINFO_UPDATE and dropping it!');
return cb([null]);
}
return cb([context.message]);
};
// vim: sw=2 ts=2 sts=2 et ai