fix(refactor): merging write-api auth middlewares with core middlewares
parent
ec5c48b188
commit
f6433ef2c5
@ -1,48 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const passport = require('passport');
|
||||
const winston = require('winston');
|
||||
|
||||
const helpers = require('../controllers/helpers');
|
||||
const middleware = module.exports;
|
||||
|
||||
middleware.authenticate = function (req, res, next) {
|
||||
if (req.headers.hasOwnProperty('authorization')) {
|
||||
passport.authenticate('bearer', { session: false }, function (err, user) {
|
||||
if (err) { return next(err); }
|
||||
if (!user) { return helpers.formatApiResponse(401, res); }
|
||||
|
||||
// If the token received was a master token, a _uid must also be present for all calls
|
||||
if (user.hasOwnProperty('uid')) {
|
||||
req.login(user, function (err) {
|
||||
if (err) { return helpers.formatApiResponse(500, res, err); }
|
||||
|
||||
req.uid = user.uid;
|
||||
req.loggedIn = req.uid > 0;
|
||||
next();
|
||||
});
|
||||
} else if (user.hasOwnProperty('master') && user.master === true) {
|
||||
if (req.body.hasOwnProperty('_uid') || req.query.hasOwnProperty('_uid')) {
|
||||
user.uid = req.body._uid || req.query._uid;
|
||||
delete user.master;
|
||||
|
||||
req.login(user, function (err) {
|
||||
if (err) { return helpers.formatApiResponse(500, res, err); }
|
||||
|
||||
req.uid = user.uid;
|
||||
req.loggedIn = req.uid > 0;
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
return helpers.formatApiResponse(400, res, new Error('A master token was received without a corresponding `_uid` in the request body'));
|
||||
}
|
||||
} else {
|
||||
winston.warn('[api/authenticate] Unable to find user after verifying token');
|
||||
helpers.formatApiResponse(500, res);
|
||||
}
|
||||
})(req, res, next);
|
||||
} else {
|
||||
// No bearer token, reject request
|
||||
helpers.formatApiResponse(401, res);
|
||||
}
|
||||
};
|
@ -0,0 +1,177 @@
|
||||
'use strict';
|
||||
|
||||
const users = require('../../user');
|
||||
|
||||
const middleware = require('../../middleware');
|
||||
const helpers = require('../../controllers/helpers');
|
||||
// Messaging = require.main.require('./src/messaging'),
|
||||
// apiMiddleware = require('./middleware'),
|
||||
// errorHandler = require('../../lib/errorHandler'),
|
||||
// auth = require('../../lib/auth'),
|
||||
// utils = require('./utils'),
|
||||
// async = require.main.require('async');
|
||||
|
||||
|
||||
module.exports = function () {
|
||||
var app = require('express').Router();
|
||||
|
||||
app.post('/', middleware.authenticate, middleware.isAdmin, async (req, res) => {
|
||||
helpers.checkRequired(['username'], req, res);
|
||||
// if (!utils.checkRequired(['username'], req, res)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
const uid = await users.create(req.body);
|
||||
helpers.formatApiResponse(200, res, {
|
||||
uid: uid,
|
||||
});
|
||||
});
|
||||
|
||||
// app.route('/:uid')
|
||||
// .put(apiMiddleware.requireUser, apiMiddleware.exposeAdmin, function(req, res) {
|
||||
// if (parseInt(req.params.uid, 10) !== parseInt(req.user.uid, 10) && !res.locals.isAdmin) {
|
||||
// return errorHandler.respond(401, res);
|
||||
// }
|
||||
|
||||
// // `uid` in `updateProfile` refers to calling user, not target user
|
||||
// req.body.uid = req.params.uid;
|
||||
|
||||
// Users.updateProfile(req.user.uid, req.body, function(err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.exposeAdmin, function(req, res) {
|
||||
// if (parseInt(req.params.uid, 10) !== parseInt(req.user.uid, 10) && !res.locals.isAdmin) {
|
||||
// return errorHandler.respond(401, res);
|
||||
// }
|
||||
|
||||
// // Clear out any user tokens belonging to the to-be-deleted user
|
||||
// async.waterfall([
|
||||
// async.apply(auth.getTokens, req.params.uid),
|
||||
// function(tokens, next) {
|
||||
// async.each(tokens, function(token, next) {
|
||||
// auth.revokeToken(token, 'user', next);
|
||||
// }, next);
|
||||
// },
|
||||
// async.apply(Users.delete, req.user.uid, req.params.uid)
|
||||
// ], function(err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.put('/:uid/password', apiMiddleware.requireUser, apiMiddleware.exposeAdmin, function(req, res) {
|
||||
// if (parseInt(req.params.uid, 10) !== parseInt(req.user.uid, 10) && !res.locals.isAdmin) {
|
||||
// return errorHandler.respond(401, res);
|
||||
// }
|
||||
|
||||
// Users.changePassword(req.user.uid, {
|
||||
// uid: req.params.uid,
|
||||
// currentPassword: req.body.current || '',
|
||||
// newPassword: req.body['new'] || ''
|
||||
// }, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.put('/:uid/follow', apiMiddleware.requireUser, function(req, res) {
|
||||
// Users.follow(req.user.uid, req.params.uid, function(err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.delete('/:uid/follow', apiMiddleware.requireUser, function(req, res) {
|
||||
// Users.unfollow(req.user.uid, req.params.uid, function(err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.route('/:uid/chats')
|
||||
// .post(apiMiddleware.requireUser, function(req, res) {
|
||||
// if (!utils.checkRequired(['message'], req, res)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// var timestamp = parseInt(req.body.timestamp, 10) || Date.now();
|
||||
|
||||
// function addMessage(roomId) {
|
||||
// Messaging.addMessage({
|
||||
// uid: req.user.uid,
|
||||
// roomId: roomId,
|
||||
// content: req.body.message,
|
||||
// timestamp: timestamp,
|
||||
// }, function(err, message) {
|
||||
// if (parseInt(req.body.quiet, 10) !== 1) {
|
||||
// Messaging.notifyUsersInRoom(req.user.uid, roomId, message);
|
||||
// }
|
||||
|
||||
// return errorHandler.handle(err, res, message);
|
||||
// });
|
||||
// }
|
||||
|
||||
// Messaging.canMessageUser(req.user.uid, req.params.uid, function(err) {
|
||||
// if (err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
// }
|
||||
|
||||
// if (req.body.roomId) {
|
||||
// addMessage(req.body.roomId);
|
||||
// } else {
|
||||
// Messaging.newRoom(req.user.uid, [req.params.uid], function(err, roomId) {
|
||||
// if (err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
// }
|
||||
|
||||
// addMessage(roomId);
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.route('/:uid/ban')
|
||||
// .put(apiMiddleware.requireUser, apiMiddleware.requireAdmin, function(req, res) {
|
||||
// Users.bans.ban(req.params.uid, req.body.until || 0, req.body.reason || '', function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.requireAdmin, function(req, res) {
|
||||
// Users.bans.unban(req.params.uid, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.route('/:uid/tokens')
|
||||
// .get(apiMiddleware.requireUser, function(req, res) {
|
||||
// if (parseInt(req.params.uid, 10) !== parseInt(req.user.uid, 10)) {
|
||||
// return errorHandler.respond(401, res);
|
||||
// }
|
||||
|
||||
// auth.getTokens(req.params.uid, function(err, tokens) {
|
||||
// return errorHandler.handle(err, res, {
|
||||
// tokens: tokens
|
||||
// });
|
||||
// });
|
||||
// })
|
||||
// .post(apiMiddleware.requireUser, function(req, res) {
|
||||
// if (parseInt(req.params.uid, 10) !== parseInt(req.user.uid)) {
|
||||
// return errorHandler.respond(401, res);
|
||||
// }
|
||||
|
||||
// auth.generateToken(req.params.uid, function(err, token) {
|
||||
// return errorHandler.handle(err, res, {
|
||||
// token: token
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.delete('/:uid/tokens/:token', apiMiddleware.requireUser, function(req, res) {
|
||||
// if (parseInt(req.params.uid, 10) !== req.user.uid) {
|
||||
// return errorHandler.respond(401, res);
|
||||
// }
|
||||
|
||||
// auth.revokeToken(req.params.token, 'user', function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
return app;
|
||||
};
|
Loading…
Reference in New Issue