Search code examples
amazon-web-servicesboto3amazon-cognitoaws-userpools

how to identify a cognito id belongs to a pool using boto3?


Using bellow code i am getting an OpenID token for an IdentityId.

import boto3
cognito_client = boto3.client('cognito-identity')
data = {'IdentityPoolId': identity_pool_id,
        'Logins': logins,
        'TokenDuration': 24 * 60 * 60,
        'IdentityId': identity_id}
cognito_client.get_open_id_token_for_developer_identity(
            **data)

But i have two IdentityPoolId, so i need to identify proper IdentityPoolId for a given IdentityId.
Using boto3 library how can i identify a IdentityId belongs to a IdentityPoolId or not?


Solution

  • If all you have available is just the IdentityId value you would need to use list_identities() function of the Cognito-Identity client to get all of the IDs from a pool and then determine if your ID is part of the pool.

    https://boto3.readthedocs.io/en/latest/reference/services/cognito-identity.html#CognitoIdentity.Client.list_identities

    import boto3
    
    def getPoolID(boto3Client,PoolIDs,idVal):
        For PoolID in PoolIDs: 
            response = boto3Client.list_identities(IdentityPoolId=PoolID)   
            for ID in response['Identities']:
                if idVal == ID['IdentityId']:
                    return PoolID
    
    cognitoClient = boto3.client('cognito-identity')
    PoolIDList=['PoolID1','PoolID2'] 
    IdentityIdToCheck='Value'
    FinalPoolID = getPoolID(cognitoClient,PoolIDList,IdentityIdToCheck)
    print FinalPoolID
    

    You might need to adjust how you get the response based on the number of IDs in your ID pools, as you may need to paginate through the records