Clear single sign on cache file when removing account

This commit is contained in:
Sylvain Berfini 2024-11-06 12:23:32 +01:00
parent 5c8e4bcc22
commit cda5deb18d
3 changed files with 22 additions and 7 deletions

View file

@ -297,6 +297,10 @@ class CorePreferences @UiThread constructor(private val context: Context) {
val vfsCachePath: String
get() = context.cacheDir.absolutePath + "/evfs/"
@get:AnyThread
val ssoCacheFile: String
get() = context.filesDir.absolutePath + "/auth_state.json"
@UiThread
fun copyAssetsFromPackage() {
copy("linphonerc_default", configPath)

View file

@ -22,7 +22,10 @@ package org.linphone.ui.main.settings.viewmodel
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import java.io.File
import java.util.Locale
import kotlinx.coroutines.launch
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
@ -41,6 +44,7 @@ import org.linphone.ui.main.model.AccountModel
import org.linphone.ui.main.model.isEndToEndEncryptionMandatory
import org.linphone.ui.main.settings.model.AccountDeviceModel
import org.linphone.utils.Event
import org.linphone.utils.FileUtils
class AccountProfileViewModel @UiThread constructor() : GenericViewModel() {
companion object {
@ -261,6 +265,16 @@ class AccountProfileViewModel @UiThread constructor() : GenericViewModel() {
val authInfo = account.findAuthInfo()
if (authInfo != null) {
Log.i("$TAG Found auth info for account, removing it")
if (authInfo.password.isNullOrEmpty() && authInfo.ha1.isNullOrEmpty() && authInfo.accessToken != null) {
Log.i("$TAG Auth info was using bearer token instead of password")
val ssoCache = File(corePreferences.ssoCacheFile)
if (ssoCache.exists()) {
Log.i("$TAG Found auth_state.json file, deleting it")
viewModelScope.launch {
FileUtils.deleteFile(ssoCache.absolutePath)
}
}
}
core.removeAuthInfo(authInfo)
} else {
Log.w("$TAG Failed to find matching auth info for account")

View file

@ -157,10 +157,7 @@ class SingleSignOnViewModel : GenericViewModel() {
authService = AuthorizationService(coreContext.context)
}
val authStateJsonFile = File(
coreContext.context.filesDir.absolutePath,
"auth_state.json"
)
val authStateJsonFile = File(corePreferences.ssoCacheFile)
Log.i("$TAG Starting refresh token request")
try {
authService.performTokenRequest(
@ -230,7 +227,7 @@ class SingleSignOnViewModel : GenericViewModel() {
@UiThread
private suspend fun getAuthState(): AuthState {
val file = File(coreContext.context.filesDir.absolutePath, "auth_state.json")
val file = File(corePreferences.ssoCacheFile)
if (file.exists()) {
Log.i("$TAG Auth state file found, trying to read it")
val content = FileUtils.readFile(file)
@ -256,7 +253,7 @@ class SingleSignOnViewModel : GenericViewModel() {
Log.i("$TAG Trying to save serialized authState as JSON file")
val data = authState.jsonSerializeString()
Log.d("$TAG Date to save is [$data]")
val file = File(coreContext.context.filesDir.absolutePath, "auth_state.json")
val file = File(corePreferences.ssoCacheFile)
viewModelScope.launch {
if (FileUtils.dumpStringToFile(data, file)) {
Log.i("$TAG Service configuration saved as JSON as [${file.absolutePath}]")
@ -297,7 +294,7 @@ class SingleSignOnViewModel : GenericViewModel() {
}
} else {
Log.w("$TAG Access token expiration info not available")
val file = File(coreContext.context.filesDir.absolutePath, "auth_state.json")
val file = File(corePreferences.ssoCacheFile)
viewModelScope.launch {
FileUtils.deleteFile(file.absolutePath)
singleSignOn()