Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
*/
public interface PrivateEncryptedChannelEventListener extends PrivateChannelEventListener {

// TODO: add onDecryptionFailure
void onDecryptionFailure(String event, String reason);
}
52 changes: 31 additions & 21 deletions src/main/java/com/pusher/client/channel/impl/ChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,29 @@ public boolean isSubscribed() {

/* InternalChannel implementation */

@Override
public PusherEvent prepareEvent(String event, String message) {
return GSON.fromJson(message, PusherEvent.class);
}

@Override
public void onMessage(final String event, final String message) {

if (event.equals(SUBSCRIPTION_SUCCESS_EVENT)) {
updateState(ChannelState.SUBSCRIBED);
}
else {
final Set<SubscriptionEventListener> listeners;
synchronized (lock) {
final Set<SubscriptionEventListener> sharedListeners = eventNameToListenerMap.get(event);
if (sharedListeners != null) {
listeners = new HashSet<SubscriptionEventListener>(sharedListeners);
}
else {
listeners = null;
}
}

} else {
final Set<SubscriptionEventListener> listeners = getInterestedListeners(event);
if (listeners != null) {
for (final SubscriptionEventListener listener : listeners) {
final PusherEvent e = GSON.fromJson(message, PusherEvent.class);
factory.queueOnEventThread(new Runnable() {
@Override
public void run() {
listener.onEvent(e);
}
});
final PusherEvent pusherEvent = prepareEvent(event, message);
if (pusherEvent != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances might this be null? If it's possible then it seems like an error we should handle, if it isn't then this check can be omitted...

Copy link
Contributor Author

@daniellevass daniellevass Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The circumstance is that we failed to decrypt an encrypted message - we return null for the prepareEvent method and this then means it won't notify the listeners for onEvent instead we'll be notifying them through the PrivateChannelImpl that of an onDecryptionFailure.

for (final SubscriptionEventListener listener : listeners) {
factory.queueOnEventThread(new Runnable() {
@Override
public void run() {
listener.onEvent(pusherEvent);
}
});
}
}
}
}
Expand Down Expand Up @@ -213,4 +209,18 @@ private void validateArguments(final String eventName, final SubscriptionEventLi
"Cannot bind or unbind to events on a channel that has been unsubscribed. Call Pusher.subscribe() to resubscribe to this channel");
}
}

protected Set<SubscriptionEventListener> getInterestedListeners(String event) {
synchronized (lock) {

final Set<SubscriptionEventListener> sharedListeners =
eventNameToListenerMap.get(event);

if (sharedListeners == null) {
return null;
}

return new HashSet<>(sharedListeners);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import com.pusher.client.channel.Channel;
import com.pusher.client.channel.ChannelEventListener;
import com.pusher.client.channel.ChannelState;
import com.pusher.client.channel.PusherEvent;

public interface InternalChannel extends Channel, Comparable<InternalChannel> {

String toSubscribeMessage();

String toUnsubscribeMessage();

PusherEvent prepareEvent(String event, String message);

void onMessage(String event, String message);

void updateState(ChannelState state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
import com.pusher.client.channel.ChannelState;
import com.pusher.client.channel.PrivateEncryptedChannel;
import com.pusher.client.channel.PrivateEncryptedChannelEventListener;
import com.pusher.client.channel.PusherEvent;
import com.pusher.client.channel.SubscriptionEventListener;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionState;
import com.pusher.client.connection.ConnectionStateChange;
import com.pusher.client.connection.impl.InternalConnection;
import com.pusher.client.crypto.nacl.AuthenticityException;
import com.pusher.client.crypto.nacl.SecretBoxOpener;
import com.pusher.client.crypto.nacl.SecretBoxOpenerFactory;
import com.pusher.client.util.Factory;
import com.pusher.client.util.internal.Base64;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class PrivateEncryptedChannelImpl extends ChannelImpl implements PrivateEncryptedChannel {

Expand Down Expand Up @@ -124,6 +127,69 @@ public void updateState(ChannelState state) {
}
}

@Override
public PusherEvent prepareEvent(String event, String message) {

try {
return decryptMessage(message);
} catch (AuthenticityException e1) {

// retry once only.
disposeSecretBoxOpener();
authenticate();

try {
return decryptMessage(message);
} catch (AuthenticityException e2) {
// deliberately not destroying the secretBoxOpener so the next message
// has an opportunity to fetch a new key and decrypt
notifyListenersOfDecryptFailure(event, "Failed to decrypt message.");
}
}

return null;
}

private void notifyListenersOfDecryptFailure(final String event, final String reason) {
Set<SubscriptionEventListener> listeners = getInterestedListeners(event);
if (listeners != null) {
for (SubscriptionEventListener listener : listeners) {
((PrivateEncryptedChannelEventListener)listener).onDecryptionFailure(
event, reason);
}
}
}

private class EncryptedReceivedData {
String nonce;
String ciphertext;

public byte[] getNonce() {
return Base64.decode(nonce);
}

public byte[] getCiphertext() {
return Base64.decode(ciphertext);
}
}

private PusherEvent decryptMessage(String message) {

Map<String, Object> receivedMessage =
GSON.<Map<String, Object>>fromJson(message, Map.class);

final EncryptedReceivedData encryptedReceivedData =
GSON.fromJson((String)receivedMessage.get("data"), EncryptedReceivedData.class);

String decryptedData = new String(secretBoxOpener.open(
encryptedReceivedData.getCiphertext(),
encryptedReceivedData.getNonce()));

receivedMessage.replace("data", decryptedData);

return new PusherEvent(receivedMessage);
}

private void disposeSecretBoxOpener() {
if (secretBoxOpener != null) {
secretBoxOpener.clearKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@ public void onError(String message, String code, Exception e) {
code,
e));
}

@Override
public void onDecryptionFailure(String event, String reason) {
System.out.println(String.format(
"An error was received decrypting message for event:[%s] - reason: [%s]", event, reason));
}
}
Loading