diff --git a/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java b/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java index e1a49aa350..2db6444737 100644 --- a/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java +++ b/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java @@ -380,7 +380,7 @@ public class PushServiceSocket { body.put("client", androidSmsRetriever ? "android-2021-03" : "android"); - try (Response response = makeServiceRequest(path, "POST", jsonRequestBody(JsonUtil.toJson(body)), headers, new RegistrationSessionResponseHandler(), Optional.empty(), false)) { + try (Response response = makeServiceRequest(path, "POST", jsonRequestBody(JsonUtil.toJson(body)), headers, new RegistrationCodeRequestResponseHandler(), Optional.empty(), false)) { return parseSessionMetadataResponse(response); } } @@ -389,7 +389,7 @@ public class PushServiceSocket { String path = String.format(VERIFICATION_CODE_PATH, sessionId); Map body = new HashMap<>(); body.put("code", verificationCode); - try (Response response = makeServiceRequest(path, "PUT", jsonRequestBody(JsonUtil.toJson(body)), NO_HEADERS, new RegistrationCodeRequestResponseHandler(), Optional.empty(), false)) { + try (Response response = makeServiceRequest(path, "PUT", jsonRequestBody(JsonUtil.toJson(body)), NO_HEADERS, new RegistrationCodeSubmissionResponseHandler(), Optional.empty(), false)) { return parseSessionMetadataResponse(response); } } @@ -2572,6 +2572,44 @@ public class PushServiceSocket { private static class RegistrationSessionResponseHandler implements ResponseCodeHandler { + @Override + public void handle(int responseCode, ResponseBody body) throws NonSuccessfulResponseCodeException, PushNetworkException { + + if (responseCode == 403) { + throw new IncorrectRegistrationRecoveryPasswordException(); + } else if (responseCode == 404) { + throw new NoSuchSessionException(); + } else if (responseCode == 409) { + RegistrationSessionMetadataJson response; + try { + response = JsonUtil.fromJson(body.string(), RegistrationSessionMetadataJson.class); + } catch (IOException e) { + Log.e(TAG, "Unable to read response body.", e); + throw new NonSuccessfulResponseCodeException(409); + } + if (response.pushChallengedRequired()) { + throw new PushChallengeRequiredException(); + } else if (response.captchaRequired()) { + throw new CaptchaRequiredException(); + } else { + throw new HttpConflictException(); + } + } else if (responseCode == 502) { + VerificationCodeFailureResponseBody response; + try { + response = JsonUtil.fromJson(body.string(), VerificationCodeFailureResponseBody.class); + } catch (IOException e) { + Log.e(TAG, "Unable to read response body.", e); + throw new NonSuccessfulResponseCodeException(responseCode); + } + throw new ExternalServiceFailureException(response.getPermanentFailure(), response.getReason()); + } + } + } + + + private static class RegistrationCodeRequestResponseHandler implements ResponseCodeHandler { + @Override public void handle(int responseCode, ResponseBody body) throws NonSuccessfulResponseCodeException, PushNetworkException { @@ -2642,7 +2680,7 @@ public class PushServiceSocket { } } - private static class RegistrationCodeRequestResponseHandler implements ResponseCodeHandler { + private static class RegistrationCodeSubmissionResponseHandler implements ResponseCodeHandler { @Override public void handle(int responseCode, ResponseBody body) throws NonSuccessfulResponseCodeException, PushNetworkException {