Search in sources :

Example 6 with AWSKeyValueStore

use of com.amazonaws.internal.keyvaluestore.AWSKeyValueStore in project aws-sdk-android by aws-amplify.

the class CognitoDeviceHelper method clearCachedDevice.

/**
 * Clears cached device details for this user.
 *
 * @param username          REQUIRED: The current user.
 * @param userPoolId        REQUIRED: Client ID of the device.
 * @param context           REQUIRED: App context, needed to access device datastore.
 */
public static void clearCachedDevice(String username, String userPoolId, Context context) {
    try {
        final AWSKeyValueStore awsKeyValueStore = getAWSKeyValueStore(context, username, userPoolId);
        awsKeyValueStore.clear();
    } catch (final Exception e) {
        LOGGER.error("Error accessing SharedPreferences", e);
    }
}
Also used : AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 7 with AWSKeyValueStore

use of com.amazonaws.internal.keyvaluestore.AWSKeyValueStore in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method testCacheWithEncryptionKeyNotRetrieved.

@Test
public void testCacheWithEncryptionKeyNotRetrieved() throws Exception {
    SharedPreferences sharedPreferencesForEncryptionMaterials = appContext.getSharedPreferences(SHARED_PREFERENCES_NAME + ".encryptionKey", Context.MODE_PRIVATE);
    // Test with a user cached in shared preferences
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + ".LastAuthUser", TEST_USER_NAME);
    CognitoUser user = testPool.getCurrentUser();
    assertNotNull(user);
    String username = user.getUserId();
    assertEquals(TEST_USER_NAME, username);
    deleteAllEncryptionKeys();
    CognitoUserPool testPoolAfterKeyDeleted = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET);
    AWSKeyValueStore store = getAWSKeyValueStorageUtility(testPoolAfterKeyDeleted);
    assertNull(store.get("CognitoIdentityProvider." + TEST_CLIENT_ID + ".LastAuthUser"));
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) SharedPreferences(android.content.SharedPreferences) AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) Test(org.junit.Test)

Example 8 with AWSKeyValueStore

use of com.amazonaws.internal.keyvaluestore.AWSKeyValueStore in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method newUserPoolWithPinpointWithCachedApp.

@Test
public void newUserPoolWithPinpointWithCachedApp() {
    // Test new user pool creation with PP app id.
    // Test shared Pinpoint context
    awsKeyValueStorageUtility = new AWSKeyValueStore(ApplicationProvider.getApplicationContext(), TEST_PP_APP_ID_1 + PP_PREFERENCES_AND_FILE_MANAGER_SUFFIX, true);
    awsKeyValueStorageUtility.put("UNIQUE_ID_KEY", TEST_PP_ENDPOINT_1);
    CognitoUserPool pool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, new ClientConfiguration(), TEST_AWS_REGION_1, TEST_PP_APP_ID_1);
    assertNotNull(pool);
    assertNotNull(pool.getUserPoolId());
    assertEquals(TEST_USER_POOL, pool.getUserPoolId());
    assertNotNull(pool.getClientId());
    assertEquals(TEST_CLIENT_ID, pool.getClientId());
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) ClientConfiguration(com.amazonaws.ClientConfiguration) Test(org.junit.Test)

Example 9 with AWSKeyValueStore

use of com.amazonaws.internal.keyvaluestore.AWSKeyValueStore in project aws-sdk-android by aws-amplify.

the class CognitoDeviceHelper method cacheDeviceKey.

/**
 * This method caches the device key. Device key is assigned by the Amazon Cognito service and is
 * used as a device identifier.
 *
 * @param username          REQUIRED: The current user.
 * @param userPoolId        REQUIRED: Client ID of the device.
 * @param deviceKey         REQUIRED: Cognito assigned device key.
 * @param context           REQUIRED: App context, needed to access device datastore.
 */
public static void cacheDeviceKey(String username, String userPoolId, String deviceKey, Context context) {
    try {
        final AWSKeyValueStore awsKeyValueStore = getAWSKeyValueStore(context, username, userPoolId);
        awsKeyValueStore.put(COGNITO_DEVICE_KEY, deviceKey);
    } catch (final Exception e) {
        LOGGER.error("Error accessing SharedPreferences", e);
    }
}
Also used : AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 10 with AWSKeyValueStore

use of com.amazonaws.internal.keyvaluestore.AWSKeyValueStore in project aws-sdk-android by aws-amplify.

the class CognitoDeviceHelper method getAWSKeyValueStore.

/**
 * Retrieve an instance of AWSKeyValueStore for the sharedPreferencesName.
 *
 * @param context application context
 * @param username username of current authenticated user
 * @param userPoolId identifier of the cognito userpool
 * @return the instance of utility that provides access to SharedPreferences.
 */
private static AWSKeyValueStore getAWSKeyValueStore(Context context, String username, String userPoolId) {
    synchronized (LOCK) {
        try {
            final String sharedPreferencesName = getDeviceDetailsCacheForUser(username, userPoolId);
            if (awsKeyValueStoreMap.containsKey(sharedPreferencesName)) {
                return awsKeyValueStoreMap.get(sharedPreferencesName);
            } else {
                AWSKeyValueStore awsKeyValueStore = new AWSKeyValueStore(context, sharedPreferencesName, isPersistenceEnabled);
                awsKeyValueStoreMap.put(sharedPreferencesName, awsKeyValueStore);
                return awsKeyValueStore;
            }
        } catch (Exception ex) {
            LOGGER.error("Error in retrieving the persistent store.", ex);
            return null;
        }
    }
}
Also used : AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

AWSKeyValueStore (com.amazonaws.internal.keyvaluestore.AWSKeyValueStore)17 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 Test (org.junit.Test)4 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonServiceException (com.amazonaws.AmazonServiceException)2 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)2 UnknownHostException (java.net.UnknownHostException)2 JSONException (org.json.JSONException)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AuthUserSession (com.amazonaws.mobileconnectors.cognitoauth.AuthUserSession)1 AccessToken (com.amazonaws.mobileconnectors.cognitoauth.tokens.AccessToken)1 IdToken (com.amazonaws.mobileconnectors.cognitoauth.tokens.IdToken)1 RefreshToken (com.amazonaws.mobileconnectors.cognitoauth.tokens.RefreshToken)1 CognitoUser (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser)1 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 JSONObject (org.json.JSONObject)1