Rename the "answer challenge" method and normalize "not permitted" responses

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-09-16 10:27:38 -04:00
1 parent 580960d958
commit c09aab080f
3 files changed
+54 -26

No files matched your search

@@ -32,7 +32,7 @@ public class ChallengeGrpcService extends SimpleChallengeGrpc.ChallengeImplBase
}
@Override
public AnswerChallengeResponse handleChallengeResponse(final AnswerChallengeRequest request)
public AnswerChallengeResponse answerChallenge(final AnswerChallengeRequest request)
throws RateLimitExceededException, IOException {
final Account account = requireAuthenticatedAccount();
@@ -40,8 +40,13 @@ public class ChallengeGrpcService extends SimpleChallengeGrpc.ChallengeImplBase
challengeConstraintChecker.challengeConstraintsGrpc(account);
final boolean success = switch (request.getRequestCase()) {
case PUSH -> constraints.pushPermitted() && rateLimitChallengeManager.answerPushChallenge(account,
request.getPush().getChallenge());
case PUSH -> {
if (!constraints.pushPermitted()) {
throw GrpcExceptions.rateLimitExceeded(null);
}
yield rateLimitChallengeManager.answerPushChallenge(account, request.getPush().getChallenge());
}
case CAPTCHA -> {
try {
yield rateLimitChallengeManager.answerCaptchaChallenge(
@@ -50,7 +55,7 @@ public class ChallengeGrpcService extends SimpleChallengeGrpc.ChallengeImplBase
RequestAttributesUtil.getRemoteAddress().getHostAddress(),
RequestAttributesUtil.getUserAgent().orElse(null),
constraints.captchaScoreThreshold());
} catch (InvalidCaptchaArgumentException e) {
} catch (final InvalidCaptchaArgumentException e) {
throw GrpcExceptions.invalidArguments(e.getMessage());
}
}
@@ -16,13 +16,14 @@ import "org/signal/chat/tag.proto";
service Challenge {
option (require.auth) = AUTH_ONLY_AUTHENTICATED;
// Submit proof of a challenge completion
// Submit proof of a challenge completion.
//
// Some server endpoints (the "send message" endpoint, for example) may return
// a response indicating the client must complete a challenge before continuing.
// Clients may use this endpoint to provide proof of a completed challenge.
// If successful, the client may then continue their original operation.
rpc HandleChallengeResponse(AnswerChallengeRequest) returns (AnswerChallengeResponse) {}
rpc AnswerChallenge(AnswerChallengeRequest) returns (AnswerChallengeResponse) {}
// Requests that a challenge value be sent to the authenticated account's
// primary device via push notification. Once received, callers may present
@@ -71,7 +72,6 @@ message AnswerChallengeRequest {
AnswerPushChallengeRequest push = 2;
AnswerCaptchaChallengeRequest captcha = 3;
}
}
message AnswerChallengeResponse {
@@ -8,16 +8,14 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import java.io.IOException;
import java.time.Duration;
import java.util.Optional;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junitpioneer.jupiter.cartesian.CartesianTest;
import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
import org.mockito.Mock;
import org.signal.chat.challenge.AnswerChallengeRequest;
import org.signal.chat.challenge.AnswerChallengeResponse;
@@ -58,21 +56,42 @@ public class ChallengeGrpcServiceTest extends
);
}
@CartesianTest
void handlePushChallengeResponse(
@Values(booleans = {true, false}) final boolean pushPermitted,
@Values(booleans = {true, false}) final boolean success) throws RateLimitExceededException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(pushPermitted, Optional.empty()));
@ParameterizedTest
@ValueSource(booleans = {true, false})
void handlePushChallengeResponse(final boolean success) throws RateLimitExceededException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account))
.thenReturn(new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
when(rateLimitChallengeManager.answerPushChallenge(account, "foo")).thenReturn(success);
final AnswerChallengeResponse response = authenticatedServiceStub().handleChallengeResponse(
final AnswerChallengeResponse response = authenticatedServiceStub().answerChallenge(
AnswerChallengeRequest.newBuilder()
.setToken("token")
.setPush(AnswerChallengeRequest.AnswerPushChallengeRequest.newBuilder()
.setChallenge("foo")
.build())
.build());
assertEquals(pushPermitted && success, response.getSuccess());
assertEquals(success, response.getSuccess());
}
@Test
void handlePushChallengeResponseNotPermitted() throws RateLimitExceededException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account))
.thenReturn(new ChallengeConstraintChecker.ChallengeConstraints(false, Optional.empty()));
when(rateLimitChallengeManager.answerPushChallenge(account, "foo")).thenReturn(true);
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class,
() -> authenticatedServiceStub().answerChallenge(
AnswerChallengeRequest.newBuilder()
.setToken("token")
.setPush(AnswerChallengeRequest.AnswerPushChallengeRequest.newBuilder()
.setChallenge("foo")
.build())
.build()));
assertEquals(Status.RESOURCE_EXHAUSTED.getCode(), statusRuntimeException.getStatus().getCode());
verify(rateLimitChallengeManager, never()).answerPushChallenge(any(), any());
}
@Test
@@ -82,8 +101,10 @@ public class ChallengeGrpcServiceTest extends
final Duration retryAfter = Duration.ofMinutes(1);
doThrow(new RateLimitExceededException(retryAfter)).when(rateLimitChallengeManager)
.answerPushChallenge(account, "foo");
//noinspection ResultOfMethodCallIgnored
GrpcTestUtils.assertRateLimitExceeded(retryAfter,
() -> authenticatedServiceStub().handleChallengeResponse(AnswerChallengeRequest.newBuilder()
() -> authenticatedServiceStub().answerChallenge(AnswerChallengeRequest.newBuilder()
.setToken("token")
.setPush(AnswerChallengeRequest.AnswerPushChallengeRequest.newBuilder()
.setChallenge("foo")
@@ -99,7 +120,7 @@ public class ChallengeGrpcServiceTest extends
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
when(rateLimitChallengeManager.answerCaptchaChallenge(any(), any(), any(), any(), any())).thenReturn(
captchaSuccess);
final AnswerChallengeResponse response = authenticatedServiceStub().handleChallengeResponse(
final AnswerChallengeResponse response = authenticatedServiceStub().answerChallenge(
AnswerChallengeRequest.newBuilder()
.setToken("token")
.setCaptcha(AnswerChallengeRequest.AnswerCaptchaChallengeRequest.newBuilder()
@@ -117,8 +138,10 @@ public class ChallengeGrpcServiceTest extends
final Duration retryAfter = Duration.ofMinutes(1);
doThrow(new RateLimitExceededException(retryAfter)).when(rateLimitChallengeManager)
.answerCaptchaChallenge(any(), any(), any(), any(), any());
//noinspection ResultOfMethodCallIgnored
GrpcTestUtils.assertRateLimitExceeded(retryAfter,
() -> authenticatedServiceStub().handleChallengeResponse(AnswerChallengeRequest.newBuilder()
() -> authenticatedServiceStub().answerChallenge(AnswerChallengeRequest.newBuilder()
.setToken("token")
.setCaptcha(AnswerChallengeRequest.AnswerCaptchaChallengeRequest.newBuilder()
.setCaptcha("captcha")
@@ -131,7 +154,7 @@ public class ChallengeGrpcServiceTest extends
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
GrpcTestUtils.assertStatusInvalidArgument(
() -> authenticatedServiceStub().handleChallengeResponse(AnswerChallengeRequest.newBuilder().build()));
() -> authenticatedServiceStub().answerChallenge(AnswerChallengeRequest.newBuilder().build()));
}
@Test
@@ -139,7 +162,7 @@ public class ChallengeGrpcServiceTest extends
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
GrpcTestUtils.assertStatusInvalidArgument(
() -> authenticatedServiceStub().handleChallengeResponse(AnswerChallengeRequest.newBuilder()
() -> authenticatedServiceStub().answerChallenge(AnswerChallengeRequest.newBuilder()
.setPush(AnswerChallengeRequest.AnswerPushChallengeRequest.newBuilder()
.build())
.build()));
@@ -150,7 +173,7 @@ public class ChallengeGrpcServiceTest extends
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
GrpcTestUtils.assertStatusInvalidArgument(
() -> authenticatedServiceStub().handleChallengeResponse(AnswerChallengeRequest.newBuilder()
() -> authenticatedServiceStub().answerChallenge(AnswerChallengeRequest.newBuilder()
.setToken("token")
.setPush(AnswerChallengeRequest.AnswerPushChallengeRequest.newBuilder()
.build())
@@ -162,7 +185,7 @@ public class ChallengeGrpcServiceTest extends
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
GrpcTestUtils.assertStatusInvalidArgument(
() -> authenticatedServiceStub().handleChallengeResponse(AnswerChallengeRequest.newBuilder()
() -> authenticatedServiceStub().answerChallenge(AnswerChallengeRequest.newBuilder()
.setToken("token")
.setCaptcha(AnswerChallengeRequest.AnswerCaptchaChallengeRequest.newBuilder()
.build())