Friday 20 August 2021

How to change error message in API authorizer in AWS?

I am using Lambda for creating APIs for my project. I started using API Authorizer for Token Base Authentication. Below is my code:

import json, jwt
from datetime import datetime

def lambda_handler(event, context):
    timestamp = datetime.timestamp(datetime.now())
    
    decode_data=jwt.decode(jwt=event['authorizationToken'], key="", algorithms=["RS256"], options={"verify_signature": False})

    auth = 'Deny'
    if timestamp < decode_data['exp']:
        if decode_data['custom:user'] == 'Customer':
            auth = 'Allow'
 
    authResponse = { "principalId": "abc123", "policyDocument": { "Version": "2012-10-17", "Statement": [{"Action": "*", "Resource": "*", "Effect": auth}] }}
    return authResponse

I applied this authorizer with another Lambda for validation. So, it is working fine when token is valid, but when token get expired it give below message:

{
    "Message": "User is not authorized to access this resource with an explicit deny"
}

Now I want to customise this error and it status code also. How to do that? Any suggestion.



from How to change error message in API authorizer in AWS?

No comments:

Post a Comment