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

@ -486,6 +486,20 @@
"allDeclaredMethods":true, "allDeclaredMethods":true,
"allDeclaredConstructors":true} "allDeclaredConstructors":true}
, ,
{
"name":"org.asamk.signal.commands.RegisterCommand$RegistrationParams",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.Boolean","java.lang.String"] }]}
,
{
"name":"org.asamk.signal.commands.VerifyCommand$VerifyParams",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String"] }]}
,
{ {
"name":"org.asamk.signal.json.JsonAttachment", "name":"org.asamk.signal.json.JsonAttachment",
"allDeclaredFields":true, "allDeclaredFields":true,
@ -2410,6 +2424,13 @@
{"name":"type_"} {"name":"type_"}
]} ]}
, ,
{
"name":"org.whispersystems.signalservice.internal.push.SignalServiceProtos$SyncMessage$Groups",
"fields":[
{"name":"bitField0_"},
{"name":"blob_"}
]}
,
{ {
"name":"org.whispersystems.signalservice.internal.push.SignalServiceProtos$SyncMessage$Keys", "name":"org.whispersystems.signalservice.internal.push.SignalServiceProtos$SyncMessage$Keys",
"fields":[ "fields":[

View file

@ -100,7 +100,7 @@ public class Main {
} }
SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install(); SLF4JBridgeHandler.install();
// java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.FINEST); java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.FINEST);
} }
private static int getStatusForError(final CommandException e) { private static int getStatusForError(final CommandException e) {

View file

@ -0,0 +1,10 @@
package org.asamk.signal.commands;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.RegistrationManager;
public interface JsonRpcRegistrationCommand<T> extends JsonRpcCommand<T> {
void handleCommand(T request, RegistrationManager m, JsonWriter jsonWriter) throws CommandException;
}

View file

@ -1,9 +1,13 @@
package org.asamk.signal.commands; package org.asamk.signal.commands;
import com.fasterxml.jackson.core.type.TypeReference;
import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
@ -11,8 +15,9 @@ import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.api.CaptchaRequiredException; import org.asamk.signal.manager.api.CaptchaRequiredException;
import java.io.IOException; import java.io.IOException;
import java.util.List;
public class RegisterCommand implements RegistrationCommand { public class RegisterCommand implements RegistrationCommand, JsonRpcRegistrationCommand<RegisterCommand.RegistrationParams> {
@Override @Override
public String getName() { public String getName() {
@ -32,9 +37,31 @@ public class RegisterCommand implements RegistrationCommand {
@Override @Override
public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException { public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException {
final boolean voiceVerification = Boolean.TRUE.equals(ns.getBoolean("voice")); final boolean voiceVerification = Boolean.TRUE.equals(ns.getBoolean("voice"));
final var captchaString = ns.getString("captcha"); final var captcha = ns.getString("captcha");
final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", "");
register(m, voiceVerification, captcha);
}
@Override
public TypeReference<RegistrationParams> getRequestType() {
return new TypeReference<>() {};
}
@Override
public List<OutputType> getSupportedOutputTypes() {
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
}
@Override
public void handleCommand(
final RegistrationParams request, final RegistrationManager m, final JsonWriter jsonWriter
) throws CommandException {
register(m, Boolean.TRUE.equals(request.voice()), request.captcha());
}
private void register(
final RegistrationManager m, final boolean voiceVerification, final String captcha
) throws UserErrorException, IOErrorException {
try { try {
m.register(voiceVerification, captcha); m.register(voiceVerification, captcha);
} catch (CaptchaRequiredException e) { } catch (CaptchaRequiredException e) {
@ -53,4 +80,6 @@ public class RegisterCommand implements RegistrationCommand {
throw new IOErrorException("Request verify error: " + e.getMessage(), e); throw new IOErrorException("Request verify error: " + e.getMessage(), e);
} }
} }
record RegistrationParams(Boolean voice, String captcha) {}
} }

View file

@ -1,18 +1,27 @@
package org.asamk.signal.commands; package org.asamk.signal.commands;
import com.fasterxml.jackson.core.type.TypeReference;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.RegistrationManager; import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.api.IncorrectPinException; import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.PinLockedException; import org.asamk.signal.manager.api.PinLockedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.List;
public class VerifyCommand implements RegistrationCommand { public class VerifyCommand implements RegistrationCommand, JsonRpcRegistrationCommand<VerifyCommand.VerifyParams> {
private final static Logger logger = LoggerFactory.getLogger(VerifyCommand.class);
@Override @Override
public String getName() { public String getName() {
@ -22,15 +31,38 @@ public class VerifyCommand implements RegistrationCommand {
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.help("Verify the number using the code received via SMS or voice."); subparser.help("Verify the number using the code received via SMS or voice.");
subparser.addArgument("verificationCode").help("The verification code you received via sms or voice call."); subparser.addArgument("verification-code").help("The verification code you received via sms or voice call.");
subparser.addArgument("-p", "--pin").help("The registration lock PIN, that was set by the user (Optional)"); subparser.addArgument("-p", "--pin").help("The registration lock PIN, that was set by the user (Optional)");
} }
@Override @Override
public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException { public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException {
var verificationCode = ns.getString("verificationCode"); var verificationCode = ns.getString("verification-code");
var pin = ns.getString("pin"); var pin = ns.getString("pin");
verify(m, verificationCode, pin);
}
@Override
public TypeReference<VerifyParams> getRequestType() {
return new TypeReference<>() {};
}
@Override
public List<OutputType> getSupportedOutputTypes() {
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
}
@Override
public void handleCommand(
final VerifyParams request, final RegistrationManager m, final JsonWriter jsonWriter
) throws CommandException {
verify(m, request.verificationCode(), request.pin());
}
private void verify(
final RegistrationManager m, final String verificationCode, final String pin
) throws UserErrorException, IOErrorException {
try { try {
m.verifyAccount(verificationCode, pin); m.verifyAccount(verificationCode, pin);
} catch (PinLockedException e) { } catch (PinLockedException e) {
@ -44,4 +76,6 @@ public class VerifyCommand implements RegistrationCommand {
throw new IOErrorException("Verify error: " + e.getMessage(), e); throw new IOErrorException("Verify error: " + e.getMessage(), e);
} }
} }
record VerifyParams(String verificationCode, String pin) {}
} }

View file

@ -13,6 +13,7 @@ import org.asamk.signal.JsonWriter;
import org.asamk.signal.commands.Command; import org.asamk.signal.commands.Command;
import org.asamk.signal.commands.Commands; import org.asamk.signal.commands.Commands;
import org.asamk.signal.commands.JsonRpcMultiCommand; import org.asamk.signal.commands.JsonRpcMultiCommand;
import org.asamk.signal.commands.JsonRpcRegistrationCommand;
import org.asamk.signal.commands.JsonRpcSingleCommand; import org.asamk.signal.commands.JsonRpcSingleCommand;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; 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.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager; import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -120,11 +122,26 @@ public class SignalJsonRpcDispatcherHandler {
final ObjectMapper objectMapper, final String method, ContainerNode<?> params final ObjectMapper objectMapper, final String method, ContainerNode<?> params
) throws JsonRpcException { ) throws JsonRpcException {
var command = getCommand(method); var command = getCommand(method);
// TODO implement register, verify, link // TODO implement link
if (c != null) { if (c != null) {
if (command instanceof JsonRpcMultiCommand<?> jsonRpcCommand) { if (command instanceof JsonRpcMultiCommand<?> jsonRpcCommand) {
return runCommand(objectMapper, params, new MultiCommandRunnerImpl<>(c, 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 (command instanceof JsonRpcSingleCommand<?> jsonRpcCommand) {
if (m != null) { if (m != null) {
@ -147,7 +164,7 @@ public class SignalJsonRpcDispatcherHandler {
} }
private Manager getManagerFromParams(final ContainerNode<?> params) { 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()); final var manager = c.getManager(params.get("account").asText());
((ObjectNode) params).remove("account"); ((ObjectNode) params).remove("account");
return manager; return manager;
@ -155,6 +172,20 @@ public class SignalJsonRpcDispatcherHandler {
return null; 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) { private Command getCommand(final String method) {
if ("subscribeReceive".equals(method)) { if ("subscribeReceive".equals(method)) {
return new SubscribeReceiveCommand(); 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>( private record MultiCommandRunnerImpl<T>(
MultiAccountManager c, JsonRpcMultiCommand<T> command MultiAccountManager c, JsonRpcMultiCommand<T> command
) implements CommandRunner<T> { ) implements CommandRunner<T> {