Support multiple sequential captcha challenges.

This commit is contained in:
Nicholas
2023-03-03 09:51:27 -05:00
committed by GitHub
parent 89ad213994
commit d1d73fef30
5 changed files with 53 additions and 2 deletions

View File

@@ -0,0 +1,6 @@
package org.whispersystems.signalservice.api.push.exceptions
/**
* Exception representing that the submitted information was not accepted (e.g. the push challenge token or captcha did not match)
*/
class TokenNotAcceptedException : NonSuccessfulResponseCodeException(403)

View File

@@ -86,6 +86,7 @@ import org.whispersystems.signalservice.api.push.exceptions.RateLimitException;
import org.whispersystems.signalservice.api.push.exceptions.RemoteAttestationResponseExpiredException;
import org.whispersystems.signalservice.api.push.exceptions.ResumeLocationInvalidException;
import org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException;
import org.whispersystems.signalservice.api.push.exceptions.TokenNotAcceptedException;
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
import org.whispersystems.signalservice.api.push.exceptions.UsernameIsNotAssociatedWithAnAccountException;
import org.whispersystems.signalservice.api.push.exceptions.UsernameIsNotReservedException;
@@ -348,7 +349,7 @@ public class PushServiceSocket {
String path = VERIFICATION_SESSION_PATH + "/" + sessionId;
final UpdateVerificationSessionRequestBody requestBody = new UpdateVerificationSessionRequestBody(captchaToken, pushToken, pushChallengeToken, mcc, mnc);
try (Response response = makeServiceRequest(path, "PATCH", jsonRequestBody(JsonUtil.toJson(requestBody)), NO_HEADERS, new RegistrationSessionResponseHandler(), Optional.empty(), false)) {
try (Response response = makeServiceRequest(path, "PATCH", jsonRequestBody(JsonUtil.toJson(requestBody)), NO_HEADERS, new PatchRegistrationSessionResponseHandler(), Optional.empty(), false)) {
return parseSessionMetadataResponse(response);
}
}
@@ -2543,6 +2544,35 @@ public class PushServiceSocket {
}
}
private static class PatchRegistrationSessionResponseHandler implements ResponseCodeHandler {
@Override
public void handle(int responseCode, ResponseBody body) throws NonSuccessfulResponseCodeException, PushNetworkException {
switch (responseCode) {
case 403:
throw new TokenNotAcceptedException();
case 404:
throw new NoSuchSessionException();
case 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();
}
}
}
}
private static class RegistrationCodeRequestResponseHandler implements ResponseCodeHandler {
@Override public void handle(int responseCode, ResponseBody body) throws NonSuccessfulResponseCodeException, PushNetworkException {
switch (responseCode) {