Enforce phone number normalization when creating accounts or changing numbers

This commit is contained in:
Jon Chambers
2021-10-20 16:13:39 -04:00
committed by Jon Chambers
parent 7762afc497
commit 534c577f59
10 changed files with 235 additions and 91 deletions

View File

@@ -5,13 +5,13 @@
package org.whispersystems.textsecuregcm.util;
public class ImpossibleNumberException extends Exception {
public class ImpossiblePhoneNumberException extends Exception {
public ImpossibleNumberException() {
public ImpossiblePhoneNumberException() {
super();
}
public ImpossibleNumberException(final Throwable cause) {
public ImpossiblePhoneNumberException(final Throwable cause) {
super(cause);
}
}

View File

@@ -5,12 +5,12 @@
package org.whispersystems.textsecuregcm.util;
public class NonNormalizedNumberException extends Exception {
public class NonNormalizedPhoneNumberException extends Exception {
private final String originalNumber;
private final String normalizedNumber;
public NonNormalizedNumberException(final String originalNumber, final String normalizedNumber) {
public NonNormalizedPhoneNumberException(final String originalNumber, final String normalizedNumber) {
this.originalNumber = originalNumber;
this.normalizedNumber = normalizedNumber;
}

View File

@@ -45,21 +45,17 @@ public class Util {
}
}
public static boolean isValidNumber(String number) {
return number.matches("^\\+[0-9]+") && PHONE_NUMBER_UTIL.isPossibleNumber(number, null);
}
/**
* Checks that the given number is a valid, E164-normalized phone number.
*
* @param number the number to check
*
* @throws ImpossibleNumberException if the given number is not a valid phone number at all
* @throws NonNormalizedNumberException if the given number is a valid phone number, but isn't E164-normalized
* @throws ImpossiblePhoneNumberException if the given number is not a valid phone number at all
* @throws NonNormalizedPhoneNumberException if the given number is a valid phone number, but isn't E164-normalized
*/
public static void requireNormalizedNumber(final String number) throws ImpossibleNumberException, NonNormalizedNumberException {
public static void requireNormalizedNumber(final String number) throws ImpossiblePhoneNumberException, NonNormalizedPhoneNumberException {
if (!PHONE_NUMBER_UTIL.isPossibleNumber(number, null)) {
throw new ImpossibleNumberException();
throw new ImpossiblePhoneNumberException();
}
try {
@@ -67,10 +63,10 @@ public class Util {
final String normalizedNumber = PHONE_NUMBER_UTIL.format(phoneNumber, PhoneNumberFormat.E164);
if (!number.equals(normalizedNumber)) {
throw new NonNormalizedNumberException(number, normalizedNumber);
throw new NonNormalizedPhoneNumberException(number, normalizedNumber);
}
} catch (final NumberParseException e) {
throw new ImpossibleNumberException(e);
throw new ImpossiblePhoneNumberException(e);
}
}