Search in sources :

Example 1 with CognitoUserAttributes

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes 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 CognitoUserAttributes

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

the class SignUpActivity method signUp.

/**
 * Retrieve input and return to caller.
 * @param view the Android View
 */
public void signUp(final View view) {
    final String username = signUpView.getUserName();
    final String password = signUpView.getPassword();
    final String givenName = signUpView.getGivenName();
    final String email = signUpView.getEmail();
    final String phone = signUpView.getPhone();
    Log.d(LOG_TAG, "username = " + username);
    Log.d(LOG_TAG, "given_name = " + givenName);
    Log.d(LOG_TAG, "email = " + email);
    Log.d(LOG_TAG, "phone = " + phone);
    if (username.isEmpty()) {
        showError(getString(R.string.sign_up_username_missing));
        return;
    }
    if (password.length() < 6) {
        showError(getString(R.string.password_length_validation_failed));
        return;
    }
    final CognitoUserAttributes userAttributes = new CognitoUserAttributes();
    userAttributes.addAttribute(GIVEN_NAME, givenName);
    userAttributes.addAttribute(EMAIL_ADDRESS, email);
    if (!phone.isEmpty()) {
        userAttributes.addAttribute(PHONE_NUMBER, phone);
    }
    final AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(getString(R.string.sign_up_in_progress)).setMessage(getString(R.string.please_wait)).setNeutralButton(android.R.string.ok, null);
    final AlertDialog alertDialog = builder.show();
    mUserPool.signUpInBackground(username, password, userAttributes, null, new SignUpHandler() {

        @Override
        public void onSuccess(CognitoUser user, SignUpResult signUpResult) {
            alertDialog.dismiss();
            final Intent intent = new Intent();
            intent.putExtra(USERNAME, username);
            intent.putExtra(PASSWORD, password);
            intent.putExtra(IS_SIGN_UP_CONFIRMED, signUpResult.getUserConfirmed());
            intent.putExtra(CONFIRMATION_DESTINATION, signUpResult.getCodeDeliveryDetails().getDestination());
            setResult(RESULT_OK, intent);
            finish();
        }

        @Override
        public void onFailure(Exception exception) {
            alertDialog.dismiss();
            showError(exception.getLocalizedMessage() != null ? getErrorMessageFromException(exception) : "");
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) SignUpHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler) Intent(android.content.Intent) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) CognitoUserPoolsSignInProvider.getErrorMessageFromException(com.amazonaws.mobile.auth.userpools.CognitoUserPoolsSignInProvider.getErrorMessageFromException) SignUpResult(com.amazonaws.services.cognitoidentityprovider.model.SignUpResult)

Example 3 with CognitoUserAttributes

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes 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 CognitoUserAttributes

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes 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 CognitoUserAttributes

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

the class CognitoIdentityProviderUserPoolTest method init.

@Before
public void init() {
    if (firstTime) {
        // Initialization
        MockitoAnnotations.initMocks(this);
        testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET);
        awsKeyValueStorageUtility = getAWSKeyValueStorageUtility(testPool);
        // Data for tests
        TEST_IN_USER_ATTRIBUTES_LIST = new CognitoUserAttributes();
        TEST_IN_USER_ATTRIBUTES_LIST.addAttribute("email", TEST_USER_EMAIL);
        TEST_IN_USER_ATTRIBUTES_LIST.addAttribute("phone_number", TEST_USER_PHONE);
        TEST_IN_VALIDATION_DATA = new HashMap<String, String>();
        TEST_IN_VALIDATION_DATA.put("DummyAttribute_1", "Value4DummyAttribute_1");
        TEST_IN_VALIDATION_DATA.put("DummyAttribute_2", "Value4DummyAttribute_2");
        TEST_IN_VALIDATION_DATA.put("DummyAttribute_3", "Value4DummyAttribute_3");
        TEST_REGISTER_USER_RESPONSE = new SignUpResult();
        TEST_REGISTER_USER_RESPONSE.setUserConfirmed(true);
        TEST_REGISTER_USER_RESPONSE.setCodeDeliveryDetails(TEST_CODE_DELIVERY_DETAIL);
        firstTime = false;
    }
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) SignUpResult(com.amazonaws.services.cognitoidentityprovider.model.SignUpResult) Before(org.junit.Before)

Aggregations

CognitoUserAttributes (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes)10 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)8 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)7 UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)7 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)6 UpdateUserAttributesRequest (com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest)6 Test (org.junit.Test)6 SharedPreferences (android.content.SharedPreferences)4 AmazonClientException (com.amazonaws.AmazonClientException)2 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)2 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)2 CognitoUser (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser)2 SignUpHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler)2 NotAuthorizedException (com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)2 GetUserRequest (com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest)2 InvalidUserPoolConfigurationException (com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException)2 SignUpResult (com.amazonaws.services.cognitoidentityprovider.model.SignUpResult)2 JSONException (org.json.JSONException)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 AlertDialog (android.app.AlertDialog)1