mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Move account deletion into AccountManager.
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package org.briarproject.briar.android.controller;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -14,7 +12,7 @@ public interface ConfigController {
|
||||
|
||||
boolean storeEncryptedDatabaseKey(String hex);
|
||||
|
||||
void deleteAccount(Context ctx);
|
||||
void deleteAccount();
|
||||
|
||||
boolean accountExists();
|
||||
|
||||
|
||||
@@ -1,64 +1,32 @@
|
||||
package org.briarproject.briar.android.controller;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.v7.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.AndroidUtils;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
// TODO: Remove this class, which just delegates to AccountManager
|
||||
|
||||
@NotNullByDefault
|
||||
public class ConfigControllerImpl implements ConfigController {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConfigControllerImpl.class.getName());
|
||||
|
||||
private static final String PREF_DB_KEY = "key";
|
||||
|
||||
private final SharedPreferences briarPrefs;
|
||||
protected final AccountManager accountManager;
|
||||
protected final DatabaseConfig databaseConfig;
|
||||
|
||||
@Inject
|
||||
public ConfigControllerImpl(SharedPreferences briarPrefs,
|
||||
AccountManager accountManager, DatabaseConfig databaseConfig) {
|
||||
this.briarPrefs = briarPrefs;
|
||||
public ConfigControllerImpl(AccountManager accountManager) {
|
||||
// TODO: Remove
|
||||
Log.i(getClass().getName(), "Using account manager "
|
||||
+ accountManager.getClass().getName());
|
||||
this.accountManager = accountManager;
|
||||
this.databaseConfig = databaseConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getEncryptedDatabaseKey() {
|
||||
String key = getDatabaseKeyFromPreferences();
|
||||
if (key == null) key = accountManager.getEncryptedDatabaseKey();
|
||||
else migrateDatabaseKeyToFile(key);
|
||||
return key;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getDatabaseKeyFromPreferences() {
|
||||
String key = briarPrefs.getString(PREF_DB_KEY, null);
|
||||
if (key == null) LOG.info("No database key in preferences");
|
||||
else LOG.info("Found database key in preferences");
|
||||
return key;
|
||||
}
|
||||
|
||||
private void migrateDatabaseKeyToFile(String key) {
|
||||
if (accountManager.storeEncryptedDatabaseKey(key)) {
|
||||
if (briarPrefs.edit().remove(PREF_DB_KEY).commit())
|
||||
LOG.info("Database key migrated to file");
|
||||
else LOG.warning("Database key not removed from preferences");
|
||||
} else {
|
||||
LOG.warning("Database key not migrated to file");
|
||||
}
|
||||
return accountManager.getEncryptedDatabaseKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,17 +35,13 @@ public class ConfigControllerImpl implements ConfigController {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAccount(Context ctx) {
|
||||
LOG.info("Deleting account");
|
||||
SharedPreferences defaultPrefs =
|
||||
PreferenceManager.getDefaultSharedPreferences(ctx);
|
||||
AndroidUtils.deleteAppData(ctx, briarPrefs, defaultPrefs);
|
||||
public void deleteAccount() {
|
||||
accountManager.deleteAccount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accountExists() {
|
||||
return getEncryptedDatabaseKey() != null &&
|
||||
databaseConfig.getDatabaseDirectory().isDirectory();
|
||||
return accountManager.hasDatabaseKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -105,7 +105,7 @@ public class PasswordActivity extends BaseActivity {
|
||||
}
|
||||
|
||||
private void deleteAccount() {
|
||||
passwordController.deleteAccount(this);
|
||||
passwordController.deleteAccount();
|
||||
Localizer.reinitialize();
|
||||
UiUtils.setTheme(this, getString(R.string.pref_theme_light_value));
|
||||
setResult(RESULT_CANCELED);
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.briarproject.briar.android.controller.ConfigControllerImpl;
|
||||
@@ -33,11 +30,10 @@ public class PasswordControllerImpl extends ConfigControllerImpl
|
||||
private final PasswordStrengthEstimator strengthEstimator;
|
||||
|
||||
@Inject
|
||||
PasswordControllerImpl(SharedPreferences briarPrefs,
|
||||
AccountManager accountManager, DatabaseConfig databaseConfig,
|
||||
PasswordControllerImpl(AccountManager accountManager,
|
||||
@CryptoExecutor Executor cryptoExecutor, CryptoComponent crypto,
|
||||
PasswordStrengthEstimator strengthEstimator) {
|
||||
super(briarPrefs, accountManager, databaseConfig);
|
||||
super(accountManager);
|
||||
this.cryptoExecutor = cryptoExecutor;
|
||||
this.crypto = crypto;
|
||||
this.strengthEstimator = strengthEstimator;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
@@ -8,7 +7,6 @@ import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
@@ -33,13 +31,11 @@ public class SetupControllerImpl extends PasswordControllerImpl
|
||||
private volatile SetupActivity setupActivity;
|
||||
|
||||
@Inject
|
||||
SetupControllerImpl(SharedPreferences briarPrefs,
|
||||
AccountManager accountManager, DatabaseConfig databaseConfig,
|
||||
SetupControllerImpl(AccountManager accountManager,
|
||||
@CryptoExecutor Executor cryptoExecutor, CryptoComponent crypto,
|
||||
PasswordStrengthEstimator strengthEstimator,
|
||||
IdentityManager identityManager) {
|
||||
super(briarPrefs, accountManager, databaseConfig, cryptoExecutor,
|
||||
crypto, strengthEstimator);
|
||||
super(accountManager, cryptoExecutor, crypto, strengthEstimator);
|
||||
this.identityManager = identityManager;
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ public class PanicResponderActivity extends BriarActivity {
|
||||
|
||||
private void deleteAllData() {
|
||||
androidExecutor.runOnBackgroundThread(() -> {
|
||||
configController.deleteAccount(PanicResponderActivity.this);
|
||||
configController.deleteAccount();
|
||||
// TODO somehow delete/shred the database more thoroughly
|
||||
PanicResponder.deleteAllAppData(PanicResponderActivity.this);
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ public class SplashScreenActivity extends BaseActivity {
|
||||
startActivity(new Intent(this, OpenDatabaseActivity.class));
|
||||
} else {
|
||||
LOG.info("Account does not exist");
|
||||
configController.deleteAccount(this);
|
||||
configController.deleteAccount();
|
||||
startActivity(new Intent(this, SetupActivity.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package org.briarproject.briar.android.controller;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
public class ConfigControllerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final SharedPreferences prefs =
|
||||
context.mock(SharedPreferences.class);
|
||||
private final AccountManager accountManager =
|
||||
context.mock(AccountManager.class);
|
||||
private final DatabaseConfig databaseConfig =
|
||||
context.mock(DatabaseConfig.class);
|
||||
private final Editor editor = context.mock(Editor.class);
|
||||
|
||||
private final String encryptedKeyHex = toHexString(getRandomBytes(123));
|
||||
|
||||
@Test
|
||||
public void testDbKeyIsMigratedFromPreferencesToFile() {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(prefs).getString("key", null);
|
||||
will(returnValue(encryptedKeyHex));
|
||||
oneOf(accountManager).storeEncryptedDatabaseKey(encryptedKeyHex);
|
||||
will(returnValue(true));
|
||||
oneOf(prefs).edit();
|
||||
will(returnValue(editor));
|
||||
oneOf(editor).remove("key");
|
||||
will(returnValue(editor));
|
||||
oneOf(editor).commit();
|
||||
will(returnValue(true));
|
||||
}});
|
||||
|
||||
ConfigControllerImpl c = new ConfigControllerImpl(prefs, accountManager,
|
||||
databaseConfig);
|
||||
|
||||
assertEquals(encryptedKeyHex, c.getEncryptedDatabaseKey());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.ImmediateExecutor;
|
||||
import org.jmock.Expectations;
|
||||
@@ -22,12 +19,8 @@ import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
public class PasswordControllerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final SharedPreferences briarPrefs =
|
||||
context.mock(SharedPreferences.class);
|
||||
private final AccountManager accountManager =
|
||||
context.mock(AccountManager.class);
|
||||
private final DatabaseConfig databaseConfig =
|
||||
context.mock(DatabaseConfig.class);
|
||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||
private final PasswordStrengthEstimator estimator =
|
||||
context.mock(PasswordStrengthEstimator.class);
|
||||
@@ -46,8 +39,6 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
|
||||
public void testChangePasswordReturnsTrue() {
|
||||
context.checking(new Expectations() {{
|
||||
// Look up the encrypted DB key
|
||||
oneOf(briarPrefs).getString("key", null);
|
||||
will(returnValue(null));
|
||||
oneOf(accountManager).getEncryptedDatabaseKey();
|
||||
will(returnValue(oldEncryptedKeyHex));
|
||||
// Decrypt and re-encrypt the key
|
||||
@@ -60,9 +51,8 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(true));
|
||||
}});
|
||||
|
||||
PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs,
|
||||
accountManager, databaseConfig, cryptoExecutor, crypto,
|
||||
estimator);
|
||||
PasswordControllerImpl p = new PasswordControllerImpl(accountManager,
|
||||
cryptoExecutor, crypto, estimator);
|
||||
|
||||
AtomicBoolean capturedResult = new AtomicBoolean(false);
|
||||
p.changePassword(oldPassword, newPassword, capturedResult::set);
|
||||
@@ -73,8 +63,6 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
|
||||
public void testChangePasswordReturnsFalseIfOldPasswordIsWrong() {
|
||||
context.checking(new Expectations() {{
|
||||
// Look up the encrypted DB key
|
||||
oneOf(briarPrefs).getString("key", null);
|
||||
will(returnValue(null));
|
||||
oneOf(accountManager).getEncryptedDatabaseKey();
|
||||
will(returnValue(oldEncryptedKeyHex));
|
||||
// Try to decrypt the key - the password is wrong
|
||||
@@ -82,9 +70,8 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(null));
|
||||
}});
|
||||
|
||||
PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs,
|
||||
accountManager, databaseConfig, cryptoExecutor, crypto,
|
||||
estimator);
|
||||
PasswordControllerImpl p = new PasswordControllerImpl(accountManager,
|
||||
cryptoExecutor, crypto, estimator);
|
||||
|
||||
AtomicBoolean capturedResult = new AtomicBoolean(true);
|
||||
p.changePassword(oldPassword, newPassword, capturedResult::set);
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
@@ -28,12 +25,8 @@ import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
public class SetupControllerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final SharedPreferences briarPrefs =
|
||||
context.mock(SharedPreferences.class);
|
||||
private final AccountManager accountManager =
|
||||
context.mock(AccountManager.class);
|
||||
private final DatabaseConfig databaseConfig =
|
||||
context.mock(DatabaseConfig.class);
|
||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||
private final PasswordStrengthEstimator estimator =
|
||||
context.mock(PasswordStrengthEstimator.class);
|
||||
@@ -84,9 +77,8 @@ public class SetupControllerImplTest extends BrambleMockTestCase {
|
||||
oneOf(accountManager).setDatabaseKey(key);
|
||||
}});
|
||||
|
||||
SetupControllerImpl s = new SetupControllerImpl(briarPrefs,
|
||||
accountManager, databaseConfig, cryptoExecutor, crypto,
|
||||
estimator, identityManager);
|
||||
SetupControllerImpl s = new SetupControllerImpl(accountManager,
|
||||
cryptoExecutor, crypto, estimator, identityManager);
|
||||
s.setSetupActivity(setupActivity);
|
||||
|
||||
AtomicBoolean called = new AtomicBoolean(false);
|
||||
|
||||
Reference in New Issue
Block a user