mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 04:46:53 +00:00
New API to support multiple accounts per # (FREEBIE)
This commit is contained in:
1 parent
4cd1082a4a
commit
ef1160eda8
35 files changed
+1591
-388
No files matched your search
+84
-12
@@ -1,9 +1,15 @@
|
||||
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.common.base.Optional;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.whispersystems.textsecuregcm.controllers.AccountController;
|
||||
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
||||
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
||||
@@ -12,23 +18,54 @@ import org.whispersystems.textsecuregcm.sms.SmsSender;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.PendingAccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.util.VerificationCode;
|
||||
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class AccountControllerTest extends ResourceTest {
|
||||
/** The AccountAttributes used in protocol v1 (no fetchesMessages) */
|
||||
static class V1AccountAttributes {
|
||||
@JsonProperty
|
||||
@NotEmpty
|
||||
private String signalingKey;
|
||||
|
||||
@JsonProperty
|
||||
private boolean supportsSms;
|
||||
|
||||
public V1AccountAttributes(String signalingKey, boolean supportsSms) {
|
||||
this.signalingKey = signalingKey;
|
||||
this.supportsSms = supportsSms;
|
||||
}
|
||||
}
|
||||
|
||||
@Path("/v1/accounts")
|
||||
static class DumbVerificationAccountController extends AccountController {
|
||||
public DumbVerificationAccountController(PendingAccountsManager pendingAccounts, PendingDevicesManager pendingDevices, AccountsManager accounts, RateLimiters rateLimiters, SmsSender smsSenderFactory) {
|
||||
super(pendingAccounts, pendingDevices, accounts, rateLimiters, smsSenderFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected VerificationCode generateVerificationCode() {
|
||||
return new VerificationCode(5678901);
|
||||
}
|
||||
}
|
||||
|
||||
private static final String SENDER = "+14152222222";
|
||||
|
||||
private PendingAccountsManager pendingAccountsManager = mock(PendingAccountsManager.class);
|
||||
private AccountsManager accountsManager = mock(AccountsManager.class );
|
||||
private RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
private SmsSender smsSender = mock(SmsSender.class );
|
||||
private PendingAccountsManager pendingAccountsManager = mock(PendingAccountsManager.class);
|
||||
private PendingDevicesManager pendingDevicesManager = mock(PendingDevicesManager.class);
|
||||
private AccountsManager accountsManager = mock(AccountsManager.class );
|
||||
private RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
private SmsSender smsSender = mock(SmsSender.class );
|
||||
|
||||
@Override
|
||||
protected void setUpResources() throws Exception {
|
||||
@@ -40,10 +77,17 @@ public class AccountControllerTest extends ResourceTest {
|
||||
|
||||
when(pendingAccountsManager.getCodeForNumber(SENDER)).thenReturn(Optional.of("1234"));
|
||||
|
||||
addResource(new AccountController(pendingAccountsManager,
|
||||
accountsManager,
|
||||
rateLimiters,
|
||||
smsSender));
|
||||
when(pendingDevicesManager.getCodeForNumber(AuthHelper.VALID_NUMBER)).thenReturn(Optional.of("5678901"));
|
||||
|
||||
Mockito.doAnswer(new Answer() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
((Account)invocation.getArguments()[0]).setDeviceId(2);
|
||||
return null;
|
||||
}
|
||||
}).when(accountsManager).createAccountOnExistingNumber(any(Account.class));
|
||||
|
||||
addResource(new DumbVerificationAccountController(pendingAccountsManager, pendingDevicesManager, accountsManager, rateLimiters, smsSender));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -62,13 +106,17 @@ public class AccountControllerTest extends ResourceTest {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/accounts/code/%s", "1234"))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
||||
.entity(new AccountAttributes("keykeykeykey", false))
|
||||
.entity(new V1AccountAttributes("keykeykeykey", false))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
.put(ClientResponse.class);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(204);
|
||||
|
||||
verify(accountsManager).create(isA(Account.class));
|
||||
verify(accountsManager).createResetNumber(isA(Account.class));
|
||||
|
||||
ArgumentCaptor<String> number = ArgumentCaptor.forClass(String.class);
|
||||
verify(pendingAccountsManager).remove(number.capture());
|
||||
assertThat(number.getValue()).isEqualTo(SENDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,7 +124,7 @@ public class AccountControllerTest extends ResourceTest {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/accounts/code/%s", "1111"))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
||||
.entity(new AccountAttributes("keykeykeykey", false))
|
||||
.entity(new V1AccountAttributes("keykeykeykey", false))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
.put(ClientResponse.class);
|
||||
|
||||
@@ -85,4 +133,28 @@ public class AccountControllerTest extends ResourceTest {
|
||||
verifyNoMoreInteractions(accountsManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validDeviceRegisterTest() throws Exception {
|
||||
VerificationCode deviceCode = client().resource("/v1/accounts/registerdevice")
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(VerificationCode.class);
|
||||
|
||||
assertThat(deviceCode).isEqualTo(new VerificationCode(5678901));
|
||||
|
||||
Long deviceId = client().resource(String.format("/v1/accounts/device/5678901"))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, "password1"))
|
||||
.entity(new AccountAttributes("keykeykeykey", false, true))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
.put(Long.class);
|
||||
assertThat(deviceId).isNotEqualTo(AuthHelper.DEFAULT_DEVICE_ID);
|
||||
|
||||
ArgumentCaptor<Account> newAccount = ArgumentCaptor.forClass(Account.class);
|
||||
verify(accountsManager).createAccountOnExistingNumber(newAccount.capture());
|
||||
assertThat(deviceId).isEqualTo(newAccount.getValue().getDeviceId());
|
||||
|
||||
ArgumentCaptor<String> number = ArgumentCaptor.forClass(String.class);
|
||||
verify(pendingDevicesManager).remove(number.capture());
|
||||
assertThat(number.getValue()).isEqualTo(AuthHelper.VALID_NUMBER);
|
||||
}
|
||||
|
||||
}
|
||||
+59
-10
@@ -1,15 +1,24 @@
|
||||
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.sun.jersey.api.client.GenericType;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.textsecuregcm.controllers.KeysController;
|
||||
import org.whispersystems.textsecuregcm.entities.PreKey;
|
||||
import org.whispersystems.textsecuregcm.entities.UnstructuredPreKeyList;
|
||||
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
||||
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.Keys;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
|
||||
import javax.jws.WebResult;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -18,26 +27,41 @@ public class KeyControllerTest extends ResourceTest {
|
||||
private final String EXISTS_NUMBER = "+14152222222";
|
||||
private final String NOT_EXISTS_NUMBER = "+14152222220";
|
||||
|
||||
private final PreKey SAMPLE_KEY = new PreKey(1, EXISTS_NUMBER, 1234, "test1", "test2", false);
|
||||
private final Keys keys = mock(Keys.class);
|
||||
private final PreKey SAMPLE_KEY = new PreKey(1, EXISTS_NUMBER, AuthHelper.DEFAULT_DEVICE_ID, 1234, "test1", "test2", false);
|
||||
private final PreKey SAMPLE_KEY2 = new PreKey(2, EXISTS_NUMBER, 2, 5667, "test3", "test4", false);
|
||||
private final Keys keys = mock(Keys.class);
|
||||
|
||||
Account[] fakeAccount;
|
||||
|
||||
@Override
|
||||
protected void setUpResources() {
|
||||
addProvider(AuthHelper.getAuthenticator());
|
||||
|
||||
RateLimiters rateLimiters = mock(RateLimiters.class);
|
||||
RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
RateLimiters rateLimiters = mock(RateLimiters.class);
|
||||
RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
AccountsManager accounts = mock(AccountsManager.class);
|
||||
|
||||
fakeAccount = new Account[2];
|
||||
fakeAccount[0] = mock(Account.class);
|
||||
fakeAccount[1] = mock(Account.class);
|
||||
|
||||
when(rateLimiters.getPreKeysLimiter()).thenReturn(rateLimiter);
|
||||
|
||||
when(keys.get(EXISTS_NUMBER)).thenReturn(SAMPLE_KEY);
|
||||
when(keys.get(NOT_EXISTS_NUMBER)).thenReturn(null);
|
||||
when(keys.get(eq(EXISTS_NUMBER), anyList())).thenReturn(new UnstructuredPreKeyList(Arrays.asList(SAMPLE_KEY, SAMPLE_KEY2)));
|
||||
when(keys.get(eq(NOT_EXISTS_NUMBER), anyList())).thenReturn(null);
|
||||
|
||||
addResource(new KeysController(rateLimiters, keys, null));
|
||||
when(fakeAccount[0].getDeviceId()).thenReturn(AuthHelper.DEFAULT_DEVICE_ID);
|
||||
when(fakeAccount[1].getDeviceId()).thenReturn((long) 2);
|
||||
|
||||
when(accounts.getAllByNumber(EXISTS_NUMBER)).thenReturn(Arrays.asList(fakeAccount[0], fakeAccount[1]));
|
||||
when(accounts.getAllByNumber(NOT_EXISTS_NUMBER)).thenReturn(new LinkedList<Account>());
|
||||
|
||||
addResource(new KeysController.V1(rateLimiters, keys, accounts, null));
|
||||
addResource(new KeysController.V2(rateLimiters, keys, accounts, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validRequestTest() throws Exception {
|
||||
public void validRequestsTest() throws Exception {
|
||||
PreKey result = client().resource(String.format("/v1/keys/%s", EXISTS_NUMBER))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(PreKey.class);
|
||||
@@ -49,7 +73,32 @@ public class KeyControllerTest extends ResourceTest {
|
||||
assertThat(result.getId() == 0);
|
||||
assertThat(result.getNumber() == null);
|
||||
|
||||
verify(keys).get(EXISTS_NUMBER);
|
||||
verify(keys).get(eq(EXISTS_NUMBER), eq(Arrays.asList(fakeAccount)));
|
||||
verifyNoMoreInteractions(keys);
|
||||
|
||||
List<PreKey> results = client().resource(String.format("/v2/keys/%s", EXISTS_NUMBER))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(new GenericType<List<PreKey>>(){});
|
||||
|
||||
assertThat(results.size()).isEqualTo(2);
|
||||
result = results.get(0);
|
||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY.getKeyId());
|
||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY.getPublicKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY.getIdentityKey());
|
||||
|
||||
assertThat(result.getId() == 0);
|
||||
assertThat(result.getNumber() == null);
|
||||
|
||||
result = results.get(1);
|
||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY2.getKeyId());
|
||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY2.getPublicKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY2.getIdentityKey());
|
||||
|
||||
assertThat(result.getId() == 1);
|
||||
assertThat(result.getNumber() == null);
|
||||
|
||||
verify(keys, times(2)).get(eq(EXISTS_NUMBER), eq(Arrays.asList(fakeAccount[0], fakeAccount[1])));
|
||||
verifyNoMoreInteractions(keys);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,7 +109,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
|
||||
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(404);
|
||||
|
||||
verify(keys).get(NOT_EXISTS_NUMBER);
|
||||
verify(keys).get(NOT_EXISTS_NUMBER, new LinkedList<Account>());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -14,7 +14,7 @@ public class PreKeyTest {
|
||||
|
||||
@Test
|
||||
public void serializeToJSON() throws Exception {
|
||||
PreKey preKey = new PreKey(1, "+14152222222", 1234, "test", "identityTest", false);
|
||||
PreKey preKey = new PreKey(1, "+14152222222", 0, 1234, "test", "identityTest", false);
|
||||
|
||||
assertThat("Basic Contact Serialization works",
|
||||
asJson(preKey),
|
||||
|
||||
@@ -15,6 +15,7 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class AuthHelper {
|
||||
public static final long DEFAULT_DEVICE_ID = 1;
|
||||
|
||||
public static final String VALID_NUMBER = "+14150000000";
|
||||
public static final String VALID_PASSWORD = "foo";
|
||||
@@ -29,7 +30,7 @@ public class AuthHelper {
|
||||
|
||||
when(credentials.verify("foo")).thenReturn(true);
|
||||
when(account.getAuthenticationCredentials()).thenReturn(credentials);
|
||||
when(accounts.get(VALID_NUMBER)).thenReturn(Optional.of(account));
|
||||
when(accounts.get(VALID_NUMBER, DEFAULT_DEVICE_ID)).thenReturn(Optional.of(account));
|
||||
|
||||
return new MultiBasicAuthProvider<>(new FederatedPeerAuthenticator(new FederationConfiguration()),
|
||||
FederatedPeer.class,
|
||||
@@ -41,4 +42,7 @@ public class AuthHelper {
|
||||
return "Basic " + Base64.encodeBytes((number + ":" + password).getBytes());
|
||||
}
|
||||
|
||||
public static String getV2AuthHeader(String number, long deviceId, String password) {
|
||||
return "Basic " + Base64.encodeBytes((number + "." + deviceId + ":" + password).getBytes());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user