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

feat: add Coroutines to setGenericPassword and getGenericPassword #658

Merged
merged 1 commit into from
Sep 23, 2024
Merged
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
113 changes: 62 additions & 51 deletions android/src/main/java/com/oblador/keychain/KeychainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import com.oblador.keychain.decryptionHandler.DecryptionResultHandlerProvider
import com.oblador.keychain.exceptions.CryptoFailedException
import com.oblador.keychain.exceptions.EmptyParameterException
import com.oblador.keychain.exceptions.KeyStoreAccessException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.launch

@ReactModule(name = KeychainModule.KEYCHAIN_MODULE)
@Suppress("unused")
Expand Down Expand Up @@ -123,6 +126,9 @@ class KeychainModule(reactContext: ReactApplicationContext) :
/** Shared preferences storage. */
private val prefsStorage: PrefsStorage

/** Launches a coroutine to perform non-blocking UI operations */
private val mainCoroutineScope = CoroutineScope(Dispatchers.Default)

// endregion
// region Initialization
/** Default constructor. */
Expand Down Expand Up @@ -184,26 +190,29 @@ class KeychainModule(reactContext: ReactApplicationContext) :
options: ReadableMap?,
promise: Promise
) {
try {
throwIfEmptyLoginPassword(username, password)
val level = getSecurityLevelOrDefault(options)
val storage = getSelectedStorage(options)
throwIfInsufficientLevel(storage, level)
val result = storage.encrypt(alias, username, password, level)
prefsStorage.storeEncryptedEntry(alias, result)
val results = Arguments.createMap()
results.putString(Maps.SERVICE, alias)
results.putString(Maps.STORAGE, storage.getCipherStorageName())
promise.resolve(results)
} catch (e: EmptyParameterException) {
Log.e(KEYCHAIN_MODULE, e.message, e)
promise.reject(Errors.E_EMPTY_PARAMETERS, e)
} catch (e: CryptoFailedException) {
Log.e(KEYCHAIN_MODULE, e.message, e)
promise.reject(Errors.E_CRYPTO_FAILED, e)
} catch (fail: Throwable) {
Log.e(KEYCHAIN_MODULE, fail.message, fail)
promise.reject(Errors.E_UNKNOWN_ERROR, fail)
mainCoroutineScope.launch {
try {
throwIfEmptyLoginPassword(username, password)
val level = getSecurityLevelOrDefault(options)
val storage = getSelectedStorage(options)
throwIfInsufficientLevel(storage, level)

val result = storage.encrypt(alias, username, password, level)
prefsStorage.storeEncryptedEntry(alias, result)
val results = Arguments.createMap()
results.putString(Maps.SERVICE, alias)
results.putString(Maps.STORAGE, storage.getCipherStorageName())
promise.resolve(results)
} catch (e: EmptyParameterException) {
Log.e(KEYCHAIN_MODULE, e.message, e)
promise.reject(Errors.E_EMPTY_PARAMETERS, e)
} catch (e: CryptoFailedException) {
Log.e(KEYCHAIN_MODULE, e.message, e)
promise.reject(Errors.E_CRYPTO_FAILED, e)
} catch (fail: Throwable) {
Log.e(KEYCHAIN_MODULE, fail.message, fail)
promise.reject(Errors.E_UNKNOWN_ERROR, fail)
}
}
}

Expand Down Expand Up @@ -237,21 +246,22 @@ class KeychainModule(reactContext: ReactApplicationContext) :
}

private fun getGenericPassword(alias: String, options: ReadableMap?, promise: Promise) {
try {
val resultSet = prefsStorage.getEncryptedEntry(alias)
if (resultSet == null) {
Log.e(KEYCHAIN_MODULE, "No entry found for service: $alias")
promise.resolve(false)
return
}
val storageName = resultSet.cipherStorageName
val rules = getSecurityRulesOrDefault(options)
val promptInfo = getPromptInfo(options)
var cipher: CipherStorage? = null

// Only check for upgradable ciphers for FacebookConseal as that
// is the only cipher that can be upgraded
cipher =
mainCoroutineScope.launch {
try {
val resultSet = prefsStorage.getEncryptedEntry(alias)
if (resultSet == null) {
Log.e(KEYCHAIN_MODULE, "No entry found for service: $alias")
promise.resolve(false)
return@launch
}
val storageName = resultSet.cipherStorageName
val rules = getSecurityRulesOrDefault(options)
val promptInfo = getPromptInfo(options)
var cipher: CipherStorage? = null

// Only check for upgradable ciphers for FacebookConseal as that
// is the only cipher that can be upgraded
cipher =
if (rules == Rules.AUTOMATIC_UPGRADE && storageName == KnownCiphers.FB) {
// get the best storage
val accessControl = getAccessControlOrDefault(options)
Expand All @@ -260,22 +270,23 @@ class KeychainModule(reactContext: ReactApplicationContext) :
} else {
getCipherStorageByName(storageName)
}
val decryptionResult = decryptCredentials(alias, cipher!!, resultSet, rules, promptInfo)
val credentials = Arguments.createMap()
credentials.putString(Maps.SERVICE, alias)
credentials.putString(Maps.USERNAME, decryptionResult.username)
credentials.putString(Maps.PASSWORD, decryptionResult.password)
credentials.putString(Maps.STORAGE, cipher.getCipherStorageName())
promise.resolve(credentials)
} catch (e: KeyStoreAccessException) {
Log.e(KEYCHAIN_MODULE, e.message!!)
promise.reject(Errors.E_KEYSTORE_ACCESS_ERROR, e)
} catch (e: CryptoFailedException) {
Log.e(KEYCHAIN_MODULE, e.message!!)
promise.reject(Errors.E_CRYPTO_FAILED, e)
} catch (fail: Throwable) {
Log.e(KEYCHAIN_MODULE, fail.message, fail)
promise.reject(Errors.E_UNKNOWN_ERROR, fail)
val decryptionResult = decryptCredentials(alias, cipher!!, resultSet, rules, promptInfo)
val credentials = Arguments.createMap()
credentials.putString(Maps.SERVICE, alias)
credentials.putString(Maps.USERNAME, decryptionResult.username)
credentials.putString(Maps.PASSWORD, decryptionResult.password)
credentials.putString(Maps.STORAGE, cipher.getCipherStorageName())
promise.resolve(credentials)
} catch (e: KeyStoreAccessException) {
Log.e(KEYCHAIN_MODULE, e.message!!)
promise.reject(Errors.E_KEYSTORE_ACCESS_ERROR, e)
} catch (e: CryptoFailedException) {
Log.e(KEYCHAIN_MODULE, e.message!!)
promise.reject(Errors.E_CRYPTO_FAILED, e)
} catch (fail: Throwable) {
Log.e(KEYCHAIN_MODULE, fail.message, fail)
promise.reject(Errors.E_UNKNOWN_ERROR, fail)
}
}
}

Expand Down