Implement register and verify commands for json rpc

This commit is contained in:
AsamK 2021-11-11 15:49:23 +01:00
parent 4a1af0786c
commit 79cc225869
6 changed files with 149 additions and 9 deletions

View file

@ -13,6 +13,7 @@ import org.asamk.signal.JsonWriter;
import org.asamk.signal.commands.Command;
import org.asamk.signal.commands.Commands;
import org.asamk.signal.commands.JsonRpcMultiCommand;
import org.asamk.signal.commands.JsonRpcRegistrationCommand;
import org.asamk.signal.commands.JsonRpcSingleCommand;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
@ -20,6 +21,7 @@ import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -120,11 +122,26 @@ public class SignalJsonRpcDispatcherHandler {
final ObjectMapper objectMapper, final String method, ContainerNode<?> params
) throws JsonRpcException {
var command = getCommand(method);
// TODO implement register, verify, link
// TODO implement link
if (c != null) {
if (command instanceof JsonRpcMultiCommand<?> jsonRpcCommand) {
return runCommand(objectMapper, params, new MultiCommandRunnerImpl<>(c, jsonRpcCommand));
}
if (command instanceof JsonRpcRegistrationCommand<?> jsonRpcCommand) {
try (var manager = getRegistrationManagerFromParams(params)) {
if (manager != null) {
return runCommand(objectMapper,
params,
new RegistrationCommandRunnerImpl<>(manager, c, jsonRpcCommand));
} else {
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_PARAMS,
"Method requires valid account parameter",
null));
}
} catch (IOException e) {
logger.warn("Failed to close registration manager", e);
}
}
}
if (command instanceof JsonRpcSingleCommand<?> jsonRpcCommand) {
if (m != null) {
@ -147,7 +164,7 @@ public class SignalJsonRpcDispatcherHandler {
}
private Manager getManagerFromParams(final ContainerNode<?> params) {
if (params.has("account")) {
if (params != null && params.has("account")) {
final var manager = c.getManager(params.get("account").asText());
((ObjectNode) params).remove("account");
return manager;
@ -155,6 +172,20 @@ public class SignalJsonRpcDispatcherHandler {
return null;
}
private RegistrationManager getRegistrationManagerFromParams(final ContainerNode<?> params) {
if (params != null && params.has("account")) {
try {
final var registrationManager = c.getNewRegistrationManager(params.get("account").asText());
((ObjectNode) params).remove("account");
return registrationManager;
} catch (IOException | IllegalStateException e) {
logger.warn("Failed to load registration manager", e);
return null;
}
}
return null;
}
private Command getCommand(final String method) {
if ("subscribeReceive".equals(method)) {
return new SubscribeReceiveCommand();
@ -178,6 +209,21 @@ public class SignalJsonRpcDispatcherHandler {
}
}
private record RegistrationCommandRunnerImpl<T>(
RegistrationManager m, MultiAccountManager c, JsonRpcRegistrationCommand<T> command
) implements CommandRunner<T> {
@Override
public void handleCommand(final T request, final JsonWriter jsonWriter) throws CommandException {
command.handleCommand(request, m, jsonWriter);
}
@Override
public TypeReference<T> getRequestType() {
return command.getRequestType();
}
}
private record MultiCommandRunnerImpl<T>(
MultiAccountManager c, JsonRpcMultiCommand<T> command
) implements CommandRunner<T> {