How to list all Amazon Cognito Users using Java

Amazon Cognito User Pools provide a secure user directory that scales to hundreds of millions of users. As a fully managed service, User Pools are easy to set up without any worries about standing up server infrastructure.

With the Amazon Cognito SDK, you just write a few lines of code to enable your users to sign-up and sign-in to your mobile and web apps.

Lists the users in the Amazon Cognito user pool.

Resets the specified user's password in a user pool as an administrator. Works on any user.

The cognitoClient has listUsers metod with gives number of users in the cognito pool as list.The maximum number of users to be returned in one single request is 60.So we need to call listUsrs API multiple times if users are greater than 60.

The listUsers method which is based on pagination. If there are still more users left after a successful listUsers-request, the listUsers-result contains a PaginationToken, that can be used for the next request.

Maven dependency

<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-core</artifactId> <version>1.11.764</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-cognitoidp</artifactId> <version>1.11.764</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.11.360</version> </dependency>

We have cognitoClient instance

public static AWSCognitoIdentityProvider getAWSCognitoIdentityClient() { System.setProperty("aws.accessKeyId", "-- your accessKey Id--"); System.setProperty("aws.secretKey", "-- your secret Key--"); AWSCognitoIdentityProvider cognitoClient = AWSCognitoIdentityProviderClientBuilder.standard().withRegion(Regions.AP_SOUTH_1).withCredentials(new SystemPropertiesCredentialsProvider()).build(); return client; }

Request Syntax

{ "AttributesToGet": [ "string" ], "Filter": "string", "Limit": number, "PaginationToken": "string", "UserPoolId": "string" }

AttributesToGet

An array of strings, where each string is the name of a user attribute to be returned for each user in the search results. If the array is null, all attributes are returned.

Filter

A filter string of the form "AttributeName Filter-Type "AttributeValue"". Quotation marks within the filter string must be escaped using the backslash (\) character. For example, "family_name = \"Reddy\"".

AttributeName: The name of the attribute to search for. You can only search for one attribute at a time.

Filter-Type:

For an exact match, use =, for example, "given_name = \"Jon\"". For a prefix ("starts with") match, use ^=, for example, "given_name ^= \"Jon\"".

AttributeValue:

The attribute value that must be matched for each user.

If the filter string is empty, ListUsers returns all users in the user pool.

You can only search for the following standard attributes:

  • username (case-sensitive)
  • email
  • phone_number
  • name
  • given_name
  • family_name
  • preferred_username
  • cognito:user_status (called Status in the Console) (case-insensitive)
  • status (called Enabled in the Console) (case-sensitive)
  • sub

Limit

Maximum number of users to be returned. Type: Integer

PaginationToken

An identifier that was returned from the previous call to this operation, which can be used to return the next set of items in the list.
Type: String

UserPoolId

The user pool ID for the user pool on which the search should be performed. Type: String

Response Syntax { "PaginationToken": "string", "Users": [ { "Attributes": [ { "Name": "string", "Value": "string" } ], "Enabled": boolean, "MFAOptions": [ { "AttributeName": "string", "DeliveryMedium": "string" } ], "UserCreateDate": number, "UserLastModifiedDate": number, "Username": "string", "UserStatus": "string" } ] }

Response Elements

If the action is successful, the service sends back an HTTP 200 response.

The following data is returned in JSON format by the service.

PaginationToken

An identifier that was returned from the previous call to this operation, which can be used to return the next set of items in the list.
Type: String
Length Constraints: Minimum length of 1.
Pattern: [\S]+

Users

The users returned in the request to list users.
Type: Array of UserType objects

ListUsersResult result = cognitoClient.listUsers(listUsersRequest); List < UserType > users = new ArrayList < UserType > (); while (result.getPaginationToken() != null) { try { listUsersRequest.setPaginationToken(result.getPaginationToken()); result = identityProvider.listUsers(listUsersRequest); users.addAll(result.getUsers()); } catch(TooManyRequestsException e) { } } //process users as our needs

In this article, we have seen How to list all Amazon Cognito Users using Java.