Search in sources :

Example 1 with DeviceLimitExceededException

use of org.whispersystems.signalservice.internal.push.DeviceLimitExceededException 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 2 with DeviceLimitExceededException

use of org.whispersystems.signalservice.internal.push.DeviceLimitExceededException 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)

Example 3 with DeviceLimitExceededException

use of org.whispersystems.signalservice.internal.push.DeviceLimitExceededException in project Signal-Android by signalapp.

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 DeviceLimitExceededException

use of org.whispersystems.signalservice.internal.push.DeviceLimitExceededException in project Signal-Android by signalapp.

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)4 DeviceLimitExceededException (org.whispersystems.signalservice.internal.push.DeviceLimitExceededException)4 SuppressLint (android.annotation.SuppressLint)2 Context (android.content.Context)2 IOException (java.io.IOException)2 ProfileKey (org.signal.zkgroup.profiles.ProfileKey)2 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)2 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)2 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)2 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)2 AuthorizationFailedException (org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException)2 CaptchaRequiredException (org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException)2 DeprecatedVersionException (org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException)2 ExpectationFailedException (org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException)2 MalformedResponseException (org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException)2 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)2 ProofRequiredException (org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException)2 RateLimitException (org.whispersystems.signalservice.api.push.exceptions.RateLimitException)2 ServerRejectedException (org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException)2 AuthCredentials (org.whispersystems.signalservice.internal.push.AuthCredentials)2