Add more generic SMS verification code pattern.
This commit is contained in:
parent
e38d41d67a
commit
ed878ec4b4
3 changed files with 45 additions and 33 deletions
|
@ -26,7 +26,7 @@ import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class VerificationCodeParser {
|
public class VerificationCodeParser {
|
||||||
|
|
||||||
private static final Pattern CHALLENGE_PATTERN = Pattern.compile(".*Your (Signal|TextSecure) verification code:? ([0-9]{3,4})-([0-9]{3,4}).*", Pattern.DOTALL);
|
private static final Pattern CHALLENGE_PATTERN = Pattern.compile("(.*\\D|^)([0-9]{3,4})-([0-9]{3,4}).*", Pattern.DOTALL);
|
||||||
|
|
||||||
public static Optional<String> parse(Context context, String messageBody) {
|
public static Optional<String> parse(Context context, String messageBody) {
|
||||||
if (messageBody == null) {
|
if (messageBody == null) {
|
||||||
|
@ -39,6 +39,7 @@ public class VerificationCodeParser {
|
||||||
return Optional.absent();
|
return Optional.absent();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.of(challengeMatcher.group(2) + challengeMatcher.group(3));
|
return Optional.of(challengeMatcher.group(challengeMatcher.groupCount() - 1) +
|
||||||
|
challengeMatcher.group(challengeMatcher.groupCount()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,8 @@ import org.junit.Test;
|
||||||
import org.thoughtcrime.securesms.BaseUnitTest;
|
import org.thoughtcrime.securesms.BaseUnitTest;
|
||||||
import org.whispersystems.libsignal.util.guava.Optional;
|
import org.whispersystems.libsignal.util.guava.Optional;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Collection;
|
||||||
import java.util.Map.Entry;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -16,31 +15,44 @@ import static org.mockito.Matchers.contains;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class VerificationCodeParserTest extends BaseUnitTest {
|
public class VerificationCodeParserTest extends BaseUnitTest {
|
||||||
private static Map<String, String> CHALLENGES = new HashMap<String,String>() {{
|
|
||||||
put("Your TextSecure verification code: 337-337", "337337");
|
|
||||||
put("XXX\nYour TextSecure verification code: 1337-1337", "13371337");
|
|
||||||
put("Your TextSecure verification code: 337-1337", "3371337");
|
|
||||||
put("Your TextSecure verification code: 1337-337", "1337337");
|
|
||||||
put("Your TextSecure verification code: 1337-1337", "13371337");
|
|
||||||
put("XXXYour TextSecure verification code: 1337-1337", "13371337");
|
|
||||||
put("Your TextSecure verification code: 1337-1337XXX", "13371337");
|
|
||||||
put("Your TextSecure verification code 1337-1337", "13371337");
|
|
||||||
|
|
||||||
put("Your Signal verification code: 337-337", "337337");
|
public static Collection<String[]> challenges() {
|
||||||
put("XXX\nYour Signal verification code: 1337-1337", "13371337");
|
return Arrays.asList(new String[][]{
|
||||||
put("Your Signal verification code: 337-1337", "3371337");
|
{"Your TextSecure verification code: 337-337", "337337"},
|
||||||
put("Your Signal verification code: 1337-337", "1337337");
|
{"XXX\nYour TextSecure verification code: 1337-1337", "13371337"},
|
||||||
put("Your Signal verification code: 1337-1337", "13371337");
|
{"Your TextSecure verification code: 337-1337", "3371337"},
|
||||||
put("XXXYour Signal verification code: 1337-1337", "13371337");
|
{"Your TextSecure verification code: 1337-337", "1337337"},
|
||||||
put("Your Signal verification code: 1337-1337XXX", "13371337");
|
{"Your TextSecure verification code: 1337-1337", "13371337"},
|
||||||
put("Your Signal verification code 1337-1337", "13371337");
|
{"XXXYour TextSecure verification code: 1337-1337", "13371337"},
|
||||||
|
{"Your TextSecure verification code: 1337-1337XXX", "13371337"},
|
||||||
|
{"Your TextSecure verification code 1337-1337", "13371337"},
|
||||||
|
|
||||||
put("<#>Your Signal verification code: 1337-1337 aAbBcCdDeEf", "13371337");
|
{"Your Signal verification code: 337-337", "337337"},
|
||||||
put("<#> Your Signal verification code: 1337-1337 aAbBcCdDeEf", "13371337");
|
{"XXX\nYour Signal verification code: 1337-1337", "13371337"},
|
||||||
put("<#>Your Signal verification code: 1337-1337\naAbBcCdDeEf", "13371337");
|
{"Your Signal verification code: 337-1337", "3371337"},
|
||||||
put("<#> Your Signal verification code: 1337-1337\naAbBcCdDeEf", "13371337");
|
{"Your Signal verification code: 1337-337", "1337337"},
|
||||||
put("<#> Your Signal verification code: 1337-1337\n\naAbBcCdDeEf", "13371337");
|
{"Your Signal verification code: 1337-1337", "13371337"},
|
||||||
}};
|
{"XXXYour Signal verification code: 1337-1337", "13371337"},
|
||||||
|
{"Your Signal verification code: 1337-1337XXX", "13371337"},
|
||||||
|
{"Your Signal verification code 1337-1337", "13371337"},
|
||||||
|
|
||||||
|
{"<#>Your Signal verification code: 1337-1337 aAbBcCdDeEf", "13371337"},
|
||||||
|
{"<#> Your Signal verification code: 1337-1337 aAbBcCdDeEf", "13371337"},
|
||||||
|
{"<#>Your Signal verification code: 1337-1337\naAbBcCdDeEf", "13371337"},
|
||||||
|
{"<#> Your Signal verification code: 1337-1337\naAbBcCdDeEf", "13371337"},
|
||||||
|
{"<#> Your Signal verification code: 1337-1337\n\naAbBcCdDeEf", "13371337"},
|
||||||
|
|
||||||
|
{" 1234-5678", "12345678"},
|
||||||
|
{"1234-5678", "12345678"},
|
||||||
|
{">1234-5678 is your verification code.", "12345678"},
|
||||||
|
{"1234-5678 is your verification code.", "12345678"},
|
||||||
|
{"$1234-5678", "12345678"},
|
||||||
|
{"hi 1234-5678\n\nsgnl://verify/1234-5678\n\naAbBcCdDeEf", "12345678"},
|
||||||
|
{"howdy 1234-5678\n\nsgnl://verify/1234-5678\n\naAbBcCdDeEf", "12345678"},
|
||||||
|
{"test 1234-5678\n\nsgnl://verify/1234-5678", "12345678"},
|
||||||
|
{"%#($#&@**$@(@*1234-5678\naAbBcCdDeEf", "12345678"}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@Override
|
@Override
|
||||||
|
@ -51,11 +63,10 @@ public class VerificationCodeParserTest extends BaseUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testChallenges() {
|
public void testChallenges() {
|
||||||
for (Entry<String,String> challenge : CHALLENGES.entrySet()) {
|
for (String[] challenge : challenges()) {
|
||||||
Optional<String> result = VerificationCodeParser.parse(context, challenge.getKey());
|
Optional<String> result = VerificationCodeParser.parse(context, challenge[0]);
|
||||||
|
|
||||||
assertTrue(result.isPresent());
|
assertTrue(result.isPresent());
|
||||||
assertEquals(result.get(), challenge.getValue());
|
assertEquals(challenge[1], result.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -235,7 +235,7 @@ public class PushServiceSocket {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestSmsVerificationCode(boolean androidSmsRetriever, Optional<String> captchaToken, Optional<String> challenge) throws IOException {
|
public void requestSmsVerificationCode(boolean androidSmsRetriever, Optional<String> captchaToken, Optional<String> challenge) throws IOException {
|
||||||
String path = String.format(CREATE_ACCOUNT_SMS_PATH, credentialsProvider.getE164(), androidSmsRetriever ? "android-ng" : "android");
|
String path = String.format(CREATE_ACCOUNT_SMS_PATH, credentialsProvider.getE164(), androidSmsRetriever ? "android-2020-01" : "android");
|
||||||
|
|
||||||
if (captchaToken.isPresent()) {
|
if (captchaToken.isPresent()) {
|
||||||
path += "&captcha=" + captchaToken.get();
|
path += "&captcha=" + captchaToken.get();
|
||||||
|
|
Loading…
Add table
Reference in a new issue