Sahil Khanna
Sahil Khanna

Reputation: 4382

AWS Cognito Developer Authenticated Identities using JavaScript SDK

I need to implement Developer Authenticated Identities using JavaScript SDK, but am facing issues with it. I've configured an identity pool with a Custom Authentication Provider

On Server:

AWS.config = new AWS.Config({
    region: 'ap-northeast-2',   
    credentials: new AWS.Credentials('XXXXXS7FJBAOO5IXXXXX', 'XXXXXYBo4jmfsu7K0qJSFvu3nlVvYOcVz4GXXXXX')
});

var params = {
    IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
    Logins: {
        'com.abc.xyz': '9876543210' // different value for each user
    }
};

var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, function(err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
    }
    else {
        console.log(data);           // successful response
    }
});

Server Result:

IdentityId: "ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX"
Token: "eyJra.....sL8bg"

On Browser:

AWS.config = new AWS.Config({
    region: 'ap-northeast-2'
});

var params = {
    IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX',      //Received from server
    CustomRoleArn: 'arn:aws:iam::356127965XXX:role/XXXXX_Customer',
    Logins: {
        'com.abc.xyz': '9876543210'
    }
};

var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getCredentialsForIdentity(params, function(err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
    }
    else {
        console.log(data);           // successful response
    }
});

Browser Result:

Please provide a valid public provider

Identity Pool Configuration Identity Pool Configuration

Upvotes: 5

Views: 1244

Answers (2)

piisexactly3
piisexactly3

Reputation: 779

I realize this is an old post, but in case anyone comes across this, I believe your first approach would have worked had you changed:

Logins: {
    'com.abc.xyz': '9876543210'
}

To

Logins: {
    'cognito-identity.amazonaws.com': "eyJra.....sL8bg"
}

I feel that any solution without using the token you generated in step 1) is incomplete.

Upvotes: 1

Sahil Khanna
Sahil Khanna

Reputation: 4382

Based on the this post, I've made the following changes in Browser part

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX',      //Received from server
IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
    Logins: {
        'cognito-identity.amazonaws.com': '9876543210'
    }
});

AWS.config.credentials.get(function(err, data) {
    if (err) {
        console.log(err); // an error occurred
    }
    else {
        console.log(data);           // successful response
    }
});

AWS.config.credentials

Now I'm able to receive the response that contains accessKeyId, expireTime, secretAccessKey and sessionToken

Upvotes: 3

Related Questions