Pin : some clean up and fixes
This commit is contained in:
parent
c635281954
commit
a896b42fa2
5 changed files with 33 additions and 15 deletions
|
|
@ -22,6 +22,8 @@ import com.squareup.anvil.annotations.ContributesBinding
|
|||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import javax.inject.Inject
|
||||
|
|
@ -38,6 +40,7 @@ class SharedPreferencesPinCodeStore @Inject constructor(
|
|||
) : PinCodeStore {
|
||||
|
||||
private val listeners = CopyOnWriteArrayList<PinCodeStore.Listener>()
|
||||
private val mutex = Mutex()
|
||||
|
||||
override suspend fun getEncryptedCode(): String? = withContext(dispatchers.io) {
|
||||
sharedPreferences.getString(ENCODED_PIN_CODE_KEY, null)
|
||||
|
|
@ -68,20 +71,26 @@ class SharedPreferencesPinCodeStore @Inject constructor(
|
|||
}
|
||||
|
||||
override suspend fun getRemainingPinCodeAttemptsNumber(): Int = withContext(dispatchers.io) {
|
||||
sharedPreferences.getInt(REMAINING_PIN_CODE_ATTEMPTS_KEY, MAX_PIN_CODE_ATTEMPTS_NUMBER_BEFORE_LOGOUT)
|
||||
mutex.withLock {
|
||||
sharedPreferences.getInt(REMAINING_PIN_CODE_ATTEMPTS_KEY, MAX_PIN_CODE_ATTEMPTS_NUMBER_BEFORE_LOGOUT)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun onWrongPin(): Int = withContext(dispatchers.io) {
|
||||
val remaining = getRemainingPinCodeAttemptsNumber() - 1
|
||||
sharedPreferences.edit {
|
||||
putInt(REMAINING_PIN_CODE_ATTEMPTS_KEY, remaining)
|
||||
mutex.withLock {
|
||||
val remaining = getRemainingPinCodeAttemptsNumber() - 1
|
||||
sharedPreferences.edit {
|
||||
putInt(REMAINING_PIN_CODE_ATTEMPTS_KEY, remaining)
|
||||
}
|
||||
remaining
|
||||
}
|
||||
remaining
|
||||
}
|
||||
|
||||
override suspend fun resetCounter() = withContext(dispatchers.io) {
|
||||
sharedPreferences.edit {
|
||||
remove(REMAINING_PIN_CODE_ATTEMPTS_KEY)
|
||||
mutex.withLock {
|
||||
sharedPreferences.edit {
|
||||
remove(REMAINING_PIN_CODE_ATTEMPTS_KEY)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class DefaultPinCodeManagerTest {
|
|||
private val pinCodeManager = DefaultPinCodeManager(secretKeyProvider, encryptionDecryptionService, pinCodeStore)
|
||||
|
||||
@Test
|
||||
fun given_a_pin_code_when_create_and_delete_assert_no_pin_code_left() = runTest {
|
||||
fun `given a pin code when create and delete assert no pin code left`() = runTest {
|
||||
pinCodeManager.createPinCode("1234")
|
||||
assertThat(pinCodeManager.isPinCodeAvailable()).isTrue()
|
||||
pinCodeManager.deletePinCode()
|
||||
|
|
@ -39,14 +39,14 @@ class DefaultPinCodeManagerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun given_a_pin_code_when_create_and_verify_with_the_same_pin_succeed() = runTest {
|
||||
fun `given a pin code when create and verify with the same pin succeed`() = runTest {
|
||||
val pinCode = "1234"
|
||||
pinCodeManager.createPinCode(pinCode)
|
||||
assertThat(pinCodeManager.verifyPinCode(pinCode)).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given_a_pin_code_when_create_and_verify_with_a_different_pin_fails() = runTest {
|
||||
fun `given a pin code when create and verify with a different pin fails`() = runTest {
|
||||
pinCodeManager.createPinCode("1234")
|
||||
assertThat(pinCodeManager.verifyPinCode("1235")).isFalse()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,12 @@
|
|||
|
||||
package io.element.android.features.lockscreen.impl.pin.storage
|
||||
|
||||
private const val DEFAULT_REMAINING_ATTEMPTS = 3
|
||||
|
||||
class InMemoryPinCodeStore : PinCodeStore {
|
||||
|
||||
private var pinCode: String? = null
|
||||
private var remainingAttempts: Int = 3
|
||||
private var remainingAttempts: Int = DEFAULT_REMAINING_ATTEMPTS
|
||||
|
||||
override suspend fun getRemainingPinCodeAttemptsNumber(): Int {
|
||||
return remainingAttempts
|
||||
|
|
@ -30,7 +32,7 @@ class InMemoryPinCodeStore : PinCodeStore {
|
|||
}
|
||||
|
||||
override suspend fun resetCounter() {
|
||||
remainingAttempts = 3
|
||||
remainingAttempts = DEFAULT_REMAINING_ATTEMPTS
|
||||
}
|
||||
|
||||
override fun addListener(listener: PinCodeStore.Listener) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue