-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extract PrefsStorageBase interface * Add kotlin support * Replace shared preferences with preferences data store * Add support to migrate from sharedPrefs * Test migration from sharedPrefs to dataStore * Remove todo from DataStorePrefsStorage * Remove PrefsStorage in favour of DataStorePrefsStorage * Replace mentions of shared preferences with preferences * Update android gradle plugin * Downgrade kotlin version and add it to implementation deps * fix: React Methods region * refactor: Convert PrefsStorageBase to Kotlin & use DataStorePrefsStorage * fix: Use module coroutineScope in prefs data store * chore: Update datastore-preferences & clean diff * Remove shared preferences * fix: Use Mutex to ensure no concurrent encryption operations * fix: mutex --------- Co-authored-by: Dorian Mazur <[email protected]> Co-authored-by: MazurDorian <[email protected]>
- Loading branch information
1 parent
44ccaa0
commit c9e2322
Showing
5 changed files
with
190 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
android/src/main/java/com/oblador/keychain/DataStorePrefsStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.oblador.keychain | ||
|
||
import android.content.Context | ||
import android.util.Base64 | ||
import androidx.datastore.core.DataMigration | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.SharedPreferencesMigration | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.oblador.keychain.KeychainModule.KnownCiphers | ||
import com.oblador.keychain.PrefsStorageBase.Companion.KEYCHAIN_DATA | ||
import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForCipherStorage | ||
import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForPassword | ||
import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForUsername | ||
import com.oblador.keychain.PrefsStorageBase.Companion.isKeyForCipherStorage | ||
import com.oblador.keychain.PrefsStorageBase.ResultSet | ||
import com.oblador.keychain.cipherStorage.CipherStorage | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
|
||
class DataStorePrefsStorage( | ||
reactContext: ReactApplicationContext, | ||
private val coroutineScope: CoroutineScope, | ||
) : PrefsStorageBase { | ||
|
||
private val Context.prefs: DataStore<Preferences> by preferencesDataStore( | ||
name = KEYCHAIN_DATA, | ||
produceMigrations = ::sharedPreferencesMigration, | ||
scope = coroutineScope, | ||
) | ||
private val prefs: DataStore<Preferences> = reactContext.prefs | ||
private val prefsData: Preferences get() = callSuspendable { prefs.data.first() } | ||
|
||
private fun sharedPreferencesMigration(context: Context): List<DataMigration<Preferences>> { | ||
return listOf(SharedPreferencesMigration(context, KEYCHAIN_DATA)) | ||
} | ||
|
||
override fun getEncryptedEntry(service: String): ResultSet? { | ||
val bytesForUsername = getBytesForUsername(service) | ||
val bytesForPassword = getBytesForPassword(service) | ||
var cipherStorageName = getCipherStorageName(service) | ||
|
||
// in case of wrong password or username | ||
if (bytesForUsername == null || bytesForPassword == null) return null | ||
if (cipherStorageName == null) { | ||
// If the CipherStorage name is not found, we assume it is because the entry was written by an | ||
// older version of this library which used Facebook Conceal, so we default to that. | ||
cipherStorageName = KnownCiphers.FB | ||
} | ||
return ResultSet(cipherStorageName, bytesForUsername, bytesForPassword) | ||
} | ||
|
||
override fun removeEntry(service: String) { | ||
val keyForUsername = stringPreferencesKey(getKeyForUsername(service)) | ||
val keyForPassword = stringPreferencesKey(getKeyForPassword(service)) | ||
val keyForCipherStorage = stringPreferencesKey(getKeyForCipherStorage(service)) | ||
callSuspendable { | ||
prefs.edit { | ||
it.remove(keyForUsername) | ||
it.remove(keyForPassword) | ||
it.remove(keyForCipherStorage) | ||
} | ||
} | ||
} | ||
|
||
override fun storeEncryptedEntry( | ||
service: String, | ||
encryptionResult: CipherStorage.EncryptionResult, | ||
) { | ||
val keyForUsername = stringPreferencesKey(getKeyForUsername(service)) | ||
val keyForPassword = stringPreferencesKey(getKeyForPassword(service)) | ||
val keyForCipherStorage = stringPreferencesKey(getKeyForCipherStorage(service)) | ||
callSuspendable { | ||
prefs.edit { | ||
it[keyForUsername] = Base64.encodeToString(encryptionResult.username, Base64.DEFAULT) | ||
it[keyForPassword] = Base64.encodeToString(encryptionResult.password, Base64.DEFAULT) | ||
it[keyForCipherStorage] = encryptionResult.cipherName | ||
} | ||
} | ||
} | ||
|
||
override val usedCipherNames: Set<String?> | ||
get() { | ||
val result: MutableSet<String?> = HashSet() | ||
val keys = prefsData.asMap().keys.map { it.name } | ||
for (key in keys) { | ||
if (isKeyForCipherStorage(key)) { | ||
val cipher = prefsData[stringPreferencesKey(key)] | ||
result.add(cipher) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
private fun <T> callSuspendable(block: suspend () -> T): T { | ||
return runBlocking(coroutineScope.coroutineContext) { | ||
block() | ||
} | ||
} | ||
|
||
private fun getBytesForUsername(service: String): ByteArray? { | ||
val key = stringPreferencesKey(getKeyForUsername(service)) | ||
return getBytes(key) | ||
} | ||
|
||
private fun getBytesForPassword(service: String): ByteArray? { | ||
val key = stringPreferencesKey(getKeyForPassword(service)) | ||
return getBytes(key) | ||
} | ||
|
||
private fun getCipherStorageName(service: String): String? { | ||
val key = stringPreferencesKey(getKeyForCipherStorage(service)) | ||
return prefsData[key] | ||
} | ||
|
||
private fun getBytes(prefKey: Preferences.Key<String>): ByteArray? { | ||
return prefsData[prefKey]?.let { Base64.decode(it, Base64.DEFAULT) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 0 additions & 125 deletions
125
android/src/main/java/com/oblador/keychain/PrefsStorage.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.