AWS Developer Tools Blog
Building a serverless developer authentication API in Java using AWS Lambda, Amazon DynamoDB, and Amazon Cognito – Part 2
In part 1 of this blog post, we showed you how to leverage the AWS Toolkit for Eclipse to quickly develop Java functions for AWS Lambda. We then set up a skeleton project and the structure to handle custom objects sent to your Java function.
In part 2 of this blog post, we will implement the handleRequest function that will handle the logic of interacting with Amazon DynamoDB and then generate an OpenID token by using the Amazon Cognito API.
We will now implement the handleRequest function within the AuthenticateUser class. Our final handleRequest function looks like the following:
@Override
public AuthenticateUserResponse handleRequest(Object input, Context context) {
AuthenticateUserResponse authenticateUserResponse = new AuthenticateUserResponse();
@SuppressWarnings("unchecked")
LinkedHashMap inputHashMap = (LinkedHashMap)input;
User user = authenticateUser(inputHashMap);
if(user!=null){
authenticateUserResponse.setUserId(user.getUserId());
authenticateUserResponse.setStatus("true");
authenticateUserResponse.setOpenIdToken(user.getOpenIdToken());
}else{
authenticateUserResponse.setUserId(null);
authenticateUserResponse.setStatus("false");
authenticateUserResponse.setOpenIdToken(null);
}
return authenticateUserResponse;
}
We will need to implement the authenticateUser function for this Lambda Java function to compile properly. Implement the function as shown here:
public User authenticateUser(LinkedHashMap input){
User user=null;
String userName = input.get("userName");
String passwordHash = input.get("passwordHash");
try{
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(Regions.US_EAST_1));
DynamoDBMapper mapper = new DynamoDBMapper(client);
user = mapper.load(User.class, userName);
if(user!=null){
if(user.getPasswordHash().equalsIgnoreCase(passwordHash)){
String openIdToken = getOpenIdToken(user.getUserId());
user.setOpenIdToken(openIdToken);
return user;
}
}
}catch(Exception e){
System.out.println(e.toString());
}
return user;
}
In this function, we use the DynamoDB Mapper to check if a row with the provided username attribute exists in the table User. Make sure you set the region in your code. If a row with the username exists, the code makes a simple check against the provided password hash value. If the passwords match, we will authenticate this user and then follow the developer authentication flow to get an OpenID token from the CognitoIdentityBroker. The token will be passed to the client as an attribute in the AuthenticationResponse object. For about information about the authentication flow for developer authenticated identities, see the Amazon Cognito documentation here. For this Java Lambda function, we will be using the enhanced authflow.
Before we can get an OpenID token, we need to create an identity pool in Amazon Cognito and then register our developer authentication provider with this identity pool. When you create the identity pool, you can keep the default roles provided by the console. In the Authentication Providers field, in the Custom section, type login.yourname.services.
After the pool is created, implement the getOpenIdToken as shown:
private String getOpenIdToken(Integer userId){
AmazonCognitoIdentityClient client = new AmazonCognitoIdentityClient();
GetOpenIdTokenForDeveloperIdentityRequest tokenRequest = new GetOpenIdTokenForDeveloperIdentityRequest();
tokenRequest.setIdentityPoolId("us-east-1:6dbccdfd-9444-4d4c-9e1b-5d1139cbe863");
HashMap map = new HashMap();
map.put("login.dhruv.services", userId.toString());
tokenRequest.setLogins(map);
tokenRequest.setTokenDuration(new Long(10001));
GetOpenIdTokenForDeveloperIdentityResult result = client.getOpenIdTokenForDeveloperIdentity(tokenRequest);
String token = result.getToken();
return token;
}
This code calls the GetOpenIdTokenForDeveloperIdentity function in the Amazon Cognito API. You need to pass in your Amazon Cognito identity pool ID along with the unique identity provider string you entered in the Custom field earlier. You also have to provide a unique identifier for the user so Amazon Cognito can map that to its Cognito ID. This unique ID is usually the user ID you use internally, but it can be any other unique attribute that allows both your authentication back end and Amazon Cognito to identify a user.
In part 3 of this blog, we will test the Java Lambda function locally using JUnit. Then we will upload and test the function on Lambda.