Skip to content
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

Auth Middleware does not distinguish type of JWT Token #28

Closed
ulrichelias opened this issue Sep 14, 2020 · 1 comment
Closed

Auth Middleware does not distinguish type of JWT Token #28

ulrichelias opened this issue Sep 14, 2020 · 1 comment

Comments

@ulrichelias
Copy link

I am new to Express/NodeJS, so please let me know if I am understanding this incorrectly.

This boilerplate uses 3 different types of JWTs namely Access Tokens, Refresh Tokens and Reset Password Token.

All types of tokens are created with the same content and structure.
const generateToken = (userId, expires, secret = config.jwt.secret) => { const payload = { sub: userId, iat: moment().unix(), exp: expires.unix(), }; return jwt.sign(payload, secret); };

Also the auth middleware does not differenciate between the different types:

`const verifyCallback = (req, resolve, reject, requiredRights) => async (
err,
user,
info
) => {
if (err || info || !user) {
return reject(new ApiError(httpStatus.UNAUTHORIZED, 'Please authenticate'));
}
req.user = user;

if (requiredRights.length) {
const userRights = roleRights.get(user.role);
const hasRequiredRights = requiredRights.every((requiredRight) =>
userRights.includes(requiredRight)
);

if (!hasRequiredRights && req.params.userId !== user.id) {
  return reject(new ApiError(httpStatus.FORBIDDEN, 'Forbidden'));
}

}

resolve();
};

const auth = (...requiredRights) => async (req, res, next) => {
return new Promise((resolve, reject) => {
passport.authenticate(
'jwt',
{ session: false },
verifyCallback(req, resolve, reject, requiredRights)
)(req, res, next);
})
.then(() => next())
.catch((err) => next(err));
};

module.exports = auth;`

This leads to all kinds of tokens being valid access tokens.

I am not sure if this is even a problem for security or if it is the intended behavior. It still seems odd to me.
Let me know if I am missing anything.

@hagopj13
Copy link
Owner

@ulrichelias thanks for the note and you're right. I just added a fix for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants