Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AWS Cognito can I resolve the authenticated IdentityId given a disabled unauthenticated IdentityId?

I have a JavaScript web application that supports Cognito unauthenticated identities. I'm trying to figure out how to identify the linked authenticated IdentityId for a DISABLED unauthenticated IdentityId.

First unauthenticated users are issued an IdentityId via AWS.config.credentials.get. Internally CognitoIdentityCredentials is using getId to generate a new unauthenticated IdentityId.

let unathenticatedIdentityId;

const AWS = require('aws-sdk');
AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId
});
AWS.config.credentials.get(err => {
    unathenticatedIdentityId = AWS.config.credentials.identityId;
});

Then our user authenticates to a Cognito User Pool via amazon-cognito-identity-js and the unauthenticated IdentityId changes to the authenticated IdentityId associated with their Cognito Login. The unauthenticated IdentityId is automatically marked DISABLED and is linked internally to the authenticated IdentityId.

let authenticatedIdentityId;

const { CognitoUserPool, CognitoUser, AuthenticationDetails } = require('amazon-cognito-identity-js');
const Pool = new CognitoUserPool({
    UserPoolId,
    ClientId,
});
const authDetails = new AuthenticationDetails({
    Username,
    Password,
});
const user = new CognitoUser({
    Pool,
    Username,
});
user.authenticateUser(authDetails, {
    onSuccess: (session) => {
        AWS.config.credentials.params.Logins = {
            [PoolProviderName]: session.idToken.jwtToken,
        };
        AWS.config.credentials.expired = true;

        AWS.config.credentials.refresh(err => {
            authenticatedIdentityId = AWS.config.credentials.identityId;
        });
    },
});

I have the value for unathenticatedIdentityId and authenticatedIdentityId but I do not see a way in the AWS Cognito API's to resolve that the DISABLED unauthenticatedIdentityId has been linked to the authenticatedIdentityId. Conversely I do not see a way to identify what IdentityIds have been linked to the authenticatedIdentityId. The describeIdentity API will tell me that unauthenticatedIdentityId is DISABLED and that it has no Logins, but it does not point to the linked authenticatedIdentityId.

How can I, with only the value of the linked/DISABLED unauthenticatedIdentityId, resolve the value authenticatedIdentityId?

like image 674
Eric Schoonover Avatar asked Sep 04 '17 22:09

Eric Schoonover


1 Answers

I have an app that uses AWS Cognito to obtain an identity id and then possibly authenticate it. The situation is a client uses the app first as unauthenticated (guest) and then logs in using Facebook, making him/herself as authenticated, and AWS preserves the given identity ID for the authenticated user, because he is a new user. Now, the problem comes, when you log out of the app and someone else wants to use this app as unauthenticated or even authenticated. Cognito will error out saying that the access to the identity ID is forbidden, because it has already been linked to the previous user's Facebook account.

The Cognito mobile SDKs have a way built in to handle this. They cache the identity id when using it, which is causing the issue you are seeing. When you log out, you'll want to clear that cache. I'm not sure which SDK you're using, but in iOS it's AWSCognitoIdentityProvider.clear() and CognitoCachingCredentialsProvider.clear() in Android. Similarly, if you're using Cognito Sync, there's a method in that client that will wipe the cached id and sync data.

Also have a look at https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-authentication/

Hope you are also following https://aws.amazon.com/blogs/mobile/using-the-amazon-cognito-credentials-provider/

like image 145
N.K Avatar answered Oct 03 '22 14:10

N.K