Search in sources :

Example 11 with AuthorizationFailedException

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

the class PushServiceSocket method makeRequest.

private Response makeRequest(ConnectionHolder connectionHolder, String authorization, List<String> cookies, String path, String method, String body) throws PushNetworkException, NonSuccessfulResponseCodeException {
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + path);
    if (body != null) {
        request.method(method, RequestBody.create(MediaType.parse("application/json"), body));
    } else {
        request.method(method, null);
    }
    if (connectionHolder.getHostHeader().isPresent()) {
        request.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    if (authorization != null) {
        request.addHeader("Authorization", authorization);
    }
    if (cookies != null && !cookies.isEmpty()) {
        request.addHeader("Cookie", Util.join(cookies, "; "));
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response;
    try {
        response = call.execute();
        if (response.isSuccessful()) {
            return response;
        }
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
    switch(response.code()) {
        case 401:
        case 403:
            throw new AuthorizationFailedException(response.code(), "Authorization failed!");
        case 409:
            throw new RemoteAttestationResponseExpiredException("Remote attestation response expired");
        case 429:
            throw new RateLimitException("Rate limit exceeded: " + response.code());
    }
    throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
}
Also used : 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) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) 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) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) IOException(java.io.IOException) RemoteAttestationResponseExpiredException(org.whispersystems.signalservice.api.push.exceptions.RemoteAttestationResponseExpiredException)

Example 12 with AuthorizationFailedException

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

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 13 with AuthorizationFailedException

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

the class SignalServiceMessageSender method sendMessage.

private SendMessageResult sendMessage(SignalServiceAddress recipient, Optional<UnidentifiedAccess> unidentifiedAccess, long timestamp, byte[] content, boolean online) throws UntrustedIdentityException, IOException {
    for (int i = 0; i < 4; i++) {
        try {
            OutgoingPushMessageList messages = getEncryptedMessages(socket, recipient, unidentifiedAccess, timestamp, content, online);
            Optional<SignalServiceMessagePipe> pipe = this.pipe.get();
            Optional<SignalServiceMessagePipe> unidentifiedPipe = this.unidentifiedPipe.get();
            if (pipe.isPresent() && !unidentifiedAccess.isPresent()) {
                try {
                    Log.w(TAG, "Transmitting over pipe...");
                    SendMessageResponse response = pipe.get().send(messages, Optional.<UnidentifiedAccess>absent());
                    return SendMessageResult.success(recipient, false, response.getNeedsSync() || isMultiDevice.get());
                } catch (IOException e) {
                    Log.w(TAG, e);
                    Log.w(TAG, "Falling back to new connection...");
                }
            } else if (unidentifiedPipe.isPresent() && unidentifiedAccess.isPresent()) {
                try {
                    Log.w(TAG, "Transmitting over unidentified pipe...");
                    SendMessageResponse response = unidentifiedPipe.get().send(messages, unidentifiedAccess);
                    return SendMessageResult.success(recipient, true, response.getNeedsSync() || isMultiDevice.get());
                } catch (IOException e) {
                    Log.w(TAG, e);
                    Log.w(TAG, "Falling back to new connection...");
                }
            }
            Log.w(TAG, "Not transmitting over pipe...");
            SendMessageResponse response = socket.sendMessage(messages, unidentifiedAccess);
            return SendMessageResult.success(recipient, unidentifiedAccess.isPresent(), response.getNeedsSync() || isMultiDevice.get());
        } catch (InvalidKeyException ike) {
            Log.w(TAG, ike);
            unidentifiedAccess = Optional.absent();
        } catch (AuthorizationFailedException afe) {
            Log.w(TAG, afe);
            if (unidentifiedAccess.isPresent()) {
                unidentifiedAccess = Optional.absent();
            } else {
                throw afe;
            }
        } catch (MismatchedDevicesException mde) {
            Log.w(TAG, mde);
            handleMismatchedDevices(socket, recipient, mde.getMismatchedDevices());
        } catch (StaleDevicesException ste) {
            Log.w(TAG, ste);
            handleStaleDevices(recipient, ste.getStaleDevices());
        }
    }
    throw new IOException("Failed to resolve conflicts after 3 attempts!");
}
Also used : OutgoingPushMessageList(org.whispersystems.signalservice.internal.push.OutgoingPushMessageList) SendMessageResponse(org.whispersystems.signalservice.internal.push.SendMessageResponse) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 14 with AuthorizationFailedException

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

the class PushServiceSocket method makeRequest.

private Response makeRequest(ConnectionHolder connectionHolder, String authorization, List<String> cookies, String path, String method, String body) throws PushNetworkException, NonSuccessfulResponseCodeException {
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + path);
    if (body != null) {
        request.method(method, RequestBody.create(MediaType.parse("application/json"), body));
    } else {
        request.method(method, null);
    }
    if (connectionHolder.getHostHeader().isPresent()) {
        request.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    if (authorization != null) {
        request.addHeader("Authorization", authorization);
    }
    if (cookies != null && !cookies.isEmpty()) {
        request.addHeader("Cookie", Util.join(cookies, "; "));
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response;
    try {
        response = call.execute();
        if (response.isSuccessful()) {
            return response;
        }
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
    switch(response.code()) {
        case 401:
        case 403:
            throw new AuthorizationFailedException(response.code(), "Authorization failed!");
        case 409:
            throw new RemoteAttestationResponseExpiredException("Remote attestation response expired");
        case 429:
            throw new RateLimitException("Rate limit exceeded: " + response.code());
    }
    throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
}
Also used : 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) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) 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) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) IOException(java.io.IOException) RemoteAttestationResponseExpiredException(org.whispersystems.signalservice.api.push.exceptions.RemoteAttestationResponseExpiredException)

Aggregations

AuthorizationFailedException (org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException)14 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)11 RateLimitException (org.whispersystems.signalservice.api.push.exceptions.RateLimitException)11 IOException (java.io.IOException)10 MismatchedDevicesException (org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException)9 StaleDevicesException (org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException)9 NotFoundException (org.whispersystems.signalservice.api.push.exceptions.NotFoundException)8 Response (okhttp3.Response)7 PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)7 DeprecatedVersionException (org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException)6 ExpectationFailedException (org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException)6 DiscoveryResponse (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse)6 Call (okhttp3.Call)5 OkHttpClient (okhttp3.OkHttpClient)5 Request (okhttp3.Request)5 DiscoveryRequest (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest)5 ProofRequiredException (org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException)4 ServerRejectedException (org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException)4 GroupNotFoundException (org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException)4 ProfileKeyCredentialRequest (org.signal.zkgroup.profiles.ProfileKeyCredentialRequest)3