Search in sources :

Example 1 with NotFoundException

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

the class DeliveryReceiptJobTest method testNetworkError.

@Test
public void testNetworkError() throws IOException {
    SignalServiceMessageSender textSecureMessageSender = mock(SignalServiceMessageSender.class);
    long timestamp = System.currentTimeMillis();
    Mockito.doThrow(new PushNetworkException("network error")).when(textSecureMessageSender).sendDeliveryReceipt(any(SignalServiceAddress.class), eq(timestamp));
    DeliveryReceiptJob deliveryReceiptJob = new DeliveryReceiptJob(context, "+14152222222", timestamp, "foo");
    ObjectGraph objectGraph = ObjectGraph.create(new TestModule(textSecureMessageSender));
    objectGraph.inject(deliveryReceiptJob);
    try {
        deliveryReceiptJob.onRun();
        throw new AssertionError();
    } catch (IOException e) {
        assertTrue(deliveryReceiptJob.onShouldRetry(e));
    }
    Mockito.doThrow(new NotFoundException("not found")).when(textSecureMessageSender).sendDeliveryReceipt(any(SignalServiceAddress.class), eq(timestamp));
    try {
        deliveryReceiptJob.onRun();
        throw new AssertionError();
    } catch (IOException e) {
        assertFalse(deliveryReceiptJob.onShouldRetry(e));
    }
}
Also used : PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) ObjectGraph(dagger.ObjectGraph) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IOException(java.io.IOException) Test(org.junit.Test) BaseUnitTest(org.thoughtcrime.securesms.BaseUnitTest)

Example 2 with NotFoundException

use of org.whispersystems.signalservice.api.push.exceptions.NotFoundException in project libsignal-service-java by signalapp.

the class PushServiceSocket method makeServiceRequest.

private String makeServiceRequest(String urlFragment, String method, String body) throws NonSuccessfulResponseCodeException, PushNetworkException {
    Response response = getServiceConnection(urlFragment, method, body);
    int responseCode;
    String responseMessage;
    String responseBody;
    try {
        responseCode = response.code();
        responseMessage = response.message();
        responseBody = response.body().string();
    } catch (IOException ioe) {
        throw new PushNetworkException(ioe);
    }
    switch(responseCode) {
        case 413:
            throw new RateLimitException("Rate limit exceeded: " + responseCode);
        case 401:
        case 403:
            throw new AuthorizationFailedException("Authorization failed!");
        case 404:
            throw new NotFoundException("Not found");
        case 409:
            MismatchedDevices mismatchedDevices;
            try {
                mismatchedDevices = JsonUtil.fromJson(responseBody, MismatchedDevices.class);
            } catch (JsonProcessingException e) {
                Log.w(TAG, e);
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new MismatchedDevicesException(mismatchedDevices);
        case 410:
            StaleDevices staleDevices;
            try {
                staleDevices = JsonUtil.fromJson(responseBody, StaleDevices.class);
            } catch (JsonProcessingException e) {
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new StaleDevicesException(staleDevices);
        case 411:
            DeviceLimit deviceLimit;
            try {
                deviceLimit = JsonUtil.fromJson(responseBody, DeviceLimit.class);
            } catch (JsonProcessingException e) {
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new DeviceLimitExceededException(deviceLimit);
        case 417:
            throw new ExpectationFailedException();
        case 423:
            RegistrationLockFailure accountLockFailure;
            try {
                accountLockFailure = JsonUtil.fromJson(responseBody, RegistrationLockFailure.class);
            } catch (JsonProcessingException e) {
                Log.w(TAG, e);
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new LockedException(accountLockFailure.length, accountLockFailure.timeRemaining);
    }
    if (responseCode != 200 && responseCode != 204) {
        throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
    }
    return responseBody;
}
Also used : PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) ExpectationFailedException(org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) IOException(java.io.IOException) Response(okhttp3.Response) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with NotFoundException

use of org.whispersystems.signalservice.api.push.exceptions.NotFoundException in project libsignal-service-java by signalapp.

the class PushServiceSocket method getPreKeys.

public List<PreKeyBundle> getPreKeys(SignalServiceAddress destination, int deviceIdInteger) throws IOException {
    try {
        String deviceId = String.valueOf(deviceIdInteger);
        if (deviceId.equals("1"))
            deviceId = "*";
        String path = String.format(PREKEY_DEVICE_PATH, destination.getNumber(), deviceId);
        if (destination.getRelay().isPresent()) {
            path = path + "?relay=" + destination.getRelay().get();
        }
        String responseText = makeServiceRequest(path, "GET", null);
        PreKeyResponse response = JsonUtil.fromJson(responseText, PreKeyResponse.class);
        List<PreKeyBundle> bundles = new LinkedList<>();
        for (PreKeyResponseItem device : response.getDevices()) {
            ECPublicKey preKey = null;
            ECPublicKey signedPreKey = null;
            byte[] signedPreKeySignature = null;
            int preKeyId = -1;
            int signedPreKeyId = -1;
            if (device.getSignedPreKey() != null) {
                signedPreKey = device.getSignedPreKey().getPublicKey();
                signedPreKeyId = device.getSignedPreKey().getKeyId();
                signedPreKeySignature = device.getSignedPreKey().getSignature();
            }
            if (device.getPreKey() != null) {
                preKeyId = device.getPreKey().getKeyId();
                preKey = device.getPreKey().getPublicKey();
            }
            bundles.add(new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey()));
        }
        return bundles;
    } catch (NotFoundException nfe) {
        throw new UnregisteredUserException(destination.getNumber(), nfe);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) LinkedList(java.util.LinkedList)

Example 4 with NotFoundException

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

the class RecipientUtil method toSignalServiceAddress.

/**
 * This method will do it's best to craft a fully-populated {@link SignalServiceAddress} based on
 * the provided recipient. This includes performing a possible network request if no UUID is
 * available. If the request to get a UUID fails or the user is not registered, an IOException is thrown.
 */
@WorkerThread
@NonNull
public static SignalServiceAddress toSignalServiceAddress(@NonNull Context context, @NonNull Recipient recipient) throws IOException {
    recipient = recipient.resolve();
    if (!recipient.getServiceId().isPresent() && !recipient.getE164().isPresent()) {
        throw new AssertionError(recipient.getId() + " - No UUID or phone number!");
    }
    if (!recipient.getServiceId().isPresent()) {
        Log.i(TAG, recipient.getId() + " is missing a UUID...");
        RegisteredState state = DirectoryHelper.refreshDirectoryFor(context, recipient, false);
        recipient = Recipient.resolved(recipient.getId());
        Log.i(TAG, "Successfully performed a UUID fetch for " + recipient.getId() + ". Registered: " + state);
    }
    if (recipient.hasServiceId()) {
        return new SignalServiceAddress(recipient.requireServiceId(), Optional.fromNullable(recipient.resolve().getE164().orNull()));
    } else {
        throw new NotFoundException(recipient.getId() + " is not registered!");
    }
}
Also used : RegisteredState(org.thoughtcrime.securesms.database.RecipientDatabase.RegisteredState) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 5 with NotFoundException

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

the class DeviceActivity method onLink.

@SuppressLint("StaticFieldLeak")
@Override
public void onLink(final Uri uri) {
    new ProgressDialogAsyncTask<Void, Void, Integer>(this, R.string.DeviceProvisioningActivity_content_progress_title, R.string.DeviceProvisioningActivity_content_progress_content) {

        private static final int SUCCESS = 0;

        private static final int NO_DEVICE = 1;

        private static final int NETWORK_ERROR = 2;

        private static final int KEY_ERROR = 3;

        private static final int LIMIT_EXCEEDED = 4;

        private static final int BAD_CODE = 5;

        @Override
        protected Integer doInBackground(Void... params) {
            boolean isMultiDevice = TextSecurePreferences.isMultiDevice(DeviceActivity.this);
            try {
                Context context = DeviceActivity.this;
                SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
                String verificationCode = accountManager.getNewDeviceVerificationCode();
                String ephemeralId = uri.getQueryParameter("uuid");
                String publicKeyEncoded = uri.getQueryParameter("pub_key");
                if (TextUtils.isEmpty(ephemeralId) || TextUtils.isEmpty(publicKeyEncoded)) {
                    Log.w(TAG, "UUID or Key is empty!");
                    return BAD_CODE;
                }
                ECPublicKey publicKey = Curve.decodePoint(Base64.decode(publicKeyEncoded), 0);
                IdentityKeyPair aciIdentityKeyPair = SignalStore.account().getAciIdentityKey();
                IdentityKeyPair pniIdentityKeyPair = SignalStore.account().getPniIdentityKey();
                ProfileKey profileKey = ProfileKeyUtil.getSelfProfileKey();
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, true);
                accountManager.addDevice(ephemeralId, publicKey, aciIdentityKeyPair, pniIdentityKeyPair, profileKey, verificationCode);
                return SUCCESS;
            } catch (NotFoundException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return NO_DEVICE;
            } catch (DeviceLimitExceededException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return LIMIT_EXCEEDED;
            } catch (IOException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return NETWORK_ERROR;
            } catch (InvalidKeyException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return KEY_ERROR;
            }
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            Context context = DeviceActivity.this;
            switch(result) {
                case SUCCESS:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_success, Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                case NO_DEVICE:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_no_device, Toast.LENGTH_LONG).show();
                    break;
                case NETWORK_ERROR:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_network_error, Toast.LENGTH_LONG).show();
                    break;
                case KEY_ERROR:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_key_error, Toast.LENGTH_LONG).show();
                    break;
                case LIMIT_EXCEEDED:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_sorry_you_have_too_many_devices_linked_already, Toast.LENGTH_LONG).show();
                    break;
                case BAD_CODE:
                    Toast.makeText(context, R.string.DeviceActivity_sorry_this_is_not_a_valid_device_link_qr_code, Toast.LENGTH_LONG).show();
                    break;
            }
            getSupportFragmentManager().popBackStackImmediate();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : Context(android.content.Context) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SuppressLint(android.annotation.SuppressLint) ProfileKey(org.signal.zkgroup.profiles.ProfileKey) DeviceLimitExceededException(org.whispersystems.signalservice.internal.push.DeviceLimitExceededException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SuppressLint(android.annotation.SuppressLint)

Aggregations

NotFoundException (org.whispersystems.signalservice.api.push.exceptions.NotFoundException)21 IOException (java.io.IOException)10 UnregisteredUserException (org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException)8 GroupNotFoundException (org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException)7 LinkedList (java.util.LinkedList)6 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)6 GroupsV2AuthorizationString (org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString)6 ByteString (com.google.protobuf.ByteString)5 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)5 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)5 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)5 GroupMismatchedDevicesException (org.whispersystems.signalservice.internal.push.exceptions.GroupMismatchedDevicesException)5 GroupStaleDevicesException (org.whispersystems.signalservice.internal.push.exceptions.GroupStaleDevicesException)5 InvalidUnidentifiedAccessHeaderException (org.whispersystems.signalservice.internal.push.exceptions.InvalidUnidentifiedAccessHeaderException)5 Optional (org.whispersystems.libsignal.util.guava.Optional)4 AuthorizationFailedException (org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException)4 PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)4 ServerRejectedException (org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException)4 MismatchedDevicesException (org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException)4 StaleDevicesException (org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException)4