feat: migrating write-api skeleton into core
parent
dc152a558c
commit
ec5c48b188
@ -0,0 +1,48 @@
|
||||
'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,54 @@
|
||||
'use strict';
|
||||
|
||||
const middleware = require('../../middleware/api');
|
||||
const helpers = require('../../controllers/helpers');
|
||||
|
||||
const Write = module.exports;
|
||||
|
||||
Write.reload = (params) => {
|
||||
const router = params.router;
|
||||
|
||||
// router.use('/api', function (req, res, next) {
|
||||
// if (req.protocol !== 'https') {
|
||||
// res.set('Upgrade', 'TLS/1.0, HTTP/1.1');
|
||||
// return helpers.formatApiResponse(426, res);
|
||||
// } else {
|
||||
// next();
|
||||
// }
|
||||
// });
|
||||
|
||||
// router.use('/users', require('./users')(coreMiddleware));
|
||||
// router.use('/groups', require('./groups')(coreMiddleware));
|
||||
// router.use('/posts', require('./posts')(coreMiddleware));
|
||||
// router.use('/topics', require('./topics')(coreMiddleware));
|
||||
// router.use('/categories', require('./categories')(coreMiddleware));
|
||||
// router.use('/util', require('./util')(coreMiddleware));
|
||||
|
||||
router.get('/api/ping', function (req, res) {
|
||||
helpers.formatApiResponse(200, res, {
|
||||
pong: true,
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/api/ping', middleware.authenticate, function (req, res) {
|
||||
helpers.formatApiResponse(200, res, {
|
||||
uid: req.user.uid,
|
||||
});
|
||||
});
|
||||
|
||||
// This router is reserved exclusively for plugins to add their own routes into the write api plugin. Confused yet? :trollface:
|
||||
// var customRouter = require('express').Router();
|
||||
// plugins.fireHook('filter:plugin.write-api.routes', {
|
||||
// router: customRouter,
|
||||
// apiMiddleware: apiMiddleware,
|
||||
// middleware: coreMiddleware,
|
||||
// errorHandler: errorHandler
|
||||
// }, function (err, payload) {
|
||||
// router.use('/', payload.router);
|
||||
|
||||
// router.use(function(req, res) {
|
||||
// // Catch-all
|
||||
// errorHandler.respond(404, res);
|
||||
// });
|
||||
// });
|
||||
};
|
Loading…
Reference in New Issue