I'm building a Lambda authorizer to use with an API Gateway Web Socket api.
To handle the token, I'm using jsonwebtoken
and decoding the token like that:
const jwt = require('jsonwebtoken');
const decodedJwt = jwt.decode(token, {complete: true});
When I run this locally on my machine it works fine, but when its deployed to AWS it is not working.
The jwt.decode call is returning null
and no exception is thrown. Also, I've looked through the documents but can't find a way to get any error message or something like this.
I did some logging and the token is correctly retrieved from the request and passed to the function. Also, if I copy the token from AWS logs and run my code locally it works fine.
I've tried it with Nodejs runtime versions 8.10 and 10.x
Any idea on how to solve this or how to get an error message to understand what is happening?
So after being struggled for some time, I realised what the issue was.
When retrieving the token on AWS I was using the following code:
const token = event.queryStringParameters.Authorization;
But for some reason, event.queryStringParameters.Authorization
will also include double quotes on the beginning and end of the token.
So I thought I was passing TOKEN
to the decode function while I was actually passing "TOKEN"
.
I've used substring to remove the extra double quotes...
const token = event.queryStringParameters.Authorization.substring(1, event.queryStringParameters.Authorization.length - 1);
... and now it is working fine.
Answering it here in case anyone has the same issue.