Search in sources :

Example 1 with UpdateAttributesHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.

the class OAuth2Utils method _updateUserAttributes.

private Runnable _updateUserAttributes(final Map<String, String> userAttributes, final Map<String, String> clientMetadata, final Callback<List<UserCodeDeliveryDetails>> callback) {
    return new Runnable() {

        @Override
        public void run() {
            if (!waitForSignIn()) {
                callback.onError(new Exception("Operation requires a signed-in state"));
                return;
            }
            final CognitoUserAttributes cognitoUserAttributes = new CognitoUserAttributes();
            if (userAttributes != null) {
                for (final String key : userAttributes.keySet()) {
                    cognitoUserAttributes.addAttribute(key, userAttributes.get(key));
                }
            }
            userpool.getCurrentUser().updateAttributes(cognitoUserAttributes, clientMetadata, new UpdateAttributesHandler() {

                @Override
                public void onSuccess(List<CognitoUserCodeDeliveryDetails> attributesVerificationList) {
                    final List<UserCodeDeliveryDetails> list = new LinkedList<UserCodeDeliveryDetails>();
                    for (CognitoUserCodeDeliveryDetails details : attributesVerificationList) {
                        list.add(new UserCodeDeliveryDetails(details.getDestination(), details.getDeliveryMedium(), details.getAttributeName()));
                    }
                    callback.onResult(list);
                }

                @Override
                public void onFailure(Exception exception) {
                    callback.onError(exception);
                }
            });
        }
    };
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)

Example 2 with UpdateAttributesHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.

the class CognitoUser method updateAttributesInBackground.

/**
 * Updates attributes for a user. Runs in background.
 * <p>
 * Requires valid accessToken.
 * </p>
 *
 * @param clientMetadata A map of custom key-value pairs that is passed to the lambda function for
 *                       custom workflow.
 * @param attributes REQUIRED: All attributes and values that need to be
 *            updated for this user.
 * @param callback REQUIRED: {@link UpdateAttributesHandler} callback.
 */
public void updateAttributesInBackground(final Map<String, String> clientMetadata, final CognitoUserAttributes attributes, final UpdateAttributesHandler callback) {
    if (callback == null) {
        throw new CognitoParameterInvalidException("callback is null");
    }
    final CognitoUser user = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Handler handler = new Handler(context.getMainLooper());
            Runnable returnCallback;
            try {
                final CognitoUserSession session = user.getCachedSession();
                final UpdateUserAttributesResult updateUserAttributesResult = updateAttributesInternal(clientMetadata, attributes, session);
                final List<CognitoUserCodeDeliveryDetails> attributesVerificationList = new ArrayList<CognitoUserCodeDeliveryDetails>();
                if (updateUserAttributesResult.getCodeDeliveryDetailsList() != null) {
                    for (final CodeDeliveryDetailsType details : updateUserAttributesResult.getCodeDeliveryDetailsList()) {
                        attributesVerificationList.add(new CognitoUserCodeDeliveryDetails(details));
                    }
                }
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onSuccess(attributesVerificationList);
                    }
                };
            } catch (final Exception e) {
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onFailure(e);
                    }
                };
            }
            handler.post(returnCallback);
        }
    }).start();
}
Also used : CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) ArrayList(java.util.ArrayList) Handler(android.os.Handler) GetDetailsHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) VerificationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.VerificationHandler) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) ForgotPasswordHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.ForgotPasswordHandler) RegisterMfaHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.RegisterMfaHandler) DevicesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.DevicesHandler) CodeDeliveryDetailsType(com.amazonaws.services.cognitoidentityprovider.model.CodeDeliveryDetailsType) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) UserNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException) CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException) UpdateUserAttributesResult(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesResult)

Example 3 with UpdateAttributesHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInCurrentThreadWithNoCachedTokens.

@Test
public void updateUserAttributeInCurrentThreadWithNoCachedTokens() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_UPDATE_USER_RESULT).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.addAttribute("TestAttribute1", "TestData1");
    attributes.addAttribute("TestAttribute2", "TestData2");
    testUser.updateAttributes(attributes, new UpdateAttributesHandler() {

        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
            assertNotNull(var1);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) UpdateUserAttributesRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 4 with UpdateAttributesHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInCurrentThreadWithCachedTokensServiceException.

// Get user attributes -
@Test
public void updateUserAttributeInCurrentThreadWithCachedTokensServiceException() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    InvalidParameterException exception = new InvalidParameterException("registration failed");
    doThrow(exception).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN).commit();
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.addAttribute("TestAttribute1", "TestData1");
    attributes.addAttribute("TestAttribute2", "TestData2");
    testUser.updateAttributes(attributes, new UpdateAttributesHandler() {

        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
            assertNotNull(var1);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        // System.out.println("Exception: "+exception.toString());
        }
    });
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) UpdateUserAttributesRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) SharedPreferences(android.content.SharedPreferences) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 5 with UpdateAttributesHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInBackgroundWithCachedTokensServiceException.

// Get user attributes - in background
@Test
public void updateUserAttributeInBackgroundWithCachedTokensServiceException() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    InvalidParameterException exception = new InvalidParameterException("failed");
    doThrow(exception).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN).commit();
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.addAttribute("TestAttribute1", "TestData1");
    attributes.addAttribute("TestAttribute2", "TestData2");
    testUser.updateAttributesInBackground(attributes, new UpdateAttributesHandler() {

        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
            assertNotNull(var1);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) UpdateUserAttributesRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) SharedPreferences(android.content.SharedPreferences) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Aggregations

UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)8 CognitoUserAttributes (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes)7 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)7 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)7 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)6 UpdateUserAttributesRequest (com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest)6 Test (org.junit.Test)6 SharedPreferences (android.content.SharedPreferences)4 GetUserRequest (com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest)2 ArrayList (java.util.ArrayList)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 Handler (android.os.Handler)1 AmazonClientException (com.amazonaws.AmazonClientException)1 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)1 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)1 CognitoInternalErrorException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException)1 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)1 CognitoParameterInvalidException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException)1 AuthenticationHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler)1 DevicesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.DevicesHandler)1