Skip to content

Commit

Permalink
Add account management interface to libtextsecure api
Browse files Browse the repository at this point in the history
  • Loading branch information
moxie0 committed Nov 12, 2014
1 parent ae178fc commit 601e233
Show file tree
Hide file tree
Showing 32 changed files with 464 additions and 492 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.whispersystems.textsecure.api;

import org.whispersystems.libaxolotl.IdentityKey;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.SignedPreKeyEntity;

import java.io.IOException;
import java.util.List;
import java.util.Set;

public class TextSecureAccountManager {

private final PushServiceSocket pushServiceSocket;

public TextSecureAccountManager(String url, PushServiceSocket.TrustStore trustStore,
String user, String password)
{
this.pushServiceSocket = new PushServiceSocket(url, trustStore, user, password);
}

public void setGcmId(Optional<String> gcmRegistrationId) throws IOException {
if (gcmRegistrationId.isPresent()) {
this.pushServiceSocket.registerGcmId(gcmRegistrationId.get());
} else {
this.pushServiceSocket.unregisterGcmId();
}
}

public void requestSmsVerificationCode() throws IOException {
this.pushServiceSocket.createAccount(false);
}

public void requestVoiceVerificationCode() throws IOException {
this.pushServiceSocket.createAccount(true);
}

public void verifyAccount(String verificationCode, String signalingKey,
boolean supportsSms, int axolotlRegistrationId)
throws IOException
{
this.pushServiceSocket.verifyAccount(verificationCode, signalingKey,
supportsSms, axolotlRegistrationId);
}

public void setPreKeys(IdentityKey identityKey, PreKeyRecord lastResortKey,
SignedPreKeyRecord signedPreKey, List<PreKeyRecord> oneTimePreKeys)
throws IOException
{
this.pushServiceSocket.registerPreKeys(identityKey, lastResortKey, signedPreKey, oneTimePreKeys);
}

public int getPreKeysCount() throws IOException {
return this.pushServiceSocket.getAvailablePreKeys();
}

public void setSignedPreKey(SignedPreKeyRecord signedPreKey) throws IOException {
this.pushServiceSocket.setCurrentSignedPreKey(signedPreKey);
}

public SignedPreKeyEntity getSignedPreKey() throws IOException {
return this.pushServiceSocket.getCurrentSignedPreKey();
}

public Optional<ContactTokenDetails> getContact(String contactToken) throws IOException {
return Optional.fromNullable(this.pushServiceSocket.getContactTokenDetails(contactToken));
}

public List<ContactTokenDetails> getContacts(Set<String> contactTokens) {
return this.pushServiceSocket.retrieveDirectory(contactTokens);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.whispersystems.textsecure.api;

import android.content.Context;

import org.whispersystems.libaxolotl.InvalidMessageException;
import org.whispersystems.libaxolotl.state.AxolotlStore;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer;
import org.whispersystems.textsecure.api.crypto.AttachmentCipherInputStream;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer;
import org.whispersystems.textsecure.push.PushServiceSocket;

import java.io.File;
Expand All @@ -18,15 +16,14 @@ public class TextSecureMessageReceiver {
private final AxolotlStore axolotlStore;
private final PushServiceSocket socket;


public TextSecureMessageReceiver(Context context, String signalingKey, String url,
public TextSecureMessageReceiver(String signalingKey, String url,
PushServiceSocket.TrustStore trustStore,
String user, String password,
AxolotlStore axolotlStore)
{
this.axolotlStore = axolotlStore;
this.signalingKey = signalingKey;
this.socket = new PushServiceSocket(context, url, trustStore, user, password);
this.socket = new PushServiceSocket(url, trustStore, user, password);
}

public InputStream retrieveAttachment(TextSecureAttachmentPointer pointer, File destination)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.whispersystems.textsecure.api;

import android.content.Context;
import android.util.Log;

import com.google.protobuf.ByteString;
Expand All @@ -11,12 +10,12 @@
import org.whispersystems.libaxolotl.state.AxolotlStore;
import org.whispersystems.libaxolotl.state.PreKeyBundle;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
import org.whispersystems.textsecure.api.messages.TextSecureGroup;
import org.whispersystems.textsecure.api.messages.TextSecureMessage;
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
import org.whispersystems.textsecure.push.MismatchedDevices;
import org.whispersystems.textsecure.push.OutgoingPushMessage;
import org.whispersystems.textsecure.push.OutgoingPushMessageList;
Expand Down Expand Up @@ -48,17 +47,19 @@ public class TextSecureMessageSender {
private final AxolotlStore store;
private final Optional<EventListener> eventListener;

public TextSecureMessageSender(Context context, String url,
PushServiceSocket.TrustStore trustStore,
String user, String password,
AxolotlStore store,
public TextSecureMessageSender(String url, PushServiceSocket.TrustStore trustStore,
String user, String password, AxolotlStore store,
Optional<EventListener> eventListener)
{
this.socket = new PushServiceSocket(context, url, trustStore, user, password);
this.socket = new PushServiceSocket(url, trustStore, user, password);
this.store = store;
this.eventListener = eventListener;
}

public void sendDeliveryReceipt(PushAddress recipient, long messageId) throws IOException {
this.socket.sendReceipt(recipient.getNumber(), messageId, recipient.getRelay());
}

public void sendMessage(PushAddress recipient, TextSecureMessage message)
throws UntrustedIdentityException, IOException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.whispersystems.textsecure.push;

import android.content.Context;

import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.storage.RecipientDevice;

public class PushAddress extends RecipientDevice {
Expand All @@ -24,9 +21,4 @@ public String getRelay() {
return relay;
}

public static PushAddress create(Context context, long recipientId, String e164number, int deviceId) {
String relay = Directory.getInstance(context).getRelay(e164number);
return new PushAddress(recipientId, e164number, deviceId, relay);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.whispersystems.textsecure.push;

import android.content.Context;
import android.util.Log;

import com.google.thoughtcrimegson.Gson;
Expand Down Expand Up @@ -88,16 +87,14 @@ public class PushServiceSocket {

private static final boolean ENFORCE_SSL = true;

private final Context context;
private final String serviceUrl;
private final String localNumber;
private final String password;
private final TrustManager[] trustManagers;

public PushServiceSocket(Context context, String serviceUrl, TrustStore trustStore,
public PushServiceSocket(String serviceUrl, TrustStore trustStore,
String localNumber, String password)
{
this.context = context.getApplicationContext();
this.serviceUrl = serviceUrl;
this.localNumber = localNumber;
this.password = password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
import org.thoughtcrime.securesms.components.OutgoingSmsPreference;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.contacts.ContactIdentityManager;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.DynamicLanguage;
Expand All @@ -64,9 +65,9 @@
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Trimmer;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.exceptions.AuthorizationFailedException;
import org.whispersystems.textsecure.push.PushServiceSocket;

import java.io.IOException;

Expand Down Expand Up @@ -362,10 +363,10 @@ protected void onPostExecute(Integer result) {
@Override
protected Integer doInBackground(Void... params) {
try {
Context context = getActivity();
PushServiceSocket socket = PushServiceSocketFactory.create(context);
Context context = getActivity();
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);

socket.unregisterGcmId();
accountManager.setGcmId(Optional.<String>absent());
GoogleCloudMessaging.getInstance(context).unregister();

return SUCCESS;
Expand Down
8 changes: 3 additions & 5 deletions src/org/thoughtcrime/securesms/GroupCreateActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import org.thoughtcrime.securesms.database.NotInDirectoryException;
import org.whispersystems.textsecure.util.InvalidNumberException;
import org.whispersystems.textsecure.util.ListenableFutureTask;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
Expand All @@ -77,7 +76,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import ws.com.google.android.mms.MmsException;

Expand Down Expand Up @@ -171,7 +169,7 @@ private void enableWhisperGroupUi() {

private static boolean isActiveInDirectory(Context context, Recipient recipient) {
try {
if (!Directory.getInstance(context).isActiveNumber(Util.canonicalizeNumber(context, recipient.getNumber()))) {
if (!TextSecureDirectory.getInstance(context).isActiveNumber(Util.canonicalizeNumber(context, recipient.getNumber()))) {
return false;
}
} catch (NotInDirectoryException e) {
Expand Down
16 changes: 9 additions & 7 deletions src/org/thoughtcrime/securesms/RegistrationProgressActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
import android.widget.Toast;


import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.service.RegistrationService;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.exceptions.ExpectationFailedException;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.exceptions.RateLimitException;
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
import org.whispersystems.textsecure.util.Util;
Expand Down Expand Up @@ -514,9 +514,11 @@ protected void onPostExecute(Integer result) {
@Override
protected Integer doInBackground(Void... params) {
try {
PushServiceSocket socket = PushServiceSocketFactory.create(context, e164number, password);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
int registrationId = TextSecurePreferences.getLocalRegistrationId(context);
socket.verifyAccount(code, signalingKey, true, registrationId);

accountManager.verifyAccount(code, signalingKey, true, registrationId);

return SUCCESS;
} catch (ExpectationFailedException e) {
Log.w("RegistrationProgressActivity", e);
Expand Down Expand Up @@ -605,8 +607,8 @@ public void run() {
@Override
protected Integer doInBackground(Void... params) {
try {
PushServiceSocket socket = PushServiceSocketFactory.create(context, e164number, password);
socket.createAccount(true);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
accountManager.requestVoiceVerificationCode();

return SUCCESS;
} catch (RateLimitException e) {
Expand Down
4 changes: 2 additions & 2 deletions src/org/thoughtcrime/securesms/contacts/ContactAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import android.support.v4.content.Loader;
import android.telephony.PhoneNumberUtils;

import org.whispersystems.textsecure.directory.Directory;
import org.thoughtcrime.securesms.database.TextSecureDirectory;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -94,7 +94,7 @@ public Collection<ContactData> getContactsWithPush(Context context) {
final ContentResolver resolver = context.getContentResolver();
final String[] inProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};

List<String> pushNumbers = Directory.getInstance(context).getActiveNumbers();
List<String> pushNumbers = TextSecureDirectory.getInstance(context).getActiveNumbers();
final Collection<ContactData> lookupData = new ArrayList<ContactData>(pushNumbers.size());

for (String pushNumber : pushNumbers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.whispersystems.textsecure.directory;
package org.thoughtcrime.securesms.database;

public class NotInDirectoryException extends Throwable {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.whispersystems.textsecure.directory;
package org.thoughtcrime.securesms.database;

import android.content.ContentValues;
import android.content.Context;
Expand All @@ -19,7 +19,7 @@
import java.util.List;
import java.util.Set;

public class Directory {
public class TextSecureDirectory {

private static final int INTRODUCED_CHANGE_FROM_TOKEN_TO_E164_NUMBER = 2;

Expand All @@ -41,13 +41,13 @@ public class Directory {
TIMESTAMP + " INTEGER);";

private static final Object instanceLock = new Object();
private static volatile Directory instance;
private static volatile TextSecureDirectory instance;

public static Directory getInstance(Context context) {
public static TextSecureDirectory getInstance(Context context) {
if (instance == null) {
synchronized (instanceLock) {
if (instance == null) {
instance = new Directory(context);
instance = new TextSecureDirectory(context);
}
}
}
Expand All @@ -58,7 +58,7 @@ public static Directory getInstance(Context context) {
private final DatabaseHelper databaseHelper;
private final Context context;

private Directory(Context context) {
private TextSecureDirectory(Context context) {
this.context = context;
this.databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.thoughtcrime.securesms.database.EncryptingPartDatabase;
import org.thoughtcrime.securesms.database.PartDatabase;
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
import org.thoughtcrime.securesms.push.TextSecureMessageReceiverFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
Expand Down Expand Up @@ -89,12 +89,13 @@ public boolean onShouldRetry(Throwable throwable) {
private void retrievePart(MasterSecret masterSecret, PduPart part, long messageId, long partId)
throws IOException
{
TextSecureMessageReceiver receiver = TextSecureMessageReceiverFactory.create(context, masterSecret);
TextSecureMessageReceiver receiver = TextSecureCommunicationFactory.createReceiver(context, masterSecret);
EncryptingPartDatabase database = DatabaseFactory.getEncryptingPartDatabase(context, masterSecret);
File attachmentFile = null;

try {
attachmentFile = createTempFile();

TextSecureAttachmentPointer pointer = createAttachmentPointer(masterSecret, part);
InputStream attachment = receiver.retrieveAttachment(pointer, attachmentFile);

Expand Down

0 comments on commit 601e233

Please sign in to comment.