58

I am new Cognito. I am trying to implement AWS Cognito using Lambda. This is the tutorial I am following.

AmazonCognitoIdentityClient client =
                new AmazonCognitoIdentityClient();
    GetOpenIdTokenForDeveloperIdentityRequest tokenRequest = new GetOpenIdTokenForDeveloperIdentityRequest();
    tokenRequest.setIdentityPoolId("us-east-1_XXXXXXX");

This is the pool Id that I am using in the setIdentityPoolId

enter image description here

This is the JUnit test

public class AuthenticateUser implements RequestHandler<Object, Object> {

@Override
public Object handleRequest(Object input, Context context) {

    AuthenticateUserResponse authenticateUserResponse = new AuthenticateUserResponse();
    @SuppressWarnings("unchecked")
    LinkedHashMap inputHashMap = (LinkedHashMap)input;
    User user = authenticateUser(inputHashMap);
    return null;
}

public User authenticateUser(LinkedHashMap input){
    User user = null;
    String userName = (String) input.get("userName");
    String passwordHash = (String) 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){
            System.out.println("user found");
            if(user.getPasswordHash().equals(passwordHash)){
                System.out.println("user password matched");
                String openIdToken = getOpenIdToken(user.getUserId());
                user.setOpenIdToken(openIdToken);
                return user;
            } else {
                System.out.println("password unmatched");
            }
        } else {
            System.out.println("user not found");
        }
    } catch (Exception e) {
        System.out.println("Error: " + e.toString());
    }

    return user; 
}

This is the output

user found
user password matched

But I am getting the following error and hence, the return user statement is failing

1 validation error detected: Value 'us-east-1_XXXXXX' at 'identityPoolId' 
failed to satisfy constraint: Member must satisfy regular expression pattern: [\w-]+:[0-9a-f-]+ 
(Service: AmazonCognitoIdentity; Status Code: 400; Error Code: ValidationException; 
suku
  • 10,507
  • 16
  • 75
  • 120

3 Answers3

160

You are using a Cognito user pool id as the identity pool id. They are two different things. Identity pool ids are of format us-east-1:XXXX-XXXXXX-XXXX-XXXX.

To get an identity pool id you should use the "Manage Federated Identities" parts of the Cognito console not the "Manage User Pools" section.

starball
  • 20,030
  • 7
  • 43
  • 238
Chetan Mehta
  • 5,491
  • 1
  • 22
  • 21
  • 3
    @Chetan- I think you should use the upvotes on this answer as feedback to make the tutorial better and the better name than "Manage Federated Identities" for the Identity Pool – suku Feb 12 '17 at 14:38
  • 1
    @Chetan How would you get identity pool id from user pool id? Or get the current role that is associated with the cognito user? – Baked Inhalf Oct 05 '17 at 06:35
  • There is also the describeUserPool that returns the informations about the user pool: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#describeUserPool-property – Kadu Diógenes Apr 05 '18 at 16:55
  • I created "Identity pool", in the Authentication providers added Cognito User Pool. And I still have this issue. It looks like some mapping should be added.... – Dennis Liger Jun 03 '22 at 07:17
1

"User Pools" and "Federated Identities" are different things. Make sure that you are not providing "aws_cognito_identity_pool_id" in config.

My config looks like:

...
    "Auth": {
        "region": "us-east-1",
        "userPoolId": "<...>",
        "userPoolWebClientId": "<...>",
        "mandatorySignIn": false,
        "oauth": {
            "domain": "<...>.auth.us-east-1.amazoncognito.com",
            "scope": [
                "phone",
                "email",
                "openid",
                "profile",
                "aws.cognito.signin.user.admin"
            ],
            "redirectSignIn": "<...>",
            "redirectSignOut": "<...>",
            "responseType": "code"
        }
    }
...

In User Pool - Allowed OAuth Flows

  • Authorization code grant - check
  • Implicit grant - check
Dennis Liger
  • 1,488
  • 2
  • 13
  • 28
0

You can find it in User Pools > Federated Identities > App clients > App client id

enter image description here

Abudayah
  • 3,816
  • 7
  • 40
  • 60