aws-sdk-android icon indicating copy to clipboard operation
aws-sdk-android copied to clipboard

CognitoUserPool should read Endpoint from awsconfiguration.json

Open mohamed-khaled-hsn opened this issue 3 years ago • 6 comments

CognitoUserPool doesn't read the value of the custom endpoint set in awsconfiguration.json file

Constructor with the problem

public CognitoUserPool(Context context, AWSConfiguration awsConfiguration)

This behavior is implemented in AWSMobileClient here

A fix will essentially read the Endpoint set in the file and call client.setEndpoint(cognitoUserPoolCustomEndpoint) instead of setting the region after checking if it is set in the file

I can open a PR for the fix, I wanted to check if this behavior is done intentionally or not

mohamed-khaled-hsn avatar Dec 20 '21 15:12 mohamed-khaled-hsn

How did you set the custom endpoint in awsconfiguration.json file? Manully change the file or using Amplify CLI?

changxu0306 avatar Dec 23 '21 09:12 changxu0306

How did you set the custom endpoint in awsconfiguration.json file? Manully change the file or using Amplify CLI?

By manually changing it or it can also be built by a JSON object it looks like this in the file

  "CognitoUserPool": {
    "Default": {
      "PoolId": "us-west-2_",
      "AppClientId": "123",
      "Region": "us-west-2",
      "Endpoint":  "https://example.com/"
    }
  }

You can also see it being parsed in AWSMobileClient here

mohamed-khaled-hsn avatar Dec 23 '21 09:12 mohamed-khaled-hsn

The issue here is for the repo aws-android-sdk-cognitoidentityprovider CognitoUserPool.java class

mohamed-khaled-hsn avatar Dec 23 '21 12:12 mohamed-khaled-hsn

For reference: PR in amplify-ios to fix this issue https://github.com/aws-amplify/amplify-ios/pull/1715

div5yesh avatar Apr 12 '22 18:04 div5yesh

@mohamed-khaled-hsn My understanding is this is already present in Android SDK as per this PR and comment and description in the iOS fix PR. Is my understanding correct? or are you looking for any additional feature?

sktimalsina avatar May 04 '22 19:05 sktimalsina

@mohamed-khaled-hsn My understanding is this is already present in Android SDK as per this PR and comment and description in the iOS fix PR. Is my understanding correct? or are you looking for any additional feature?

I'm looking for additional feature would like to use the CognitoUserPool constructor that takes an AWSConfiguration but I can't because I'm using a custom endpoint and currently it doesn't parse it

I had written a workaround here that uses the constructor mentioned in the PR you linked

/**
 * Builds a [CognitoUserPool] from the passed this [AWSConfiguration],
 * this doesn't use the constructor that takes [AWSConfiguration] because it doesn't parse
 * custom cognito endpoint, see issue https://github.com/aws-amplify/aws-sdk-android/issues/2747
 */
fun AWSConfiguration.cognitoUserPool(context: Context): CognitoUserPool {
    val cognitoUserPoolJson = optJsonObject("CognitoUserPool")
    val poolId = cognitoUserPoolJson.getString("PoolId")
    val clientId = cognitoUserPoolJson.getString("AppClientId")
    val clientSecret = cognitoUserPoolJson.optString("AppClientSecret")
    val pinpointEndpointId = CognitoPinpointSharedContext.getPinpointEndpoint(
        context,
        cognitoUserPoolJson.optString("PinpointAppId")
    )
    val cognitoUserPoolCustomEndpoint = cognitoUserPoolJson.optString("Endpoint")
    val client = AmazonCognitoIdentityProviderClient(
        AnonymousAWSCredentials(),
        ClientConfiguration()
    ).apply {
        if (cognitoUserPoolCustomEndpoint.isNotEmpty()) {
            endpoint = cognitoUserPoolCustomEndpoint
        } else {
            setRegion(Region.getRegion(Regions.fromName(cognitoUserPoolJson.getString("Region"))))
        }
    }
    return CognitoUserPool(
        context,
        poolId,
        clientId,
        clientSecret,
        client,
        pinpointEndpointId,
        cognitoUserPoolCustomEndpoint
    )
}

Hope that cleared the confusion

mohamed-khaled-hsn avatar May 04 '22 20:05 mohamed-khaled-hsn