Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): throw correct auth exception for code mismatch #1370

Merged
merged 13 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,8 @@ public void onResult(UserStateDetails result) {

@Override
public void onError(Exception exception) {
onException.accept(new AuthException(
"An error occurred while attempting to retrieve your user details",
exception,
"See attached exception for more details"
));
onException.accept(CognitoAuthExceptionConverter.lookup(
exception, "Fetching authorization session failed."));
}
});
} catch (Throwable exception) {
Expand All @@ -582,11 +579,8 @@ public void onResult(Void result) {

@Override
public void onError(Exception exception) {
onException.accept(new AuthException(
"An error occurred while remembering a device",
exception,
"See attached exception for more details"
));
onException.accept(CognitoAuthExceptionConverter.lookup(
exception, "Remember device failed."));
}
});
}
Expand All @@ -604,11 +598,8 @@ public void onResult(Void result) {

@Override
public void onError(Exception exception) {
onException.accept(new AuthException(
"An error occurred while forgetting a device",
exception,
"See attached exception for more details"
));
onException.accept(CognitoAuthExceptionConverter.lookup(
exception, "Forget device failed."));
}
});
}
Expand All @@ -627,11 +618,8 @@ public void onResult(Void result) {

@Override
public void onError(Exception exception) {
onException.accept(new AuthException(
"An error occurred while forgetting a device",
exception,
"See attached exception for more details"
));
onException.accept(CognitoAuthExceptionConverter.lookup(
exception, "Forget device failed."));
}
});
}
Expand All @@ -653,11 +641,8 @@ public void onResult(ListDevicesResult result) {

@Override
public void onError(Exception exception) {
onException.accept(new AuthException(
"An error occurred while fetching remembered devices.",
exception,
"See attached exception for more details"
));
onException.accept(CognitoAuthExceptionConverter.lookup(
exception, "Fetching devices failed."));
}
});
}
Expand Down Expand Up @@ -690,11 +675,8 @@ public void onResult(ForgotPasswordResult result) {

@Override
public void onError(Exception exception) {
onException.accept(new AuthException(
"An error occurred triggering password recovery",
exception,
"See attached exception for more details"
));
onException.accept(CognitoAuthExceptionConverter.lookup(
exception, "Reset password failed."));
}
});
}
Expand Down Expand Up @@ -725,11 +707,8 @@ public void onResult(ForgotPasswordResult result) {

@Override
public void onError(Exception error) {
onException.accept(new AuthException(
"An error occurred confirming password recovery code",
error,
"See attached exception for more details"
));
onException.accept(CognitoAuthExceptionConverter.lookup(
error, "Confirm reset password failed."));
}
}
);
Expand All @@ -749,12 +728,9 @@ public void onResult(Void result) {
}

@Override
public void onError(Exception error) {
onException.accept(new AuthException(
"Failed to change password",
error,
"See attached exception for more details"
));
public void onError(Exception exception) {
onException.accept(CognitoAuthExceptionConverter.lookup(
exception, "Update password failed."));
}
});
}
Expand All @@ -777,11 +753,8 @@ public void onResult(Map<String, String> result) {

@Override
public void onError(Exception error) {
onError.accept(new AuthException(
"Failed to fetch user attributes",
error,
"Ensure that you are logged in and online"
));
onError.accept(CognitoAuthExceptionConverter.lookup(
error, "Fetching user attributes failed."));
}
});
}
Expand Down Expand Up @@ -932,11 +905,8 @@ public void onResult(Void result) {

@Override
public void onError(Exception error) {
onError.accept(new AuthException(
"An error occurred confirming user attribute",
error,
"See attached exception for more details"
));
onError.accept(CognitoAuthExceptionConverter.lookup(
error, "Confirming user attributes failed."));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
import com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException;
import com.amazonaws.services.cognitoidentityprovider.model.InvalidPasswordException;
import com.amazonaws.services.cognitoidentityprovider.model.LimitExceededException;
import com.amazonaws.services.cognitoidentityprovider.model.MFAMethodNotFoundException;
import com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException;
import com.amazonaws.services.cognitoidentityprovider.model.PasswordResetRequiredException;
import com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException;
import com.amazonaws.services.cognitoidentityprovider.model.SoftwareTokenMFANotFoundException;
import com.amazonaws.services.cognitoidentityprovider.model.TooManyFailedAttemptsException;
import com.amazonaws.services.cognitoidentityprovider.model.TooManyRequestsException;
import com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException;
import com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException;
import com.amazonaws.services.cognitoidentityprovider.model.UsernameExistsException;
Expand Down Expand Up @@ -91,14 +95,30 @@ public static AuthException lookup(@NonNull Exception error, @NonNull String fal
return new AuthException.LimitExceededException(error);
}

if (error instanceof MFAMethodNotFoundException) {
return new AuthException.MFAMethodNotFoundException(error);
}

if (error instanceof NotAuthorizedException) {
return new AuthException.NotAuthorizedException(error);
}

if (error instanceof ResourceNotFoundException) {
return new AuthException.ResourceNotFoundException(error);
}

if (error instanceof SoftwareTokenMFANotFoundException) {
return new AuthException.SoftwareTokenMFANotFoundException(error);
}

if (error instanceof TooManyFailedAttemptsException) {
return new AuthException.FailedAttemptsLimitExceededException(error);
}

if (error instanceof TooManyRequestsException) {
return new AuthException.TooManyRequestsException(error);
}

if (error instanceof PasswordResetRequiredException) {
return new AuthException.PasswordResetRequiredException(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class KotlinDataStoreFacade(private val delegate: Delegate = Amplify.DataStore)
}

@Throws(DataStoreException::class)
override suspend fun <T : Model> delete(itemClass: KClass<T>, filter: QueryPredicate) {
override suspend fun <T : Model> delete(byClass: KClass<T>, filter: QueryPredicate) {
return suspendCoroutine { continuation ->
delegate.delete(
itemClass.java,
byClass.java,
filter,
{ continuation.resume(Unit) },
{ continuation.resumeWithException(it) }
Expand Down
72 changes: 72 additions & 0 deletions core/src/main/java/com/amplifyframework/auth/AuthException.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,42 @@ public LimitExceededException(Throwable cause) {
}
}

/**
* Could not find multi-factor authentication (MFA) method in AWS Cognito.
*/
public static class MFAMethodNotFoundException extends AuthException {
private static final long serialVersionUID = 1L;
private static final String MESSAGE = "Could not find multi-factor authentication (MFA) method.";
private static final String RECOVERY_SUGGESTION =
"Configure multi-factor authentication using Amplify CLI or AWS Cognito console.";

/**
* Default message/recovery suggestion with a cause.
* @param cause The original error.
*/
public MFAMethodNotFoundException(Throwable cause) {
super(MESSAGE, cause, RECOVERY_SUGGESTION);
}
}

/**
* Could not perform the operation since user is not authorized.
*/
public static class NotAuthorizedException extends AuthException {
private static final long serialVersionUID = 1L;
private static final String MESSAGE = "Failed since user is not authorized.";
private static final String RECOVERY_SUGGESTION =
"Check whether the given values are correct and the user is authorized to perform the operation.";

/**
* Default message/recovery suggestion with a cause.
* @param cause The original error.
*/
public NotAuthorizedException(Throwable cause) {
super(MESSAGE, cause, RECOVERY_SUGGESTION);
}
}

/**
* Could not perform the action because password needs to be reset.
*/
Expand Down Expand Up @@ -431,6 +467,24 @@ public ResourceNotFoundException(Throwable cause) {
}
}

/**
* Could not find software token MFA for the user.
*/
public static class SoftwareTokenMFANotFoundException extends AuthException {
private static final long serialVersionUID = 1L;
private static final String MESSAGE = "Could not find software token MFA.";
private static final String RECOVERY_SUGGESTION =
"Enable the software token MFA for the user.";

/**
* Default message/recovery suggestion with a cause.
* @param cause The original error.
*/
public SoftwareTokenMFANotFoundException(Throwable cause) {
super(MESSAGE, cause, RECOVERY_SUGGESTION);
}
}

/**
* Could not perform the action because user made too many failed attempts for a given action.
*/
Expand All @@ -449,6 +503,24 @@ public FailedAttemptsLimitExceededException(Throwable cause) {
}
}

/**
* Could not perform the operation since user made too many requests.
*/
public static class TooManyRequestsException extends AuthException {
private static final long serialVersionUID = 1L;
private static final String MESSAGE = "Failed since the user made too many requests.";
private static final String RECOVERY_SUGGESTION =
"Make sure the requests send are controlled and the errors are properly handled.";

/**
* Default message/recovery suggestion with a cause.
* @param cause The original error.
*/
public TooManyRequestsException(Throwable cause) {
super(MESSAGE, cause, RECOVERY_SUGGESTION);
}
}

/**
* Could not complete an action because it was cancelled by the user.
*/
Expand Down