Search in sources :

Example 1 with CognitoCachingCredentialsProvider

use of com.amazonaws.auth.CognitoCachingCredentialsProvider in project aws-sdk-android by aws-amplify.

the class EndpointProfileIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    JSONObject testConfig = getPackageConfigure("pinpoint");
    String identityPoolId = testConfig.getString("identity_pool_id");
    String appId = testConfig.getString("AppId");
    Regions regions = Regions.fromName(testConfig.getString("Region"));
    getApplicationContext().deleteDatabase("awspinpoint.db");
    goOnline();
    credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(), identityPoolId, regions);
    PinpointConfiguration pinpointConfiguration = new PinpointConfiguration(getApplicationContext(), appId, regions, ChannelType.GCM, credentialsProvider);
    pinpointManager = new PinpointManager(pinpointConfiguration);
}
Also used : PinpointConfiguration(com.amazonaws.mobileconnectors.pinpoint.PinpointConfiguration) JSONObject(org.json.JSONObject) Regions(com.amazonaws.regions.Regions) PinpointManager(com.amazonaws.mobileconnectors.pinpoint.PinpointManager) CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider) Before(org.junit.Before)

Example 2 with CognitoCachingCredentialsProvider

use of com.amazonaws.auth.CognitoCachingCredentialsProvider in project aws-sdk-android by aws-amplify.

the class CognitoCachingCredentialsProviderDataCorruptionIntegrationTest method testCachedAWSCredentialsWithKeyNotRetrieved.

// getCredentials() will store the encrypted credentials in SharedPreferences.
// Now delete the key held in Android KeyStore
// getCredentials() now cannot read the content, so it will delete the encrypted
// credentials and go to clean state and fetch credentials from AWS.
@Test
public void testCachedAWSCredentialsWithKeyNotRetrieved() throws Exception {
    verifyGetCredentialsAndIdentityId();
    verifySharedPreferencesContents();
    final String cachedIdentityId = credentialsProvider.getCachedIdentityId();
    deleteAllEncryptionKeys();
    CognitoCachingCredentialsProvider credentialsProviderAfterKeyDeleted = new CognitoCachingCredentialsProvider(ApplicationProvider.getApplicationContext(), getPackageConfigure().getString("identity_pool_id"), Regions.US_EAST_1);
    assertNull(credentialsProviderAfterKeyDeleted.getCachedIdentityId());
    assertNotNull(credentialsProviderAfterKeyDeleted.getCredentials());
    assertNotNull(credentialsProviderAfterKeyDeleted.getIdentityId());
    final String cachedIdentityIdAfterKeyDeleted = credentialsProviderAfterKeyDeleted.getCachedIdentityId();
    assertNotEquals(cachedIdentityId, cachedIdentityIdAfterKeyDeleted);
}
Also used : CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider) Test(org.junit.Test)

Example 3 with CognitoCachingCredentialsProvider

use of com.amazonaws.auth.CognitoCachingCredentialsProvider in project aws-sdk-android by aws-amplify.

the class CognitoCachingCredentialsProviderDataCorruptionIntegrationTest method testCachedAWSCredentialsWithCredentialsExpired.

@Test
public void testCachedAWSCredentialsWithCredentialsExpired() throws Exception {
    verifyGetCredentialsAndIdentityId();
    verifySharedPreferencesContents();
    final AWSSessionCredentials currentCredentials = credentialsProvider.getCredentials();
    // modify the expiration date of credentials
    sharedPreferencesForAuth.edit().putString(credentialsProvider.getIdentityPoolId() + ".expirationDate.encrypted", "-00000011111").commit();
    assertEquals("-00000011111", sharedPreferencesForAuth.getString(credentialsProvider.getIdentityPoolId() + ".expirationDate.encrypted", null));
    // getCredentials() now will fetch the SAME credentials from Amazon Cognito by making a network call
    // as the credentials are only expired on client locally.
    AWSSessionCredentials credentialsFromCredentialsProvider = credentialsProvider.getCredentials();
    assertNotNull("getCredentials() with credentials expired locally should fetch new credentials", credentialsFromCredentialsProvider);
    assertEquals(currentCredentials.getAWSAccessKeyId(), credentialsFromCredentialsProvider.getAWSAccessKeyId());
    assertEquals(currentCredentials.getAWSSecretKey(), credentialsFromCredentialsProvider.getAWSSecretKey());
    assertEquals(currentCredentials.getSessionToken(), credentialsFromCredentialsProvider.getSessionToken());
    // refresh will get NEW credentials from Amazon Cognito by making a network call
    credentialsProvider.refresh();
    AWSSessionCredentials credentialsFromCredentialsProviderAfterRefresh = credentialsProvider.getCredentials();
    assertNotNull("getCredentials() after refresh should fetch new credentials", credentialsFromCredentialsProviderAfterRefresh);
    assertNotEquals(credentialsFromCredentialsProviderAfterRefresh.getAWSAccessKeyId(), credentialsFromCredentialsProvider.getAWSAccessKeyId());
    assertNotEquals(credentialsFromCredentialsProviderAfterRefresh.getAWSSecretKey(), credentialsFromCredentialsProvider.getAWSSecretKey());
    assertNotEquals(credentialsFromCredentialsProviderAfterRefresh.getSessionToken(), credentialsFromCredentialsProvider.getSessionToken());
    // Repeat getCredentials() for an other credentials provider
    CognitoCachingCredentialsProvider credentialsProvider1 = new CognitoCachingCredentialsProvider(ApplicationProvider.getApplicationContext(), getPackageConfigure().getString("identity_pool_id"), Regions.US_EAST_1);
    final AWSSessionCredentials newCredentials = credentialsProvider1.getCredentials();
    assertNotNull("getCredentials() with corrupted key should fetch new credentials", newCredentials);
    assertNotEquals(currentCredentials.getAWSAccessKeyId(), newCredentials.getAWSAccessKeyId());
    assertNotEquals(currentCredentials.getAWSSecretKey(), newCredentials.getAWSSecretKey());
    assertNotEquals(currentCredentials.getSessionToken(), newCredentials.getSessionToken());
}
Also used : AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider) Test(org.junit.Test)

Example 4 with CognitoCachingCredentialsProvider

use of com.amazonaws.auth.CognitoCachingCredentialsProvider in project aws-sdk-android by aws-amplify.

the class CognitoCachingCredentialsProviderDataCorruptionIntegrationTest method testCachedAWSCredentialsWithCorruptedAccessKey.

@Test
public void testCachedAWSCredentialsWithCorruptedAccessKey() throws Exception {
    verifyGetCredentialsAndIdentityId();
    verifySharedPreferencesContents();
    final AWSSessionCredentials currentCredentials = credentialsProvider.getCredentials();
    // make the access key null
    sharedPreferencesForAuth.edit().putString(credentialsProvider.getIdentityPoolId() + ".accessKey.encrypted", null).commit();
    assertNull(sharedPreferencesForAuth.getString(credentialsProvider.getIdentityPoolId() + ".accessKey.encrypted", null));
    // getCredentials() now will fetch the same credentials from AWS Cognito by making a network call
    final AWSSessionCredentials credentialsFromCredentialsProvider = credentialsProvider.getCredentials();
    assertNotNull("getCredentials() with corrupted data should fetch new credentials", credentialsFromCredentialsProvider);
    assertEquals(currentCredentials.getAWSAccessKeyId(), credentialsFromCredentialsProvider.getAWSAccessKeyId());
    assertEquals(currentCredentials.getAWSSecretKey(), credentialsFromCredentialsProvider.getAWSSecretKey());
    assertEquals(currentCredentials.getSessionToken(), credentialsFromCredentialsProvider.getSessionToken());
    // Repeat getCredentials() for an other credentials provider
    CognitoCachingCredentialsProvider credentialsProvider1 = new CognitoCachingCredentialsProvider(ApplicationProvider.getApplicationContext(), getPackageConfigure().getString("identity_pool_id"), Regions.US_EAST_1);
    final AWSSessionCredentials newCredentials = credentialsProvider1.getCredentials();
    assertNotNull("getCredentials() with corrupted key should fetch new credentials", newCredentials);
    assertNotEquals(currentCredentials.getAWSAccessKeyId(), newCredentials.getAWSAccessKeyId());
    assertNotEquals(currentCredentials.getAWSSecretKey(), newCredentials.getAWSSecretKey());
    assertNotEquals(currentCredentials.getSessionToken(), newCredentials.getSessionToken());
}
Also used : AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider) Test(org.junit.Test)

Example 5 with CognitoCachingCredentialsProvider

use of com.amazonaws.auth.CognitoCachingCredentialsProvider in project aws-sdk-android by aws-amplify.

the class CognitoCachingCredentialsProviderIntegrationTest method verifySharedPreferencesContents.

private void verifySharedPreferencesContents() {
    assert sharedPreferencesForAuth.getAll().keySet().size() == credentialsProviders.size() * 5;
    Log.d(TAG, "SharedPreferences Keys = " + sharedPreferencesForAuth.getAll().keySet().toString());
    for (int iterator = 0; iterator < credentialsProviders.size(); iterator++) {
        final CognitoCachingCredentialsProvider cccp = credentialsProviders.get(iterator);
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".accessKey.encrypted", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".secretKey.encrypted", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".sessionToken.encrypted", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".identityId.encrypted", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".expirationDate.encrypted", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".accessKey.encrypted.iv", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".secretKey.encrypted.iv", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".sessionToken.encrypted.iv", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".identityId.encrypted.iv", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".expirationDate.encrypted.iv", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".accessKey.encrypted.keyvaluestoreversion", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".secretKey.encrypted.keyvaluestoreversion", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".sessionToken.encrypted.keyvaluestoreversion", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".identityId.encrypted.keyvaluestoreversion", null));
        assertNotNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".expirationDate.encrypted.keyvaluestoreversion", null));
        assertNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".accessKey", null));
        assertNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".secretKey", null));
        assertNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".sessionToken", null));
        assertNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".identityId", null));
        assertNull(sharedPreferencesForAuth.getString(cccp.getIdentityPoolId() + ".expirationDate", null));
    }
}
Also used : CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider)

Aggregations

CognitoCachingCredentialsProvider (com.amazonaws.auth.CognitoCachingCredentialsProvider)23 Test (org.junit.Test)10 JSONObject (org.json.JSONObject)5 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)4 Regions (com.amazonaws.regions.Regions)3 Before (org.junit.Before)3 PinpointConfiguration (com.amazonaws.mobileconnectors.pinpoint.PinpointConfiguration)2 PinpointManager (com.amazonaws.mobileconnectors.pinpoint.PinpointManager)2 JSONException (org.json.JSONException)2 Context (android.content.Context)1 CustomTabsCallback (androidx.browser.customtabs.CustomTabsCallback)1 AmazonClientException (com.amazonaws.AmazonClientException)1 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSAbstractCognitoIdentityProvider (com.amazonaws.auth.AWSAbstractCognitoIdentityProvider)1 AnonymousAWSCredentials (com.amazonaws.auth.AnonymousAWSCredentials)1 IdentityManager (com.amazonaws.mobile.auth.core.IdentityManager)1 SignInStateChangeListener (com.amazonaws.mobile.auth.core.SignInStateChangeListener)1 InternalCallback (com.amazonaws.mobile.client.internal.InternalCallback)1 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)1 OAuth2Client (com.amazonaws.mobile.client.internal.oauth2.OAuth2Client)1