diff --git a/src/main/java/org/whispersystems/textsecuregcm/WhisperServerConfiguration.java b/src/main/java/org/whispersystems/textsecuregcm/WhisperServerConfiguration.java index 79e5cda19..9bf73cdae 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/WhisperServerConfiguration.java +++ b/src/main/java/org/whispersystems/textsecuregcm/WhisperServerConfiguration.java @@ -25,12 +25,18 @@ import org.whispersystems.textsecuregcm.configuration.RateLimitsConfiguration; import org.whispersystems.textsecuregcm.configuration.RedPhoneConfiguration; import org.whispersystems.textsecuregcm.configuration.RedisConfiguration; import org.whispersystems.textsecuregcm.configuration.S3Configuration; +import org.whispersystems.textsecuregcm.configuration.TestDeviceConfiguration; import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration; import org.whispersystems.textsecuregcm.configuration.WebsocketConfiguration; import javax.validation.Valid; import javax.validation.constraints.NotNull; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + import io.dropwizard.Configuration; import io.dropwizard.client.JerseyClientConfiguration; import io.dropwizard.db.DataSourceFactory; @@ -70,6 +76,10 @@ public class WhisperServerConfiguration extends Configuration { @JsonProperty private DataSourceFactory messageStore; + @Valid + @NotNull + @JsonProperty + private List testDevices = new LinkedList<>(); @Valid @JsonProperty @@ -157,4 +167,15 @@ public class WhisperServerConfiguration extends Configuration { public RedPhoneConfiguration getRedphoneConfiguration() { return redphone; } + + public Map getTestDevices() { + Map results = new HashMap<>(); + + for (TestDeviceConfiguration testDeviceConfiguration : testDevices) { + results.put(testDeviceConfiguration.getNumber(), + testDeviceConfiguration.getCode()); + } + + return results; + } } diff --git a/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 5df9246a7..16f3e184e 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -192,7 +192,7 @@ public class WhisperServerService extends Application authorizationKey; + private final Map testDevices; public AccountController(PendingAccountsManager pendingAccounts, AccountsManager accounts, @@ -78,7 +80,8 @@ public class AccountController { SmsSender smsSenderFactory, MessagesManager messagesManager, TimeProvider timeProvider, - Optional authorizationKey) + Optional authorizationKey, + Map testDevices) { this.pendingAccounts = pendingAccounts; this.accounts = accounts; @@ -87,6 +90,7 @@ public class AccountController { this.messagesManager = messagesManager; this.timeProvider = timeProvider; this.authorizationKey = authorizationKey; + this.testDevices = testDevices; } @Timed @@ -112,10 +116,12 @@ public class AccountController { throw new WebApplicationException(Response.status(422).build()); } - VerificationCode verificationCode = generateVerificationCode(); + VerificationCode verificationCode = generateVerificationCode(number); pendingAccounts.store(number, verificationCode.getVerificationCode()); - if (transport.equals("sms")) { + if (testDevices.containsKey(number)) { + // noop + } else if (transport.equals("sms")) { smsSender.deliverSmsVerification(number, verificationCode.getVerificationCodeDisplay()); } else if (transport.equals("voice")) { smsSender.deliverVoxVerification(number, verificationCode.getVerificationCodeSpeech()); @@ -289,8 +295,12 @@ public class AccountController { logger.debug("Stored device..."); } - @VisibleForTesting protected VerificationCode generateVerificationCode() { + @VisibleForTesting protected VerificationCode generateVerificationCode(String number) { try { + if (testDevices.containsKey(number)) { + return new VerificationCode(testDevices.get(number)); + } + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); int randomInt = 100000 + random.nextInt(900000); return new VerificationCode(randomInt); diff --git a/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/AccountControllerTest.java b/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/AccountControllerTest.java index ee2c0d5af..8a15fd4dd 100644 --- a/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/AccountControllerTest.java +++ b/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/AccountControllerTest.java @@ -22,6 +22,8 @@ import org.whispersystems.textsecuregcm.tests.util.AuthHelper; import javax.ws.rs.core.MediaType; +import java.util.HashMap; + import io.dropwizard.testing.junit.ResourceTestRule; import static org.fest.assertions.api.Assertions.assertThat; import static org.mockito.Matchers.anyString; @@ -49,7 +51,8 @@ public class AccountControllerTest { smsSender, storedMessages, timeProvider, - Optional.of(authorizationKey))) + Optional.of(authorizationKey), + new HashMap())) .build();