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.
pull/14/head
Tom Hacohen 7 years ago
parent a57936982d
commit 4c6176a6f4

@ -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();

@ -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;

@ -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<Configuration> {
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

Loading…
Cancel
Save