Skip to content

PROD - Better 401/403 error messaging for debugging #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ module.exports = (app) => {
if (req.authUser.isMachine) {
// M2M
if (!req.authUser.scopes || !helper.checkIfExists(def.scopes, req.authUser.scopes)) {
next(new errors.ForbiddenError("You are not allowed to perform this action!"));
next(new errors.ForbiddenError(`You are not allowed to perform this action, because the scopes are incorrect. \
Required scopes: ${JSON.stringify(def.scopes)} \
Provided scopes: ${JSON.stringify(req.authUser.scopes)}`));
} else {
req.authUser.handle = config.M2M_AUDIT_HANDLE;
req.authUser.userId = config.M2M_AUDIT_USERID;
Expand All @@ -71,14 +73,17 @@ module.exports = (app) => {
_.map(req.authUser.roles, (r) => r.toLowerCase())
)
) {
next(new errors.ForbiddenError("You are not allowed to perform this action!"));
next(new errors.ForbiddenError(`You are not allowed to perform this action, because the roles are incorrect. \
Required roles: ${JSON.stringify(def.access)} \
Provided roles: ${JSON.stringify(req.authUser.roles)}`));
} else {
// user token is used in create/update challenge to ensure user can create/update challenge under specific project
req.userToken = req.headers.authorization.split(" ")[1];
next();
}
} else {
next(new errors.ForbiddenError("You are not authorized to perform this action"));
next(new errors.ForbiddenError("You are not authorized to perform this action, \
because no roles were provided"));
}
}
});
Expand Down