Skip to content

Commit b14099d

Browse files
committed
Throw AuthorizationFailureException if user_id is not in channel data
Changed storeMyUserId to extractUserIdFromChannelData and moved the side effect of storing the id into toSubscribeMessage.
1 parent 9745b3d commit b14099d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/main/java/com/pusher/client/channel/impl/PresenceChannelImpl.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.pusher.client.channel.impl;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonSyntaxException;
45
import com.google.gson.annotations.SerializedName;
6+
import com.pusher.client.AuthorizationFailureException;
57
import com.pusher.client.Authorizer;
68
import com.pusher.client.channel.ChannelEventListener;
79
import com.pusher.client.channel.PresenceChannel;
@@ -66,7 +68,7 @@ else if (event.equals(MEMBER_REMOVED_EVENT)) {
6668
@Override
6769
public String toSubscribeMessage() {
6870
String msg = super.toSubscribeMessage();
69-
storeMyUserId(channelData);
71+
myUserID = extractUserIdFromChannelData((String)channelData);
7072
return msg;
7173
}
7274

@@ -161,9 +163,24 @@ private static PresenceData extractPresenceDataFrom(final String message) {
161163
}
162164

163165
@SuppressWarnings("rawtypes")
164-
private void storeMyUserId(final Object channelData) {
165-
final Map channelDataMap = GSON.fromJson((String)channelData, Map.class);
166-
myUserID = String.valueOf(channelDataMap.get("user_id"));
166+
private String extractUserIdFromChannelData(final String channelData) {
167+
final Map channelDataMap;
168+
try {
169+
channelDataMap = GSON.fromJson((String)channelData, Map.class);
170+
} catch (final JsonSyntaxException e) {
171+
throw new AuthorizationFailureException("Invalid response from Authorizer: unable to parse channel_data object: " + channelData, e);
172+
}
173+
Object maybeUserId;
174+
try {
175+
maybeUserId = channelDataMap.get("user_id");
176+
} catch (final NullPointerException e) {
177+
throw new AuthorizationFailureException("Invalid response from Authorizer: no user_id key in channel_data object: " + channelData);
178+
}
179+
if (maybeUserId == null) {
180+
throw new AuthorizationFailureException("Invalid response from Authorizer: no user_id key in channel_data object: " + channelData);
181+
}
182+
// user_id can be a string or an integer in the Channels websocket protocol
183+
return String.valueOf(maybeUserId);
167184
}
168185

169186
private class MemberData {

0 commit comments

Comments
 (0)