Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get AWSCredentials given a AWS Cognito access_token

In an android app, I receive a JWT access_token from http://<domain>.auth.<region>.amazoncognito.com/login once the user is done authenticating to a Cognito User Pool. That User Pool is linked to a Cognito Identity Pool.

What API should I call with that access_token to get an AWSCredentials object.

The closest one I found would be AssumeRoleWithWebIdentity, but that is an STS API, and some of what I've read on the web seems to recommend developers not use STS directly but rely on Cognito.

Moreover, I do not expect the API I need to require specifying a role name. Cognito Identity Pools are already configured to give authenticated users a specific role. And AssumeRoleWithWebIdentity takes a role name as input to the API. Hence that does not look like right.

I've looked at Cognito Identity Pool API Reference, and can't find an API that takes access_token and return AWS credentials.

UPDATE: The following answer which uses GetCredentialsForIdentity throws ResourceNotFoundException saying it cannot find the specified IdentityId.

string access_token = ...
var jwtAccessToken = System.IdentityModel.Tokens.Jwt.JwtSecurityToken(access_token);

var client = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials(),REGION);

var response = await client.GetCredentialsForIdentityAsync(new GetCredentialsForIdentityRequest
{
    IdentityId=String.Format("{0}:{1}", REGION, jwtAccessToken.id),
    Logins=new Dictionary<string,string> 
    { 
        {String.Format("cognito-idp.{0}.amazonaws.com/{1}", REGION, USER_POOL_ID),
         access_token}
    }
});
like image 581
mipnw Avatar asked Apr 08 '18 20:04

mipnw


People also ask

How do I get user pool ID in Cognito?

In order to get your Identity Pool's ID in AWS Cognito, you have to: Open the AWS Cognito console and click on Manage Identity Pools. Select your Identity pool from the list. Click on the Edit identity pool button at the top right corner.

How do I get my AWS Cognito access token?

You can request an access token for a custom scope from the token endpoint when, in the app client, the requested scope is enabled, you have configured a client secret, and you have allowed client_credentials grants. Required. The ID of an app client in your user pool.

How do you use the code returned from Cognito to get AWS credentials?

Simply, You can request the id/access/refresh tokens using the code and the Cognito clientId+hostname, then use the id and access token to identify the user in your API calls.

How do I get AWS Cognito refresh token?

Initiate new refresh tokens (API)Pass REFRESH_TOKEN_AUTH for the AuthFlow parameter. The authorization parameter, AuthParameters , is a key-value map where the key is "REFRESH_TOKEN" and the value is the actual refresh token. Amazon Cognito returns new ID and access tokens after your API request passes all challenges.


1 Answers

After much investigation, I found the answer.

1- One needs an id_token not an access_token to authenticate to Cognito, as misleading as this might sound. AWS's documentation which says you ask for id_token when you need to have user attributes like name / email etc... and ask for an access_token when you don't need that information and just want to authenticate is wrong, or at the very least misleading.

2- And here's how you use an id-token to get AWS Credentials:

var credentials = CognitoAWSCredentials(<identity pool Id>, region);
credentials.AddLogin(
    "cognito-idp.<region>.amazonaws.com/<user_pool_id>",
    id_token); // the raw token

Note that you do not need AssumeRoleWithIdentity, or GetCredentialsWithIdentity, you do not even need a AmazonCognitoIdentityClient.

like image 157
mipnw Avatar answered Oct 03 '22 03:10

mipnw