Search in sources :

Example 1 with ServerRejectedException

use of org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method sendGroupMessage.

public SendGroupMessageResponse sendGroupMessage(byte[] body, byte[] joinedUnidentifiedAccess, long timestamp, boolean online) throws IOException {
    ServiceConnectionHolder connectionHolder = (ServiceConnectionHolder) getRandom(serviceClients, random);
    String path = String.format(Locale.US, GROUP_MESSAGE_PATH, timestamp, online);
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(String.format("%s%s", connectionHolder.getUrl(), path));
    requestBuilder.put(RequestBody.create(MediaType.get("application/vnd.signal-messenger.mrm"), body));
    requestBuilder.addHeader("Unidentified-Access-Key", Base64.encodeBytes(joinedUnidentifiedAccess));
    if (signalAgent != null) {
        requestBuilder.addHeader("X-Signal-Agent", signalAgent);
    }
    if (connectionHolder.getHostHeader().isPresent()) {
        requestBuilder.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    Call call = connectionHolder.getUnidentifiedClient().newCall(requestBuilder.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response;
    try {
        response = call.execute();
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
    switch(response.code()) {
        case 200:
            return readBodyJson(response.body(), SendGroupMessageResponse.class);
        case 401:
            throw new InvalidUnidentifiedAccessHeaderException();
        case 404:
            throw new NotFoundException("At least one unregistered user in message send.");
        case 409:
            GroupMismatchedDevices[] mismatchedDevices = readBodyJson(response.body(), GroupMismatchedDevices[].class);
            throw new GroupMismatchedDevicesException(mismatchedDevices);
        case 410:
            GroupStaleDevices[] staleDevices = readBodyJson(response.body(), GroupStaleDevices[].class);
            throw new GroupStaleDevicesException(staleDevices);
        case 508:
            throw new ServerRejectedException();
        default:
            throw new NonSuccessfulResponseCodeException(response.code());
    }
}
Also used : Call(okhttp3.Call) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) GroupMismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupMismatchedDevicesException) ReceiptCredentialRequest(org.signal.zkgroup.receipts.ReceiptCredentialRequest) Request(okhttp3.Request) ChangePhoneNumberRequest(org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest) DiscoveryRequest(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest) KeyBackupRequest(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupRequest) ProfileKeyCredentialRequest(org.signal.zkgroup.profiles.ProfileKeyCredentialRequest) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) IOException(java.io.IOException) GroupStaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupStaleDevicesException) CallingResponse(org.whispersystems.signalservice.api.messages.calls.CallingResponse) Response(okhttp3.Response) KeyBackupResponse(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupResponse) ReceiptCredentialResponse(org.signal.zkgroup.receipts.ReceiptCredentialResponse) CredentialResponse(org.whispersystems.signalservice.api.groupsv2.CredentialResponse) StorageAuthResponse(org.whispersystems.signalservice.api.storage.StorageAuthResponse) VerifyDeviceResponse(org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse) DiscoveryResponse(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse) ProfileKeyCredentialResponse(org.signal.zkgroup.profiles.ProfileKeyCredentialResponse) TokenResponse(org.whispersystems.signalservice.internal.contacts.entities.TokenResponse) InvalidUnidentifiedAccessHeaderException(org.whispersystems.signalservice.internal.push.exceptions.InvalidUnidentifiedAccessHeaderException)

Example 2 with ServerRejectedException

use of org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method validateServiceResponse.

private Response validateServiceResponse(Response response) throws NonSuccessfulResponseCodeException, PushNetworkException, MalformedResponseException {
    int responseCode = response.code();
    String responseMessage = response.message();
    switch(responseCode) {
        case 413:
            throw new RateLimitException("Rate limit exceeded: " + responseCode);
        case 401:
        case 403:
            throw new AuthorizationFailedException(responseCode, "Authorization failed!");
        case 404:
            throw new NotFoundException("Not found");
        case 409:
            MismatchedDevices mismatchedDevices = readResponseJson(response, MismatchedDevices.class);
            throw new MismatchedDevicesException(mismatchedDevices);
        case 410:
            StaleDevices staleDevices = readResponseJson(response, StaleDevices.class);
            throw new StaleDevicesException(staleDevices);
        case 411:
            DeviceLimit deviceLimit = readResponseJson(response, DeviceLimit.class);
            throw new DeviceLimitExceededException(deviceLimit);
        case 417:
            throw new ExpectationFailedException();
        case 423:
            RegistrationLockFailure accountLockFailure = readResponseJson(response, RegistrationLockFailure.class);
            AuthCredentials credentials = accountLockFailure.backupCredentials;
            String basicStorageCredentials = credentials != null ? credentials.asBasic() : null;
            throw new LockedException(accountLockFailure.length, accountLockFailure.timeRemaining, basicStorageCredentials);
        case 428:
            ProofRequiredResponse proofRequiredResponse = readResponseJson(response, ProofRequiredResponse.class);
            String retryAfterRaw = response.header("Retry-After");
            long retryAfter = Util.parseInt(retryAfterRaw, -1);
            throw new ProofRequiredException(proofRequiredResponse, retryAfter);
        case 499:
            throw new DeprecatedVersionException();
        case 508:
            throw new ServerRejectedException();
    }
    if (responseCode != 200 && responseCode != 202 && responseCode != 204) {
        throw new NonSuccessfulResponseCodeException(responseCode, "Bad response: " + responseCode + " " + responseMessage);
    }
    return response;
}
Also used : RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) DeprecatedVersionException(org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException) ExpectationFailedException(org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) GroupStaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupStaleDevicesException) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) GroupMismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupMismatchedDevicesException) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException)

Example 3 with ServerRejectedException

use of org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException in project Signal-Android by WhisperSystems.

the class DefaultErrorMapper method parseError.

@Override
public Throwable parseError(int status, String body, Function<String, String> getHeader) {
    if (customErrorMappers.containsKey(status)) {
        try {
            return customErrorMappers.get(status).parseError(status, body, getHeader);
        } catch (MalformedResponseException e) {
            return e;
        }
    }
    switch(status) {
        case 401:
        case 403:
            return new AuthorizationFailedException(status, "Authorization failed!");
        case 402:
            return new CaptchaRequiredException();
        case 404:
            return new NotFoundException("Not found");
        case 409:
            try {
                return new MismatchedDevicesException(JsonUtil.fromJsonResponse(body, MismatchedDevices.class));
            } catch (MalformedResponseException e) {
                return e;
            }
        case 410:
            try {
                return new StaleDevicesException(JsonUtil.fromJsonResponse(body, StaleDevices.class));
            } catch (MalformedResponseException e) {
                return e;
            }
        case 411:
            try {
                return new DeviceLimitExceededException(JsonUtil.fromJsonResponse(body, DeviceLimit.class));
            } catch (MalformedResponseException e) {
                return e;
            }
        case 413:
            return new RateLimitException("Rate limit exceeded: " + status);
        case 417:
            return new ExpectationFailedException();
        case 423:
            PushServiceSocket.RegistrationLockFailure accountLockFailure;
            try {
                accountLockFailure = JsonUtil.fromJsonResponse(body, PushServiceSocket.RegistrationLockFailure.class);
            } catch (MalformedResponseException e) {
                return e;
            }
            AuthCredentials credentials = accountLockFailure.backupCredentials;
            String basicStorageCredentials = credentials != null ? credentials.asBasic() : null;
            return new LockedException(accountLockFailure.length, accountLockFailure.timeRemaining, basicStorageCredentials);
        case 428:
            ProofRequiredResponse proofRequiredResponse;
            try {
                proofRequiredResponse = JsonUtil.fromJsonResponse(body, ProofRequiredResponse.class);
            } catch (MalformedResponseException e) {
                return e;
            }
            String retryAfterRaw = getHeader.apply("Retry-After");
            long retryAfter = Util.parseInt(retryAfterRaw, -1);
            return new ProofRequiredException(proofRequiredResponse, retryAfter);
        case 499:
            return new DeprecatedVersionException();
        case 508:
            return new ServerRejectedException();
    }
    if (status != 200 && status != 202 && status != 204) {
        return new NonSuccessfulResponseCodeException(status, "Bad response: " + status);
    }
    return null;
}
Also used : LockedException(org.whispersystems.signalservice.internal.push.LockedException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) DeprecatedVersionException(org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException) ExpectationFailedException(org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException) DeviceLimit(org.whispersystems.signalservice.internal.push.DeviceLimit) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) MalformedResponseException(org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException) MismatchedDevices(org.whispersystems.signalservice.internal.push.MismatchedDevices) StaleDevices(org.whispersystems.signalservice.internal.push.StaleDevices) DeviceLimitExceededException(org.whispersystems.signalservice.internal.push.DeviceLimitExceededException) AuthCredentials(org.whispersystems.signalservice.internal.push.AuthCredentials) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException) PushServiceSocket(org.whispersystems.signalservice.internal.push.PushServiceSocket) CaptchaRequiredException(org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException) ProofRequiredResponse(org.whispersystems.signalservice.internal.push.ProofRequiredResponse)

Example 4 with ServerRejectedException

use of org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException in project Signal-Android by WhisperSystems.

the class SignalServiceMessageSender method sendMessage.

private List<SendMessageResult> sendMessage(List<SignalServiceAddress> recipients, List<Optional<UnidentifiedAccess>> unidentifiedAccess, long timestamp, EnvelopeContent content, boolean online, PartialSendCompleteListener partialListener, CancelationSignal cancelationSignal) throws IOException {
    Log.d(TAG, "[" + timestamp + "] Sending to " + recipients.size() + " recipients.");
    enforceMaxContentSize(content);
    long startTime = System.currentTimeMillis();
    List<Future<SendMessageResult>> futureResults = new LinkedList<>();
    Iterator<SignalServiceAddress> recipientIterator = recipients.iterator();
    Iterator<Optional<UnidentifiedAccess>> unidentifiedAccessIterator = unidentifiedAccess.iterator();
    while (recipientIterator.hasNext()) {
        SignalServiceAddress recipient = recipientIterator.next();
        Optional<UnidentifiedAccess> access = unidentifiedAccessIterator.next();
        futureResults.add(executor.submit(() -> {
            SendMessageResult result = sendMessage(recipient, access, timestamp, content, online, cancelationSignal);
            if (partialListener != null) {
                partialListener.onPartialSendComplete(result);
            }
            return result;
        }));
    }
    List<SendMessageResult> results = new ArrayList<>(futureResults.size());
    recipientIterator = recipients.iterator();
    for (Future<SendMessageResult> futureResult : futureResults) {
        SignalServiceAddress recipient = recipientIterator.next();
        try {
            results.add(futureResult.get());
        } catch (ExecutionException e) {
            if (e.getCause() instanceof UntrustedIdentityException) {
                Log.w(TAG, e);
                results.add(SendMessageResult.identityFailure(recipient, ((UntrustedIdentityException) e.getCause()).getIdentityKey()));
            } else if (e.getCause() instanceof UnregisteredUserException) {
                Log.w(TAG, "[" + timestamp + "] Found unregistered user.");
                results.add(SendMessageResult.unregisteredFailure(recipient));
            } else if (e.getCause() instanceof PushNetworkException) {
                Log.w(TAG, e);
                results.add(SendMessageResult.networkFailure(recipient));
            } else if (e.getCause() instanceof ServerRejectedException) {
                Log.w(TAG, e);
                throw ((ServerRejectedException) e.getCause());
            } else if (e.getCause() instanceof ProofRequiredException) {
                Log.w(TAG, e);
                results.add(SendMessageResult.proofRequiredFailure(recipient, (ProofRequiredException) e.getCause()));
            } else {
                throw new IOException(e);
            }
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
    }
    double sendsForAverage = 0;
    for (SendMessageResult result : results) {
        if (result.getSuccess() != null && result.getSuccess().getDuration() != -1) {
            sendsForAverage++;
        }
    }
    double average = 0;
    if (sendsForAverage > 0) {
        for (SendMessageResult result : results) {
            if (result.getSuccess() != null && result.getSuccess().getDuration() != -1) {
                average += result.getSuccess().getDuration() / sendsForAverage;
            }
        }
    }
    Log.d(TAG, "[" + timestamp + "] Completed send to " + recipients.size() + " recipients in " + (System.currentTimeMillis() - startTime) + " ms, with an average time of " + Math.round(average) + " ms per send.");
    return results;
}
Also used : UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) Optional(org.whispersystems.libsignal.util.guava.Optional) ArrayList(java.util.ArrayList) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) UnidentifiedAccess(org.whispersystems.signalservice.api.crypto.UnidentifiedAccess) Future(java.util.concurrent.Future) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with ServerRejectedException

use of org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException in project Signal-Android by signalapp.

the class PushTextSendJob method deliver.

private boolean deliver(SmsMessageRecord message) throws UntrustedIdentityException, InsecureFallbackApprovalException, UndeliverableMessageException, IOException {
    try {
        rotateSenderCertificateIfNecessary();
        Recipient messageRecipient = message.getIndividualRecipient().resolve();
        if (messageRecipient.isUnregistered()) {
            throw new UndeliverableMessageException(messageRecipient.getId() + " not registered!");
        }
        SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
        SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, messageRecipient);
        Optional<byte[]> profileKey = getProfileKey(messageRecipient);
        Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, messageRecipient);
        log(TAG, String.valueOf(message.getDateSent()), "Have access key to use: " + unidentifiedAccess.isPresent());
        SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder().withTimestamp(message.getDateSent()).withBody(message.getBody()).withExpiration((int) (message.getExpiresIn() / 1000)).withProfileKey(profileKey.orNull()).asEndSessionMessage(message.isEndSession()).build();
        if (Util.equals(SignalStore.account().getAci(), address.getServiceId())) {
            Optional<UnidentifiedAccessPair> syncAccess = UnidentifiedAccessUtil.getAccessForSync(context);
            SignalLocalMetrics.IndividualMessageSend.onDeliveryStarted(messageId);
            SendMessageResult result = messageSender.sendSyncMessage(textSecureMessage);
            SignalDatabase.messageLog().insertIfPossible(messageRecipient.getId(), message.getDateSent(), result, ContentHint.RESENDABLE, new MessageId(messageId, false));
            return syncAccess.isPresent();
        } else {
            SignalLocalMetrics.IndividualMessageSend.onDeliveryStarted(messageId);
            SendMessageResult result = messageSender.sendDataMessage(address, unidentifiedAccess, ContentHint.RESENDABLE, textSecureMessage, new MetricEventListener(messageId));
            SignalDatabase.messageLog().insertIfPossible(messageRecipient.getId(), message.getDateSent(), result, ContentHint.RESENDABLE, new MessageId(messageId, false));
            return result.getSuccess().isUnidentified();
        }
    } catch (UnregisteredUserException e) {
        warn(TAG, "Failure", e);
        throw new InsecureFallbackApprovalException(e);
    } catch (ServerRejectedException e) {
        throw new UndeliverableMessageException(e);
    }
}
Also used : UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Aggregations

ServerRejectedException (org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException)14 SendMessageResult (org.whispersystems.signalservice.api.messages.SendMessageResult)6 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)6 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)6 NotFoundException (org.whispersystems.signalservice.api.push.exceptions.NotFoundException)6 ProofRequiredException (org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException)6 UnregisteredUserException (org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException)6 MessageId (org.thoughtcrime.securesms.database.model.MessageId)5 UndeliverableMessageException (org.thoughtcrime.securesms.transport.UndeliverableMessageException)5 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)5 IOException (java.io.IOException)4 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 GroupsV2AuthorizationString (org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString)4 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)4 Preview (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Preview)4 SharedContact (org.whispersystems.signalservice.api.messages.shared.SharedContact)4 AuthorizationFailedException (org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException)4 DeprecatedVersionException (org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException)4 ExpectationFailedException (org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException)4 PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)4