Skip to content

Commit

Permalink
cache token on kmp side
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Nov 20, 2024
1 parent 893a066 commit b317e51
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.rebble.cobble.MainActivity
import io.rebble.cobble.bridges.FlutterBridge
import io.rebble.cobble.bridges.ui.BridgeLifecycleController
import io.rebble.cobble.pigeons.Pigeons
import io.rebble.cobble.shared.datastore.SecureStorage
import io.rebble.cobble.shared.domain.state.CurrentToken
import io.rebble.cobble.shared.ui.nav.Routes
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -14,6 +15,7 @@ import javax.inject.Inject

class KMPApiBridge @Inject constructor(
private val tokenState: MutableStateFlow<CurrentToken>,
private val secureStorage: SecureStorage,
bridgeLifecycleController: BridgeLifecycleController,
private val activity: FlutterMainActivity? = null
): FlutterBridge, Pigeons.KMPApi {
Expand All @@ -24,6 +26,7 @@ class KMPApiBridge @Inject constructor(

override fun updateToken(token: Pigeons.StringWrapper) {
tokenState.value = token.value?.let { CurrentToken.LoggedIn(it) } ?: CurrentToken.LoggedOut
secureStorage.token = token.value
}

override fun openLockerView() {
Expand Down
5 changes: 5 additions & 0 deletions android/app/src/main/kotlin/io/rebble/cobble/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.rebble.cobble.shared.database.dao.NotificationChannelDao
import io.rebble.cobble.shared.database.dao.PersistedNotificationDao
import io.rebble.cobble.shared.datastore.FlutterPreferences
import io.rebble.cobble.shared.datastore.KMPPrefs
import io.rebble.cobble.shared.datastore.SecureStorage
import io.rebble.cobble.shared.domain.calendar.CalendarSync
import io.rebble.cobble.shared.domain.state.CurrentToken
import io.rebble.cobble.shared.errors.GlobalExceptionHandler
Expand Down Expand Up @@ -61,6 +62,10 @@ abstract class AppModule {
return KoinPlatformTools.defaultContext().get().get(named("currentToken"))
}
@Provides
fun provideSecureStorage(): SecureStorage {
return KoinPlatformTools.defaultContext().get().get()
}
@Provides
fun providePersistedNotificationDao(context: Context): PersistedNotificationDao {
return AppDatabase.instance().persistedNotificationDao()
}
Expand Down
2 changes: 2 additions & 0 deletions android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ errorproneVersion = "2.26.1"
rruleVersion = "1.0.3"
spotbugsVersion = "4.8.6"
atomicfu = "0.25.0"
securityCrypto = "1.1.0-alpha06"

protoliteWellKnownTypes = "18.0.0"
room = "2.7.0-alpha11"
Expand Down Expand Up @@ -57,6 +58,7 @@ androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx
androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidxTest" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxTest" }
androidx-test-monitor = { module = "androidx.test:monitor", version.ref = "androidxTest" }
androidx-security-crypto-ktx = { module = "androidx.security:security-crypto-ktx", version.ref = "securityCrypto" }
androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workManagerVersion" }
dagger = { module = "com.google.dagger:dagger", version.ref = "daggerVersion" }
dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref = "daggerVersion" }
Expand Down
2 changes: 2 additions & 0 deletions android/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ kotlin {
implementation(libs.androidx.core.ktx)
implementation(libs.timber)
implementation(libs.rrule)
implementation(libs.androidx.security.crypto.ktx)
implementation(project(":pebblekit_android"))
implementation(project(":speex_codec"))
}
Expand Down Expand Up @@ -106,6 +107,7 @@ android {
}
dependencies {
implementation(libs.protolite.wellknowntypes)
implementation(libs.androidx.security.crypto.ktx)
add("kspCommonMainMetadata", libs.androidx.room.compiler)
add("kspAndroid", libs.androidx.room.compiler)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.rebble.cobble.shared.datastore

import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey


class AndroidSecureStorage(context: Context): SecureStorage() {
private val masterKey: MasterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()

private val sharedPreferences: SharedPreferences = EncryptedSharedPreferences.create(
context,
"secret_shared_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
override fun putString(key: String, value: String?) {
sharedPreferences.edit().putString(key, value).apply()
}

override fun getString(key: String): String? {
return sharedPreferences.getString(key, null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import android.service.notification.StatusBarNotification
import com.benasher44.uuid.Uuid
import io.rebble.cobble.shared.AndroidPlatformContext
import io.rebble.cobble.shared.PlatformContext
import io.rebble.cobble.shared.datastore.AndroidSecureStorage
import io.rebble.cobble.shared.datastore.FlutterPreferences
import io.rebble.cobble.shared.datastore.SecureStorage
import io.rebble.cobble.shared.datastore.createDataStore
import io.rebble.cobble.shared.domain.calendar.AndroidCalendarActionExecutor
import io.rebble.cobble.shared.domain.calendar.PlatformCalendarActionExecutor
Expand Down Expand Up @@ -80,4 +82,5 @@ val androidModule = module {
} else {
factoryOf(::NullDictationService) bind DictationService::class
}
singleOf(::AndroidSecureStorage) bind SecureStorage::class
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.rebble.cobble.shared.datastore

abstract class SecureStorage {
protected abstract fun putString(key: String, value: String?)
protected abstract fun getString(key: String): String?

var token: String?
get() = getString("token")
set(value) {
if (value == null) {
putString("token", null)
} else {
putString("token", value)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.rebble.cobble.shared.di

import io.rebble.cobble.shared.datastore.SecureStorage
import io.rebble.cobble.shared.domain.state.ConnectionState
import io.rebble.cobble.shared.domain.state.CurrentToken
import io.rebble.cobble.shared.domain.state.CurrentToken.LoggedOut.tokenOrNull
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
Expand All @@ -20,6 +22,7 @@ val stateModule = module {
}

single(named("currentToken")) {
MutableStateFlow<CurrentToken>(CurrentToken.LoggedOut)
val token = get<SecureStorage>().token
MutableStateFlow(token?.let { CurrentToken.LoggedIn(token) } ?: CurrentToken.LoggedOut)
} bind StateFlow::class
}

0 comments on commit b317e51

Please sign in to comment.