Skip to content

Commit

Permalink
check owner of box id for authorized requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ubergesundheit committed Aug 17, 2017
1 parent e3d797d commit 42a55ec
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
10 changes: 8 additions & 2 deletions lib/controllers/boxesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const restify = require('restify'),
parseAndValidateTimeParams,
GET_DATA_MULTI_DEFAULT_COLUMNS,
GET_DATA_MULTI_ALLOWED_COLUMNS,
validateBboxParam
validateBboxParam,
checkBoxIdOwner
} = require('../requestUtils'),
{ point } = require('@turf/helpers'),
outlierTransformer = require('../statistics').outlierTransformer,
Expand Down Expand Up @@ -857,9 +858,13 @@ module.exports = {
retrieveParameters([
{ name: 'password', dataType: 'String', required: true }
]),
checkBoxIdOwner,
deleteBox
],
getScript,
getScript: [
checkBoxIdOwner,
getScript
],
getData: [
retrieveParameter('format', 'String', 'json', ['json', 'csv']),
retrieveParameter('download', 'String', false, ['true', 'false']),
Expand Down Expand Up @@ -894,6 +899,7 @@ module.exports = {
{ name: 'sensors', dataType: ['object'] },
{ name: 'addons', dataType: 'object' }
]),
checkBoxIdOwner,
updateBox
],
getMeasurements,
Expand Down
1 change: 1 addition & 0 deletions lib/controllers/sensorsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ module.exports = {
{ name: 'timestamps', dataType: ['ISO8601'] }
]),
requestUtils.parseAndValidateTimeParamsOptional,
requestUtils.checkBoxIdOwner,
deleteSensorData
]
};
12 changes: 9 additions & 3 deletions lib/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,26 @@ userSchema.methods.addBox = function addBox (params) {
});
};

userSchema.methods.removeBox = function removeBox (boxId) {
userSchema.methods.checkBoxOwner = function checkBoxOwner (boxId) {
const user = this;

// first check if the box belongs to this user
if (!user.boxes) {
return Promise.reject(new Error('user does not own this senseBox'));
throw new ModelError('User does not own this senseBox', { type: 'ForbiddenError' });
}

const userOwnsBox = user.boxes.some(b => b.equals(boxId));

if (userOwnsBox === false) {
return Promise.reject(new Error('user does not own this senseBox'));
throw new ModelError('User does not own this senseBox', { type: 'ForbiddenError' });
}

return true;
};

userSchema.methods.removeBox = function removeBox (boxId) {
const user = this;

return Box.findById(boxId)
.exec()
.then(function (box) {
Expand Down
13 changes: 12 additions & 1 deletion lib/requestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,16 @@ const clearCache = function clearCache (identifiers) {
}
};

const checkBoxIdOwner = function checkBoxIdOwner (req, res, next) {
try {
req.user.checkBoxOwner(req._userParams.boxId);

return next();
} catch (err) {
return next(new restify.ForbiddenError(err.message));
}
};

module.exports = {
checkContentType,
validateIdParams,
Expand All @@ -591,5 +601,6 @@ module.exports = {
GET_DATA_MULTI_ALLOWED_COLUMNS: ['createdAt', 'value', 'lat', 'lon', 'unit', 'boxId', 'sensorId', 'phenomenon', 'sensorType', 'boxName', 'exposure'],
setHoneybadgerContext,
addCache,
clearCache
clearCache,
checkBoxIdOwner
};

0 comments on commit 42a55ec

Please sign in to comment.