-
Notifications
You must be signed in to change notification settings - Fork 27
/
auth.js
231 lines (193 loc) · 7.16 KB
/
auth.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2015, Joyent, Inc.
*/
var restify = require('restify');
var util = require('util');
var AUTH_TOKEN_HEADER = 'x-adminui-token';
var ACCOUNT_LOCKED_MESSAGE = 'Account is temporarily locked after too many failed auth attempts';
var MAX_LOGIN_ATTEMPTS = 5;
var LOCKED_TIME = 4 * 60 * 60;
var userLockTimes = {};
var userLoginAttempts = {};
var MANTA_ENALBED = require('../etc/config.json')['manta'] || false;
var Auth = module.exports = {};
Auth.optionalAuth = function optionalAuth(req, res, next) {
var token = req.headers[AUTH_TOKEN_HEADER];
if (typeof (token) === 'undefined') {
return next();
}
if (!req.sessions) {
return next(restify.ServiceUnavailableError('Moray unavailable'));
}
req.sessions.get(token, function (err, data) {
if (err) {
return next(err);
}
if (data === null) {
return next();
}
req.session = {
token: token,
data: data
};
return next();
});
return null;
};
Auth.requireRole = function requireRole() {
var roles = Array.prototype.slice.call(arguments);
return function (req, res, next) {
if (!req.session) {
return next(new restify.NotAuthorizedError('Invalid Auth Token'));
}
req.log.debug('requireRole', roles);
req.log.info(req.session.data, 'session data');
var userRoles = req.session.data.roles;
for (var i = 0; i < roles.length; i++) {
var r = roles[i];
if (userRoles.indexOf(r) !== -1) {
return next();
}
}
return next(new restify.ForbiddenError('No Permission'));
};
};
Auth.requireAuth = function requireAuth(req, res, next) {
if (typeof (req.headers[AUTH_TOKEN_HEADER]) === 'undefined') {
return next(new restify.NotAuthorizedError(
util.format('%s not present', AUTH_TOKEN_HEADER)));
}
if (!req.sessions) {
return next(new restify.ServiceUnavailableError('service unavailable (moray)'));
}
var token = req.headers[AUTH_TOKEN_HEADER];
req.sessions.get(token, function (err, data) {
if (err) {
err.message = err.message || '';
return next(new restify.NotAuthorizedError(err.message));
}
if (data === null) {
return next(new restify.NotAuthorizedError('Invalid Auth Token'));
}
req.sessions.touch(token);
req.session = {data: data, token: token};
return next();
});
return null;
};
Auth.getAuth = function getAuth(req, res, next) {
if (req.session) {
res.send(req.session);
return next();
} else {
return next(new restify.NotFoundError('No Session Data'));
}
};
/**
* authenticate
*
* @requires
* - req.sessions
* - req.ufds
*
* @param username {String} username
* @param password {String} password
*/
Auth.authenticate = function authenticate(req, res, next) {
var params = req.body;
var username = params.username;
if (typeof (username) === 'undefined') {
return next(new restify.InvalidArgumentError('Username Required'));
}
if (typeof (params.password) === 'undefined') {
return next(new restify.InvalidArgumentError('Password Required'));
}
if (typeof (req.sessions) === 'undefined') {
return next(new restify.ServiceUnavailableError('Service Unavailable (moray)'));
}
// set login attempts if UFDS master is not available
var isSlaveUfds = req.ufdsSlave.connected && !req.ufdsMaster.connected;
userLoginAttempts[username] = isSlaveUfds && userLoginAttempts[username] || 1;
req.ufds.getUser(username, function (err, user) {
if (err) {
if (err.restCode === 'ResourceNotFound') {
return next(new restify.ConflictError('The credentials provided are invalid'));
} else {
req.log.fatal(err, 'Error while retrieving user via UFDS');
return next(new restify.InternalError('System Error: ' + err.message));
}
}
var lockedTime = userLockTimes[username] || user.pwdaccountlockedtime;
var isLocked = lockedTime && lockedTime > Date.now();
if (isLocked) {
return next(new restify.ConflictError(ACCOUNT_LOCKED_MESSAGE));
}
var pwdEndTime = user.pwdendtime;
if (pwdEndTime && pwdEndTime <= Date.now()) {
return next(new restify.ConflictError('Your password has already expired'));
}
return doAuth();
});
return null;
function doAuth() {
req.ufds.authenticate(username, params.password, function (err, user) {
if (err) {
if (err.restCode === 'InvalidCredentials' || err.restCode === 'ResourceNotFound') {
if (userLoginAttempts[username] > MAX_LOGIN_ATTEMPTS) {
req.log.error({username: username}, ACCOUNT_LOCKED_MESSAGE + ' via slave UFDS');
userLockTimes[username] = Date.now() + LOCKED_TIME;
userLoginAttempts[username] = 0;
}
userLoginAttempts[username] += 1;
return next(new restify.ConflictError('The credentials provided are invalid'));
}
if (err.message === 'not connected') {
return next(new restify.InternalError('Service Unavailable: Not Connected to UFDS.'));
}
req.log.fatal(err, 'Error while attempting user auth via UFDS');
return next(new restify.InternalError('System Error: ' + err.message));
}
if (isSlaveUfds) {
req.log.warn(user, 'User has been authenticated with the slave UFDS');
}
delete userLockTimes[username];
delete userLoginAttempts[username];
if (user.isAdmin() === false && user.isReader() === false) {
return next(new restify.ConflictError('User does not have permission to use Operations Portal.'));
}
req.sessions.create({
roles: user.groups(),
user: user.uuid,
manta: MANTA_ENALBED
}, function (err2, token) {
if (err2) {
req.log.info(err2, 'error creating session');
return next(restify.ConflictError('Error creating session'));
}
res.send({
user: user,
token: token,
adminUuid: req.adminUuid,
manta: MANTA_ENALBED,
dc: req.dc,
roles: user.groups()
});
return next();
});
return null;
});
}
};
Auth.signout = function signout(req, res, next) {
var token = req.headers[AUTH_TOKEN_HEADER];
if (token) {
req.sessions.destroy(token);
}
res.end('');
return next();
};