diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcService.java index 6afba8b5b..3c544b10f 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcService.java @@ -157,16 +157,19 @@ public class BackupsAnonymousGrpcService extends SimpleBackupsAnonymousGrpc.Back } @Override - public SetPublicKeyResponse setPublicKey(final SetPublicKeyRequest request) - throws BackupFailedZkAuthenticationException { + public SetPublicKeyResponse setPublicKey(final SetPublicKeyRequest request) { final ECPublicKey publicKey = deserialize(ECPublicKey::new, request.getPublicKey().toByteArray()); final BackupAuthCredentialPresentation presentation = deserialize( BackupAuthCredentialPresentation::new, request.getSignedPresentation().getPresentation().toByteArray()); final byte[] signature = request.getSignedPresentation().getPresentationSignature().toByteArray(); - backupManager.setPublicKey(presentation, signature, publicKey); - return SetPublicKeyResponse.newBuilder().setSuccess(Empty.getDefaultInstance()).build(); + try { + backupManager.setPublicKey(presentation, signature, publicKey); + return SetPublicKeyResponse.newBuilder().setSuccess(Empty.getDefaultInstance()).build(); + } catch (BackupFailedZkAuthenticationException e) { + return SetPublicKeyResponse.newBuilder().setFailedAuthentication(FailedZkAuthentication.getDefaultInstance()).build(); + } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcServiceTest.java index d41a78c7c..a9da87578 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/BackupsAnonymousGrpcServiceTest.java @@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import com.google.common.collect.Iterators; @@ -114,6 +115,16 @@ class BackupsAnonymousGrpcServiceTest extends .getResponseCase()).isEqualTo(SetPublicKeyResponse.ResponseCase.SUCCESS); } + @Test + void setPublicKeyBadZkAuth() throws BackupFailedZkAuthenticationException { + doThrow(BackupFailedZkAuthenticationException.class).when(backupManager).setPublicKey(any(), any(), any()); + assertThat(unauthenticatedServiceStub().setPublicKey(SetPublicKeyRequest.newBuilder() + .setPublicKey(ByteString.copyFrom(ECKeyPair.generate().getPublicKey().serialize())) + .setSignedPresentation(signedPresentation(presentation)) + .build()) + .getResponseCase()).isEqualTo(SetPublicKeyResponse.ResponseCase.FAILED_AUTHENTICATION); + } + @Test void setBadPublicKey() { assertThatExceptionOfType(StatusRuntimeException.class).isThrownBy(() ->