HelloCoding
HelloCoding

Reputation: 119

Can I use the ListUsers API to query a Cognito User Pool by a user's uuid?

The docs for cognito user pools can be found here:

http://docs.aws.amazon.com/cognito/latest/developerguide/how-to-manage-user-accounts.html

In this they do not say whether you can query users by the automatically generated sub attribute, which is a uuid. It explicitly says you can't search for users by custom attributes, but sub/uuid is not a custom attribute. Weirdly though, in the list of searchable attributes sub/uuid is not one of them. Surely though you can look up users by their UUID, how would this be done though??

Upvotes: 0

Views: 5240

Answers (1)

Matt
Matt

Reputation: 492

You know, I have used COgnito but never needed to look up via sub (or other params other than the username). I looked into it because surely you can, but it is not very clear (like a lot of their documentation). Here is what I saw that you could try... hope it helps man.

        // the imported ListUsersResult is...
        import com.amazonaws.services.cognitoidp.model.ListUsersRequest;
        import com.amazonaws.services.cognitoidp.model.ListUsersResult;

             // class var

         protected final AWSCognitoIdentityProviderClient identityUserPoolProviderClient;

        // omitted stuff... 
        // initialize the Cognito Provider client.  This is used to talk to the user pool
        identityUserPoolProviderClient = new AWSCognitoIdentityProviderClient(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)); // creds are loaded via variables that are supplied to my program dynamically
        identityUserPoolProviderClient.setRegion(RegionUtils.getRegion(USER_POOL_REGION)); // var loaded 


        // ...some code omitted        
        ListUsersRequest listUsersRequest = new ListUsersRequest();
        listUsersRequest.withUserPoolId(USER_POOL_ID); // id of the userpool, look this up in Cognito console
        listUsersRequest.withFilter("sub=xyz");  // i THINK this is how the Filter works... the documentation is terribad


    // get the results 
    ListUsersResult result = identityUserPoolProviderClient.listUsers(listUsersRequest); 

    List<UserType> userTypeList = result.getUsers();
    // loop through them
    for (UserType userType : userTypeList) {
        List<AttributeType> attributeList = userType.getAttributes();
        for (AttributeType attribute : attributeList) {
            String attName = attribute.getName();
            String attValue = attribute.getValue();
            System.out.println(attName + ": " + attValue);
        }
    }

If you have the username you could get the user like this

    // build the request
    AdminGetUserRequest idRequest = new AdminGetUserRequest();
    idRequest.withUserPoolId(USER_POOL_ID);
    idRequest.withUsername(username);

    // call cognito for the result
    AdminGetUserResult result = identityUserPoolProviderClient.adminGetUser(idRequest);
    // loop through results 

Upvotes: 2

Related Questions