Skip to content

Commit

Permalink
start impersonation route
Browse files Browse the repository at this point in the history
  • Loading branch information
birm committed Jun 21, 2024
1 parent 5d207e7 commit 001c58c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
3 changes: 3 additions & 0 deletions caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var HANDLERS = {
"requestResetPassword": customHandlers.requestResetPassword,
"getOwnUser": customHandlers.getOwnUser,
"editOwnUser": customHandlers.editOwnUser,
"impersonate": function() {
return customHandlers.impersonate(userFunction);
},
"dataEnforce": auth.dataEnforce,
"monitorCheck": monitor.check,
"mongoFind": dataHandlers.General.find,
Expand Down
23 changes: 17 additions & 6 deletions handlers/customHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ function editOwnUser(db, collection) {
let token = auth.tokenVerify(req, auth.PRIKEY);
let updates = req.body;
// don't let users update some fields.
delete update.email;
delete update.collections;
delete update.userFilter;
delete update.userType;
delete update.create_date;
delete update['_id'];
delete updates.email;
delete updates.collections;
delete updates.userFilter;
delete updates.userType;
delete updates.create_date;
delete updates['_id'];
var newVals = {
$set: JSON.parse(req.body),
};
Expand All @@ -139,8 +139,19 @@ function editOwnUser(db, collection) {
};
}

function impersonate(userFunction) {
return function(req, res, next) {
let fakeToken = {'email': req.body.email};
let user = userFunction(fakeToken);
let token = auth.makeJwt(user, auth.PRIKEY, auth.EXPIRY);
req.data = token;
next();
};
}

var customHandlers = {};
customHandlers.userRegistrationFlow = userRegistrationFlow;
customHandlers.resetPassword = resetPassword;
customHandlers.requestResetPassword = requestResetPassword;
customHandlers.impersonate = impersonate;
module.exports = customHandlers;

0 comments on commit 001c58c

Please sign in to comment.