From 8b36a40be135183aa12fb9f182e88c4898e5e9e4 Mon Sep 17 00:00:00 2001 From: danilodominguezperez Date: Tue, 31 Aug 2021 13:21:21 -0500 Subject: [PATCH 1/3] Add check body for unverified accounts in 403 responses --- .../com/simperium/client/AuthException.java | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/Simperium/src/main/java/com/simperium/client/AuthException.java b/Simperium/src/main/java/com/simperium/client/AuthException.java index cd6e90c..b8a6c00 100644 --- a/Simperium/src/main/java/com/simperium/client/AuthException.java +++ b/Simperium/src/main/java/com/simperium/client/AuthException.java @@ -11,6 +11,7 @@ public class AuthException extends SimperiumException { static public final String UNVERIFIED_ACCOUNT_MESSAGE = "Account verification required"; static public final String COMPROMISED_PASSWORD_MESSAGE = "Password has been compromised"; static public final String COMPROMISED_PASSWORD_BODY = "compromised password"; + static public final String VERIFICATION_REQUIRED_BODY = "verification required"; static public final int ERROR_STATUS_CODE = -1; @@ -39,20 +40,15 @@ public static AuthException exceptionForStatusCode(int statusCode) { } public static AuthException exceptionForStatusCode(int statusCode, Throwable cause){ - switch (statusCode) { - case 409: - return new AuthException(FailureType.EXISTING_ACCOUNT, EXISTING_USER_FAILURE_MESSAGE, cause); - case 403: - return new AuthException(FailureType.UNVERIFIED_ACCOUNT, UNVERIFIED_ACCOUNT_MESSAGE, cause); - case 401: - // Code 401 can be obtain because credentials are wrong or the user's password has been compromised - // To differentiate both responses, we check the response's body - String message = cause != null && cause.getMessage() != null ? cause.getMessage().toLowerCase() : ""; - if (Objects.equals(message, COMPROMISED_PASSWORD_BODY)) { - return new AuthException(FailureType.COMPROMISED_PASSWORD, COMPROMISED_PASSWORD_MESSAGE, cause); - } - default: - return new AuthException(FailureType.INVALID_ACCOUNT, GENERIC_FAILURE_MESSAGE, cause); + String message = cause != null && cause.getMessage() != null ? cause.getMessage().toLowerCase() : ""; + if (statusCode == 409) { + return new AuthException(FailureType.EXISTING_ACCOUNT, EXISTING_USER_FAILURE_MESSAGE, cause); + } else if (statusCode == 403 && Objects.equals(message, VERIFICATION_REQUIRED_BODY)) { + return new AuthException(FailureType.UNVERIFIED_ACCOUNT, UNVERIFIED_ACCOUNT_MESSAGE, cause); + } else if (statusCode == 401 && Objects.equals(message, COMPROMISED_PASSWORD_BODY)) { + return new AuthException(FailureType.COMPROMISED_PASSWORD, COMPROMISED_PASSWORD_MESSAGE, cause); + } else { + return new AuthException(FailureType.INVALID_ACCOUNT, GENERIC_FAILURE_MESSAGE, cause); } } } \ No newline at end of file From 77e6375bb79c5a0529e058610157de96e8bbd5ba Mon Sep 17 00:00:00 2001 From: danilodominguezperez Date: Wed, 1 Sep 2021 10:33:47 -0500 Subject: [PATCH 2/3] Add space at the end of file --- Simperium/src/main/java/com/simperium/client/AuthException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Simperium/src/main/java/com/simperium/client/AuthException.java b/Simperium/src/main/java/com/simperium/client/AuthException.java index b8a6c00..30a4808 100644 --- a/Simperium/src/main/java/com/simperium/client/AuthException.java +++ b/Simperium/src/main/java/com/simperium/client/AuthException.java @@ -51,4 +51,4 @@ public static AuthException exceptionForStatusCode(int statusCode, Throwable cau return new AuthException(FailureType.INVALID_ACCOUNT, GENERIC_FAILURE_MESSAGE, cause); } } -} \ No newline at end of file +} From b39349535a80eec6d0f32581847d14d000d237f4 Mon Sep 17 00:00:00 2001 From: danilodominguezperez Date: Wed, 1 Sep 2021 10:45:01 -0500 Subject: [PATCH 3/3] Format code --- Simperium/src/main/java/com/simperium/client/AuthException.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Simperium/src/main/java/com/simperium/client/AuthException.java b/Simperium/src/main/java/com/simperium/client/AuthException.java index 30a4808..cba660c 100644 --- a/Simperium/src/main/java/com/simperium/client/AuthException.java +++ b/Simperium/src/main/java/com/simperium/client/AuthException.java @@ -41,6 +41,7 @@ public static AuthException exceptionForStatusCode(int statusCode) { public static AuthException exceptionForStatusCode(int statusCode, Throwable cause){ String message = cause != null && cause.getMessage() != null ? cause.getMessage().toLowerCase() : ""; + if (statusCode == 409) { return new AuthException(FailureType.EXISTING_ACCOUNT, EXISTING_USER_FAILURE_MESSAGE, cause); } else if (statusCode == 403 && Objects.equals(message, VERIFICATION_REQUIRED_BODY)) {