Implement change phone number

Closes #1240
This commit is contained in:
AsamK 2023-10-12 21:15:00 +02:00
parent 56ee173d03
commit 33c4e17c0d
15 changed files with 382 additions and 63 deletions

View file

@ -14,6 +14,7 @@ public class Commands {
addCommand(new BlockCommand());
addCommand(new DaemonCommand());
addCommand(new DeleteLocalAccountDataCommand());
addCommand(new FinishChangeNumberCommand());
addCommand(new FinishLinkCommand());
addCommand(new GetAttachmentCommand());
addCommand(new GetUserStatusCommand());
@ -43,6 +44,7 @@ public class Commands {
addCommand(new SendTypingCommand());
addCommand(new SetPinCommand());
addCommand(new SubmitRateLimitChallengeCommand());
addCommand(new StartChangeNumberCommand());
addCommand(new StartLinkCommand());
addCommand(new TrustCommand());
addCommand(new UnblockCommand());

View file

@ -0,0 +1,58 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.NotPrimaryDeviceException;
import org.asamk.signal.manager.api.PinLockedException;
import org.asamk.signal.output.OutputWriter;
import java.io.IOException;
public class FinishChangeNumberCommand implements JsonRpcLocalCommand {
@Override
public String getName() {
return "finishChangeNumber";
}
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Verify the new number using the code received via SMS or voice.");
subparser.addArgument("number").help("The new phone number in E164 format.").required(true);
subparser.addArgument("-v", "--verification-code")
.help("The verification code you received via sms or voice call.")
.required(true);
subparser.addArgument("-p", "--pin").help("The registration lock PIN, that was set by the user (Optional)");
}
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var newNumber = ns.getString("number");
final var verificationCode = ns.getString("verification-code");
final var pin = ns.getString("pin");
try {
m.finishChangeNumber(newNumber, verificationCode, pin);
} catch (PinLockedException e) {
throw new UserErrorException(
"Verification failed! This number is locked with a pin. Hours remaining until reset: "
+ (e.getTimeRemaining() / 1000 / 60 / 60)
+ "\nUse '--pin PIN_CODE' to specify the registration lock PIN");
} catch (IncorrectPinException e) {
throw new UserErrorException("Verification failed! Invalid pin, tries remaining: " + e.getTriesRemaining());
} catch (NotPrimaryDeviceException e) {
throw new UserErrorException("This command doesn't work on linked devices.");
} catch (IOException e) {
throw new IOErrorException("Failed to change number: %s (%s)".formatted(e.getMessage(),
e.getClass().getSimpleName()), e);
}
}
}

View file

@ -16,7 +16,7 @@ import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
import org.asamk.signal.manager.api.RateLimitException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.util.DateUtils;
import org.asamk.signal.util.CommandUtil;
import java.io.IOException;
import java.util.List;
@ -69,26 +69,10 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
try {
m.register(voiceVerification, captcha);
} catch (RateLimitException e) {
String message = "Rate limit reached";
if (e.getNextAttemptTimestamp() > 0) {
message += "\nNext attempt may be tried at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
}
final var message = CommandUtil.getRateLimitMessage(e);
throw new RateLimitErrorException(message, e);
} catch (CaptchaRequiredException e) {
String message;
if (captcha == null) {
message = """
Captcha required for verification, use --captcha CAPTCHA
To get the token, go to https://signalcaptchas.org/registration/generate.html
Check the developer tools (F12) console for a failed redirect to signalcaptcha://
Everything after signalcaptcha:// is the captcha token.""";
} else {
message = "Invalid captcha given.";
}
if (e.getNextAttemptTimestamp() > 0) {
message += "\nNext Captcha may be provided at "
+ DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
}
final var message = CommandUtil.getCaptchaRequiredMessage(e, captcha != null);
throw new UserErrorException(message);
} catch (NonNormalizedPhoneNumberException e) {
throw new UserErrorException("Failed to register: " + e.getMessage(), e);

View file

@ -0,0 +1,64 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.RateLimitErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
import org.asamk.signal.manager.api.NotPrimaryDeviceException;
import org.asamk.signal.manager.api.RateLimitException;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.util.CommandUtil;
import java.io.IOException;
public class StartChangeNumberCommand implements JsonRpcLocalCommand {
@Override
public String getName() {
return "startChangeNumber";
}
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Change account to a new phone number with SMS or voice verification.");
subparser.addArgument("number").help("The new phone number in E164 format.").required(true);
subparser.addArgument("-v", "--voice")
.help("The verification should be done over voice, not SMS.")
.action(Arguments.storeTrue());
subparser.addArgument("--captcha")
.help("The captcha token, required if change number failed with a captcha required error.");
}
@Override
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var newNumber = ns.getString("number");
final var voiceVerification = Boolean.TRUE.equals(ns.getBoolean("voice"));
final var captcha = ns.getString("captcha");
try {
m.startChangeNumber(newNumber, voiceVerification, captcha);
} catch (RateLimitException e) {
final var message = CommandUtil.getRateLimitMessage(e);
throw new RateLimitErrorException(message, e);
} catch (CaptchaRequiredException e) {
final var message = CommandUtil.getCaptchaRequiredMessage(e, captcha != null);
throw new UserErrorException(message);
} catch (NonNormalizedPhoneNumberException e) {
throw new UserErrorException("Failed to change number: " + e.getMessage(), e);
} catch (NotPrimaryDeviceException e) {
throw new UserErrorException("This command doesn't work on linked devices.");
} catch (IOException e) {
throw new IOErrorException("Failed to change number: %s (%s)".formatted(e.getMessage(),
e.getClass().getSimpleName()), e);
}
}
}

View file

@ -4,6 +4,7 @@ import org.asamk.Signal;
import org.asamk.signal.DbusConfig;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.Configuration;
import org.asamk.signal.manager.api.Contact;
import org.asamk.signal.manager.api.Device;
@ -17,15 +18,19 @@ import org.asamk.signal.manager.api.GroupSendingNotAllowedException;
import org.asamk.signal.manager.api.Identity;
import org.asamk.signal.manager.api.IdentityVerificationCode;
import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.InvalidUsernameException;
import org.asamk.signal.manager.api.LastGroupAdminException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
import org.asamk.signal.manager.api.NotAGroupMemberException;
import org.asamk.signal.manager.api.NotPrimaryDeviceException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.PinLockedException;
import org.asamk.signal.manager.api.RateLimitException;
import org.asamk.signal.manager.api.ReceiveConfig;
import org.asamk.signal.manager.api.Recipient;
import org.asamk.signal.manager.api.RecipientAddress;
@ -166,6 +171,20 @@ public class DbusManagerImpl implements Manager {
throw new UnsupportedOperationException();
}
@Override
public void startChangeNumber(
final String newNumber, final boolean voiceVerification, final String captcha
) throws RateLimitException, IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException {
throw new UnsupportedOperationException();
}
@Override
public void finishChangeNumber(
final String newNumber, final String verificationCode, final String pin
) throws IncorrectPinException, PinLockedException, IOException {
throw new UnsupportedOperationException();
}
@Override
public void unregister() throws IOException {
signal.unregister();

View file

@ -2,9 +2,11 @@ package org.asamk.signal.util;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.GroupId;
import org.asamk.signal.manager.api.GroupIdFormatException;
import org.asamk.signal.manager.api.InvalidNumberException;
import org.asamk.signal.manager.api.RateLimitException;
import org.asamk.signal.manager.api.RecipientIdentifier;
import java.util.Collection;
@ -96,4 +98,29 @@ public class CommandUtil {
throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage(), e);
}
}
public static String getCaptchaRequiredMessage(final CaptchaRequiredException e, final boolean captchaProvided) {
String message;
if (!captchaProvided) {
message = """
Captcha required for verification, use --captcha CAPTCHA
To get the token, go to https://signalcaptchas.org/registration/generate.html
Check the developer tools (F12) console for a failed redirect to signalcaptcha://
Everything after signalcaptcha:// is the captcha token.""";
} else {
message = "Invalid captcha given.";
}
if (e.getNextAttemptTimestamp() > 0) {
message += "\nNext Captcha may be provided at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
}
return message;
}
public static String getRateLimitMessage(final RateLimitException e) {
String message = "Rate limit reached";
if (e.getNextAttemptTimestamp() > 0) {
message += "\nNext attempt may be tried at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
}
return message;
}
}