-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
177 lines (142 loc) · 4.97 KB
/
index.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
'use strict';
const
http = require('http'),
fs = require('fs'),
qs = require('querystring'),
crypto = require('crypto'),
Cookies = require('cookies'),
LdapAuth = require('ldapauth-fork');
const
PORT = 8888,
ALGORITHM = 'aes-256-cbc',
COOKIE_NAME = process.env.COOKIE_NAME || 'LDAP_AUTH',
COOKIE_SECRET = process.env.COOKIE_SECRET || '1xEEgQamX25IhpZ04f2h';
let form = fs.readFileSync('form.html'),
ldapInstances = new Map();
let server = http.createServer(requestHandler);
server.listen(PORT);
console.log('Server is listening');
function requestHandler(request, response) {
let cookies = new Cookies(request, response);
if (request.method === 'GET' && request.url === '/auth-proxy') {
checkAuthData(request, response, cookies);
}
if (request.method === 'GET' && request.url === '/login') {
if (request.headers['content-type'] === 'application/x-www-form-urlencoded' && !cookies.get(COOKIE_NAME)) {
saveAuthData(request, response, cookies);
} else {
showAuthForm(response, cookies);
}
}
}
function checkAuthData(request, response, cookies) {
return auth(request, cookies)
.then(() => {
response.writeHead(200);
response.end();
})
.catch((error) => {
response.writeHead(401);
response.end();
});
}
function saveAuthData(request, response, cookies) {
return getFormData(request)
.then((data) => {
let authData = encrypt([data.username, data.password].join(':'));
cookies.set(COOKIE_NAME, authData, {
httpOnly: true
});
response.writeHead(200);
response.end('<script>window.location.reload()</script>');
})
.catch((error) => {
response.writeHead(500, {'Content-Type': 'application/json'});
response.end(JSON.stringify(error));
});
}
function showAuthForm(response, cookies) {
cookies.set(COOKIE_NAME, 'deleted', {
expires: new Date(0),
httpOnly: true
});
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.end(form);
}
function getFormData(request) {
return new Promise((resolve, reject) => {
let body = '';
request.on('data', (data) => {
body += data;
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6) {
request.connection.destroy();
reject();
}
});
request.on('end', () => {
resolve(qs.parse(body));
});
});
}
function getLdapInstance(request) {
let config = {
url: request.headers['x-ldap-url'],
bindDn: request.headers['x-ldap-binddn'],
bindCredentials: request.headers['x-ldap-bindpass'],
searchBase: request.headers['x-ldap-basedn'],
searchFilter: request.headers['x-ldap-template'] || '(uid={{username}})',
reconnect: true
};
if (request.headers['x-ldap-group-template']) {
config.groupSearchBase = request.headers['x-ldap-group-basedn'];
config.groupSearchFilter = request.headers['x-ldap-group-template'];
config.groupDnProperty = request.headers['x-ldap-group-dnproperty'] || 'uid'
}
let key = JSON.stringify(config);
if (!ldapInstances.has(key)) {
ldapInstances.set(key, new LdapAuth(config));
}
return ldapInstances.get(key);
}
function auth(request, cookies) {
let ldapInstance = getLdapInstance(request);
return new Promise((resolve, reject) => {
if (!cookies.get(COOKIE_NAME)) {
reject(401);
return;
}
let data = decrypt(cookies.get(COOKIE_NAME)).split(':'),
username = data[0],
password = data[1];
ldapInstance.authenticate(username, password, (err, user) => {
if (err) {
return reject(err);
}
if (request.headers['x-ldap-group-template'] && !user._groups.length) {
reject('User is not a member of required group')
} else {
resolve(user);
}
});
});
}
function encrypt(text) {
let iv = crypto.randomBytes(16);
let cipher = crypto.createCipheriv(ALGORITHM, new Buffer.from(COOKIE_SECRET), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
function decrypt(text) {
let textParts = text.split(':');
let iv = new Buffer.from(textParts.shift(), 'hex');
let encryptedText = new Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv(ALGORITHM, new Buffer.from(COOKIE_SECRET), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}