Skip to content

Commit

Permalink
Merge pull request #235 from Simperium/add-account-verification-body-…
Browse files Browse the repository at this point in the history
…check

Add check body for unverified accounts in 403 responses
  • Loading branch information
danilo04 authored Sep 3, 2021
2 parents c0bbbb8 + b393495 commit 78e8200
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions Simperium/src/main/java/com/simperium/client/AuthException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -39,20 +40,16 @@ 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);
}
}
}
}

0 comments on commit 78e8200

Please sign in to comment.