use of com.amazonaws.mobile.config.AWSConfiguration in project aws-sdk-android by aws-amplify.
the class AWSMobileClientPersistenceWithRestartabilityTest method beforeClass.
@BeforeClass
public static void beforeClass() throws Exception {
setUpCredentials();
Context appContext = ApplicationProvider.getApplicationContext();
final CountDownLatch latch = new CountDownLatch(1);
AWSMobileClient.getInstance().initialize(appContext, new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails result) {
latch.countDown();
}
@Override
public void onError(Exception e) {
latch.countDown();
}
});
latch.await();
final AWSConfiguration awsConfiguration = AWSMobileClient.getInstance().getConfiguration();
JSONObject userPoolConfig = awsConfiguration.optJsonObject("CognitoUserPool");
assertNotNull(userPoolConfig);
clientRegion = Regions.fromName(userPoolConfig.getString("Region"));
userPoolId = userPoolConfig.getString("PoolId");
JSONObject identityPoolConfig = awsConfiguration.optJsonObject("CredentialsProvider").getJSONObject("CognitoIdentity").getJSONObject("Default");
assertNotNull(identityPoolConfig);
deleteAllUsers(userPoolId);
}
use of com.amazonaws.mobile.config.AWSConfiguration in project aws-sdk-android by aws-amplify.
the class AWSMobileClientTest method testGetConfiguration.
@Test
public void testGetConfiguration() {
final AWSConfiguration awsConfiguration = AWSMobileClient.getInstance().getConfiguration();
assertNotNull(awsConfiguration.optJsonObject("CognitoUserPool"));
try {
assertEquals("us-west-2", awsConfiguration.optJsonObject("CognitoUserPool").getString("Region"));
} catch (JSONException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of com.amazonaws.mobile.config.AWSConfiguration in project aws-sdk-android by aws-amplify.
the class TransferUtilityTest method testLocalTestingEnabled.
/**
* Tests that transfer utility successfully mutated Amazon S3 client
* if local testing flag is set to true.
*
* @throws Exception
*/
@Test
public void testLocalTestingEnabled() throws Exception {
final String jsonString = "{\n" + " \"S3TransferUtility\": {\n" + " \"Default\": {\n" + " \"Bucket\": \"name\",\n" + " \"Region\": \"us-east-1\",\n" + " \"DangerouslyConnectToHTTPEndpointForTesting\": true\n" + " }\n" + " }\n" + "}";
try {
JSONObject jsonConfig = new JSONObject(jsonString);
awsConfig = new AWSConfiguration(jsonConfig);
} catch (JSONException e) {
fail("Error in constructing AWSConfiguration." + e.getLocalizedMessage());
}
AWSCredentials creds = new BasicAWSCredentials("accessKey", "secretKey");
Region region = Region.getRegion(Regions.DEFAULT_REGION);
AmazonS3Client s3 = new AmazonS3Client(creds, region);
TransferUtility.builder().context(RuntimeEnvironment.application.getApplicationContext()).awsConfiguration(awsConfig).s3Client(s3).build();
assertEquals(s3.getEndpoint(), Constants.LOCAL_TESTING_ENDPOINT);
assertTrue(s3.clientOptions.isPathStyleAccess());
assertTrue(s3.clientOptions.isContentMd5CheckSkipped());
}
use of com.amazonaws.mobile.config.AWSConfiguration in project amplify-android by aws-amplify.
the class AWSCognitoAuthPlugin method configure.
@Override
public void configure(@NonNull JSONObject pluginConfiguration, @NonNull Context context) throws AuthException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Exception> asyncException = new AtomicReference<>();
JSONObject mobileClientConfig;
LogFactory.setLevel(LogFactory.Level.OFF);
try {
mobileClientConfig = new JSONObject(pluginConfiguration.toString());
mobileClientConfig.put("UserAgentOverride", UserAgent.string());
} catch (JSONException exception) {
throw new AuthException("Failed to set user agent string", exception, AmplifyException.REPORT_BUG_TO_AWS_SUGGESTION);
}
awsMobileClient.initialize(context, new AWSConfiguration(mobileClientConfig), new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails result) {
switch(result.getUserState()) {
case GUEST:
case SIGNED_OUT:
lastEvent = AuthChannelEventName.SIGNED_OUT;
userId = null;
break;
case SIGNED_IN:
lastEvent = AuthChannelEventName.SIGNED_IN;
userId = getUserIdFromToken(result.getDetails().get(MOBILE_CLIENT_TOKEN_KEY));
break;
case SIGNED_OUT_USER_POOLS_TOKENS_INVALID:
case SIGNED_OUT_FEDERATED_TOKENS_INVALID:
lastEvent = AuthChannelEventName.SESSION_EXPIRED;
userId = getUserIdFromToken(result.getDetails().get(MOBILE_CLIENT_TOKEN_KEY));
break;
default:
userId = null;
lastEvent = null;
}
// Set up a listener to asynchronously update the user id if the user state changes in the future
awsMobileClient.addUserStateListener(userStateDetails -> {
switch(userStateDetails.getUserState()) {
case SIGNED_OUT:
case GUEST:
userId = null;
if (lastEvent != AuthChannelEventName.SIGNED_OUT) {
lastEvent = AuthChannelEventName.SIGNED_OUT;
Amplify.Hub.publish(HubChannel.AUTH, HubEvent.create(AuthChannelEventName.SIGNED_OUT));
}
break;
case SIGNED_IN:
fetchAndSetUserId(() -> {
if (lastEvent != AuthChannelEventName.SIGNED_IN) {
lastEvent = AuthChannelEventName.SIGNED_IN;
Amplify.Hub.publish(HubChannel.AUTH, HubEvent.create(AuthChannelEventName.SIGNED_IN));
}
});
break;
case SIGNED_OUT_FEDERATED_TOKENS_INVALID:
case SIGNED_OUT_USER_POOLS_TOKENS_INVALID:
fetchAndSetUserId(() -> {
/* No response needed */
});
if (lastEvent != AuthChannelEventName.SESSION_EXPIRED) {
lastEvent = AuthChannelEventName.SESSION_EXPIRED;
Amplify.Hub.publish(HubChannel.AUTH, HubEvent.create(AuthChannelEventName.SESSION_EXPIRED));
}
break;
default:
userId = null;
}
});
latch.countDown();
}
@Override
public void onError(Exception error) {
asyncException.set(error);
latch.countDown();
}
});
try {
if (latch.await(SECONDS_BEFORE_TIMEOUT, TimeUnit.SECONDS)) {
if (asyncException.get() != null) {
throw new AuthException("Failed to instantiate AWSMobileClient", asyncException.get(), "See attached exception for more details");
}
return;
} else {
throw new AuthException("Failed to instantiate AWSMobileClient within " + SECONDS_BEFORE_TIMEOUT + " seconds", "Check network connectivity");
}
} catch (InterruptedException error) {
throw new AuthException("Failed to instantiate AWSMobileClient", error, "See attached exception for more details");
}
}
use of com.amazonaws.mobile.config.AWSConfiguration in project aws-mobile-appsync-sdk-android by awslabs.
the class MultiClientInstrumentationTest method testConfigHasNoClientDatabasePrefixAndUseClientDatabasePrefixTrue.
@Test
public void testConfigHasNoClientDatabasePrefixAndUseClientDatabasePrefixTrue() {
AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
awsConfiguration.setConfiguration("MultiAuthAndroidIntegTestApp_NoClientDatabasePrefix");
RuntimeException exception = assertThrows(RuntimeException.class, () -> AWSAppSyncClient.builder().context(getTargetContext()).awsConfiguration(awsConfiguration).useClientDatabasePrefix(true).build());
assertEquals("ClientDatabasePrefix is not present in AppSync configuration in awsconfiguration.json " + "however .useClientDatabasePrefix(true) is passed in.", exception.getCause().getLocalizedMessage());
}
Aggregations