-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
194 lines (147 loc) · 4.24 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const path = require('path');
const fs = require('fs');
const https = require('https');
const {randomString} = require('./utility.js');
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json')));
if (!config.debug) {
process.env.NODE_ENV = 'production';
}
const tokens = new (require('./tokens.js'))(path.join(__dirname, 'tokens.json'));
const ldap = new (require('./ldap.js'))(config.servers, config.suffix, config.base);
const express = require('express');
const redirect = express();
redirect.use((req, res) => {
res.redirect(formatUrl(req.hostname, req.url));
});
const app = express();
app.use(express.static('static'));
app.use(express.urlencoded({extended: true}));
app.set('view engine', 'pug');
app.get('/reset', (req, res) => {
res.render('reset', {form: {}});
});
app.post('/reset', (req, res, next) => {
let username = req.body.username;
let password = req.body.password;
let targetUsername = req.body['target-username'];
let showError = renderError.bind(null, res, 'reset', {form: req.body}, next);
if (username.length === 0 || password.length === 0 || targetUsername.length === 0) {
showError('emptyField');
return;
}
let targetPassword = config.passwordPrefix + randomString(config.passwordLength);
ldap.resetPassword(username, password, targetUsername, targetPassword, err => {
if (err) {
showError(err);
return;
}
let token = tokens.add(targetUsername, targetPassword, err => {
if (err) {
showError(err);
return;
}
token = formatUrl(req.hostname, '/' + token);
res.render('success', {username: targetUsername, token: token});
});
});
});
app.get('/:tokenId?', (req, res, next) => {
let tokenId = req.params.tokenId;
if (tokenId && !tokens.get(tokenId)) {
next();
return;
}
res.render('change', {token: tokenId, form: {}});
});
app.post('/:tokenId?', (req, res, next) => {
let tokenId = req.params.tokenId;
let username = req.body.username;
let password = req.body.password;
if (tokenId) {
token = tokens.get(tokenId);
if (!token) {
next();
return;
}
username = token.username;
password = token.password;
}
let newPassword = req.body['new-password'];
let newPasswordConfirm = req.body['new-password-confirm'];
let showError = renderError.bind(null, res, 'change', {token: tokenId, form: req.body}, next);
if (username.length === 0 || password.length === 0 || newPassword.length === 0 || newPasswordConfirm.length === 0) {
showError('emptyField');
return;
}
if (newPassword !== newPasswordConfirm) {
showError('passwordMismatch');
return;
}
ldap.changePassword(username, password, newPassword, err => {
if (err) {
showError(err);
return;
}
tokens.remove(tokenId, err => {
if (err) {
showError(err);
return;
}
res.render('success');
});
});
});
app.use((req, res) => {
res.status(404);
res.render('error', {code: 404});
});
app.use((err, req, res, next) => {
res.status(500);
res.render('error', {code: 500});
console.log(err);
});
if (config.httpsPort) {
redirect.listen(config.port, () => {
let options = {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'))
};
https.createServer(options, app).listen(config.httpsPort);
});
} else {
app.listen(config.port);
}
function formatUrl(hostname, suffix) {
let protocol = 'http';
let port = '';
if (config.httpsPort) {
protocol = 'https';
if (config.httpsPort !== 443) {
port = ':' + config.httpsPort;
}
} else if (config.port !== 80) {
port = ':' + config.port;
}
let url = protocol + '://' + hostname + port
if (config.baseUrl) {
url = config.baseUrl;
}
return url + suffix;
}
function renderError(res, view, locals, next, err) {
const messages = {
emptyField: 'All fields must be completed',
passwordMismatch: 'Confirmation password must match new password',
InvalidCredentialsError: 'Incorrect username or password',
UserNotFoundError: 'The user was not found',
InsufficientAccessRightsError: 'You do not have permission',
ConstraintViolationError: 'Password must meet complexity requirements'
};
let message = (typeof err === 'string') ? messages[err] : messages[err.name];
if (message) {
locals.error = message;
res.render(view, locals);
return;
}
next(err);
}