mirror of
https://github.com/bitwarden/android.git
synced 2025-03-15 10:48:47 +03:00
Add persistence for a user's "last active time" (#601)
This commit is contained in:
parent
8efd9d2c8a
commit
5d73f97831
4 changed files with 103 additions and 0 deletions
|
@ -31,6 +31,25 @@ interface AuthDiskSource {
|
|||
*/
|
||||
val userStateFlow: Flow<UserStateJson?>
|
||||
|
||||
/**
|
||||
* Retrieves the "last active time" for the given [userId], in milliseconds.
|
||||
*
|
||||
* This time is intended to be derived from a call to
|
||||
* [SystemClock.elapsedRealtime()](https://developer.android.com/reference/android/os/SystemClock#elapsedRealtime())
|
||||
*/
|
||||
fun getLastActiveTimeMillis(userId: String): Long?
|
||||
|
||||
/**
|
||||
* Stores the [lastActiveTimeMillis] for the given [userId].
|
||||
*
|
||||
* This time is intended to be derived from a call to
|
||||
* [SystemClock.elapsedRealtime()](https://developer.android.com/reference/android/os/SystemClock#elapsedRealtime())
|
||||
*/
|
||||
fun storeLastActiveTimeMillis(
|
||||
userId: String,
|
||||
lastActiveTimeMillis: Long?,
|
||||
)
|
||||
|
||||
/**
|
||||
* Retrieves a user key using a [userId].
|
||||
*/
|
||||
|
|
|
@ -19,6 +19,7 @@ private const val USER_AUTO_UNLOCK_KEY_KEY = "$ENCRYPTED_BASE_KEY:userKeyAutoUnl
|
|||
private const val UNIQUE_APP_ID_KEY = "$BASE_KEY:appId"
|
||||
private const val REMEMBERED_EMAIL_ADDRESS_KEY = "$BASE_KEY:rememberedEmail"
|
||||
private const val STATE_KEY = "$BASE_KEY:state"
|
||||
private const val LAST_ACTIVE_TIME_KEY = "$BASE_KEY:lastActiveTime"
|
||||
private const val MASTER_KEY_ENCRYPTION_USER_KEY = "$BASE_KEY:masterKeyEncryptedUserKey"
|
||||
private const val MASTER_KEY_ENCRYPTION_PRIVATE_KEY = "$BASE_KEY:encPrivateKey"
|
||||
private const val ORGANIZATIONS_KEY = "$BASE_KEY:organizations"
|
||||
|
@ -66,6 +67,19 @@ class AuthDiskSourceImpl(
|
|||
get() = mutableUserStateFlow
|
||||
.onSubscription { emit(userState) }
|
||||
|
||||
override fun getLastActiveTimeMillis(userId: String): Long? =
|
||||
getLong(key = "${LAST_ACTIVE_TIME_KEY}_$userId")
|
||||
|
||||
override fun storeLastActiveTimeMillis(
|
||||
userId: String,
|
||||
lastActiveTimeMillis: Long?,
|
||||
) {
|
||||
putLong(
|
||||
key = "${LAST_ACTIVE_TIME_KEY}_$userId",
|
||||
value = lastActiveTimeMillis,
|
||||
)
|
||||
}
|
||||
|
||||
private val mutableUserStateFlow = bufferedMutableSharedFlow<UserStateJson?>(replay = 1)
|
||||
|
||||
override fun getUserKey(userId: String): String? =
|
||||
|
|
|
@ -16,7 +16,9 @@ import kotlinx.coroutines.test.runTest
|
|||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.encodeToJsonElement
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class AuthDiskSourceTest {
|
||||
|
@ -116,6 +118,63 @@ class AuthDiskSourceTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getLastActiveTimeMillis should pull from SharedPreferences`() {
|
||||
val lastActiveTimeBaseKey = "bwPreferencesStorage:lastActiveTime"
|
||||
val mockUserId = "mockUserId"
|
||||
val mockLastActiveTime = 123456789L
|
||||
fakeSharedPreferences
|
||||
.edit()
|
||||
.putLong(
|
||||
"${lastActiveTimeBaseKey}_$mockUserId",
|
||||
mockLastActiveTime,
|
||||
)
|
||||
.apply()
|
||||
val actual = authDiskSource.getLastActiveTimeMillis(userId = mockUserId)
|
||||
assertEquals(
|
||||
mockLastActiveTime,
|
||||
actual,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `storeLastActiveTimeMillis for non-null values should update SharedPreferences`() {
|
||||
val lastActiveTimeBaseKey = "bwPreferencesStorage:lastActiveTime"
|
||||
val mockUserId = "mockUserId"
|
||||
val mockLastActiveTime = 123456789L
|
||||
authDiskSource.storeLastActiveTimeMillis(
|
||||
userId = mockUserId,
|
||||
lastActiveTimeMillis = mockLastActiveTime,
|
||||
)
|
||||
val actual = fakeSharedPreferences
|
||||
.getLong(
|
||||
"${lastActiveTimeBaseKey}_$mockUserId",
|
||||
0L,
|
||||
)
|
||||
assertEquals(
|
||||
mockLastActiveTime,
|
||||
actual,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `storeLastActiveTimeMillis for null values should clear SharedPreferences`() {
|
||||
val lastActiveTimeBaseKey = "bwPreferencesStorage:lastActiveTime"
|
||||
val mockUserId = "mockUserId"
|
||||
val mockLastActiveTime = 123456789L
|
||||
val lastActiveTimeKey = "${lastActiveTimeBaseKey}_$mockUserId"
|
||||
fakeSharedPreferences
|
||||
.edit()
|
||||
.putLong(lastActiveTimeKey, mockLastActiveTime)
|
||||
.apply()
|
||||
assertTrue(fakeSharedPreferences.contains(lastActiveTimeKey))
|
||||
authDiskSource.storeLastActiveTimeMillis(
|
||||
userId = mockUserId,
|
||||
lastActiveTimeMillis = null,
|
||||
)
|
||||
assertFalse(fakeSharedPreferences.contains(lastActiveTimeKey))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getUserKey should pull from SharedPreferences`() {
|
||||
val userKeyBaseKey = "bwPreferencesStorage:masterKeyEncryptedUserKey"
|
||||
|
|
|
@ -19,6 +19,7 @@ class FakeAuthDiskSource : AuthDiskSource {
|
|||
mutableMapOf<String, MutableSharedFlow<List<SyncResponseJson.Profile.Organization>?>>()
|
||||
private val mutableUserStateFlow = bufferedMutableSharedFlow<UserStateJson?>(replay = 1)
|
||||
|
||||
private val storedLastActiveTimeMillis = mutableMapOf<String, Long?>()
|
||||
private val storedUserKeys = mutableMapOf<String, String?>()
|
||||
private val storedPrivateKeys = mutableMapOf<String, String?>()
|
||||
private val storedUserAutoUnlockKeys = mutableMapOf<String, String?>()
|
||||
|
@ -35,6 +36,16 @@ class FakeAuthDiskSource : AuthDiskSource {
|
|||
override val userStateFlow: Flow<UserStateJson?>
|
||||
get() = mutableUserStateFlow.onSubscription { emit(userState) }
|
||||
|
||||
override fun getLastActiveTimeMillis(userId: String): Long? =
|
||||
storedLastActiveTimeMillis[userId]
|
||||
|
||||
override fun storeLastActiveTimeMillis(
|
||||
userId: String,
|
||||
lastActiveTimeMillis: Long?,
|
||||
) {
|
||||
storedLastActiveTimeMillis[userId] = lastActiveTimeMillis
|
||||
}
|
||||
|
||||
override fun getUserKey(userId: String): String? = storedUserKeys[userId]
|
||||
|
||||
override fun storeUserKey(userId: String, userKey: String?) {
|
||||
|
|
Loading…
Add table
Reference in a new issue