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

fix(auth-storage): Thread safety #99

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions data/persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(libs.multiplatform.settings)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.json)
implementation(libs.io.insert.koin.core)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ interface DodoAuthStorage {
/**
* Get the Access token for @param server
*/
fun getAccessToken(server: String): String?
suspend fun getAccessToken(server: String): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ package social.androiddev.common.persistence.localstorage
import com.russhwolf.settings.Settings
import com.russhwolf.settings.get
import com.russhwolf.settings.set
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json

internal class DodoAuthStorageImpl(
private val settings: Settings,
private val json: Json
private val json: Json,
private val lock: Mutex = Mutex()
) : DodoAuthStorage {

override var currentDomain: String?
get() = settings[KEY_DOMAIN_CACHE]
set(value) {
Expand All @@ -31,29 +33,31 @@ internal class DodoAuthStorageImpl(
* The user can set up multiple accounts on their device. So we
* key the AccessToken by the unique server/domain
*/
private var diskCache: Map<String, AccessToken>
private var diskCache: LinkedHashMap<String, AccessToken>
get() {
return settings
.getStringOrNull(KEY_ACCESS_TOKENS_CACHE)
?.let { str ->
json.decodeFromString(ListSerializer(AccessToken.serializer()), str)
.associateBy { it.server }
.associateByTo(linkedMapOf()) { it.server }
}
?: mutableMapOf()
?: linkedMapOf()
crocsandcoffee marked this conversation as resolved.
Show resolved Hide resolved
}
set(value) {
val list = value.map { it.value }
settings[KEY_ACCESS_TOKENS_CACHE] =
json.encodeToString(ListSerializer(AccessToken.serializer()), list)
}
private val memCache: LinkedHashMap<String, AccessToken> by lazy { diskCache }

private val memCache: MutableMap<String, AccessToken> by lazy { diskCache.toMutableMap() }

override fun getAccessToken(server: String): String? = memCache[server]?.token
override suspend fun getAccessToken(server: String): String? =
krizzu marked this conversation as resolved.
Show resolved Hide resolved
lock.withLock { memCache[server]?.token }

override suspend fun saveAccessToken(server: String, token: String) {
memCache[server] = AccessToken(token = token, server = server)
diskCache = memCache
lock.withLock {
memCache[server] = AccessToken(token = token, server = server)
diskCache = memCache
}
}

private companion object {
Expand Down