javascriptnode.jsamazon-web-servicesaws-lambdacustom-authentication

Unrecognizable lambda output in AWS Lambda Custom Auth


I want to authenticate my user using CUSTOM_AUTH in AWS Lambda using nodejs 16. For now, I want to provide fixed challenge Answer. However I got an error saying Unrecognizable lambda output. My code is below


exports.handler = async (event) => {
    console.log('Received event:', JSON.stringify(event, null, 2));

    if (event.triggerSource === 'DefineAuthChallenge_Authentication') {
        
        console.log('****** DefineAuthChallenge Trigger ******');

        if (!event.request.session || event.request.session.length === 0) {
            
            console.log('****** No session or empty session. Starting new authentication. ******');
            event.response.issueTokens = false;
            event.response.failAuthentication = false;
            event.response.challengeName = 'CUSTOM_CHALLENGE';
            
        } else {
            
            const session = event.request.session;
            console.log('****** Session:', JSON.stringify(session, null, 2));
            const lastChallenge = session[session.length - 1];
            const answerCorrect = lastChallenge.challengeResult;

            if (answerCorrect) {
                console.log('****** Correct answer provided. Issuing tokens. ******');
                event.response.issueTokens = true;
                event.response.failAuthentication = false;
                event.response.finalUserStatus = 'CONFIRMED';
            } else {
                console.log('****** Incorrect answer. Failing authentication. ******');
                event.response.issueTokens = false;
                event.response.failAuthentication = true;
                event.response.challengeName = 'CUSTOM_CHALLENGE';
            }
            
        }
    } else if (event.triggerSource === 'CreateAuthChallenge_Authentication') {
        
        console.log('****** CreateAuthChallenge Trigger ******');
        
        const fixedPassword = "sPi3trbs7WL7I4PHNGa5XTP7ryavwn";
        event.response.publicChallengeParameters = {};
        event.response.privateChallengeParameters = { answer: fixedPassword };
        event.response.challengeMetadata = fixedPassword;
        
    } else if (event.triggerSource === 'VerifyAuthChallengeResponse_Authentication') {
        
        console.log('****** VerifyAuthChallengeResponse Trigger ******');
        
        const expectedAnswer = event.request.privateChallengeParameters.answer;
        console.log('Expected Answer:', expectedAnswer);
        console.log('Provided Answer:', event.request.challengeAnswer);

        if (event.request.challengeAnswer === expectedAnswer) {
            event.response.answerCorrect = true;
        } else {
            event.response.answerCorrect = false;
        }
        
    }


    console.log('======= Returning Event:', JSON.stringify(event, null, 2));
    
    
    return event;
};

Process in my Frontend code:

Below is the event response in lambda:

{
    "version": "1",
    "region": "us-west-2",
    "userPoolId": "us-west-2_4dPsZcssz",
    "userName": "venue_2002",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-nodejs-2.1593.0",
        "clientId": "6pbb8jj4061em2e18jpmnjtes0"
    },
    "triggerSource": "DefineAuthChallenge_Authentication",
    "request": {
        "userAttributes": {
            "sub": "7831d320-20b1-7078-12c7-9469a2a8d0c9",
            "cognito:user_status": "FORCE_CHANGE_PASSWORD"
        },
        "session": [
            {
                "challengeName": "CUSTOM_CHALLENGE",
                "challengeResult": true,
                "challengeMetadata": "sPi3trbs7WL7I4PHNGa5XTP7ryavwn"
            }
        ]
    },
    "response": {
        "challengeName": null,
        "issueTokens": true,
        "failAuthentication": false,
        "finalUserStatus": "CONFIRMED"
    }
}


Solution

  • Finally! I've spent a lot of time debugging this problem. Let's break down the issue and the solution.

    The logic behind the code is correct, but the error "Unrecognizable lambda output" occurred because the response sent to the trigger did not match the expected format. The issue was that I added an extra attribute/parameter that wasn't needed:

    event.response.finalUserStatus = 'CONFIRMED';
    

    By removing this line, the error was resolved and the code now works as expected. Hope this helps anyone facing the same problem!