From 4c6176a6f4e68c1f888f8d50bed2e495b678f858 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 19 Apr 2017 16:01:04 +0100 Subject: [PATCH] Fetch userinfo on account creation. We need the keypair to access shared journals, so we need to make sure to fetch it at the moment we create the local account, which is what this commit does. --- .../com/etesync/syncadapter/HttpClient.java | 4 +++ .../ui/setup/BaseConfigurationFinder.java | 2 ++ .../ui/setup/SetupEncryptionFragment.java | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/app/src/main/java/com/etesync/syncadapter/HttpClient.java b/app/src/main/java/com/etesync/syncadapter/HttpClient.java index a0e277e0..5fd080a8 100644 --- a/app/src/main/java/com/etesync/syncadapter/HttpClient.java +++ b/app/src/main/java/com/etesync/syncadapter/HttpClient.java @@ -74,6 +74,10 @@ public class HttpClient { return create(context, App.log); } + public static OkHttpClient create(@Nullable Context context, String authToken) { + return create(context, App.log, Constants.serviceUrl.getHost(), authToken); + } + private static OkHttpClient.Builder defaultBuilder(@Nullable Context context, @NonNull final Logger logger) { OkHttpClient.Builder builder = client.newBuilder(); diff --git a/app/src/main/java/com/etesync/syncadapter/ui/setup/BaseConfigurationFinder.java b/app/src/main/java/com/etesync/syncadapter/ui/setup/BaseConfigurationFinder.java index 33200efe..23110052 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/setup/BaseConfigurationFinder.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/setup/BaseConfigurationFinder.java @@ -19,6 +19,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import com.etesync.syncadapter.HttpClient; +import com.etesync.syncadapter.journalmanager.Crypto; import com.etesync.syncadapter.journalmanager.Exceptions; import com.etesync.syncadapter.journalmanager.JournalAuthenticator; import com.etesync.syncadapter.log.StringHandler; @@ -99,6 +100,7 @@ public class BaseConfigurationFinder { public final String userName, authtoken; public String rawPassword; public String password; + public Crypto.AsymmetricKeyPair keyPair; public final ServiceInfo cardDAV; public final ServiceInfo calDAV; diff --git a/app/src/main/java/com/etesync/syncadapter/ui/setup/SetupEncryptionFragment.java b/app/src/main/java/com/etesync/syncadapter/ui/setup/SetupEncryptionFragment.java index 1b4d920f..253f095e 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/setup/SetupEncryptionFragment.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/setup/SetupEncryptionFragment.java @@ -28,9 +28,12 @@ import android.support.v4.content.Loader; import com.etesync.syncadapter.AccountSettings; import com.etesync.syncadapter.App; import com.etesync.syncadapter.Constants; +import com.etesync.syncadapter.HttpClient; import com.etesync.syncadapter.InvalidAccountException; import com.etesync.syncadapter.R; import com.etesync.syncadapter.journalmanager.Crypto; +import com.etesync.syncadapter.journalmanager.Exceptions; +import com.etesync.syncadapter.journalmanager.UserInfoManager; import com.etesync.syncadapter.model.CollectionInfo; import com.etesync.syncadapter.model.JournalEntity; import com.etesync.syncadapter.model.ServiceDB; @@ -44,6 +47,8 @@ import at.bitfire.ical4android.TaskProvider; import io.requery.Persistable; import io.requery.sql.EntityDataStore; import lombok.Cleanup; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; public class SetupEncryptionFragment extends DialogFragment implements LoaderManager.LoaderCallbacks { private static final String KEY_CONFIG = "config"; @@ -114,6 +119,27 @@ public class SetupEncryptionFragment extends DialogFragment implements LoaderMan @Override public Configuration loadInBackground() { config.password = Crypto.deriveKey(config.userName, config.rawPassword); + + try { + Crypto.CryptoManager cryptoManager; + OkHttpClient httpClient = HttpClient.create(getContext(), config.authtoken); + + UserInfoManager userInfoManager = new UserInfoManager(httpClient, HttpUrl.get(config.url)); + UserInfoManager.UserInfo userInfo = userInfoManager.get(config.userName); + if (userInfo != null) { + App.log.info("Fetched userInfo for " + config.userName); + cryptoManager = new Crypto.CryptoManager(userInfo.getVersion(), config.password, "userInfo"); + userInfo.verify(cryptoManager); + config.keyPair = new Crypto.AsymmetricKeyPair(userInfo.getContent(cryptoManager), userInfo.getPubkey()); + } + } catch (Exceptions.HttpException e) { + e.printStackTrace(); + } catch (Exceptions.IntegrityException e) { + e.printStackTrace(); + } catch (Exceptions.VersionTooNewException e) { + e.printStackTrace(); + } + return config; } } @@ -138,6 +164,9 @@ public class SetupEncryptionFragment extends DialogFragment implements LoaderMan AccountSettings settings = new AccountSettings(getContext(), account); settings.setAuthToken(config.authtoken); + if (config.keyPair != null) { + settings.setKeyPair(config.keyPair); + } if (config.cardDAV != null) { // insert CardDAV service