简体   繁体   中英

AWS Lambda response in Java for Cognito

How can I write an "AWS Lambda response" in Java so that Cognito is happy?

Something like this is passed to the lambda function

{
"version": number,
"triggerSource": "string",
"region": AWSRegion,
"userPoolId": "string",
"callerContext": 
    {
        "awsSdkVersion": "string",
        "clientId": "string"
    },
"request":
    {
        "userAttributes": {
            "string": "string",
            ....
        }
    },
"response": {}
}

Now I need to make the response in Java.. and send back to Cognito. At the moment Cognito throws an "InvalidLambdaResponseException".

Java code below just returns the event..

public class LambdaFunctionHandler implements RequestHandler<CognitoEvent, CognitoEvent> 
{
    @Override
    public CognitoEvent handleRequest(CognitoEvent arg0, Context arg1) 
    {
        return arg0;
    }
}

You just need a class like this:

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JsonSerialize
public class Example {
    private int version;
    private String triggerSource;
    private String region;
    private String userPoolId;
    private Map<String, String> callerContext;
    private Request request;
    private Response response;

    @Getter
    @Setter
    @JsonSerialize
    public static class Request {
        private Map<String, String> userAttributes;
        public Request(Map<String, String> userAttr) {
            userAttributes = userAttr;
        }
    }

    @Getter
    @Setter
    @JsonSerialize
    public static class Response { }

}

That after you serialize will look like this:

{
  "version" : 1,
  "triggerSource" : "trigger",
  "region" : "us-east-1",
  "userPoolId" : "user-pool-id",
  "callerContext" : {
    "some-key" : "some-value"
  },
  "request" : {
    "userAttributes" : {
      "name" : "Michael J Leonard"
    }
  },
  "response" : { }
}

And have this as an input to your lambda. It might require some changes but this is an example of a template for the PostAuthentication lambda

If you want to write your lambda in Kotlin. It's like this

Lambda:

class ClientSignupLambda : RequestHandler<PostConfirmationConfirmSignUp, PostConfirmationConfirmSignUp> {

    override fun handleRequest(event: PostConfirmationConfirmSignUp, context: Context): PostConfirmationConfirmSignUp {
        context.logger.log("event toString $event")
        return event
    }

}

Json class:

/**
 * Example of the JSON this class represents:
 * // formatterOFF
 * {
 *  version=1,
 *  region=eu-west-2,
 *  userPoolId=eu-west-2_abcd123,
 *  "userName=foo@gmail.com",
 *  "callerContext="{
 *      "awsSdkVersion=aws-sdk-unknown-unknown",
 *      clientId=123456789123456789
 *  },
 *  "triggerSource=PostConfirmation_ConfirmSignUp",
 *  "request="{
 *      "userAttributes="{
 *          sub=12341234-1234-1234-1234-123412341234,
 *          "cognito":"user_status=CONFIRMED",
 *          "email_verified=true",
 *          "name=Blundell",
 *          "email=foo@gmail.com"
 *      }
 *  },
 *  "response="{}
 * }
 * // formmatterON
 */
@Suppress("PropertyName") // Names match example, as they are used to parse JSON
class PostConfirmationConfirmSignUp {
    var version = 0
    var region: String? = null
    var userPoolId: String? = null
    var userName: String? = null
    var callerContext: CallerContext? = null
    var triggerSource: String? = null
    var request: Request? = null
    var response: Response? = null

    class CallerContext {
        var awsSdkVersion: String? = null
        var clientId: String? = null
        override fun toString(): String {
            return "CallerContext(awsSdkVersion=$awsSdkVersion, clientId=$clientId)"
        }
    }

    class Request {
        var userAttributes: UserAttributes? = null
        override fun toString(): String {
            return "Request(userAttributes=$userAttributes)"
        }
    }

    class UserAttributes {
        var sub: String? = null
        var cognito: Cognito? = null
        var email_verified: String? = null
        var name: String? = null
        var email: String? = null
        override fun toString(): String {
            return "UserAttributes(sub=$sub, cognito=$cognito, email_verified=$email_verified, name=$name, email=$email)"
        }
    }

    class Cognito {
        var user_status: String? = null
        override fun toString(): String {
            return "Cognito(user_status=$user_status)"
        }
    }

    class Response {
        override fun toString(): String {
            return "Response()"
        }
    }

    override fun toString(): String {
        return "Example(version=$version, region=$region, userPoolId=$userPoolId, userName=$userName, callContext=$callerContext, triggerSource=$triggerSource, request=$request, response=$response)"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM