aws-node-auth0-cognito-custom-authorizers-api issues
Maybe It's just me but I've got a few issues.
(using Cognito)
Doing the minimum serverless install and changing the iss. I get iss doesn't match. The required trailing slash / is absent in the payload so I have to slice.
jwk.verify(token, pem, { issuer: iss.slice(0, -1) }, (err, decoded) => { // blah }
// instead of
jwk.verify(token, pem, { issuer: iss }, (err, decoded) => { // blah }
And from there, when postmaning, I get 502 Bad Gateway
{
"message": "Internal server error"
}
AWS testing the auth lambda function works as expected with/out token but testing the API gateway in the mess Execution failed due to configuration error: Malformed Lambda proxy response. And I'm not smart enough to fix that.
Has anyone tested this lately? It's likely I'm just dumb as rocks.
Wrap your jwk.verify code in a try/catch block and see if you can log out the actual error.
The jwt library doesn't fail gracefully and kills the lambda process
@DavidWells scooting right past jwk, no problem. Seems like it's after auth, when it's heading back to the private endpoint that it gets unhappy.
It gets into generatePolicy() - and I'll be honest, I have no idea have no idea where it goes from there - the response from there is
{
"principalId": "<<secrets>>",
"policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": "arn:aws:execute-api:us-east-2:<<secrets>>/dev/GET/api/private"
}
]
}
}
Yeah, gotta say this example just doesn't work.
this example works well for me with auth0 but completely do not work with cognito
there is quite limited possibilities for debugging this
f.e. serverless-offline support only remote authorizers so it's not possible to trigger auth in common way with enabled debugger
so tried to test it and make it working with local debugging via serverless-offline and apply verification token via headers from postman
it require to tweak code and set event.authorizationToken = event.headers.Authorization
and then it silently fall with request to https://cognito-idp.us-east-1.amazonaws.com/us-east-1_hereismycode/.well-known/jwks.json
@daveo1001: I agree with the needed change for the trailing slash. I, personally, just moved it from the const iss statement to before the .well-known/jwks.json request url. (6 vs 1/24 gross)
Your "Malformed lambda proxy response" is addressed here (i.e. a fixed needed in your code).
@casualuser and everyone: I agree testing this is a bit tricky. Here's my process:
- wrote code
- tested with
serverless invoke local --function auth --path api-gateway-event.jsonwhere api-gateway-event.json and"authorizationToken": "Bearer xxxxxx",line added. serverless deploy- test authorizer in AWS APIG Console. Go to the authorizers section of your specific gateway and there is the test button. This output gives you a bit more info as to why you might be generating a valid policy when testing locally, but the policy never leaves the authorizer. (500 errors, if I remember right)
- test that the actual lambda is being kicked off after the auth finishes by using this simple lambda with a console.log and by tailing
serverless logs -f auth -tin one terminal (i.e. your authorizer name) andserverless logs -f app -tin another terminal (i.e. your lambda). You get 502 errors here.
Hopefully that helps someone/someday to justify my hours of debugging. The need to actually deploy between tests is a pain because you can't test that hand-off between auth and app locallly.
I have to agree with @daveo1001 with the issue in the example when integrating with Cognito. When passing the issuer we need to remove the trailing / from the iss. Otherwise it'll give an error saying
Unauthorized user: jwt issuer invalid. Make sure to add below code as mentioned above
{ issuer: iss.slice(0, -1) }
when passing the issuer to the verify method.
@singhabahu yes it works perfectly.