Google Authentication fails with AWS Cognito Identity Pool on Android

mipnw

I get an NotAuthorizedException:Token is not from a supported provider of this identity pool when I call Amazon.CognitoIdentity.AmazonCognitoIdentityClient.GetIdAsync()

I do not understand why, the token was obtained by authenticating with GoogleSignInApi, and the AWS Identity Pool is configured to federate to the Google authentication provider with the same "Google WebApp Client ID" used to authenticate on the Android device.

enter image description here

Also I've tried to obtain the Google token using 2 different ways

  • using the result of .RequestIdToken() on the GoogleSignInOptions
  • by calling the GoogleAuthUtil.GetToken API

Both tokens are different when inspected, both look like good tokens, and both fail with the same error when given to AmazonCognitoIdentityClient. Clearly the user is authenticated on the Android device, the app is able to get the Email, DisplayName etc...

var googleSignInOptions = new 
GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
   .RequestIdToken("Google WebApp Client ID")
   .RequestEmail()
   .Build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
   .EnableAutoManage(
      this, // FragmentActivity
      this) // OnConnectionFailedListener
   .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
   .Build();

mGoogleApiClient.Connect();

var result = await Auth.GoogleSignInApi.SilentSignIn(mGoogleApiClient);

// Only need one or the other, trying to figure out which
var idToken = result.SignInAccount.IdToken;
var authToken = await GetGoogleAuthTokenAsync(result.SignInAccount.Email);

var shortLivedAWScredentials = new CognitoAWSCredentials("identity-pool-id", AWSConfigs.RegionEndpoint);
var cognitoClient = new AmazonCognitoIdentityClient(shortLivedAWScredentials,AWSConfigs.RegionEndpoint);

var logins = new Dictionary<string, string>();
logins["accounts.google.com"] = idToken; // same failure if I use authToken

var request = new GetIdRequest();
request.IdentityPoolId = "identity-pool-id";
request.Logins = logins;

var result = await cognitoClient.GetIdAsync(request); // THIS THROWS Amazon.CognitoIdentity.Model.NotAuthorizedException

private async Task<string> GetGoogleAuthTokenAsync(string accountEmail)
{
   Account googleAccount = new Account(accountEmail, GoogleAuthUtil.GoogleAccountType);
   string scopes = "audience:server:client_id:" + "Google WebApp Client ID"
   var token = await Task.Run(() => { return GoogleAuthUtil.GetToken(this, googleAccount, scopes); });
   return token;
}

Notes - Right after the exception, the AWS Console shows the Cognito identity pool grows by 1 unauthenticated identity, no change to the number of google identities enter image description here

mipnw

Hours of searching and I finally found a solution. Basically AWS Cognito Identity Pool Federation to Google+ is completely broken, but do not despair. What you need to do is Federate to an OpenID provider instead.

First, go to AWS console > IAM > Identity Providers > Create Provider > Provider Type = OpenID Connect > Provider URL = https://accounts.google.com > Audience = "the Google Client ID for Android or iOS you created in the Google Developer Console"

Be careful, do not use the "Google Web App Client ID" for the Audience.

Second, go to AWS Console > Cognito > Federated Identity Pools. Select your Identity Pool, then click "Edit Identity Pool", then navigate to the OpenID tab (not the Google+ tab, don't even use that one it doesn't work). You should now see a checkbox titled "accounts.google.com", check it.

Third, go edit your mobile app source code, and make sure you use the "Google WebApp Client ID" you generated in the Google Developer Console when you build the scopes string used to call GoogleAuthUtil.GetTokenGoogleAuthUtil.GetToken(this, googleAccount, scopes). That is scopes = "audience:server:client_id:" + "webClientId"

Now your call to var result = await cognitoClient.GetIdAsync(request); should succeed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

AWS Cognito Federated Identity Pool Custom Authentication Provider Sing out / logout issue

Cannot create AWS Cognito Identity Pool

AWS Cognito Invalid identity pool configuration

Cognito authentication using federated identity does not create a user in the pool

How to I configure Okta as SAML IdP for AWS Cognito Identity Pool?

AWS Cognito Identity Pool: How to check if email attribute is verified or not

How to get temporary AWS credentials via Cognito Identity Pool

aws service difference between cognito user pool and federated identity

AWS Cognito: "Unauthenticated access is not supported for this identity pool."

Get identity provider oauth tokens in AWS cognito user pool

Spring Secutity 5 as an OIDC Identity Provider for AWS Cognito User Pool

How to specify Amazon AWS User Pool in trust relationship with Cognito Identity

AWS Cognito & Lambda: add federated identity to user pool

Edit AWS Cognito Identity Pool using serverless.yml

AWS Cognito User Pool Sign In Missing Authentication Token

AWS Cognito User Pool Custom Authentication Lambdas Not Triggering

AWS CloudFormation Script Fails - Cognito is not allowed to use your email identity

How to use Cognito identity pool with UnAuthenticatd users in Amplify for Android

AWS Cognito Pool Trigger

AWS Cognito role: Distinguish between Federated Identity Pool roles and User Pool Group roles

AWS Cognito Google Identity Provider Get Profile First & Last Name

Authentication with Lambda and AWS Cognito

AWS Cognito Authentication in Reactjs

AWS Cognito HTTP authentication

AWS Cognito User Authentication

Getting cognito user pool username from cognito identity pool identityId

Android AWS SDK : Cognito Identity not was not found in region metadata

Amazon Web Services (AWS) Cognito error "Token is not from a supported provider of this identity pool."

AWS Cognito Identity user ID or User Pool user ID? Which to use?