+ public static String handleVerificationSession(
+ SignalServiceAccountManager accountManager,
+ String sessionId,
+ Consumer<String> sessionIdSaver,
+ boolean voiceVerification,
+ String captcha
+ ) throws CaptchaRequiredException, IOException, RateLimitException {
+ RegistrationSessionMetadataResponse sessionResponse;
+ try {
+ sessionResponse = getValidSession(accountManager, sessionId);
+ } catch (PushChallengeRequiredException |
+ org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException e) {
+ if (captcha != null) {
+ sessionResponse = submitCaptcha(accountManager, sessionId, captcha);
+ } else {
+ throw new CaptchaRequiredException("Captcha Required");
+ }
+ }
+
+ sessionId = sessionResponse.getBody().getId();
+ sessionIdSaver.accept(sessionId);
+
+ if (sessionResponse.getBody().getVerified()) {
+ return sessionId;
+ }
+
+ if (sessionResponse.getBody().getAllowedToRequestCode()) {
+ return sessionId;
+ }
+
+ final var nextAttempt = voiceVerification
+ ? sessionResponse.getBody().getNextCall()
+ : sessionResponse.getBody().getNextSms();
+ if (nextAttempt != null && nextAttempt > 0) {
+ final var timestamp = sessionResponse.getHeaders().getTimestamp() + nextAttempt * 1000;
+ throw new RateLimitException(timestamp);
+ }
+
+ final var nextVerificationAttempt = sessionResponse.getBody().getNextVerificationAttempt();
+ if (nextVerificationAttempt != null && nextVerificationAttempt > 0) {
+ final var timestamp = sessionResponse.getHeaders().getTimestamp() + nextVerificationAttempt * 1000;
+ throw new CaptchaRequiredException(timestamp);
+ }
+
+ if (sessionResponse.getBody().getRequestedInformation().contains("captcha")) {
+ if (captcha != null) {
+ sessionResponse = submitCaptcha(accountManager, sessionId, captcha);
+ }
+ if (!sessionResponse.getBody().getAllowedToRequestCode()) {
+ throw new CaptchaRequiredException("Captcha Required");
+ }
+ }
+
+ return sessionId;
+ }
+