I have run into some trouble configuring/using Authentication on AWS ApiGateway. I already have my lambda function set up with a code the receives the AWS authentication model, see below, which basically decodifies the JWT token and verifies if the given user can access the resource:
{
"type": "TOKEN",
"authorizationToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjotMTU1LCJwcm9kdWN0IjoiQmlsbGlvblJ1biIsInBlcm1pc3Npb25fbGV2ZWwiOjEsInNhbHQiOiJzZWNyZXRfcGhyYXNlIn0.3gZUFITe8or2mPWBAZlOxdcGF6-ybykHVsMRsqoUI_8",
"methodArn": "arn:aws:execute-api:us-east-1:123456789012:example/prod/POST/{proxy+}"
}
See below the sample outputs from ApiGateway documentation. The first one is when user is successfully verified (permission granted) and the second one is when user fails to verify (permission denied):
{
"principalId": "users",
"policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": "arn:aws:execute-api:REGION:AWS_ACCOUNT:example/prod/POST/{proxy+}"
}
]
},
"context": {
"user_id": XXX,
}
}
Permission denied:
{
"principalId": "users",
"policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Deny",
"Resource": "arn:aws:execute-api:REGION:AWS_ACCOUNT:example/prod/POST/{proxy+}"
}
]
}
}
The problem is: Every single time I test the custom authorization function, the return status is 200 (instead of 401) and the permission is granted (even when I send wrong tokens).
Also, I really feel like it is not even testing anything, although the screen shows that the custom authentication function is enabled.
Resource showing custom authorizer
------- EDIT -------
Here the code how I implemented the output:
def generate_policy(principal_id, effect, resource, context=None):
doc = {
'principalId': principal_id,
'policyDocument': {
'Version': '2012-10-17',
'Statement': [{
'Action': 'execute-api:Invoke',
'Effect': effect,
'Resource': resource
}]
}
}
if context:
doc["context"] = context
return doc
So you can call like this to "allow":
generate_policy("users", "Allow", method_arn, auth_info)
Or like this to "deny":
generate_policy("users", "Deny", method_arn)
-------- EDIT AGAIN ------ Gist with my all code:
https://gist.github.com/hermogenes-db18/1ccf3eb8273f266a3fa02643dcfd39bd