Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/main/java/io/socket/client/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ private static class Engine extends io.socket.engineio.client.Socket {
}
}

public interface AuthCallback {
void call(Map<String, String> auth);
}

public interface AuthFunction {
void call(AuthCallback callback);
}

public static class Options extends io.socket.engineio.client.Socket.Options {

public boolean reconnection = true;
Expand All @@ -562,6 +570,12 @@ public static class Options extends io.socket.engineio.client.Socket.Options {
public double randomizationFactor;
public Parser.Encoder encoder;
public Parser.Decoder decoder;
public AuthFunction authFunction = new AuthFunction() {
@Override
public void call(AuthCallback callback) {
callback.call(auth);
}
};
public Map<String, String> auth;

/**
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/io/socket/client/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class Socket extends Emitter {
private int ids;
private final String nsp;
private final Manager io;
private final Map<String, String> auth;
private final Manager.AuthFunction authFunction;
private final Map<Integer, Ack> acks = new ConcurrentHashMap<>();
private Queue<On.Handle> subs;
private final Queue<List<Object>> receiveBuffer = new ConcurrentLinkedQueue<>();
Expand All @@ -71,7 +71,7 @@ public class Socket extends Emitter {
public Socket(Manager io, String nsp, Manager.Options opts) {
this.io = io;
this.nsp = nsp;
this.auth = opts != null ? opts.auth : null;
this.authFunction = opts != null ? opts.authFunction : null;
}

private void subEvents() {
Expand Down Expand Up @@ -268,8 +268,10 @@ private void packet(Packet packet) {
private void onopen() {
logger.fine("transport is open - connecting");

if (this.auth != null) {
this.packet(new Packet<>(Parser.CONNECT, new JSONObject(this.auth)));
if (this.authFunction != null) {
this.authFunction.call(auth ->
packet(new Packet<>(Parser.CONNECT, new JSONObject(auth)))
);
} else {
this.packet(new Packet<>(Parser.CONNECT));
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/io/socket/client/SocketOptionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected SocketOptionBuilder(IO.Options options) {
.setSecure(options.secure)
.setPath(options.path)
.setQuery(options.query)
.setAuth(options.auth)
.setAuthFunction(options.authFunction)
.setExtraHeaders(options.extraHeaders);
}
}
Expand Down Expand Up @@ -175,7 +175,12 @@ public SocketOptionBuilder setPath(String path) {
}

public SocketOptionBuilder setAuth(Map<String, String> auth) {
this.options.auth = auth;
this.options.authFunction = callback -> callback.call(auth);
return this;
}

public SocketOptionBuilder setAuthFunction(Manager.AuthFunction authFunction) {
this.options.authFunction = authFunction;
return this;
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/java/io/socket/client/SocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;
Expand Down Expand Up @@ -220,6 +221,30 @@ public void call(Object... args) {
socket.disconnect();
}

@Test(timeout = TIMEOUT)
public void shouldAcceptAnAuthCallbackOption() throws InterruptedException, JSONException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<>();

IO.Options opts = new IO.Options();
opts.authFunction = callback -> callback.call(Map.of("token", "abcd"));
socket = client("/abc", opts);
socket.on("handshake", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject handshake = (JSONObject) args[0];
values.offer(Optional.ofNullable(handshake));
}
});
socket.connect();

@SuppressWarnings("unchecked")
Optional<JSONObject> handshake = values.take();
JSONObject query = handshake.get().getJSONObject("auth");
assertThat(query.getString("token"), is("abcd"));

socket.disconnect();
}

@Test(timeout = TIMEOUT)
public void shouldFireAnErrorEventOnMiddlewareFailure() throws InterruptedException, JSONException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<>();
Expand Down