This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
189 lines (160 loc) · 5.61 KB
/
server.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
var // Core server
express = require('express'),
app = express(),
bodyParser = require('body-parser'),
// Cryptography
crypto = require('crypto'),
scrypt = require('scrypt'),
// Helpers
Promise = require('bluebird'),
db = require('./db'),
User = db.User,
r = db.r,
config = require('./config'),
// Clustering
os = require('os'),
cluster = require('cluster'),
numCPUtoFork = os.cpus().length,
// Authentication
passport = require('passport'),
auth = require('./auth')(passport, db);
if (numCPUtoFork >= 12) {
numCPUtoFork = 10;
} else if (numCPUtoFork >= 8) {
numCPUtoFork = numCPUtoFork - 2;
} else if (numCPUtoFork > 1) {
numCPUtoFork--;
}
if (cluster.isWorker) {
// promisify functions here
crypto.randomBytes = Promise.promisify(crypto.randomBytes);
crypto.pbkdf2 = Promise.promisify(crypto.pbkdf2);
scrypt.passwordHash = Promise.promisify(scrypt.passwordHash);
app.use(bodyParser.json());
app.use(passport.initialize());
app.route('/').get(function(req, res) {
res.json({
main: 'It works!'
});
});
app.route('/signin')
// Sign in the user
.post(function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
// 500 (Internal Server Error)
return res.send(500, err);
}
if (!user) {
// Credentials not correct, send 401 (Unauthorized)
return res.send(401, info);
}
console.log('Signing in', user);
req.logIn(user, function(err) {
if (err) {
// 500 (Internal Server Error)
return res.send(500, err);
}
res.send({
username: user.displayUsername
});
});
})(req, res, next);
});
app.route('/users')
// Create a new user
.post(function(req, res) {
var newUser;
User.getAll(req.body.username.toLowerCase(), {
index: 'username'
}).run().then(function(dupeUser) {
if (dupeUser.length) {
console.log('Username "%s" already exits', req.body.username);
// Username already exits, send 409 (Conflict)
res.status(409);
return res.send({
message: 'Can not use that username'
});
}
newUser = {
displayUsername: req.body.username,
username: req.body.username.toLowerCase(),
passIter: config.crypt.iterations,
passHashSize: config.crypt.hashSize,
email: req.body.email
};
return crypto.randomBytes(config.crypt.saltLength);
}).then(function(randomBytes) {
var salt = randomBytes.toString('base64');
newUser.passSalt = salt;
return crypto.pbkdf2(req.body.clientHash, newUser.passSalt,
config.crypt.iterations, config.crypt.hashSize);
}).then(function(pbkdf2Hash) {
pbkdf2Hash = pbkdf2Hash.toString('base64');
return scrypt.passwordHash(pbkdf2Hash, config.crypt.scryptParameters);
}).then(function(finalHash) {
newUser.passHash = finalHash;
console.dir(newUser);
var user = new User(newUser);
return user.save();
}).then(function(user) {
console.log('User %s added to the database.', user.displayUsername);
res.status(201);
}, function(err) {
console.log('User %s NOT added to the database.', req.body.username);
console.log(err.toString());
res.status(400);
res.send({
message: err.toString()
});
});
});
app.route('/users/:username')
.delete(auth.ensureAuthenticated, function(req, res) {
var username = req.params.username;
// Only allow people to delete themselves
if (username === req.user.username) { // this needs to change when we use tokens (ask janka)
User.getAll(username, {
index: 'username'
}).run().then(function(user) {
user[0].delete().then(function(user) {
console.log('User %s removed from the database.', user.displayUsername);
res.status(204);
res.send();
});
});
} else {
// Trying to delete other user, send 401 (Unauthorized)
res.status(401);
res.send({
message: 'Not authorized'
});
}
});
app.listen(config.port);
} else {
// master process runs this
var restarter = 0,
spawnedForks = 0;
for (var i = 0; i < numCPUtoFork; i++) {
cluster.fork();
}
console.log('App listening on port %d', config.port);
// Cluster helpers
cluster.on('exit', function(worker, code, signal) {
// when a worker dies
console.log('worker %d died (%s).', worker.process.pid, signal || code);
if (restarter) {
restarter--;
console.log('Replacing a dead fork...');
cluster.fork();
}
});
cluster.on('online', function(worker) {
// when a worker is successfully spawned
spawnedForks++;
if (spawnedForks > numCPUtoFork) {
console.log("Replacement successful.");
}
});
}