AWS Cognito returning invalid signature with new userpool
I have an AWS Cognito user pool that is a replacement for an old one that I had to delete because of a custom attribute issue. I am using the same exact code as before though the keys have changed. I have them in a JSON file that I pulled from the AWS URL for getting the keys. I am getting an error now about invalid signature when trying to validate a JWT. I know my code is solid since it hasn't changed but was looking to see from others if there is something else I am missing or should do other than update my pool id, client id, and keys.json file.
Edit adding my code just incase there is an issue with it though I can't see why if nothing changed
exports.isJWTValid = () => (req, res, next) => {
let idToken = req.headers.authorization
let token = idToken.split(' ')[1]
let header = jwt_decode(token, { header: true });
let keys = keysJSON.keys
let kid = header.kid
let jwk = keys.find(r => r.kid === kid)
let pem = jwkToPem(jwk);
jwt.verify(token, pem, { algorithms: ['RS256'] }, function(err, decodedToken) {
if(err) { // error is showing up in this if(err) and returning to postman
logger.debug(err)
return res.status(401).json({success: false, err})
}
const currentSeconds = Math.floor((new Date()).valueOf() / 1000)
if (currentSeconds >= decodedToken.exp || currentSeconds < decodedToken.auth_time ) {
let message = 'Session has expired, please login again.'
return res.status(401).json({success: false, message});
}
if(decodedToken.aud !== config.ClientId) {
let message = 'Token doen\'t match app client'
return res.status(401).json({success: false, message});
}
if(decodedToken.iss !== `https://cognito-idp.us-east-1.amazonaws.com/${config.UserPoolId}`) {
let message = 'Token doen\'t match user pool'
return res.status(401).json({success: false, message});
}
if(decodedToken.token_use !== 'id' && decodedToken.token_use !== 'access') {
let message = 'Token use case doen\'t match'
return res.status(401).json({success: false, message});
}
logger.debug('decodedToken: ', decodedToken)
next()
});
};
from Recent Questions - Stack Overflow https://ift.tt/2SxUCcU
https://ift.tt/eA8V8J
Comments
Post a Comment