-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
99 lines (82 loc) · 2.95 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
var settings = require('./config/config.json');
var bodyParser = require('body-parser');
var jwt = require('jwt-simple');
var moment = require('moment');
var LdapAuth = require('ldapauth-fork');
var Promise = require('promise');
app = require('express')();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(require('cors')());
var auth = new LdapAuth(settings.ldap);
app.set('jwtTokenSecret', settings.jwt.secret);
var authenticate = function (username, password) {
return new Promise(function (resolve, reject) {
auth.authenticate(username, password, function (err, user) {
if(err)
reject(err);
else if (!user)
reject();
else
resolve(user);
});
});
};
app.post('/authenticate', function (req, res) {
if(req.body.username && req.body.password) {
authenticate(req.body.username, req.body.password)
.then(function(user) {
var expires = parseInt(moment().add(2, 'days').format("X"));
var token = jwt.encode({
exp: expires,
user_name: user.uid,
full_name: user.cn,
mail: user.mail
}, app.get('jwtTokenSecret'));
res.json({token: token, full_name: user.cn});
})
.catch(function (err) {
// Ldap reconnect config needs to be set to true to reliably
// land in this catch when the connection to the ldap server goes away.
// REF: https://github.com/vesse/node-ldapauth-fork/issues/23#issuecomment-154487871
console.log(err);
if (err.name === 'InvalidCredentialsError' || (typeof err === 'string' && err.match(/no such user/i)) ) {
res.status(401).send({ error: 'Wrong user or password'});
} else {
// ldapauth-fork or underlying connections may be in an unusable state.
// Reconnect option does re-establish the connections, but will not
// re-bind. Create a new instance of LdapAuth.
// REF: https://github.com/vesse/node-ldapauth-fork/issues/23
// REF: https://github.com/mcavage/node-ldapjs/issues/318
res.status(500).send({ error: 'Unexpected Error'});
auth = new LdapAuth(settings.ldap);
}
});
} else {
res.status(400).send({error: 'No username or password supplied'});
}
});
app.post('/verify', function (req, res) {
var token = req.body.token;
if (token) {
try {
var decoded = jwt.decode(token, app.get('jwtTokenSecret'));
if (decoded.exp <= parseInt(moment().format("X"))) {
res.status(400).send({ error: 'Access token has expired'});
} else {
res.json(decoded);
}
} catch (err) {
res.status(500).send({ error: 'Access token could not be decoded'});
}
} else {
res.status(400).send({ error: 'Access token is missing'});
}
});
var port = (process.env.PORT || 3000);
app.listen(port, function() {
console.log('Listening on port: ' + port);
if (typeof settings.ldap.reconnect === 'undefined' || settings.ldap.reconnect === null || settings.ldap.reconnect === false) {
console.warn('WARN: This service may become unresponsive when ldap reconnect is not configured.')
}
});