Search in sources :

Example 1 with ForgotPasswordResult

use of com.amazonaws.mobile.client.results.ForgotPasswordResult in project amplify-android by aws-amplify.

the class AuthComponentTest method resetPasswordWithOptions.

/**
 * Tests that the resetPassword method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.forgotPassword with
 * the username and options it received.
 * Also ensures that in the onResult case, the success callback receives a valid AuthResetPasswordResult and in
 * the onError case, the error call back receives an AuthException with the root cause attached.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void resetPasswordWithOptions() throws AuthException {
    ForgotPasswordResult amcResult = new ForgotPasswordResult(ForgotPasswordState.CONFIRMATION_CODE);
    amcResult.setParameters(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    AWSCognitoAuthResetPasswordOptions.CognitoBuilder options = AWSCognitoAuthResetPasswordOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthResetPasswordOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<ForgotPasswordResult> callback = invocation.getArgument(2);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).forgotPassword(any(), any(), Mockito.<Callback<ForgotPasswordResult>>any());
    AuthResetPasswordResult result = synchronousAuth.resetPassword(USERNAME, builtOptions);
    assertFalse(result.isPasswordReset());
    assertEquals(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, result.getNextStep().getResetPasswordStep());
    validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).forgotPassword(eq(USERNAME), eq(metadata), Mockito.<Callback<ForgotPasswordResult>>any());
}
Also used : HashMap(java.util.HashMap) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AWSCognitoAuthResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResetPasswordOptions) AuthResetPasswordResult(com.amplifyframework.auth.result.AuthResetPasswordResult) Test(org.junit.Test)

Example 2 with ForgotPasswordResult

use of com.amazonaws.mobile.client.results.ForgotPasswordResult in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method confirmResetPassword.

@Override
public void confirmResetPassword(@NonNull String newPassword, @NonNull String confirmationCode, @NonNull AuthConfirmResetPasswordOptions options, @NonNull Action onSuccess, @NonNull Consumer<AuthException> onException) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthConfirmResetPasswordOptions) {
        AWSCognitoAuthConfirmResetPasswordOptions cognitoOptions = (AWSCognitoAuthConfirmResetPasswordOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    awsMobileClient.confirmForgotPassword(newPassword, confirmationCode, clientMetadata, new Callback<ForgotPasswordResult>() {

        @Override
        public void onResult(ForgotPasswordResult result) {
            if (result.getState().equals(ForgotPasswordState.DONE)) {
                onSuccess.call();
            } else {
                onException.accept(new AuthException("Received an unsupported response while confirming password recovery code: " + result.getState(), "This is almost certainly a bug. Please report it as an issue in our GitHub repo."));
            }
        }

        @Override
        public void onError(Exception error) {
            onException.accept(CognitoAuthExceptionConverter.lookup(error, "Confirm reset password failed."));
        }
    });
}
Also used : AWSCognitoAuthConfirmResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions) HashMap(java.util.HashMap) AuthException(com.amplifyframework.auth.AuthException) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) AuthException(com.amplifyframework.auth.AuthException) JSONException(org.json.JSONException) AmplifyException(com.amplifyframework.AmplifyException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) AuthNavigationException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)

Example 3 with ForgotPasswordResult

use of com.amazonaws.mobile.client.results.ForgotPasswordResult in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method resetPassword.

@Override
public void resetPassword(@NonNull String username, @NonNull AuthResetPasswordOptions options, @NonNull Consumer<AuthResetPasswordResult> onSuccess, @NonNull Consumer<AuthException> onException) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthResetPasswordOptions) {
        AWSCognitoAuthResetPasswordOptions cognitoOptions = (AWSCognitoAuthResetPasswordOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    awsMobileClient.forgotPassword(username, clientMetadata, new Callback<ForgotPasswordResult>() {

        @Override
        public void onResult(ForgotPasswordResult result) {
            if (result.getState().equals(ForgotPasswordState.CONFIRMATION_CODE)) {
                onSuccess.accept(new AuthResetPasswordResult(false, new AuthNextResetPasswordStep(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(result.getParameters()))));
            } else {
                onException.accept(new AuthException("Received an unsupported response after triggering password recovery: " + result.getState(), "This is almost certainly a bug. Please report it as an issue in our GitHub repo."));
            }
        }

        @Override
        public void onError(Exception exception) {
            onException.accept(CognitoAuthExceptionConverter.lookup(exception, "Reset password failed."));
        }
    });
}
Also used : HashMap(java.util.HashMap) AuthException(com.amplifyframework.auth.AuthException) AuthNextResetPasswordStep(com.amplifyframework.auth.result.step.AuthNextResetPasswordStep) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) AWSCognitoAuthResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResetPasswordOptions) AuthResetPasswordResult(com.amplifyframework.auth.result.AuthResetPasswordResult) AuthException(com.amplifyframework.auth.AuthException) JSONException(org.json.JSONException) AmplifyException(com.amplifyframework.AmplifyException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) AuthNavigationException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)

Example 4 with ForgotPasswordResult

use of com.amazonaws.mobile.client.results.ForgotPasswordResult in project aws-sdk-android by aws-amplify.

the class AWSMobileClientTest method testForgotPasswordWithNewUser.

@Test(expected = InvalidParameterException.class)
public void testForgotPasswordWithNewUser() throws Exception {
    // username user is a confirmed account without a confirmed email or phone
    final ForgotPasswordResult forgotPasswordResult = auth.forgotPassword(username);
    assertEquals(ForgotPasswordState.CONFIRMATION_CODE, forgotPasswordResult.getState());
}
Also used : ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) Test(org.junit.Test)

Example 5 with ForgotPasswordResult

use of com.amazonaws.mobile.client.results.ForgotPasswordResult in project amplify-android by aws-amplify.

the class AuthComponentTest method confirmResetPasswordWithOptions.

/**
 * Tests that the confirmResetPassword method of the Auth wrapper of AWSMobileClient (AMC) calls
 * AMC.confirmForgotPassword with the new password and confirmation code it received.
 * Also ensures that in the onResult case, the success callback is triggered and in the onError case,
 * the error call back receives an AuthException with the root cause attached.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void confirmResetPasswordWithOptions() throws AuthException {
    ForgotPasswordResult amcResult = new ForgotPasswordResult(ForgotPasswordState.DONE);
    AWSCognitoAuthConfirmResetPasswordOptions.CognitoBuilder options = AWSCognitoAuthConfirmResetPasswordOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthConfirmResetPasswordOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<ForgotPasswordResult> callback = invocation.getArgument(3);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).confirmForgotPassword(anyString(), anyString(), anyMap(), Mockito.any());
    synchronousAuth.confirmResetPassword(NEW_PASSWORD, CONFIRMATION_CODE, builtOptions);
    verify(mobileClient).confirmForgotPassword(eq(NEW_PASSWORD), eq(CONFIRMATION_CODE), eq(metadata), any());
}
Also used : AWSCognitoAuthConfirmResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions) HashMap(java.util.HashMap) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

ForgotPasswordResult (com.amazonaws.mobile.client.results.ForgotPasswordResult)9 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)3 AuthResetPasswordResult (com.amplifyframework.auth.result.AuthResetPasswordResult)3 JSONException (org.json.JSONException)3 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)2 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)2 AmplifyException (com.amplifyframework.AmplifyException)2 AuthException (com.amplifyframework.auth.AuthException)2 AWSCognitoAuthConfirmResetPasswordOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions)2 AWSCognitoAuthResetPasswordOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthResetPasswordOptions)2 RandomString (com.amplifyframework.testutils.random.RandomString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 AmazonClientException (com.amazonaws.AmazonClientException)1 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)1 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)1 ForgotPasswordContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ForgotPasswordContinuation)1 ForgotPasswordHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.ForgotPasswordHandler)1 NotAuthorizedException (com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)1