Search in sources :

Example 11 with AWSKeyValueStore

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

the class CognitoDeviceHelper method cacheDeviceVerifier.

/**
 * This method caches the device verifier. Device verifier is generated locally by the SDK and
 * it is used to authenticate the device through device SRP authentication.
 *
 * @param username          REQUIRED: The current user.
 * @param userPoolId        REQUIRED: Client ID of the device.
 * @param deviceSecret      REQUIRED: Cognito assigned device key.
 * @param context           REQUIRED: App context, needed to access device datastore.
 */
public static void cacheDeviceVerifier(String username, String userPoolId, String deviceSecret, Context context) {
    try {
        final AWSKeyValueStore awsKeyValueStore = getAWSKeyValueStore(context, username, userPoolId);
        awsKeyValueStore.put(COGNITO_DEVICE_SECRET, deviceSecret);
    } catch (final Exception e) {
        LOGGER.error("Error accessing SharedPreferences", e);
    }
}
Also used : AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 12 with AWSKeyValueStore

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

the class CognitoDeviceHelper method setPersistenceEnabled.

/**
 * Set the flag that indicates if the persistence is enabled or not.
 * @param isPersistenceEnabled flag that indicates if the persistence is enabled or not.
 */
public static void setPersistenceEnabled(boolean isPersistenceEnabled) {
    synchronized (LOCK) {
        try {
            CognitoDeviceHelper.isPersistenceEnabled = isPersistenceEnabled;
            for (String sharedPreferencesName : awsKeyValueStoreMap.keySet()) {
                AWSKeyValueStore awsKeyValueStore = awsKeyValueStoreMap.get(sharedPreferencesName);
                awsKeyValueStore.setPersistenceEnabled(isPersistenceEnabled);
            }
        } catch (Exception ex) {
            LOGGER.error("Error in setting the isPersistenceEnabled flag in the key-value store.", ex);
        }
    }
}
Also used : AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 13 with AWSKeyValueStore

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

the class AWSMobileClientTestBase method setTokensDirectly.

public static void setTokensDirectly(final Context appContext, final String providerKey, final String token, final String identityId) {
    final AWSKeyValueStore awsKeyValueStore = new AWSKeyValueStore(appContext, AWSMobileClient.SHARED_PREFERENCES_KEY, true);
    awsKeyValueStore.put(AWSMobileClient.PROVIDER_KEY, providerKey);
    awsKeyValueStore.put(AWSMobileClient.TOKEN_KEY, token);
    awsKeyValueStore.put(AWSMobileClient.IDENTITY_ID_KEY, identityId);
}
Also used : AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore)

Example 14 with AWSKeyValueStore

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

the class AWSMobileClientTestBase method writeUserPoolsTokens.

public static void writeUserPoolsTokens(final Context appContext, final String clientId, final String userId, final String accessToken, final String idToken, final String refreshToken) {
    final AWSKeyValueStore awsKeyValueStore = new AWSKeyValueStore(appContext, "CognitoIdentityProviderCache", true);
    // Create keys to look for cached tokens
    final String csiIdTokenKey = "CognitoIdentityProvider." + clientId + "." + userId + ".idToken";
    final String csiAccessTokenKey = "CognitoIdentityProvider." + clientId + "." + userId + ".accessToken";
    final String csiRefreshTokenKey = "CognitoIdentityProvider." + clientId + "." + userId + ".refreshToken";
    final String csiLastUserKey = "CognitoIdentityProvider." + clientId + ".LastAuthUser";
    // Store the data in Shared Preferences
    awsKeyValueStore.put(csiAccessTokenKey, accessToken);
    awsKeyValueStore.put(csiIdTokenKey, idToken);
    awsKeyValueStore.put(csiRefreshTokenKey, refreshToken);
    awsKeyValueStore.put(csiLastUserKey, userId);
}
Also used : AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore)

Example 15 with AWSKeyValueStore

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

the class AWSMobileClientNetworkIssueTest method testNetworkExceptionPropagation_getTokens.

@Test
public void testNetworkExceptionPropagation_getTokens() throws Exception {
    reinitialize();
    final AWSKeyValueStore awsKeyValueStore = new AWSKeyValueStore(appContext, AWSMobileClient.SHARED_PREFERENCES_KEY, true);
    awsKeyValueStore.put(AWSMobileClient.PROVIDER_KEY, AWSMobileClient.getInstance().getLoginKey());
    awsKeyValueStore.put(AWSMobileClient.TOKEN_KEY, getValidJWT(-3600L));
    awsKeyValueStore.put(AWSMobileClient.IDENTITY_ID_KEY, "");
    writeUserPoolsTokens(appContext, auth.getConfiguration().optJsonObject("CognitoUserPool").getString("AppClientId"), username, -3600L);
    Field f1 = CognitoUserPool.class.getDeclaredField("client");
    f1.setAccessible(true);
    f1.set(auth.userpool, mockLowLevel);
    try {
        auth.getTokens();
        fail("Should throw exception for network issue");
    } catch (Exception e) {
        assertTrue("Deep cause should be network exception", e.getCause().getCause().getCause() instanceof UnknownHostException);
    }
}
Also used : Field(java.lang.reflect.Field) UnknownHostException(java.net.UnknownHostException) AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) JSONException(org.json.JSONException) AmazonServiceException(com.amazonaws.AmazonServiceException) UnknownHostException(java.net.UnknownHostException) AmazonClientException(com.amazonaws.AmazonClientException) Test(org.junit.Test)

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