Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix: users of same team requires clear data #670

Merged
merged 7 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.0.2-SNAPSHOT
VERSION_NAME=3.0.3-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private static void checkPlatformMigrations() {
if (shouldMigrate && StringUtils.isNotBlank(instance.context().userService().getAllSharedPreferences().fetchPioneerUser())) {//Force remote login
Utils.logoutUser(instance.context(), instance.context().applicationContext().getString(R.string.new_db_encryption_version_migration));
}
instance.context().userService().getAllSharedPreferences().migratePassphrase();
}

public static CoreLibrary getInstance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.smartregister.repository;

import android.content.SharedPreferences;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;

import org.apache.commons.lang3.StringUtils;
import org.smartregister.AllConstants;
import org.smartregister.CoreLibrary;
import org.smartregister.util.Log;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -353,12 +356,23 @@ public SharedPreferences getPreferences() {
return preferences;
}

public String getPassphrase(String encryptionParam) {
return preferences.getString(new StringBuffer(ENCRYPTED_PASSPHRASE_KEY).append('_').append(encryptionParam).toString(), null);
public String getPassphrase(String encryptionParam, String username) {
return preferences.getString(new StringBuffer(ENCRYPTED_PASSPHRASE_KEY).append('_').append(encryptionParam).append('_').append(username).toString(), null);
}

public void savePassphrase(String passphrase, String encryptionParam) {
preferences.edit().putString(new StringBuffer(ENCRYPTED_PASSPHRASE_KEY).append('_').append(encryptionParam).toString(), passphrase).commit();
public void savePassphrase(String passphrase, String encryptionParam, String username) {
preferences.edit().putString(new StringBuffer(ENCRYPTED_PASSPHRASE_KEY).append('_').append(encryptionParam).append('_').append(username).toString(), passphrase).commit();
}

/**
* Allows migration of older passphrase so that is linked to pioneer user
**/
public void migratePassphrase() {
String encryptionParam = CoreLibrary.getInstance().getSyncConfiguration().getEncryptionParam().name();
String passphrase = preferences.getString(new StringBuffer(ENCRYPTED_PASSPHRASE_KEY).append('_').append(encryptionParam).toString(), null);
if (passphrase != null) {
savePassphrase(passphrase, encryptionParam, fetchPioneerUser());
}
}

public int getDBEncryptionVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public byte[] getDecryptedPassphraseValue(String userName) {
if (keyStore != null && userName != null) {
try {
KeyStore.PrivateKeyEntry privateKeyEntry = getUserKeyPair(userName);
return decryptString(privateKeyEntry, allSharedPreferences.getPassphrase(CoreLibrary.getInstance().getSyncConfiguration().getEncryptionParam().name()));
return decryptString(privateKeyEntry, allSharedPreferences.getPassphrase(CoreLibrary.getInstance().getSyncConfiguration().getEncryptionParam().name(), userName));
} catch (Exception e) {
Timber.e(e);
}
Expand Down Expand Up @@ -615,7 +615,7 @@ public Bundle saveUserCredentials(String userName, char[] password, LoginRespons
bundle.putString(AccountHelper.INTENT_KEY.ACCOUNT_LOCAL_PASSWORD_SALT, Base64.encodeToString(localAuthHash.getSalt(), Base64.DEFAULT));

//Save db credentials
if (CredentialsHelper.shouldMigrate()) {
if (CredentialsHelper.shouldMigrate() || !username.equals(allSharedPreferences.fetchPioneerUser())) {

byte[] passphrase = DrishtiApplication.getInstance().credentialsProvider().generateDBCredentials(SecurityHelper.toChars(localAuthHash.getPassword()), userInfo);
byte[] oldPassword = allSharedPreferences.getDBEncryptionVersion() == 0 ? getGroupId(username) : DrishtiApplication.getInstance().credentialsProvider().getCredentials(username, CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH);
Expand All @@ -624,7 +624,7 @@ public Bundle saveUserCredentials(String userName, char[] password, LoginRespons
try {

DrishtiApplication.getInstance().getRepository().getReadableDatabase(SecurityHelper.toChars(oldPassword)).changePassword(SecurityHelper.toChars(passphrase));
DrishtiApplication.getInstance().credentialsProvider().saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, encryptString(privateKeyEntry, passphrase));
DrishtiApplication.getInstance().credentialsProvider().saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, encryptString(privateKeyEntry, passphrase), username);

} catch (Exception e) {
Timber.e("Database encryption migration to version %s failed!!! ", BuildConfig.DB_ENCRYPTION_VERSION);
Expand All @@ -633,7 +633,7 @@ public Bundle saveUserCredentials(String userName, char[] password, LoginRespons

} else {

DrishtiApplication.getInstance().credentialsProvider().saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, encryptString(privateKeyEntry, passphrase));
DrishtiApplication.getInstance().credentialsProvider().saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, encryptString(privateKeyEntry, passphrase), username);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public byte[] getCredentials(String username, String type) {
}


public void saveCredentials(String type, String encryptedPassphrase) {
public void saveCredentials(String type, String encryptedPassphrase,String username) {

if (CREDENTIALS_TYPE.DB_AUTH.equals(type)) {

allSharedPreferences.savePassphrase(encryptedPassphrase, CoreLibrary.getInstance().getSyncConfiguration().getEncryptionParam().name());
allSharedPreferences.savePassphrase(encryptedPassphrase, CoreLibrary.getInstance().getSyncConfiguration().getEncryptionParam().name(),username);
allSharedPreferences.setDBEncryptionVersion(BuildConfig.DB_ENCRYPTION_VERSION);

}/* else if (CREDENTIALS_TYPE.LOCAL_AUTH.equals(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.Arrays;

import static org.mockito.ArgumentMatchers.eq;

/**
* Created by ndegwamartin on 22/07/2020.
*/
Expand Down Expand Up @@ -121,12 +123,12 @@ public void testSaveCredentialsUpdatesSharedPreferencesWithEncryptedPassphrase()
Mockito.doReturn(syncConfiguration).when(coreLibrary).getSyncConfiguration();
Mockito.doReturn(SyncFilter.TEAM_ID).when(syncConfiguration).getEncryptionParam();

credentialsHelper.saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, TEST_ENCRYPTED_PWD);
credentialsHelper.saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, TEST_ENCRYPTED_PWD,TEST_USERNAME);

ArgumentCaptor<String> encryptionValueArgCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> encryptionParamArgCaptor = ArgumentCaptor.forClass(String.class);

Mockito.verify(allSharedPreferences, Mockito.times(1)).savePassphrase(encryptionValueArgCaptor.capture(), encryptionParamArgCaptor.capture());
Mockito.verify(allSharedPreferences, Mockito.times(1)).savePassphrase(encryptionValueArgCaptor.capture(), encryptionParamArgCaptor.capture(),eq(TEST_USERNAME));

Assert.assertEquals(TEST_ENCRYPTED_PWD, encryptionValueArgCaptor.getValue());
Assert.assertEquals(SyncFilter.TEAM_ID.name(), encryptionParamArgCaptor.getValue());
Expand All @@ -137,7 +139,7 @@ public void testSaveCredentialsUpdatesSharedPreferencesWithNewDBEncryptionVersio

Mockito.doReturn(SyncFilter.LOCATION_ID).when(syncConfiguration).getEncryptionParam();

credentialsHelper.saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, TEST_ENCRYPTED_PWD);
credentialsHelper.saveCredentials(CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH, TEST_ENCRYPTED_PWD,TEST_USERNAME);

ArgumentCaptor<Integer> encryptionValueArgCaptor = ArgumentCaptor.forClass(Integer.class);

Expand Down