Search in sources :

Example 11 with CognitoCachingCredentialsProvider

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

the class IdentityManager method refreshCredentialWithLogins.

/**
 * Set the loginMap of the CognitoCachingCredentialsProvider
 * and invoke refresh. This retrieves the AWS Identity and the
 * short-lived AWS Credentials to access other AWS resources.
 *
 * @param loginMap the map with a key-value pair of
 *                 sign-in provider key and the token
 */
private void refreshCredentialWithLogins(final Map<String, String> loginMap) {
    final CognitoCachingCredentialsProvider credentialsProvider = credentialsProviderHolder.getUnderlyingProvider();
    if (!shouldFederate) {
        return;
    }
    credentialsProvider.clear();
    credentialsProvider.withLogins(loginMap);
    // Calling refresh is equivalent to calling getIdentityId() + getCredentials().
    Log.d(LOG_TAG, "refresh credentials");
    credentialsProvider.refresh();
    // Set the expiration key of the Credentials Provider to 8 minutes, 30 seconds.
    awsKeyValueStore.put(credentialsProvider.getIdentityPoolId() + "." + EXPIRATION_KEY, String.valueOf(System.currentTimeMillis() + (510 * 1000)));
}
Also used : CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider)

Example 12 with CognitoCachingCredentialsProvider

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

the class IdentityManager method createCredentialsProvider.

/**
 *   The CognitoCachingCredentialProvider loads cached credentials when it is
 *   instantiated, however, it does not reload the login map, which must be reloaded
 *   in order to refresh the credentials.  Therefore, currently cached credentials are
 *   only useful for unauthenticated users.
 */
private void createCredentialsProvider(final Context context, final ClientConfiguration clientConfiguration) {
    Log.d(LOG_TAG, "Creating the Cognito Caching Credentials Provider " + "with a refreshing Cognito Identity Provider.");
    if (!shouldFederate) {
        return;
    }
    final JSONObject cognitoIdentityConfig = getCognitoIdentityPoolConfig();
    final String region;
    final String poolId;
    try {
        region = cognitoIdentityConfig.getString("Region");
        poolId = cognitoIdentityConfig.getString("PoolId");
    } catch (JSONException e) {
        throw new IllegalArgumentException("Failed to read configuration for CognitoIdentity", e);
    }
    final Regions cognitoIdentityRegion = Regions.fromName(region);
    final AWSRefreshingCognitoIdentityProvider refreshingCredentialsProvider = new AWSRefreshingCognitoIdentityProvider(null, poolId, clientConfiguration, cognitoIdentityRegion);
    final CognitoCachingCredentialsProvider cognitoCachingCredentialsProvider = new CognitoCachingCredentialsProvider(context, refreshingCredentialsProvider, cognitoIdentityRegion, clientConfiguration);
    cognitoCachingCredentialsProvider.setPersistenceEnabled(isPersistenceEnabled);
    if (clientConfiguration.getUserAgentOverride() != null) {
        cognitoCachingCredentialsProvider.setUserAgentOverride(clientConfiguration.getUserAgentOverride());
    }
    credentialsProviderHolder.setUnderlyingProvider(cognitoCachingCredentialsProvider);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Regions(com.amazonaws.regions.Regions) CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider)

Example 13 with CognitoCachingCredentialsProvider

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

the class AWSKeyValueStoreMigrationIntegrationTest 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)

Example 14 with CognitoCachingCredentialsProvider

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

the class AWSKeyValueStoreNoCachingIntegrationTest method verifyCredentialsProviderClear.

private void verifyCredentialsProviderClear() {
    for (int iterator = 0; iterator < credentialsProviders.size(); iterator++) {
        final CognitoCachingCredentialsProvider cccp = credentialsProviders.get(iterator);
        cccp.clearCredentials();
        cccp.clear();
        assertNull(cccp.getCachedIdentityId());
        assertNotNull(cccp.getIdentityId());
        assertNotNull(cccp.getCachedIdentityId());
    }
}
Also used : CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider)

Example 15 with CognitoCachingCredentialsProvider

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

the class CognitoCachingCredentialsProviderIntegrationTest method testConstructMultipleCCCPs.

@Test
public void testConstructMultipleCCCPs() throws Exception {
    CognitoCachingCredentialsProvider cccp1 = new CognitoCachingCredentialsProvider(ApplicationProvider.getApplicationContext(), getPackageConfigure().getString("identity_pool_id"), Regions.US_EAST_1);
    String identityId = cccp1.getIdentityId();
    cccp1.getCredentials();
    cccp1.clearCredentials();
    cccp1.clear();
    identityId = cccp1.getIdentityId();
    cccp1.getCredentials();
    CognitoCachingCredentialsProvider cccp2 = new CognitoCachingCredentialsProvider(ApplicationProvider.getApplicationContext(), getPackageConfigure().getString("identity_pool_id"), Regions.US_EAST_1);
    assertNotNull(cccp2.getCachedIdentityId());
    assertEquals(identityId, cccp2.getCachedIdentityId());
}
Also used : CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider) Test(org.junit.Test)

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