Add a gRPC endpoint for requesting push challenges

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

No files matched your search

@@ -1,14 +1,18 @@
package org.whispersystems.textsecuregcm.grpc;
import java.io.IOException;
import com.google.protobuf.Empty;
import org.signal.chat.challenge.AnswerChallengeRequest;
import org.signal.chat.challenge.AnswerChallengeResponse;
import org.signal.chat.challenge.RequestPushChallengeRequest;
import org.signal.chat.challenge.RequestPushChallengeResponse;
import org.signal.chat.challenge.SimpleChallengeGrpc;
import org.whispersystems.textsecuregcm.auth.grpc.AuthenticatedDevice;
import org.signal.chat.errors.FailedPrecondition;
import org.whispersystems.textsecuregcm.auth.grpc.AuthenticationUtil;
import org.whispersystems.textsecuregcm.captcha.InvalidCaptchaArgumentException;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.limits.RateLimitChallengeManager;
import org.whispersystems.textsecuregcm.push.NotPushRegisteredException;
import org.whispersystems.textsecuregcm.spam.ChallengeConstraintChecker;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountsManager;
@@ -31,12 +35,9 @@ public class ChallengeGrpcService extends SimpleChallengeGrpc.ChallengeImplBase
public AnswerChallengeResponse handleChallengeResponse(final AnswerChallengeRequest request)
throws RateLimitExceededException, IOException {
final AuthenticatedDevice authenticatedDevice = AuthenticationUtil.requireAuthenticatedDevice();
final Account account = accountsManager.getByAccountIdentifier(authenticatedDevice.accountIdentifier())
.orElseThrow(() -> GrpcExceptions.invalidCredentials("invalid credentials"));
final ChallengeConstraintChecker.ChallengeConstraints constraints = challengeConstraintChecker.challengeConstraintsGrpc(
account);
final Account account = requireAuthenticatedAccount();
final ChallengeConstraintChecker.ChallengeConstraints constraints =
challengeConstraintChecker.challengeConstraintsGrpc(account);
final boolean success = switch (request.getRequestCase()) {
case PUSH -> constraints.pushPermitted() && rateLimitChallengeManager.answerPushChallenge(account,
@@ -60,4 +61,32 @@ public class ChallengeGrpcService extends SimpleChallengeGrpc.ChallengeImplBase
.setSuccess(success)
.build();
}
@Override
public RequestPushChallengeResponse requestPushChallenge(final RequestPushChallengeRequest request) {
final Account account = requireAuthenticatedAccount();
final ChallengeConstraintChecker.ChallengeConstraints constraints =
challengeConstraintChecker.challengeConstraintsGrpc(account);
if (!constraints.pushPermitted()) {
throw GrpcExceptions.rateLimitExceeded(null);
}
try {
rateLimitChallengeManager.sendPushChallenge(account);
} catch (NotPushRegisteredException _) {
return RequestPushChallengeResponse.newBuilder()
.setNoPushToken(FailedPrecondition.getDefaultInstance())
.build();
}
return RequestPushChallengeResponse.newBuilder()
.setSentPushChallenge(Empty.getDefaultInstance())
.build();
}
private Account requireAuthenticatedAccount() {
return accountsManager.getByAccountIdentifier(AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier())
.orElseThrow(() -> GrpcExceptions.invalidCredentials("invalid credentials"));
}
}
@@ -9,17 +9,45 @@ option java_multiple_files = true;
package org.signal.chat.challenge;
import "google/protobuf/empty.proto";
import "org/signal/chat/errors.proto";
import "org/signal/chat/require.proto";
import "org/signal/chat/tag.proto";
service Challenge {
option (require.auth) = AUTH_ONLY_AUTHENTICATED;
// 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.
// 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) {}
// Requests that a challenge value be sent to the authenticated account's
// primary device via push notification. Once received, callers may present
// the challenge value via the HandleChallengeResponse RPC.
//
// APNs challenge payloads will be formatted as follows:
//
// ```
// {
// "aps": {
// "sound": "default",
// "alert": {
// "loc-key": "APN_Message"
// }
// },
// "rateLimitChallenge": "{CHALLENGE_TOKEN}"
// }
// ```
//
// FCM challenge payloads will be formatted as follows:
//
// ```
// {"rateLimitChallenge": "{CHALLENGE_TOKEN}"}
// ```
rpc RequestPushChallenge(RequestPushChallengeRequest) returns (RequestPushChallengeResponse) {}
}
message AnswerChallengeRequest {
@@ -50,3 +78,20 @@ message AnswerChallengeResponse {
// Whether the challenge proof was accepted
bool success = 1;
}
message RequestPushChallengeRequest {
}
message RequestPushChallengeResponse {
message SentPushChallenge {
}
oneof response {
// The server attempted to send a challenge value to the authenticated
// account's primary device via a push notification.
google.protobuf.Empty sent_push_challenge = 1;
// The authenticated account's primary device does not have a push token.
errors.FailedPrecondition no_push_token = 2 [(tag.reason) = "no_push_token"];
}
}
@@ -1,13 +1,18 @@
package org.whispersystems.textsecuregcm.grpc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
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;
@@ -17,9 +22,12 @@ import org.mockito.Mock;
import org.signal.chat.challenge.AnswerChallengeRequest;
import org.signal.chat.challenge.AnswerChallengeResponse;
import org.signal.chat.challenge.ChallengeGrpc;
import org.signal.chat.challenge.RequestPushChallengeRequest;
import org.signal.chat.challenge.RequestPushChallengeResponse;
import org.whispersystems.textsecuregcm.captcha.InvalidCaptchaArgumentException;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.limits.RateLimitChallengeManager;
import org.whispersystems.textsecuregcm.push.NotPushRegisteredException;
import org.whispersystems.textsecuregcm.spam.ChallengeConstraintChecker;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountsManager;
@@ -160,4 +168,43 @@ public class ChallengeGrpcServiceTest extends
.build())
.build()));
}
@Test
void requestPushChallenge() throws NotPushRegisteredException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
final RequestPushChallengeResponse response =
authenticatedServiceStub().requestPushChallenge(RequestPushChallengeRequest.getDefaultInstance());
assertEquals(RequestPushChallengeResponse.ResponseCase.SENT_PUSH_CHALLENGE, response.getResponseCase());
verify(rateLimitChallengeManager).sendPushChallenge(account);
}
@Test
void requestPushChallengeNotPermitted() throws NotPushRegisteredException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(false, Optional.empty()));
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () ->
authenticatedServiceStub().requestPushChallenge(RequestPushChallengeRequest.getDefaultInstance()));
assertEquals(Status.RESOURCE_EXHAUSTED.getCode(), statusRuntimeException.getStatus().getCode());
verify(rateLimitChallengeManager, never()).sendPushChallenge(any());
}
@Test
void requestPushChallengeNoPushToken() throws NotPushRegisteredException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
doThrow(NotPushRegisteredException.class)
.when(rateLimitChallengeManager).sendPushChallenge(any());
final RequestPushChallengeResponse response =
authenticatedServiceStub().requestPushChallenge(RequestPushChallengeRequest.getDefaultInstance());
assertEquals(RequestPushChallengeResponse.ResponseCase.NO_PUSH_TOKEN, response.getResponseCase());
}
}