Skip to content

Commit 4d337c7

Browse files
authored
"feat(aws-android-sdk-cognitoidentityprovider): support custom endpoint" (#2455)
1 parent 9830c2e commit 4d337c7

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUserPool.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ public CognitoUserPool(Context context, String userPoolId, String clientId, Stri
274274
this.pinpointEndpointId = CognitoPinpointSharedContext.getPinpointEndpoint(context, pinpointAppId);
275275
}
276276

277-
278277
/**
279278
* Constructs a user-pool with default {@link ClientConfiguration}.
280279
*
@@ -303,15 +302,37 @@ public CognitoUserPool(Context context, String userPoolId, String clientId, Stri
303302
* @param pinpointAppId REQUIRED: AWS Pinpoint App Id for analytics.
304303
*/
305304
public CognitoUserPool(Context context, String userPoolId, String clientId, String clientSecret, AmazonCognitoIdentityProvider client, String pinpointAppId) {
305+
this(context, userPoolId, clientId, clientSecret, client, pinpointAppId, null);
306+
}
307+
308+
/**
309+
* Constructs a user-pool with custom endpoint for Cognito User Pool
310+
*
311+
* @param context REQUIRED: Android application context.
312+
* @param userPoolId REQUIRED: User-pool-Id of the user-pool.
313+
* @param clientId REQUIRED: Client-Id generated for this app and user-pool at the
314+
* Cognito Identity Provider developer console.
315+
* @param clientSecret REQUIRED: Client Secret generated for this app and user-pool at
316+
* the Cognito Identity Provider developer console.
317+
* @param pinpointAppId REQUIRED: AWS Pinpoint App Id for analytics.
318+
* @param cognitoUserPoolCustomEndpoint REQUIRED: Custom endpoint for Cognito Userpool
319+
*/
320+
public CognitoUserPool(Context context, String userPoolId, String clientId, String clientSecret, AmazonCognitoIdentityProvider client, String pinpointAppId, String cognitoUserPoolCustomEndpoint) {
306321
initialize(context);
307322
this.context = context;
308323
this.userPoolId = userPoolId;
309324
this.clientId = clientId;
310325
this.clientSecret = clientSecret;
311326
this.client = client;
312327
this.pinpointEndpointId = CognitoPinpointSharedContext.getPinpointEndpoint(context, pinpointAppId);
328+
329+
// check if the custom endpoint is not empty
330+
if(cognitoUserPoolCustomEndpoint != null && !cognitoUserPoolCustomEndpoint.isEmpty()) {
331+
this.client.setEndpoint(cognitoUserPoolCustomEndpoint);
332+
}
313333
}
314334

335+
315336
private void initialize(final Context context) {
316337
this.awsKeyValueStore = new AWSKeyValueStore(context,
317338
DEFAULT_SHARED_PREFERENCES_NAME,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import android.content.Context;
17+
18+
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool;
19+
import com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProvider;
20+
import com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.mockito.Mock;
25+
import org.mockito.MockitoAnnotations;
26+
27+
import static org.mockito.Mockito.verify;
28+
import static org.mockito.Mockito.verifyZeroInteractions;
29+
30+
/**
31+
* Unit test cases for checking the custom end points for Cognito User Identity Pool
32+
*/
33+
public class CognitoIdentityProviderCustomEndPointTest {
34+
35+
private CognitoUserPool testPool;
36+
public static final String TEST_USER_POOL = "DummyUserPool";
37+
public static final String TEST_CLIENT_ID = "DummyClientId";
38+
public static final String TEST_CLIENT_SECRET = "DummyClientSecret";
39+
public static final String TEST_PINPOINT_APP_ID = "DummyPinpointAppId";
40+
public static final String DUMMY_CUSTOM_ENDPOINT = "my-custom-endpoint.amazon.com";
41+
42+
@Mock
43+
private AmazonCognitoIdentityProviderClient mockCustomEndpointClient;
44+
45+
@Mock
46+
private Context appContext;
47+
48+
@Before
49+
public void init() {
50+
MockitoAnnotations.initMocks(this);
51+
}
52+
53+
/**
54+
* Test that end point is set for the {@link AmazonCognitoIdentityProviderClient } with correct value
55+
* when {@link CognitoUserPool#CognitoUserPool(Context, String, String, String, AmazonCognitoIdentityProvider, String, String)} is called
56+
* with a valid endpoint
57+
*/
58+
@Test
59+
public void testForCustomEndpoint() {
60+
testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCustomEndpointClient, TEST_PINPOINT_APP_ID, DUMMY_CUSTOM_ENDPOINT);
61+
verify(mockCustomEndpointClient).setEndpoint(DUMMY_CUSTOM_ENDPOINT);
62+
}
63+
64+
/**
65+
* Test that end point is not set for the {@link AmazonCognitoIdentityProviderClient }
66+
* when {@link CognitoUserPool#CognitoUserPool(Context, String, String, String, AmazonCognitoIdentityProvider, String, String)} is called
67+
* with a null endpoint
68+
*/
69+
@Test
70+
public void testForCustomEndpointWhenNull() {
71+
testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCustomEndpointClient, TEST_PINPOINT_APP_ID, null);
72+
verifyZeroInteractions(mockCustomEndpointClient);
73+
}
74+
75+
/**
76+
* Test that end point is not set for the {@link AmazonCognitoIdentityProviderClient }
77+
* when {@link CognitoUserPool#CognitoUserPool(Context, String, String, String, AmazonCognitoIdentityProvider, String, String)} is called
78+
* with an empty endpoint
79+
*/
80+
@Test
81+
public void testForCustomEndpointWhenEmpty() {
82+
testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCustomEndpointClient, TEST_PINPOINT_APP_ID, "");
83+
verifyZeroInteractions(mockCustomEndpointClient);
84+
}
85+
86+
87+
}

aws-android-sdk-mobile-client/src/main/java/com/amazonaws/mobile/client/AWSMobileClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ public final class AWSMobileClient implements AWSCredentialsProvider {
189189
private static final String FACEBOOK = "FacebookSignIn";
190190
private static final String GOOGLE = "GoogleSignIn";
191191
private static final String GOOGLE_WEBAPP_CONFIG_KEY = "ClientId-WebApp";
192+
193+
/**
194+
* Configuration key for Cognito User Pool Custom Endpoint
195+
*/
196+
private static final String COGNITO_USERPOOL_CUSTOM_ENDPOINT = "Endpoint";
197+
192198
/**
193199
* Singleton instance for AWSMobileClient.
194200
*/
@@ -557,6 +563,7 @@ public void onUserSignedOut() {
557563
final String clientId = userPoolJSON.getString("AppClientId");
558564
final String clientSecret = userPoolJSON.optString("AppClientSecret");
559565
final String pinpointEndpointId = CognitoPinpointSharedContext.getPinpointEndpoint(context, userPoolJSON.optString("PinpointAppId"));
566+
final String cognitoUserPoolCustomEndpoint = userPoolJSON.optString(COGNITO_USERPOOL_CUSTOM_ENDPOINT);
560567

561568
final ClientConfiguration clientConfig = new ClientConfiguration();
562569
clientConfig.setUserAgent(DEFAULT_USER_AGENT + " " + awsConfiguration.getUserAgent());
@@ -569,7 +576,7 @@ public void onUserSignedOut() {
569576

570577
userpoolsLoginKey = String.format("cognito-idp.%s.amazonaws.com/%s", userPoolJSON.getString("Region"), userPoolJSON.getString("PoolId"));
571578

572-
userpool = new CognitoUserPool(mContext, mUserPoolPoolId, clientId, clientSecret, userpoolLL, pinpointEndpointId);
579+
userpool = new CognitoUserPool(mContext, mUserPoolPoolId, clientId, clientSecret, userpoolLL, pinpointEndpointId, cognitoUserPoolCustomEndpoint);
573580
userpool.setPersistenceEnabled(mIsPersistenceEnabled);
574581

575582
mDeviceOperations = new DeviceOperations(AWSMobileClient.this, userpoolLL);

0 commit comments

Comments
 (0)