examples icon indicating copy to clipboard operation
examples copied to clipboard

aws-node-auth0-cognito-custom-authorizers-api issues

Open daveo1001 opened this issue 8 years ago • 8 comments

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.

daveo1001 avatar Jan 03 '18 20:01 daveo1001

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 avatar Jan 03 '18 20:01 DavidWells

@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.

daveo1001 avatar Jan 03 '18 21:01 daveo1001

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"
            }
        ]
    }
}

daveo1001 avatar Jan 03 '18 21:01 daveo1001

Yeah, gotta say this example just doesn't work.

daveo1001 avatar Jan 05 '18 14:01 daveo1001

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

casualuser avatar Feb 01 '18 16:02 casualuser

@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:

  1. wrote code
  2. tested with serverless invoke local --function auth --path api-gateway-event.json where api-gateway-event.json and "authorizationToken": "Bearer xxxxxx", line added.
  3. serverless deploy
  4. 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)
  5. 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 -t in one terminal (i.e. your authorizer name) and serverless logs -f app -t in 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.

swishstar avatar Mar 08 '18 13:03 swishstar

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 avatar May 30 '18 11:05 singhabahu

@singhabahu yes it works perfectly.

Rishanthakumar avatar May 30 '18 16:05 Rishanthakumar