-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-routes.js
519 lines (414 loc) · 14.2 KB
/
json-routes.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import Fiber from 'fibers';
import connect from 'connect';
import connectRoute from 'connect-route';
import { Mongo, MongoInternals } from 'meteor/mongo';
JsonRoutes = {};
WebApp.connectHandlers.use(connect.urlencoded({limit: '50mb'})); //Override default request size
WebApp.connectHandlers.use(connect.json({limit: '50mb'})); //Override default request size
WebApp.connectHandlers.use(connect.query());
// Handler for adding middleware before an endpoint (JsonRoutes.middleWare
// is just for legacy reasons). Also serves as a namespace for middleware
// packages to declare their middleware functions.
JsonRoutes.Middleware = JsonRoutes.middleWare = connect();
WebApp.connectHandlers.use(JsonRoutes.Middleware);
// List of all defined JSON API endpoints
JsonRoutes.routes = [];
// Save reference to router for later
var connectRouter;
// Register as a middleware
WebApp.connectHandlers.use(Meteor.bindEnvironment(connectRoute(function (router) {
connectRouter = router;
})));
// Error middleware must be added last, to catch errors from prior middleware.
// That's why we cache them and then add after startup.
var errorMiddlewares = [];
JsonRoutes.ErrorMiddleware = {
use: function () {
errorMiddlewares.push(arguments);
},
};
/**
* Parses bearer token from the incoming request
*
* Accepts tokens passed via the standard headers, URL query parameters, or
* request body (whichever is found first, in that order).
*
* Stores the token in req.authToken for later middleware.
*
* The header signature is: "Authorization: Bearer <token>".
*
* The query signature is: "?access_token=<token>"
*
* @middleware
*/
JsonRoutes.Middleware.parseBearerToken = (req, res, next) => {
req.authToken = parseHeaders(req) || parseQuery(req);
next();
};
/**
* Parses bearer token from the Authorization header
*
* @param req {Object} The incoming Connect request
* @returns {String} The bearer token
* @private
*/
function parseHeaders(req) {
if (req.headers && req.headers.authorization) {
const parts = req.headers.authorization.split(' ');
if (parts.length === 2) {
const scheme = parts[0];
const credentials = parts[1];
if (/^Bearer$/i.test(scheme)) {
return credentials;
}
}
}
}
/**
* Parses bearer token from URL query parameters
*
* @param req {Object} The incoming Connect request
* @returns {String} The bearer token
*/
function parseQuery(req) {
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
if (req.query && req.query.access_token) {
return req.query.access_token;
}
// jscs:enable requireCamelCaseOrUpperCaseIdentifiers
}
// *************
/**
* SimpleRest middleware for validating a Meteor.user's login token
*
* This middleware must be processed after the request.token has been set to a
* valid login token for a Meteor.user account (from a separate layer of
* middleware). If authentication is successful, the request.userId will be set
* to the ID of the authenticated user.
*
* @middleware
*/
JsonRoutes.Middleware.authenticateMeteorUserByToken =
(req, res, next) => {
Fiber(() => {
const userId = getUserIdFromAuthToken(req.authToken);
if (userId) {
req.userId = userId;
}
next();
}).run();
};
/**
* Retrieves the ID of the Meteor.user that the given auth token belongs to
*
* @param token An unhashed auth token
* @returns {String} The ID of the authenticated Meteor.user, or null if token
* is invalid
*/
function getUserIdFromAuthToken(token) {
if (!token) return null;
const driver = new MongoInternals.RemoteCollectionDriver(Meteor.settings[dbId]); // must have the database URL in your settings.json file
const users = new Mongo.Collection("users", { _driver: driver, _suppressSameNameError: true });
const user = users.findOne({ 'services.resume.loginTokens.hashedToken': Accounts._hashLoginToken(token) }, { fields: { _id: 1 } });
if (user) return user._id;
return null;
}
// *************
RestMiddleware = {};
// import RestMiddleware from 'middleware';
/**
* Handle any connect errors with a standard JSON response
*
* Response looks like:
* {
* error: 'Error type',
* reason: 'Cause of error'
* }
*
* @middleware
*/
RestMiddleware.handleErrorAsJson = (err, request, response, next) => { // jshint ignore:line
// If we at least put in some effort to throw a user-facing Meteor.Error,
// the default code should be less severe
if (err.sanitizedError && err.sanitizedError.errorType === 'Meteor.Error') {
if (!err.sanitizedError.statusCode) {
err.sanitizedError.statusCode = err.statusCode || 400;
}
err = err.sanitizedError;
} else if (err.errorType === 'Meteor.Error') {
if (!err.statusCode) err.statusCode = 400;
} else {
// Hide internal error details
// XXX could check node_env here and return full
// error details if development
const statusCode = err.statusCode;
err = new Error();
err.statusCode = statusCode;
}
// If an error has a `data` property, we
// send that. This allows packages to include
// extra client-safe data with the errors they throw.
var body = {
error: err.error || 'internal-server-error',
reason: err.reason || 'Internal server error',
details: err.details,
data: err.data,
};
body = JSON.stringify(body, null, 2);
response.statusCode = err.statusCode || 500;
response.setHeader('Content-Type', 'application/json');
response.write(body);
response.end();
};
// *************
JsonRoutes.Middleware.use(JsonRoutes.Middleware.parseBearerToken);
JsonRoutes.Middleware.use(JsonRoutes.Middleware.authenticateMeteorUserByToken);
// Handle errors specifically for the login routes correctly
JsonRoutes.ErrorMiddleware.use('/users/login', RestMiddleware.handleErrorAsJson);
JsonRoutes.ErrorMiddleware.use('/users/token-login', RestMiddleware.handleErrorAsJson);
JsonRoutes.ErrorMiddleware.use('/users/register', RestMiddleware.handleErrorAsJson);
// *************
Meteor.startup(function () {
_.each(errorMiddlewares, function (errorMiddleware) {
errorMiddleware = _.map(errorMiddleware, function (maybeFn) {
if (_.isFunction(maybeFn)) {
// A connect error middleware needs exactly 4 arguments because they use fn.length === 4 to
// decide if something is an error middleware.
return function (a, b, c, d) {
Meteor.bindEnvironment(maybeFn)(a, b, c, d);
}
}
return maybeFn;
});
WebApp.connectHandlers.use.apply(WebApp.connectHandlers, errorMiddleware);
});
errorMiddlewares = [];
});
JsonRoutes.add = function (method, path, handler) {
// Make sure path starts with a slash
if (path[0] !== '/') {
path = '/' + path;
}
// Add to list of known endpoints
JsonRoutes.routes.push({
method: method,
path: path,
});
connectRouter[method.toLowerCase()](path, function (req, res, next) {
// Set headers on response
setHeaders(res, responseHeaders);
Fiber(function () {
try {
handler(req, res, next);
} catch (error) {
next(error);
}
}).run();
});
};
var responseHeaders = {
'Cache-Control': 'no-store',
Pragma: 'no-cache',
};
JsonRoutes.setResponseHeaders = function (headers) {
responseHeaders = headers;
};
// *********************
JsonRoutes.add('options', '/users/login', (req, res) => {
JsonRoutes.sendResult(res);
});
JsonRoutes.add('post', '/users/login', (req, res) => {
const options = req.body;
let user;
if (options.hasOwnProperty('email') && options.hasOwnProperty('dbId')) {
check(options, {
email: String,
password: String,
dbId: String
});
const driver = new MongoInternals.RemoteCollectionDriver(Meteor.settings[dbId]); // must have the database URL in your settings.json file
const users = new Mongo.Collection("users", { _driver: driver, _suppressSameNameError: true });
user = users.findOne({ 'emails.address': options.email });
} else if (options.hasOwnProperty('username') && options.hasOwnProperty('dbId')) {
check(options, {
username: String,
password: String,
dbId: String
});
const driver = new MongoInternals.RemoteCollectionDriver(Meteor.settings[dbId]); // must have the database URL in your settings.json file
const users = new Mongo.Collection("users", { _driver: driver, _suppressSameNameError: true });
user = users.findOne({ username: options.username });
} else if (options.hasOwnProperty('email')) {
check(options, {
email: String,
password: String
});
const users = new Mongo.Collection("users");
user = users.findOne({ 'emails.address': options.email });
} else if (options.hasOwnProperty('username')) {
check(options, {
username: String,
password: String
});
const users = new Mongo.Collection("users");
user = users.findOne({ username: options.username });
}
if (!user) {
throw new Meteor.Error('not-found',
'User with that username or email address not found.');
}
const result = Accounts._checkPassword(user, options.password);
check(result, {
userId: String,
error: Match.Optional(Meteor.Error),
});
if (result.error) {
throw result.error;
}
const stampedLoginToken = Accounts._generateStampedLoginToken();
check(stampedLoginToken, {
token: String,
when: Date,
});
const hashedToken = Accounts._hashStampedToken(stampedLoginToken);
users.update({ _id: result.userId }, { $addToSet: { "services.resume.loginTokens": hashedToken } });
const tokenExpiration = Accounts._tokenExpiration(stampedLoginToken.when);
check(tokenExpiration, Date);
JsonRoutes.sendResult(res, {
data: {
id: result.userId,
token: stampedLoginToken.token,
tokenExpires: tokenExpiration,
},
});
});
JsonRoutes.add('options', '/users/token-login', (req, res) => {
JsonRoutes.sendResult(res);
});
JsonRoutes.add('post', '/users/token-login', (req, res) => {
const options = req.body;
let multiMode = true;
if (options.hasOwnProperty('dbId')) {
check(options, {
dbId: String,
loginToken: String
});
} else {
multiMode = false
check(options, {
loginToken: String
});
}
const dbId = options.dbId;
const loginToken = options.loginToken;
let users = null;
if (multiMode) {
const driver = new MongoInternals.RemoteCollectionDriver(Meteor.settings[dbId]); // must have the database URL in your settings.json file
users = new Mongo.Collection("users", { _driver: driver, _suppressSameNameError: true });
}
else {
users = new Mongo.Collection("users");
}
let user = users.findOne({ 'services.login.token': loginToken });
// No user, in the wrong, could be invalid userId or database, or event token.
if (!user) {
throw new Meteor.Error('not-found',
'User with that login token not found.');
}
// We have a valid user, now assign the id to a variable.
const userId = user._id;
// You're done with this one-time login token, now throw it away so it can't be used again.
users.update({ _id: userId }, { $unset: { 'services.login.token': '' } });
// Generate the stamped token and add it to the user collection
const stampedLoginToken = Accounts._generateStampedLoginToken();
check(stampedLoginToken, {
token: String,
when: Date,
});
const hashedToken = Accounts._hashStampedToken(stampedLoginToken);
users.update({ _id: userId }, { $addToSet: { "services.resume.loginTokens": hashedToken } });
const tokenExpiration = Accounts._tokenExpiration(stampedLoginToken.when);
check(tokenExpiration, Date);
JsonRoutes.sendResult(res, {
data: {
id: userId,
token: stampedLoginToken.token,
tokenExpires: tokenExpiration,
},
});
});
JsonRoutes.add('options', '/users/register', (req, res) => {
JsonRoutes.sendResult(res);
});
JsonRoutes.add('post', '/users/register', (req, res) => {
if(Accounts._options.forbidClientAccountCreation) {
JsonRoutes.sendResult(res, {code: 403});
} else {
const options = req.body;
check(options, {
username: Match.Optional(String),
email: Match.Optional(String),
password: String,
});
const userId = Accounts.createUser(
_.pick(options, 'username', 'email', 'password'));
// Log in the new user and send back a token
const stampedLoginToken = Accounts._generateStampedLoginToken();
check(stampedLoginToken, {
token: String,
when: Date,
});
// This adds the token to the user
Accounts._insertLoginToken(userId, stampedLoginToken);
const tokenExpiration = Accounts._tokenExpiration(stampedLoginToken.when);
check(tokenExpiration, Date);
// Return the same things the login method returns
JsonRoutes.sendResult(res, {
data: {
token: stampedLoginToken.token,
tokenExpires: tokenExpiration,
id: userId,
},
});
}
});
// ********************
/**
* Sets the response headers, status code, and body, and ends it.
* The JSON response will be pretty printed if NODE_ENV is `development`.
*
* @param {Object} res Response object
* @param {Object} [options]
* @param {Number} [options.code] HTTP status code. Default is 200.
* @param {Object} [options.headers] Dictionary of headers.
* @param {Object|Array|null|undefined} [options.data] The object to
* stringify as the response. If `null`, the response will be "null".
* If `undefined`, there will be no response body.
*/
JsonRoutes.sendResult = function (res, options) {
options = options || {};
// We've already set global headers on response, but if they
// pass in more here, we set those.
if (options.headers) setHeaders(res, options.headers);
// Set status code on response
res.statusCode = options.code || 200;
// Set response body
writeJsonToBody(res, options.data);
// Send the response
res.end();
};
function setHeaders(res, headers) {
_.each(headers, function (value, key) {
res.setHeader(key, value);
});
}
function writeJsonToBody(res, json) {
if (json !== undefined) {
var shouldPrettyPrint = (process.env.NODE_ENV === 'development');
var spacer = shouldPrettyPrint ? 2 : null;
res.setHeader('Content-type', 'application/json');
res.write(JSON.stringify(json, null, spacer));
}
}
module.exports.JsonRoutes = JsonRoutes;
module.exports.RestMiddleware = RestMiddleware;