From ca5d203f1c3067d8c24c1a4c065c12b5cb211166 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Thu, 18 Jun 2026 18:40:19 -0400 Subject: [PATCH] Make `RegistrationServiceClient` blocking --- .../textsecuregcm/WhisperServerService.java | 6 +- .../auth/PhoneVerificationTokenManager.java | 20 +- .../RegistrationServiceClientFactory.java | 3 +- .../RegistrationServiceConfiguration.java | 7 +- .../controllers/VerificationController.java | 179 +++++--------- .../RegistrationServiceClient.java | 219 +++++++++--------- .../PhoneVerificationTokenManagerTest.java | 31 +-- .../StubRegistrationServiceClientFactory.java | 24 +- .../VerificationControllerTest.java | 205 ++++++---------- 9 files changed, 277 insertions(+), 417 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 48d139359..305ec8e6c 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -633,10 +633,6 @@ public class WhisperServerService extends Application new NotAuthorizedException("session not verified")); if (!MessageDigest.isEqual(number.getBytes(), session.number().getBytes())) { @@ -103,19 +101,11 @@ public class PhoneVerificationTokenManager { if (!session.verified()) { throw new NotAuthorizedException("session not verified"); } - } catch (final ExecutionException e) { - - if (e.getCause() instanceof StatusRuntimeException grpcRuntimeException) { - if (grpcRuntimeException.getStatus().getCode() == Status.Code.INVALID_ARGUMENT) { - throw new BadRequestException(); - } + } catch (final StatusRuntimeException e) { + if (e.getStatus().getCode() == Status.Code.INVALID_ARGUMENT) { + throw new BadRequestException(); } - logger.error("Registration service failure", e); - throw new ServerErrorException(Response.Status.SERVICE_UNAVAILABLE); - - } catch (final CancellationException | TimeoutException e) { - logger.error("Registration service failure", e); throw new ServerErrorException(Response.Status.SERVICE_UNAVAILABLE); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceClientFactory.java b/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceClientFactory.java index 3e18b9c93..0e670ef43 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceClientFactory.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceClientFactory.java @@ -15,6 +15,5 @@ import java.util.concurrent.ScheduledExecutorService; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = RegistrationServiceConfiguration.class) public interface RegistrationServiceClientFactory extends Discoverable { - RegistrationServiceClient build(Environment environment, Executor callbackExecutor, - ScheduledExecutorService identityRefreshExecutor); + RegistrationServiceClient build(Environment environment, ScheduledExecutorService identityRefreshExecutor); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceConfiguration.java b/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceConfiguration.java index 3f230a826..836b447b6 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceConfiguration.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/configuration/RegistrationServiceConfiguration.java @@ -5,7 +5,6 @@ import io.dropwizard.core.setup.Environment; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import java.io.IOException; -import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import org.whispersystems.textsecuregcm.configuration.secrets.SecretBytes; import org.whispersystems.textsecuregcm.registration.IdentityTokenCallCredentials; @@ -21,16 +20,16 @@ public record RegistrationServiceConfiguration(@NotBlank String host, RegistrationServiceClientFactory { @Override - public RegistrationServiceClient build(final Environment environment, final Executor callbackExecutor, + public RegistrationServiceClient build(final Environment environment, final ScheduledExecutorService identityRefreshExecutor) { + try { final IdentityTokenCallCredentials callCredentials = IdentityTokenCallCredentials.fromCredentialConfig( credentialConfigurationJson, identityTokenAudience, identityRefreshExecutor); environment.lifecycle().manage(callCredentials); - return new RegistrationServiceClient(host, port, callCredentials, registrationCaCertificate, collationKeySalt.value(), - identityRefreshExecutor); + return new RegistrationServiceClient(host, port, callCredentials, registrationCaCertificate, collationKeySalt.value()); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java index 95e3e7af0..a90763007 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java @@ -59,8 +59,6 @@ import java.util.HexFormat; import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.concurrent.CancellationException; -import java.util.concurrent.CompletionException; import org.apache.commons.lang3.Strings; import org.apache.http.HttpStatus; import org.slf4j.Logger; @@ -91,7 +89,6 @@ import org.whispersystems.textsecuregcm.registration.MessageTransport; import org.whispersystems.textsecuregcm.registration.RegistrationFraudException; import org.whispersystems.textsecuregcm.registration.RegistrationServiceClient; import org.whispersystems.textsecuregcm.registration.RegistrationServiceException; -import org.whispersystems.textsecuregcm.registration.RegistrationServiceSenderException; import org.whispersystems.textsecuregcm.registration.TransportNotAllowedException; import org.whispersystems.textsecuregcm.registration.VerificationSession; import org.whispersystems.textsecuregcm.spam.RegistrationFraudChecker; @@ -105,7 +102,6 @@ import org.whispersystems.textsecuregcm.storage.VerificationSessionManager; import org.whispersystems.textsecuregcm.telephony.CarrierData; import org.whispersystems.textsecuregcm.telephony.CarrierDataException; import org.whispersystems.textsecuregcm.telephony.CarrierDataProvider; -import org.whispersystems.textsecuregcm.util.ExceptionUtils; import org.whispersystems.textsecuregcm.util.ObsoletePhoneNumberFormatException; import org.whispersystems.textsecuregcm.util.Pair; import org.whispersystems.textsecuregcm.util.Util; @@ -225,27 +221,13 @@ public class VerificationController { maybeCarrierData = Optional.empty(); } - final RegistrationServiceSession registrationServiceSession; - try { - final String sourceHost = (String) requestContext.getProperty(RemoteAddressFilter.REMOTE_ADDRESS_ATTRIBUTE_NAME); - - registrationServiceSession = registrationServiceClient.createRegistrationSession(phoneNumber, - sourceHost, - accountsManager.getByE164(request.number()).isPresent(), - maybeCarrierData.flatMap(CarrierData::mcc).orElse(null), - maybeCarrierData.flatMap(CarrierData::mnc).orElse(null), - REGISTRATION_RPC_TIMEOUT).join(); - } catch (final CancellationException e) { - - throw new ServerErrorException("registration service unavailable", Response.Status.SERVICE_UNAVAILABLE); - } catch (final CompletionException e) { - - if (ExceptionUtils.unwrap(e) instanceof RateLimitExceededException re) { - throw re; - } - - throw new ServerErrorException(Response.Status.INTERNAL_SERVER_ERROR, e); - } + final RegistrationServiceSession registrationServiceSession = + registrationServiceClient.createRegistrationSession(phoneNumber, + (String) requestContext.getProperty(RemoteAddressFilter.REMOTE_ADDRESS_ATTRIBUTE_NAME), + accountsManager.getByE164(request.number()).isPresent(), + maybeCarrierData.flatMap(CarrierData::mcc).orElse(null), + maybeCarrierData.flatMap(CarrierData::mnc).orElse(null), + REGISTRATION_RPC_TIMEOUT); VerificationSession verificationSession = new VerificationSession(registrationServiceSession.encodedSessionId(), null, @@ -654,59 +636,44 @@ public class VerificationController { clientType, acceptLanguage.orElse(null), senderOverride, - REGISTRATION_RPC_TIMEOUT).join(); + REGISTRATION_RPC_TIMEOUT); + } catch (final VerificationSessionRateLimitExceededException e) { + throw new ClientErrorException(buildResponseForRateLimitExceeded(verificationSession, + e.getRegistrationSession(), + e.getRetryDuration())); + } catch (final RegistrationServiceException registrationServiceException) { + throw registrationServiceException.getRegistrationSession() + .map(s -> buildResponse(s, verificationSession)) + .map(verificationSessionResponse -> { + final Response response = registrationServiceException instanceof TransportNotAllowedException + ? Response.status(418).entity(verificationSessionResponse).build() + : Response.status(Response.Status.CONFLICT).entity(verificationSessionResponse).build(); - accountsManager.getByE164(registrationServiceSession.number()) - .filter(existingAccount -> - experimentEnrollmentManager.isEnrolled(existingAccount.getIdentifier(IdentityType.ACI), VERIFICATION_CODE_PUSH_NOTIFICATION_EXPERIMENT_NAME)) - .ifPresent(existingAccount -> { - try { - pushNotificationManager.sendVerificationCodeRequestedNotifications(existingAccount, clock.instant()); - } catch (final NotPushRegisteredException _) { - } - }); - } catch (final CancellationException e) { - throw new ServerErrorException("registration service unavailable", Response.Status.SERVICE_UNAVAILABLE); - } catch (final CompletionException e) { - final Throwable unwrappedException = ExceptionUtils.unwrap(e); - switch (unwrappedException) { - case RateLimitExceededException rateLimitExceededException -> { - if (rateLimitExceededException instanceof VerificationSessionRateLimitExceededException ve) { - final Response response = buildResponseForRateLimitExceeded(verificationSession, - ve.getRegistrationSession(), - ve.getRetryDuration()); - throw new ClientErrorException(response); - } - - throw new RateLimitExceededException(rateLimitExceededException.getRetryDuration().orElse(null)); - } - case RegistrationServiceException registrationServiceException -> - throw registrationServiceException.getRegistrationSession() - .map(s -> buildResponse(s, verificationSession)) - .map(verificationSessionResponse -> { - final Response response = registrationServiceException instanceof TransportNotAllowedException - ? Response.status(418).entity(verificationSessionResponse).build() - : Response.status(Response.Status.CONFLICT).entity(verificationSessionResponse).build(); - - return new ClientErrorException(response); - }) - .orElseGet(NotFoundException::new); - case RegistrationFraudException _ -> { - if (dynamicConfigurationManager.getConfiguration().getRegistrationConfiguration() - .squashDeclinedAttemptErrors()) { - return buildResponse(registrationServiceSession, verificationSession); - } else { - throw unwrappedException.getCause(); - } - } - case RegistrationServiceSenderException _ -> throw unwrappedException; - case null, default -> { - logger.error("Registration service failure", unwrappedException); - throw new ServerErrorException(Response.Status.INTERNAL_SERVER_ERROR); - } + return new ClientErrorException(response); + }) + .orElseGet(NotFoundException::new); + } catch (final RegistrationFraudException e) { + if (dynamicConfigurationManager.getConfiguration().getRegistrationConfiguration() + .squashDeclinedAttemptErrors()) { + return buildResponse(registrationServiceSession, verificationSession); + } else { + throw e.getCause(); } + } catch (final RuntimeException e) { + logger.error("Registration service failure", e); + throw new ServerErrorException(Response.Status.INTERNAL_SERVER_ERROR); } + accountsManager.getByE164(registrationServiceSession.number()) + .filter(existingAccount -> + experimentEnrollmentManager.isEnrolled(existingAccount.getIdentifier(IdentityType.ACI), VERIFICATION_CODE_PUSH_NOTIFICATION_EXPERIMENT_NAME)) + .ifPresent(existingAccount -> { + try { + pushNotificationManager.sendVerificationCodeRequestedNotifications(existingAccount, clock.instant()); + } catch (final NotPushRegisteredException _) { + } + }); + Metrics.counter(CODE_REQUESTED_COUNTER_NAME, Tags.of( UserAgentTagUtil.getPlatformTag(userAgent), Tag.of(COUNTRY_CODE_TAG_NAME, Util.getCountryCode(registrationServiceSession.number())), @@ -746,8 +713,7 @@ public class VerificationController { schema = @Schema(implementation = Integer.class))) public VerificationSessionResponse verifyCode(@PathParam("sessionId") final String encodedSessionId, @HeaderParam(HttpHeaders.USER_AGENT) final String userAgent, - @NotNull @Valid final SubmitVerificationCodeRequest submitVerificationCodeRequest) - throws RateLimitExceededException { + @NotNull @Valid final SubmitVerificationCodeRequest submitVerificationCodeRequest) { final RegistrationServiceSession registrationServiceSession = retrieveRegistrationServiceSession(encodedSessionId); final VerificationSession verificationSession = retrieveVerificationSession(registrationServiceSession); @@ -764,35 +730,17 @@ public class VerificationController { try { resultSession = registrationServiceClient.checkVerificationCode(registrationServiceSession.id(), submitVerificationCodeRequest.code(), - REGISTRATION_RPC_TIMEOUT) - .join(); - } catch (final CancellationException e) { - logger.warn("Unexpected cancellation from registration service", e); - throw new ServerErrorException(Response.Status.SERVICE_UNAVAILABLE); - } catch (final CompletionException e) { - final Throwable unwrappedException = ExceptionUtils.unwrap(e); - if (unwrappedException instanceof RateLimitExceededException rateLimitExceededException) { - - if (rateLimitExceededException instanceof VerificationSessionRateLimitExceededException ve) { - final Response response = buildResponseForRateLimitExceeded(verificationSession, ve.getRegistrationSession(), - ve.getRetryDuration()); - throw new ClientErrorException(response); - } - - throw new RateLimitExceededException(rateLimitExceededException.getRetryDuration().orElse(null)); - - } else if (unwrappedException instanceof RegistrationServiceException registrationServiceException) { - - throw registrationServiceException.getRegistrationSession() - .map(s -> buildResponse(s, verificationSession)) - .map(verificationSessionResponse -> new ClientErrorException( - Response.status(Response.Status.CONFLICT).entity(verificationSessionResponse).build())) - .orElseGet(NotFoundException::new); - - } else { - logger.error("Registration service failure", unwrappedException); - throw new ServerErrorException(Response.Status.INTERNAL_SERVER_ERROR); - } + REGISTRATION_RPC_TIMEOUT); + } catch (final VerificationSessionRateLimitExceededException e) { + throw new ClientErrorException(buildResponseForRateLimitExceeded(verificationSession, + e.getRegistrationSession(), + e.getRetryDuration())); + } catch (final RegistrationServiceException e) { + throw e.getRegistrationSession() + .map(s -> buildResponse(s, verificationSession)) + .map(verificationSessionResponse -> new ClientErrorException( + Response.status(Response.Status.CONFLICT).entity(verificationSessionResponse).build())) + .orElseGet(NotFoundException::new); } boolean existingRRP = false; @@ -868,9 +816,8 @@ public class VerificationController { } try { - final RegistrationServiceSession registrationServiceSession = registrationServiceClient.getSession(sessionId, - REGISTRATION_RPC_TIMEOUT).join() - .orElseThrow(NotFoundException::new); + final RegistrationServiceSession registrationServiceSession = + registrationServiceClient.getSession(sessionId, REGISTRATION_RPC_TIMEOUT).orElseThrow(NotFoundException::new); if (registrationServiceSession.verified()) { // Since there is a valid verification session we invalidate the other possible verification mechanism, @@ -882,14 +829,16 @@ public class VerificationController { return registrationServiceSession; - } catch (final CompletionException | CancellationException e) { - final Throwable unwrapped = ExceptionUtils.unwrap(e); - - if (unwrapped instanceof StatusRuntimeException grpcRuntimeException) { - if (grpcRuntimeException.getStatus().getCode() == Status.Code.INVALID_ARGUMENT) { - throw new BadRequestException(); - } + } catch (final StatusRuntimeException e) { + if (e.getStatus().getCode() == Status.Code.INVALID_ARGUMENT) { + throw new BadRequestException(); } + + logger.error("Registration service failure", e); + throw new ServerErrorException(Response.Status.SERVICE_UNAVAILABLE, e); + } catch (final WebApplicationException e) { + throw e; + } catch (final RuntimeException e) { logger.error("Registration service failure", e); throw new ServerErrorException(Response.Status.SERVICE_UNAVAILABLE, e); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/registration/RegistrationServiceClient.java b/service/src/main/java/org/whispersystems/textsecuregcm/registration/RegistrationServiceClient.java index b53e9cd8c..d3a809d86 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/registration/RegistrationServiceClient.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/registration/RegistrationServiceClient.java @@ -19,32 +19,31 @@ import java.security.NoSuchAlgorithmException; import java.time.Duration; import java.util.Base64; import java.util.Optional; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.lang3.StringUtils; import org.signal.registration.rpc.CheckVerificationCodeRequest; +import org.signal.registration.rpc.CheckVerificationCodeResponse; import org.signal.registration.rpc.CreateRegistrationSessionRequest; +import org.signal.registration.rpc.CreateRegistrationSessionResponse; import org.signal.registration.rpc.GetRegistrationSessionMetadataRequest; +import org.signal.registration.rpc.GetRegistrationSessionMetadataResponse; import org.signal.registration.rpc.RegistrationServiceGrpc; import org.signal.registration.rpc.RegistrationSessionMetadata; import org.signal.registration.rpc.SendVerificationCodeRequest; +import org.signal.registration.rpc.SendVerificationCodeResponse; import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; import org.whispersystems.textsecuregcm.controllers.VerificationSessionRateLimitExceededException; import org.whispersystems.textsecuregcm.entities.RegistrationServiceSession; -import org.whispersystems.textsecuregcm.util.CompletableFutureUtil; public class RegistrationServiceClient implements Managed { private static final Base64.Encoder BASE64_UNPADDED_ENCODER = Base64.getEncoder().withoutPadding(); private final ManagedChannel channel; - private final RegistrationServiceGrpc.RegistrationServiceFutureStub stub; - private final Executor callbackExecutor; + private final RegistrationServiceGrpc.RegistrationServiceBlockingStub stub; private final byte[] collationKeySalt; /** @@ -68,8 +67,7 @@ public class RegistrationServiceClient implements Managed { final int port, final CallCredentials callCredentials, final String caCertificatePem, - final byte[] collationKeySalt, - final Executor callbackExecutor) throws IOException { + final byte[] collationKeySalt) throws IOException { try (final ByteArrayInputStream certificateInputStream = new ByteArrayInputStream(caCertificatePem.getBytes(StandardCharsets.UTF_8))) { final ChannelCredentials tlsChannelCredentials = TlsChannelCredentials.newBuilder() @@ -81,9 +79,8 @@ public class RegistrationServiceClient implements Managed { .build(); } - this.stub = RegistrationServiceGrpc.newFutureStub(channel).withCallCredentials(callCredentials); + this.stub = RegistrationServiceGrpc.newBlockingStub(channel).withCallCredentials(callCredentials); this.collationKeySalt = collationKeySalt; - this.callbackExecutor = callbackExecutor; // Fail fast: reject bad keys try { @@ -93,52 +90,54 @@ public class RegistrationServiceClient implements Managed { } } - public CompletableFuture createRegistrationSession( + public RegistrationServiceSession createRegistrationSession( final Phonenumber.PhoneNumber phoneNumber, final String sourceHost, final boolean accountExistsWithPhoneNumber, @Nullable final String clientMcc, @Nullable final String clientMnc, - final Duration timeout) { + final Duration timeout) throws RateLimitExceededException { final long e164 = Long.parseLong( PhoneNumberUtil.getInstance().format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164).substring(1)); final String rateLimitCollationKey = hmac(sourceHost); - return CompletableFutureUtil.toCompletableFuture(stub.withDeadline(toDeadline(timeout)) + final CreateRegistrationSessionResponse response = stub.withDeadline(toDeadline(timeout)) .createSession(CreateRegistrationSessionRequest.newBuilder() .setE164(e164) .setAccountExistsWithE164(accountExistsWithPhoneNumber) .setRateLimitCollationKey(rateLimitCollationKey) .setMcc(clientMcc != null ? clientMcc : "") .setMnc(clientMnc != null ? clientMnc : "") - .build()), callbackExecutor) - .thenApply(response -> switch (response.getResponseCase()) { - case SESSION_METADATA -> buildSessionResponseFromMetadata(response.getSessionMetadata()); + .build()); - case ERROR -> { - switch (response.getError().getErrorType()) { - case CREATE_REGISTRATION_SESSION_ERROR_TYPE_RATE_LIMITED -> throw new CompletionException( - new RateLimitExceededException(response.getError().getMayRetry() - ? Duration.ofSeconds(response.getError().getRetryAfterSeconds()) - : null - )); - case CREATE_REGISTRATION_SESSION_ERROR_TYPE_ILLEGAL_PHONE_NUMBER -> throw new IllegalArgumentException(); - default -> throw new RuntimeException( - "Unrecognized error type from registration service: " + response.getError().getErrorType()); - } - } + return switch (response.getResponseCase()) { + case SESSION_METADATA -> buildSessionResponseFromMetadata(response.getSessionMetadata()); - case RESPONSE_NOT_SET -> throw new RuntimeException("No response from registration service"); - }); + case ERROR -> { + switch (response.getError().getErrorType()) { + case CREATE_REGISTRATION_SESSION_ERROR_TYPE_RATE_LIMITED -> + throw new RateLimitExceededException(response.getError().getMayRetry() + ? Duration.ofSeconds(response.getError().getRetryAfterSeconds()) + : null + ); + case CREATE_REGISTRATION_SESSION_ERROR_TYPE_ILLEGAL_PHONE_NUMBER -> throw new IllegalArgumentException(); + default -> throw new RuntimeException( + "Unrecognized error type from registration service: " + response.getError().getErrorType()); + } + } + + case RESPONSE_NOT_SET -> throw new RuntimeException("No response from registration service"); + }; } - public CompletableFuture sendVerificationCode(final byte[] sessionId, + public RegistrationServiceSession sendVerificationCode(final byte[] sessionId, final MessageTransport messageTransport, final ClientType clientType, @Nullable final String acceptLanguage, @Nullable final String senderOverride, - final Duration timeout) { + final Duration timeout) + throws VerificationSessionRateLimitExceededException, RegistrationServiceException, RegistrationServiceSenderException, RegistrationFraudException { final SendVerificationCodeRequest.Builder requestBuilder = SendVerificationCodeRequest.newBuilder() .setSessionId(ByteString.copyFrom(sessionId)) @@ -153,102 +152,95 @@ public class RegistrationServiceClient implements Managed { requestBuilder.setSenderName(senderOverride); } - return CompletableFutureUtil.toCompletableFuture(stub.withDeadline(toDeadline(timeout)) - .sendVerificationCode(requestBuilder.build()), callbackExecutor) - .thenApply(response -> { - if (response.hasError()) { - switch (response.getError().getErrorType()) { - case SEND_VERIFICATION_CODE_ERROR_TYPE_RATE_LIMITED -> throw new CompletionException( - new VerificationSessionRateLimitExceededException( - buildSessionResponseFromMetadata(response.getSessionMetadata()), - response.getError().getMayRetry() - ? Duration.ofSeconds(response.getError().getRetryAfterSeconds()) - : null, - true)); + final SendVerificationCodeResponse response = + stub.withDeadline(toDeadline(timeout)).sendVerificationCode(requestBuilder.build()); - case SEND_VERIFICATION_CODE_ERROR_TYPE_SESSION_NOT_FOUND -> throw new CompletionException( - new RegistrationServiceException(null)); + if (response.hasError()) { + switch (response.getError().getErrorType()) { + case SEND_VERIFICATION_CODE_ERROR_TYPE_RATE_LIMITED -> throw new VerificationSessionRateLimitExceededException( + buildSessionResponseFromMetadata(response.getSessionMetadata()), + response.getError().getMayRetry() + ? Duration.ofSeconds(response.getError().getRetryAfterSeconds()) + : null, + true); - case SEND_VERIFICATION_CODE_ERROR_TYPE_SESSION_ALREADY_VERIFIED -> throw new CompletionException( - new RegistrationServiceException(buildSessionResponseFromMetadata(response.getSessionMetadata()))); + case SEND_VERIFICATION_CODE_ERROR_TYPE_SESSION_NOT_FOUND -> throw new RegistrationServiceException(null); - case SEND_VERIFICATION_CODE_ERROR_TYPE_SENDER_REJECTED -> throw new CompletionException( - RegistrationServiceSenderException.rejected(response.getError().getMayRetry())); - case SEND_VERIFICATION_CODE_ERROR_TYPE_SUSPECTED_FRAUD -> - throw new CompletionException(new RegistrationFraudException( - RegistrationServiceSenderException.rejected(response.getError().getMayRetry()))); - case SEND_VERIFICATION_CODE_ERROR_TYPE_SENDER_ILLEGAL_ARGUMENT -> throw new CompletionException( - RegistrationServiceSenderException.illegalArgument(response.getError().getMayRetry())); - case SEND_VERIFICATION_CODE_ERROR_TYPE_UNSPECIFIED -> throw new CompletionException( - RegistrationServiceSenderException.unknown(response.getError().getMayRetry())); - case SEND_VERIFICATION_CODE_ERROR_TYPE_TRANSPORT_NOT_ALLOWED -> throw new CompletionException( - new TransportNotAllowedException(buildSessionResponseFromMetadata(response.getSessionMetadata()))); - case SEND_VERIFICATION_CODE_ERROR_NO_SENDER_AVAILABLE -> throw new CompletionException( - RegistrationServiceSenderException.unavailable(response.getError().getMayRetry())); + case SEND_VERIFICATION_CODE_ERROR_TYPE_SESSION_ALREADY_VERIFIED -> + throw new RegistrationServiceException(buildSessionResponseFromMetadata(response.getSessionMetadata())); - default -> throw new CompletionException( - new RuntimeException("Failed to send verification code: " + response.getError().getErrorType())); - } - } else { - return buildSessionResponseFromMetadata(response.getSessionMetadata()); - } - }); + case SEND_VERIFICATION_CODE_ERROR_TYPE_SENDER_REJECTED -> + throw RegistrationServiceSenderException.rejected(response.getError().getMayRetry()); + + case SEND_VERIFICATION_CODE_ERROR_TYPE_SUSPECTED_FRAUD -> + throw new RegistrationFraudException( + RegistrationServiceSenderException.rejected(response.getError().getMayRetry())); + + case SEND_VERIFICATION_CODE_ERROR_TYPE_SENDER_ILLEGAL_ARGUMENT -> + throw RegistrationServiceSenderException.illegalArgument(response.getError().getMayRetry()); + + case SEND_VERIFICATION_CODE_ERROR_TYPE_UNSPECIFIED -> + throw RegistrationServiceSenderException.unknown(response.getError().getMayRetry()); + + case SEND_VERIFICATION_CODE_ERROR_TYPE_TRANSPORT_NOT_ALLOWED -> + throw new TransportNotAllowedException(buildSessionResponseFromMetadata(response.getSessionMetadata())); + case SEND_VERIFICATION_CODE_ERROR_NO_SENDER_AVAILABLE -> + throw RegistrationServiceSenderException.unavailable(response.getError().getMayRetry()); + + default -> throw new RuntimeException("Failed to send verification code: " + response.getError().getErrorType()); + } + } else { + return buildSessionResponseFromMetadata(response.getSessionMetadata()); + } } - public CompletableFuture checkVerificationCode(final byte[] sessionId, + public RegistrationServiceSession checkVerificationCode(final byte[] sessionId, final String verificationCode, - final Duration timeout) { - return CompletableFutureUtil.toCompletableFuture(stub.withDeadline(toDeadline(timeout)) + final Duration timeout) throws VerificationSessionRateLimitExceededException, RegistrationServiceException { + + final CheckVerificationCodeResponse response = stub.withDeadline(toDeadline(timeout)) .checkVerificationCode(CheckVerificationCodeRequest.newBuilder() .setSessionId(ByteString.copyFrom(sessionId)) .setVerificationCode(verificationCode) - .build()), callbackExecutor) - .thenApply(response -> { - if (response.hasError()) { - switch (response.getError().getErrorType()) { - case CHECK_VERIFICATION_CODE_ERROR_TYPE_RATE_LIMITED -> throw new CompletionException( - new VerificationSessionRateLimitExceededException( - buildSessionResponseFromMetadata(response.getSessionMetadata()), - response.getError().getMayRetry() - ? Duration.ofSeconds(response.getError().getRetryAfterSeconds()) - : null, - true)); + .build()); - case CHECK_VERIFICATION_CODE_ERROR_TYPE_NO_CODE_SENT, CHECK_VERIFICATION_CODE_ERROR_TYPE_ATTEMPT_EXPIRED -> - throw new CompletionException( - new RegistrationServiceException(buildSessionResponseFromMetadata(response.getSessionMetadata())) - ); + if (response.hasError()) { + switch (response.getError().getErrorType()) { + case CHECK_VERIFICATION_CODE_ERROR_TYPE_RATE_LIMITED -> throw new VerificationSessionRateLimitExceededException( + buildSessionResponseFromMetadata(response.getSessionMetadata()), + response.getError().getMayRetry() + ? Duration.ofSeconds(response.getError().getRetryAfterSeconds()) + : null, + true); - case CHECK_VERIFICATION_CODE_ERROR_TYPE_SESSION_NOT_FOUND -> throw new CompletionException( - new RegistrationServiceException(null) - ); + case CHECK_VERIFICATION_CODE_ERROR_TYPE_NO_CODE_SENT, CHECK_VERIFICATION_CODE_ERROR_TYPE_ATTEMPT_EXPIRED -> + throw new RegistrationServiceException(buildSessionResponseFromMetadata(response.getSessionMetadata())); - default -> throw new CompletionException( - new RuntimeException("Failed to check verification code: " + response.getError().getErrorType())); - } - } else { - return buildSessionResponseFromMetadata(response.getSessionMetadata()); - } - }); + case CHECK_VERIFICATION_CODE_ERROR_TYPE_SESSION_NOT_FOUND -> throw new RegistrationServiceException(null); + + default -> throw new RuntimeException("Failed to check verification code: " + response.getError().getErrorType()); + } + } else { + return buildSessionResponseFromMetadata(response.getSessionMetadata()); + } } - public CompletableFuture> getSession(final byte[] sessionId, - final Duration timeout) { - return CompletableFutureUtil.toCompletableFuture(stub.withDeadline(toDeadline(timeout)).getSessionMetadata( - GetRegistrationSessionMetadataRequest.newBuilder() - .setSessionId(ByteString.copyFrom(sessionId)).build()), callbackExecutor) - .thenApply(response -> { - if (response.hasError()) { - switch (response.getError().getErrorType()) { - case GET_REGISTRATION_SESSION_METADATA_ERROR_TYPE_NOT_FOUND -> { - return Optional.empty(); - } - default -> throw new RuntimeException("Failed to get session: " + response.getError().getErrorType()); - } - } + public Optional getSession(final byte[] sessionId, final Duration timeout) { + final GetRegistrationSessionMetadataResponse response = stub.withDeadline(toDeadline(timeout)) + .getSessionMetadata(GetRegistrationSessionMetadataRequest.newBuilder() + .setSessionId(ByteString.copyFrom(sessionId)).build()); - return Optional.of(buildSessionResponseFromMetadata(response.getSessionMetadata())); - }); + if (response.hasError()) { + switch (response.getError().getErrorType()) { + case GET_REGISTRATION_SESSION_METADATA_ERROR_TYPE_NOT_FOUND -> { + return Optional.empty(); + } + + default -> throw new RuntimeException("Failed to get session: " + response.getError().getErrorType()); + } + } + + return Optional.of(buildSessionResponseFromMetadata(response.getSessionMetadata())); } private static RegistrationServiceSession buildSessionResponseFromMetadata( @@ -288,7 +280,7 @@ public class RegistrationServiceClient implements Managed { } } - private String hmac(String sourceHost) { + private String hmac(final String sourceHost) { final Mac hmacSha256 = getInitializedMac(); hmacSha256.update(sourceHost.getBytes(StandardCharsets.UTF_8)); @@ -303,7 +295,6 @@ public class RegistrationServiceClient implements Managed { } } - private static Mac getInitializedMac(byte[] key) throws InvalidKeyException { try { final Mac hmacSha256 = Mac.getInstance("HmacSHA256"); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java index 42d1e0006..7d27abbd1 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java @@ -8,7 +8,6 @@ package org.whispersystems.textsecuregcm.auth; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -23,7 +22,6 @@ import jakarta.ws.rs.container.ContainerRequestContext; import java.util.List; import java.util.Optional; import java.util.UUID; -import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; @@ -78,8 +76,8 @@ class PhoneVerificationTokenManagerTest { final byte[] sessionId = TestRandomUtil.nextBytes(16); when(registrationServiceClient.getSession(eq(sessionId), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of( - new RegistrationServiceSession(sessionId, PHONE_NUMBER, true, null, null, null, 0)))); + .thenReturn(Optional.of( + new RegistrationServiceSession(sessionId, PHONE_NUMBER, true, null, null, null, 0))); assertDoesNotThrow(() -> phoneVerificationTokenManager.verify(mock(ContainerRequestContext.class), PHONE_NUMBER, @@ -92,7 +90,7 @@ class PhoneVerificationTokenManagerTest { final byte[] sessionId = TestRandomUtil.nextBytes(16); when(registrationServiceClient.getSession(eq(sessionId), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.empty())); + .thenReturn(Optional.empty()); assertThrows(NotAuthorizedException.class, () -> phoneVerificationTokenManager.verify(mock(ContainerRequestContext.class), PHONE_NUMBER, @@ -105,8 +103,8 @@ class PhoneVerificationTokenManagerTest { final byte[] sessionId = TestRandomUtil.nextBytes(16); when(registrationServiceClient.getSession(eq(sessionId), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of( - new RegistrationServiceSession(sessionId, PHONE_NUMBER + "0", true, null, null, null, 0)))); + .thenReturn(Optional.of( + new RegistrationServiceSession(sessionId, PHONE_NUMBER + "0", true, null, null, null, 0))); assertThrows(BadRequestException.class, () -> phoneVerificationTokenManager.verify(mock(ContainerRequestContext.class), PHONE_NUMBER, @@ -119,8 +117,8 @@ class PhoneVerificationTokenManagerTest { final byte[] sessionId = TestRandomUtil.nextBytes(16); when(registrationServiceClient.getSession(eq(sessionId), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of( - new RegistrationServiceSession(sessionId, PHONE_NUMBER, false, null, null, null, 0)))); + .thenReturn(Optional.of( + new RegistrationServiceSession(sessionId, PHONE_NUMBER, false, null, null, null, 0))); assertThrows(NotAuthorizedException.class, () -> phoneVerificationTokenManager.verify(mock(ContainerRequestContext.class), PHONE_NUMBER, @@ -131,13 +129,9 @@ class PhoneVerificationTokenManagerTest { @ParameterizedTest @MethodSource void verifyRegistrationServiceClientException(final Throwable registrationServiceClientException, - final Class expectedExceptionClass) throws ExecutionException, InterruptedException, TimeoutException { + final Class expectedExceptionClass) { - @SuppressWarnings("unchecked") final CompletableFuture> mockFuture - = mock(CompletableFuture.class); - - when(mockFuture.get(anyLong(), any())).thenThrow(registrationServiceClientException); - when(registrationServiceClient.getSession(any(), any())).thenReturn(mockFuture); + when(registrationServiceClient.getSession(any(), any())).thenThrow(registrationServiceClientException); assertThrows(expectedExceptionClass, () -> phoneVerificationTokenManager.verify(mock(ContainerRequestContext.class), PHONE_NUMBER, @@ -147,10 +141,9 @@ class PhoneVerificationTokenManagerTest { private static List verifyRegistrationServiceClientException() { return List.of( - Arguments.arguments(new ExecutionException(Status.INVALID_ARGUMENT.asRuntimeException()), BadRequestException.class), - Arguments.arguments(new ExecutionException(Status.RESOURCE_EXHAUSTED.asRuntimeException()), ServerErrorException.class), - Arguments.arguments(new CancellationException(), ServerErrorException.class), - Arguments.arguments(new TimeoutException(), ServerErrorException.class) + Arguments.arguments(Status.INVALID_ARGUMENT.asRuntimeException(), BadRequestException.class), + Arguments.arguments(Status.RESOURCE_EXHAUSTED.asRuntimeException(), ServerErrorException.class), + Arguments.arguments(Status.DEADLINE_EXCEEDED.asRuntimeException(), ServerErrorException.class) ); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/configuration/StubRegistrationServiceClientFactory.java b/service/src/test/java/org/whispersystems/textsecuregcm/configuration/StubRegistrationServiceClientFactory.java index d566a599e..d0d542a16 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/configuration/StubRegistrationServiceClientFactory.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/configuration/StubRegistrationServiceClientFactory.java @@ -18,9 +18,7 @@ import java.time.Instant; import java.util.Base64; import java.util.Map; import java.util.Optional; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import org.checkerframework.checker.nullness.qual.Nullable; import org.whispersystems.textsecuregcm.configuration.secrets.SecretBytes; @@ -41,7 +39,7 @@ public class StubRegistrationServiceClientFactory implements RegistrationService private SecretBytes collationKeySalt; @Override - public RegistrationServiceClient build(final Environment environment, final Executor callbackExecutor, + public RegistrationServiceClient build(final Environment environment, final ScheduledExecutorService identityRefreshExecutor) { try { @@ -56,11 +54,11 @@ public class StubRegistrationServiceClientFactory implements RegistrationService private final static Map SESSIONS = new ConcurrentHashMap<>(); public StubRegistrationServiceClient(final String registrationCaCertificate, final byte[] collationKeySalt) throws IOException { - super("example.com", 8080, null, registrationCaCertificate, collationKeySalt, null); + super("example.com", 8080, null, registrationCaCertificate, collationKeySalt); } @Override - public CompletableFuture createRegistrationSession( + public RegistrationServiceSession createRegistrationSession( final Phonenumber.PhoneNumber phoneNumber, final String sourceHost, final boolean accountExistsWithPhoneNumber, @@ -77,18 +75,18 @@ public class StubRegistrationServiceClientFactory implements RegistrationService Instant.now().plus(Duration.ofMinutes(10)).toEpochMilli()); SESSIONS.put(Base64.getEncoder().encodeToString(id), session); - return CompletableFuture.completedFuture(session); + return session; } @Override - public CompletableFuture sendVerificationCode(final byte[] sessionId, + public RegistrationServiceSession sendVerificationCode(final byte[] sessionId, final MessageTransport messageTransport, final ClientType clientType, final @Nullable String acceptLanguage, final @Nullable String senderOverride, final Duration timeout) { - return CompletableFuture.completedFuture(SESSIONS.get(Base64.getEncoder().encodeToString(sessionId))); + return SESSIONS.get(Base64.getEncoder().encodeToString(sessionId)); } @Override - public CompletableFuture checkVerificationCode(final byte[] sessionId, + public RegistrationServiceSession checkVerificationCode(final byte[] sessionId, final String verificationCode, final Duration timeout) { final RegistrationServiceSession session = SESSIONS.get(Base64.getEncoder().encodeToString(sessionId)); @@ -97,14 +95,12 @@ public class StubRegistrationServiceClientFactory implements RegistrationService Instant.now().plus(Duration.ofMinutes(10)).toEpochMilli()); SESSIONS.put(Base64.getEncoder().encodeToString(sessionId), updatedSession); - return CompletableFuture.completedFuture(updatedSession); + return updatedSession; } @Override - public CompletableFuture> getSession(final byte[] sessionId, - final Duration timeout) { - return CompletableFuture.completedFuture( - Optional.ofNullable(SESSIONS.get(Base64.getEncoder().encodeToString(sessionId)))); + public Optional getSession(final byte[] sessionId, final Duration timeout) { + return Optional.ofNullable(SESSIONS.get(Base64.getEncoder().encodeToString(sessionId))); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java index 7b3e77bb6..ea8d84fe7 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java @@ -44,7 +44,6 @@ import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; @@ -121,8 +120,8 @@ class VerificationControllerTest { private final RateLimiter captchaLimiter = mock(RateLimiter.class); private final RateLimiter pushChallengeLimiter = mock(RateLimiter.class); - private final DynamicConfigurationManager dynamicConfigurationManager = mock( - DynamicConfigurationManager.class); + private final DynamicConfigurationManager dynamicConfigurationManager = + mock(DynamicConfigurationManager.class); private final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class); private final ResourceExtension resources = ResourceExtension.builder() @@ -206,9 +205,9 @@ class VerificationControllerTest { } @Test - void createSessionRateLimited() { + void createSessionRateLimited() throws RateLimitExceededException { when(registrationServiceClient.createRegistrationSession(any(), anyString(), anyBoolean(), any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture(new RateLimitExceededException(null))); + .thenThrow(new RateLimitExceededException(null)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session") @@ -220,9 +219,9 @@ class VerificationControllerTest { } @Test - void createSessionRegistrationServiceError() { + void createSessionRegistrationServiceError() throws RateLimitExceededException { when(registrationServiceClient.createRegistrationSession(any(), anyString(), anyBoolean(), any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture(new RuntimeException("expected service error"))); + .thenThrow(new RuntimeException("expected service error")); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session") @@ -235,12 +234,10 @@ class VerificationControllerTest { @ParameterizedTest @MethodSource - void createBeninSessionSuccess(final String requestedNumber, final String expectedNumber) { + void createBeninSessionSuccess(final String requestedNumber, final String expectedNumber) + throws RateLimitExceededException { when(registrationServiceClient.createRegistrationSession(any(), anyString(), anyBoolean(), any(), any(), any())) - .thenReturn( - CompletableFuture.completedFuture( - new RegistrationServiceSession(SESSION_ID, requestedNumber, false, null, null, null, - SESSION_EXPIRATION_SECONDS))); + .thenReturn(new RegistrationServiceSession(SESSION_ID, requestedNumber, false, null, null, null, SESSION_EXPIRATION_SECONDS)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session") @@ -269,17 +266,14 @@ class VerificationControllerTest { } @Test - void createBeninSessionFailure() { + void createBeninSessionFailure() throws RateLimitExceededException { // libphonenumber 8.13.50 and on generate new-format numbers for Benin final String newFormatBeninE164 = PhoneNumberUtil.getInstance() .format(PhoneNumberUtil.getInstance().getExampleNumber("BJ"), PhoneNumberUtil.PhoneNumberFormat.E164); final String oldFormatBeninE164 = newFormatBeninE164.replaceFirst("01", ""); when(registrationServiceClient.createRegistrationSession(any(), anyString(), anyBoolean(), any(), any(), any())) - .thenReturn( - CompletableFuture.completedFuture( - new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, - SESSION_EXPIRATION_SECONDS))); + .thenReturn(new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session") @@ -293,12 +287,9 @@ class VerificationControllerTest { @ParameterizedTest @MethodSource void createSessionSuccess(final String pushToken, final String pushTokenType, - final List expectedRequestedInformation) { + final List expectedRequestedInformation) throws RateLimitExceededException { when(registrationServiceClient.createRegistrationSession(any(), anyString(), anyBoolean(), any(), any(), any())) - .thenReturn( - CompletableFuture.completedFuture( - new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, - SESSION_EXPIRATION_SECONDS))); + .thenReturn(new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session") @@ -325,13 +316,10 @@ class VerificationControllerTest { @ParameterizedTest @ValueSource(booleans = {true, false}) - void createSessionReregistration(final boolean isReregistration) throws NumberParseException { + void createSessionReregistration(final boolean isReregistration) + throws NumberParseException, RateLimitExceededException { when(registrationServiceClient.createRegistrationSession(any(), anyString(), anyBoolean(), any(), any(), any())) - .thenReturn( - CompletableFuture.completedFuture( - new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, - SESSION_EXPIRATION_SECONDS))); - + .thenReturn(new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS)); when(accountsManager.getByE164(NUMBER)) .thenReturn(isReregistration ? Optional.of(mock(Account.class)) : Optional.empty()); @@ -371,7 +359,7 @@ class VerificationControllerTest { @Test void patchSessionNotFound() { when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.empty())); + .thenReturn(Optional.empty()); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodeSessionId(SESSION_ID)) @@ -389,9 +377,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of( new VerificationSession(encodedSessionId, null, null, List.of(VerificationSession.Information.CAPTCHA), Collections.emptyList(), @@ -430,9 +416,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, false, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); @@ -462,9 +446,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, false, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); @@ -494,9 +476,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, "challenge", null, List.of(VerificationSession.Information.PUSH_CHALLENGE), Collections.emptyList(), null, null, false, clock.millis(), clock.millis(), @@ -525,9 +505,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, List.of(VerificationSession.Information.CAPTCHA), Collections.emptyList(), null, null, false, clock.millis(), clock.millis(), @@ -568,9 +546,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, "challenge", @@ -613,8 +589,7 @@ class VerificationControllerTest { true, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, "challenge", null, List.of(), List.of(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); @@ -644,9 +619,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, "challenge", @@ -687,9 +660,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, List.of(VerificationSession.Information.CAPTCHA), Collections.emptyList(), null, null, false, clock.millis(), clock.millis(), @@ -731,9 +702,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, "challenge", @@ -779,7 +748,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, @@ -829,7 +798,7 @@ class VerificationControllerTest { @Test void getSessionInvalidArgs() { when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.failedFuture(new StatusRuntimeException(Status.INVALID_ARGUMENT))); + .thenThrow(new StatusRuntimeException(Status.INVALID_ARGUMENT)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodeSessionId(SESSION_ID)) @@ -843,7 +812,7 @@ class VerificationControllerTest { @Test void getSessionNotFound() { when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.empty())); + .thenReturn(Optional.empty()); when(verificationSessionManager.findForId(encodeSessionId(SESSION_ID))) .thenReturn(Optional.empty()); @@ -856,10 +825,7 @@ class VerificationControllerTest { } when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, - SESSION_EXPIRATION_SECONDS)))); + .thenReturn(Optional.of(new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS))); request = resources.getJerseyTest() .target("/v1/verification/session/" + encodeSessionId(SESSION_ID)) @@ -873,7 +839,7 @@ class VerificationControllerTest { @Test void getSessionError() { when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.failedFuture(new RuntimeException())); + .thenThrow(new RuntimeException()); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodeSessionId(SESSION_ID)) @@ -888,10 +854,7 @@ class VerificationControllerTest { void getSessionSuccess() { final String encodedSessionId = encodeSessionId(SESSION_ID); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, - SESSION_EXPIRATION_SECONDS)))); + .thenReturn(Optional.of(new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS))); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(mock(VerificationSession.class))); @@ -911,9 +874,7 @@ class VerificationControllerTest { true, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(mock(VerificationSession.class))); @@ -929,18 +890,19 @@ class VerificationControllerTest { } @Test - void requestVerificationCodeAlreadyVerified() { + void requestVerificationCodeAlreadyVerified() + throws RegistrationServiceException, RegistrationServiceSenderException, RegistrationFraudException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, true, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.sendVerificationCode(any(), any(), any(), any(), any(), any())) - .thenReturn(CompletableFuture.completedFuture(registrationServiceSession)); + .thenReturn(registrationServiceSession); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -963,9 +925,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, List.of(VerificationSession.Information.CAPTCHA), Collections.emptyList(), null, null, false, clock.millis(), clock.millis(), @@ -994,9 +954,7 @@ class VerificationControllerTest { false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of( - registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, false, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); @@ -1017,20 +975,19 @@ class VerificationControllerTest { } @Test - void requestVerificationCodeRateLimitExceeded() { + void requestVerificationCodeRateLimitExceeded() + throws RegistrationServiceException, RegistrationServiceSenderException, RegistrationFraudException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.sendVerificationCode(any(), any(), any(), any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture( - new CompletionException(new VerificationSessionRateLimitExceededException(registrationServiceSession, - Duration.ofMinutes(1), true)))); + .thenThrow(new VerificationSessionRateLimitExceededException(registrationServiceSession, Duration.ofMinutes(1), true)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1048,19 +1005,19 @@ class VerificationControllerTest { } @Test - void requestVerificationCodeTransportNotAllowed() { + void requestVerificationCodeTransportNotAllowed() + throws RegistrationServiceException, RegistrationServiceSenderException, RegistrationFraudException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.sendVerificationCode(any(), any(), any(), any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture( - new CompletionException(new TransportNotAllowedException(registrationServiceSession)))); + .thenThrow(new TransportNotAllowedException(registrationServiceSession)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1081,18 +1038,18 @@ class VerificationControllerTest { @CartesianTest void requestVerificationCodeSuccess(@CartesianTest.Values(booleans = {true, false}) final boolean accountExistsWithNumber, @CartesianTest.Values(booleans = {true, false}) final boolean enrolledInExperiment) - throws NotPushRegisteredException { + throws NotPushRegisteredException, RegistrationServiceException, RegistrationServiceSenderException, RegistrationFraudException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, null, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.sendVerificationCode(any(), any(), any(), any(), any(), any())) - .thenReturn(CompletableFuture.completedFuture(registrationServiceSession)); + .thenReturn(registrationServiceSession); final UUID accountIdentifier = UUID.randomUUID(); @@ -1130,21 +1087,20 @@ class VerificationControllerTest { @ParameterizedTest @MethodSource void requestVerificationCodeExternalServiceRefused(final boolean expectedPermanent, final String expectedReason, - final RegistrationServiceSenderException exception) { + final RegistrationServiceSenderException exception) + throws RegistrationServiceException, RegistrationServiceSenderException, RegistrationFraudException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.sendVerificationCode(any(), any(), any(), any(), any(), any())) - .thenReturn( - CompletableFuture.failedFuture(new CompletionException(exception))); + .thenThrow(exception); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1170,7 +1126,8 @@ class VerificationControllerTest { @ParameterizedTest @ValueSource(booleans = {false, true}) - void fraudError(boolean shadowFailure) { + void fraudError(boolean shadowFailure) + throws RegistrationServiceException, RegistrationServiceSenderException, RegistrationFraudException, VerificationSessionRateLimitExceededException { if (shadowFailure) { when(this.dynamicConfiguration.getRegistrationConfiguration()) .thenReturn(new DynamicRegistrationConfiguration(true)); @@ -1180,14 +1137,13 @@ class VerificationControllerTest { false, null, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.sendVerificationCode(any(), any(), any(), any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture(new CompletionException( - new RegistrationFraudException(RegistrationServiceSenderException.rejected(true))))); + .thenThrow(new RegistrationFraudException(RegistrationServiceSenderException.rejected(true))); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1206,20 +1162,19 @@ class VerificationControllerTest { @Test - void verifyCodeServerError() { + void verifyCodeServerError() throws RegistrationServiceException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.checkVerificationCode(any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture(new CompletionException(new RuntimeException()))); + .thenThrow(new RuntimeException()); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1238,8 +1193,7 @@ class VerificationControllerTest { true, null, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); @@ -1265,15 +1219,14 @@ class VerificationControllerTest { } @Test - void verifyCodeNoCodeRequested() { + void verifyCodeNoCodeRequested() throws RegistrationServiceException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, 0L, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); @@ -1281,9 +1234,7 @@ class VerificationControllerTest { // There is no explicit indication in the exception that no code has been sent, but we treat all RegistrationServiceExceptions // in which the response has a session object as conflicted state when(registrationServiceClient.checkVerificationCode(any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture(new CompletionException( - new RegistrationServiceException(new RegistrationServiceSession(SESSION_ID, NUMBER, false, 0L, null, null, - SESSION_EXPIRATION_SECONDS))))); + .thenThrow(new RegistrationServiceException(new RegistrationServiceSession(SESSION_ID, NUMBER, false, 0L, null, null, SESSION_EXPIRATION_SECONDS))); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1301,21 +1252,20 @@ class VerificationControllerTest { } @Test - void verifyCodeNoSession() { + void verifyCodeNoSession() throws RegistrationServiceException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.checkVerificationCode(any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture(new CompletionException(new RegistrationServiceException(null)))); + .thenThrow(new RegistrationServiceException(null)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1327,21 +1277,19 @@ class VerificationControllerTest { } @Test - void verifyCodeRateLimitExceeded() { + void verifyCodeRateLimitExceeded() + throws RegistrationServiceException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.checkVerificationCode(any(), any(), any())) - .thenReturn(CompletableFuture.failedFuture( - new CompletionException(new VerificationSessionRateLimitExceededException(registrationServiceSession, - Duration.ofMinutes(1), true)))); + .thenThrow(new VerificationSessionRateLimitExceededException(registrationServiceSession, Duration.ofMinutes(1), true)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1359,13 +1307,12 @@ class VerificationControllerTest { } @Test - void verifyCodeSuccess() { + void verifyCodeSuccess() throws RegistrationServiceException, VerificationSessionRateLimitExceededException { final String encodedSessionId = encodeSessionId(SESSION_ID); final RegistrationServiceSession registrationServiceSession = new RegistrationServiceSession(SESSION_ID, NUMBER, false, null, null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.getSession(any(), any())) - .thenReturn(CompletableFuture.completedFuture( - Optional.of(registrationServiceSession))); + .thenReturn(Optional.of(registrationServiceSession)); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); @@ -1374,7 +1321,7 @@ class VerificationControllerTest { null, 0L, SESSION_EXPIRATION_SECONDS); when(registrationServiceClient.checkVerificationCode(any(), any(), any())) - .thenReturn(CompletableFuture.completedFuture(verifiedSession)); + .thenReturn(verifiedSession); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code")