Skip to content

Commit c8ebf54

Browse files
committed
refactor: Convert PrefsStorageBase to Kotlin & use DataStorePrefsStorage
1 parent 5039e01 commit c8ebf54

File tree

4 files changed

+65
-103
lines changed

4 files changed

+65
-103
lines changed

android/src/main/java/com/oblador/keychain/KeychainModule.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
127127
private val cipherStorageMap: MutableMap<String, CipherStorage> = HashMap()
128128

129129
/** Shared preferences storage. */
130-
private val prefsStorage: PrefsStorage
130+
private val prefsStorage: PrefsStorageBase
131131

132132
/** Launches a coroutine to perform non-blocking UI operations */
133133
private val coroutineScope = CoroutineScope(Dispatchers.Default)
@@ -136,7 +136,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
136136
// region Initialization
137137
/** Default constructor. */
138138
init {
139-
prefsStorage = PrefsStorage(reactContext)
139+
prefsStorage = DataStorePrefsStorage(reactContext)
140140
addCipherStorageToMap(CipherStorageFacebookConceal(reactContext))
141141
addCipherStorageToMap(CipherStorageKeystoreAesCbc(reactContext))
142142

@@ -457,7 +457,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
457457
private fun decryptCredentials(
458458
alias: String,
459459
current: CipherStorage,
460-
resultSet: PrefsStorage.ResultSet,
460+
resultSet: PrefsStorageBase.ResultSet,
461461
@Rules rules: String,
462462
promptInfo: PromptInfo
463463
): DecryptionResult {
@@ -496,7 +496,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
496496
private fun decryptToResult(
497497
alias: String,
498498
storage: CipherStorage,
499-
resultSet: PrefsStorage.ResultSet,
499+
resultSet: PrefsStorageBase.ResultSet,
500500
promptInfo: PromptInfo
501501
): DecryptionResult {
502502
val handler = getInteractiveHandler(storage, promptInfo)

android/src/main/java/com/oblador/keychain/PrefsStorage.kt

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import android.content.SharedPreferences
55
import android.util.Base64
66
import com.facebook.react.bridge.ReactApplicationContext
77
import com.oblador.keychain.KeychainModule.KnownCiphers
8-
import com.oblador.keychain.cipherStorage.CipherStorage.CipherResult
8+
import com.oblador.keychain.PrefsStorageBase.Companion.KEYCHAIN_DATA
9+
import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForCipherStorage
10+
import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForPassword
11+
import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForUsername
12+
import com.oblador.keychain.PrefsStorageBase.Companion.isKeyForCipherStorage
13+
import com.oblador.keychain.PrefsStorageBase.ResultSet
914
import com.oblador.keychain.cipherStorage.CipherStorage.EncryptionResult
1015

1116
@Suppress("unused")
12-
class PrefsStorage(reactContext: ReactApplicationContext) {
13-
class ResultSet(
14-
@JvmField @field:KnownCiphers @param:KnownCiphers val cipherStorageName: String,
15-
usernameBytes: ByteArray?,
16-
passwordBytes: ByteArray?
17-
) : CipherResult<ByteArray?>(usernameBytes, passwordBytes)
17+
open class PrefsStorage(reactContext: ReactApplicationContext) : PrefsStorageBase {
1818

1919
private val prefs: SharedPreferences
2020

2121
init {
2222
prefs = reactContext.getSharedPreferences(KEYCHAIN_DATA, Context.MODE_PRIVATE)
2323
}
2424

25-
fun getEncryptedEntry(service: String): ResultSet? {
25+
override fun getEncryptedEntry(service: String): ResultSet? {
2626
val bytesForUsername = getBytesForUsername(service)
2727
val bytesForPassword = getBytesForPassword(service)
2828
var cipherStorageName = getCipherStorageName(service)
@@ -40,14 +40,14 @@ class PrefsStorage(reactContext: ReactApplicationContext) {
4040
return ResultSet(cipherStorageName, bytesForUsername, bytesForPassword)
4141
}
4242

43-
fun removeEntry(service: String) {
43+
override fun removeEntry(service: String) {
4444
val keyForUsername = getKeyForUsername(service)
4545
val keyForPassword = getKeyForPassword(service)
4646
val keyForCipherStorage = getKeyForCipherStorage(service)
4747
prefs.edit().remove(keyForUsername).remove(keyForPassword).remove(keyForCipherStorage).apply()
4848
}
4949

50-
fun storeEncryptedEntry(service: String, encryptionResult: EncryptionResult) {
50+
override fun storeEncryptedEntry(service: String, encryptionResult: EncryptionResult) {
5151
val keyForUsername = getKeyForUsername(service)
5252
val keyForPassword = getKeyForPassword(service)
5353
val keyForCipherStorage = getKeyForCipherStorage(service)
@@ -59,16 +59,7 @@ class PrefsStorage(reactContext: ReactApplicationContext) {
5959
.apply()
6060
}
6161

62-
val usedCipherNames: Set<String?>
63-
/**
64-
* List all types of cipher which are involved in en/decryption of the data stored herein.
65-
*
66-
* A cipher type is stored together with the datum upon encryption so the datum can later be
67-
* decrypted using correct cipher. This way, a [PrefsStorage] can involve different ciphers for
68-
* different data. This method returns all ciphers involved with this storage.
69-
*
70-
* @return set of cipher names
71-
*/
62+
override val usedCipherNames: Set<String?>
7263
get() {
7364
val result: MutableSet<String?> = HashSet()
7465
val keys: Set<String> = prefs.all.keys
@@ -102,24 +93,4 @@ class PrefsStorage(reactContext: ReactApplicationContext) {
10293
Base64.decode(value, Base64.DEFAULT)
10394
} else null
10495
}
105-
106-
companion object {
107-
const val KEYCHAIN_DATA = "RN_KEYCHAIN"
108-
109-
fun getKeyForUsername(service: String): String {
110-
return "$service:u"
111-
}
112-
113-
fun getKeyForPassword(service: String): String {
114-
return "$service:p"
115-
}
116-
117-
fun getKeyForCipherStorage(service: String): String {
118-
return "$service:c"
119-
}
120-
121-
fun isKeyForCipherStorage(key: String): Boolean {
122-
return key.endsWith(":c")
123-
}
124-
}
12596
}

android/src/main/java/com/oblador/keychain/PrefsStorageBase.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.oblador.keychain
2+
3+
import com.oblador.keychain.KeychainModule.KnownCiphers
4+
import com.oblador.keychain.cipherStorage.CipherStorage.CipherResult
5+
import com.oblador.keychain.cipherStorage.CipherStorage.EncryptionResult
6+
7+
interface PrefsStorageBase {
8+
class ResultSet(
9+
@JvmField @field:KnownCiphers @param:KnownCiphers val cipherStorageName: String,
10+
usernameBytes: ByteArray?,
11+
passwordBytes: ByteArray?,
12+
) : CipherResult<ByteArray?>(usernameBytes, passwordBytes)
13+
14+
fun getEncryptedEntry(service: String): ResultSet?
15+
16+
fun removeEntry(service: String)
17+
18+
fun storeEncryptedEntry(service: String, encryptionResult: EncryptionResult)
19+
20+
/**
21+
* List all types of cipher which are involved in en/decryption of the data stored herein.
22+
*
23+
* A cipher type is stored together with the datum upon encryption so the datum can later be
24+
* decrypted using correct cipher. This way, a [PrefsStorageBase] can involve different ciphers
25+
* for different data. This method returns all ciphers involved with this storage.
26+
*
27+
* @return set of cipher names
28+
*/
29+
val usedCipherNames: Set<String?>
30+
31+
companion object {
32+
const val KEYCHAIN_DATA = "RN_KEYCHAIN"
33+
34+
fun getKeyForUsername(service: String): String {
35+
return "$service:u"
36+
}
37+
38+
fun getKeyForPassword(service: String): String {
39+
return "$service:p"
40+
}
41+
42+
fun getKeyForCipherStorage(service: String): String {
43+
return "$service:c"
44+
}
45+
46+
fun isKeyForCipherStorage(key: String): Boolean {
47+
return key.endsWith(":c")
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)