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)));
}
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);
}
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));
}
}
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());
}
}
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());
}
Aggregations