I am having difficulty getting the jsonwebtoken verify function to work correctly.
The audience (aud) will either not verify (even though it is the same as what was in the jwt originally), or it will verify even if the content of aud is different from what was originally in the jwt.
I am signing my jwt using the following:
jwt.sign({aud: "example.com"}, privateKey, {algorithm: 'RS256'})
If I use the following to verify I get an ‘JsonWebTokenError: jwt audience invalid. expected: example.com’ even though audience is ‘example.com’
jwt.verify(token, privateKey, {
algorithms: ['RS256'],
audience: "example.com"}, //GET ERROR ALWAYS
(err, decoded) => {
if(err) {
console.log('Error: ' + err)
} else{
console.log('Decoded: ' + JSON.stringify(decoded))
return decoded
}
})
If I use ‘aud’ for the verification - it verifies even if I put in bad data.
jwt.verify(token, privateKey, {
algorithms: ['RS256'],
aud: "not what was in token"}, //ALWAYS VERIFIES
(err, decoded) => {
if(err) {
console.log('Error: ' + err)
} else{
console.log('Decoded: ' + JSON.stringify(decoded))
return decoded
}
})
I have tried signing the token using ‘audience’ (instead of ‘aud’), but have the same issue. Is there something I am missing? Any help would be greatly appreciated! Thanks
Environment
NodeJS - v10.16.2
jsonwebtoken - v8.5.1
Posted this question on Stackoverflow too.