1
0
mirror of https://github.com/etesync/android synced 2025-01-23 14:10:54 +00:00

User info: fix version detection, and don't verify on fetch.

We were not detecting the version correctly, but always just assumed
latest version, which is obviously wrong.
In addition, before this commit we used to automatically verify on
fetch, which wasn't flexible enough for some use cases. This fixes that
too.
This commit is contained in:
Tom Hacohen 2017-04-19 15:48:36 +01:00
parent 9fb9db9327
commit e15a26af9c
4 changed files with 13 additions and 12 deletions

View File

@ -30,7 +30,7 @@ public class UserInfoManager extends BaseManager {
this.client = httpClient; this.client = httpClient;
} }
public UserInfo get(Crypto.CryptoManager cryptoManager, String owner) throws Exceptions.HttpException, Exceptions.IntegrityException, Exceptions.GenericCryptoException { public UserInfo get(String owner) throws Exceptions.HttpException {
HttpUrl remote = this.remote.newBuilder().addPathSegment(owner).addPathSegment("").build(); HttpUrl remote = this.remote.newBuilder().addPathSegment(owner).addPathSegment("").build();
Request request = new Request.Builder() Request request = new Request.Builder()
.get() .get()
@ -50,7 +50,6 @@ public class UserInfoManager extends BaseManager {
ResponseBody body = response.body(); ResponseBody body = response.body();
UserInfo ret = GsonHelper.gson.fromJson(body.charStream(), UserInfo.class); UserInfo ret = GsonHelper.gson.fromJson(body.charStream(), UserInfo.class);
ret.verify(cryptoManager);
ret.setOwner(owner); ret.setOwner(owner);
return ret; return ret;
@ -109,7 +108,7 @@ public class UserInfoManager extends BaseManager {
this.content = Arrays.concatenate(calculateHmac(crypto, content), content); this.content = Arrays.concatenate(calculateHmac(crypto, content), content);
} }
void verify(Crypto.CryptoManager crypto) throws Exceptions.IntegrityException { public void verify(Crypto.CryptoManager crypto) throws Exceptions.IntegrityException {
if (this.content == null) { if (this.content == null) {
// Nothing to verify. // Nothing to verify.
return; return;

View File

@ -79,9 +79,7 @@ public class AddMemberFragment extends DialogFragment {
try { try {
UserInfoManager userInfoManager = new UserInfoManager(httpClient, remote); UserInfoManager userInfoManager = new UserInfoManager(httpClient, remote);
Crypto.CryptoManager crypto = new Crypto.CryptoManager(info.version, settings.password(), info.uid); memberPubKey = userInfoManager.get(memberEmail).getPubkey();
memberPubKey = userInfoManager.get(crypto, memberEmail).getPubkey();
return new AddResult(null); return new AddResult(null);
} catch (Exception e) { } catch (Exception e) {
return new AddResult(e); return new AddResult(e);

View File

@ -18,6 +18,7 @@ import com.etesync.syncadapter.App;
import com.etesync.syncadapter.HttpClient; import com.etesync.syncadapter.HttpClient;
import com.etesync.syncadapter.InvalidAccountException; import com.etesync.syncadapter.InvalidAccountException;
import com.etesync.syncadapter.R; import com.etesync.syncadapter.R;
import com.etesync.syncadapter.journalmanager.Constants;
import com.etesync.syncadapter.journalmanager.Crypto; import com.etesync.syncadapter.journalmanager.Crypto;
import com.etesync.syncadapter.journalmanager.UserInfoManager; import com.etesync.syncadapter.journalmanager.UserInfoManager;
@ -88,18 +89,21 @@ public class SetupUserInfoFragment extends DialogFragment {
@Override @Override
protected SetupUserInfo.SetupUserInfoResult doInBackground(Account... accounts) { protected SetupUserInfo.SetupUserInfoResult doInBackground(Account... accounts) {
try { try {
Crypto.CryptoManager cryptoManager;
OkHttpClient httpClient = HttpClient.create(getContext(), account); OkHttpClient httpClient = HttpClient.create(getContext(), account);
Crypto.CryptoManager cryptoManager = new Crypto.CryptoManager(com.etesync.syncadapter.journalmanager.Constants.CURRENT_VERSION, settings.password(), "userInfo");
UserInfoManager userInfoManager = new UserInfoManager(httpClient, HttpUrl.get(settings.getUri())); UserInfoManager userInfoManager = new UserInfoManager(httpClient, HttpUrl.get(settings.getUri()));
UserInfoManager.UserInfo userInfo = userInfoManager.get(cryptoManager, account.name); UserInfoManager.UserInfo userInfo = userInfoManager.get(account.name);
if (userInfo == null) { if (userInfo == null) {
App.log.info("Creating userInfo for " + account.name); App.log.info("Creating userInfo for " + account.name);
cryptoManager = new Crypto.CryptoManager(Constants.CURRENT_VERSION, settings.password(), "userInfo");
userInfo = UserInfoManager.UserInfo.generate(cryptoManager, account.name); userInfo = UserInfoManager.UserInfo.generate(cryptoManager, account.name);
userInfoManager.create(userInfo); userInfoManager.create(userInfo);
} else { } else {
App.log.info("Fetched userInfo for " + account.name); App.log.info("Fetched userInfo for " + account.name);
cryptoManager = new Crypto.CryptoManager(userInfo.getVersion(), settings.password(), "userInfo");
userInfo.verify(cryptoManager);
} }
Crypto.AsymmetricKeyPair keyPair = new Crypto.AsymmetricKeyPair(userInfo.getContent(cryptoManager), userInfo.getPubkey()); Crypto.AsymmetricKeyPair keyPair = new Crypto.AsymmetricKeyPair(userInfo.getContent(cryptoManager), userInfo.getPubkey());

View File

@ -210,7 +210,7 @@ public class ServiceTest {
UserInfoManager manager = new UserInfoManager(httpClient, remote); UserInfoManager manager = new UserInfoManager(httpClient, remote);
// Get when there's nothing // Get when there's nothing
userInfo = manager.get(cryptoManager, Helpers.USER); userInfo = manager.get(Helpers.USER);
assertNull(userInfo); assertNull(userInfo);
// Create // Create
@ -218,20 +218,20 @@ public class ServiceTest {
manager.create(userInfo); manager.create(userInfo);
// Get // Get
userInfo2 = manager.get(cryptoManager, Helpers.USER); userInfo2 = manager.get(Helpers.USER);
assertNotNull(userInfo2); assertNotNull(userInfo2);
assertArrayEquals(userInfo.getContent(cryptoManager), userInfo2.getContent(cryptoManager)); assertArrayEquals(userInfo.getContent(cryptoManager), userInfo2.getContent(cryptoManager));
// Update // Update
userInfo.setContent(cryptoManager, "test".getBytes(Charsets.UTF_8)); userInfo.setContent(cryptoManager, "test".getBytes(Charsets.UTF_8));
manager.update(userInfo); manager.update(userInfo);
userInfo2 = manager.get(cryptoManager, Helpers.USER); userInfo2 = manager.get(Helpers.USER);
assertNotNull(userInfo2); assertNotNull(userInfo2);
assertArrayEquals(userInfo.getContent(cryptoManager), userInfo2.getContent(cryptoManager)); assertArrayEquals(userInfo.getContent(cryptoManager), userInfo2.getContent(cryptoManager));
// Delete // Delete
manager.delete(userInfo); manager.delete(userInfo);
userInfo = manager.get(cryptoManager, Helpers.USER); userInfo = manager.get(Helpers.USER);
assertNull(userInfo); assertNull(userInfo);
} }