Skip to content

Commit 45775d7

Browse files
authored
Merge pull request #3299 from element-hq/feature/bma/rotateSessionPath
Ensure sessionPath is not reused for different homeserver. Fixes not loading media issue.
2 parents 498f843 + 01ece74 commit 45775d7

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/RustMatrixAuthenticationService.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import javax.inject.Inject
6060
@ContributesBinding(AppScope::class)
6161
@SingleIn(AppScope::class)
6262
class RustMatrixAuthenticationService @Inject constructor(
63-
baseDirectory: File,
63+
private val baseDirectory: File,
6464
private val coroutineDispatchers: CoroutineDispatchers,
6565
private val sessionStore: SessionStore,
6666
private val rustMatrixClientFactory: RustMatrixClientFactory,
@@ -70,10 +70,19 @@ class RustMatrixAuthenticationService @Inject constructor(
7070
// Passphrase which will be used for new sessions. Existing sessions will use the passphrase
7171
// stored in the SessionData.
7272
private val pendingPassphrase = getDatabasePassphrase()
73-
private val sessionPath = File(baseDirectory, UUID.randomUUID().toString()).absolutePath
73+
74+
// Need to keep a copy of the current session path to eventually delete it.
75+
// Ideally it would be possible to get the sessionPath from the Client to avoid doing this.
76+
private var sessionPath: File? = null
7477
private var currentClient: Client? = null
7578
private var currentHomeserver = MutableStateFlow<MatrixHomeServerDetails?>(null)
7679

80+
private fun rotateSessionPath(): File {
81+
sessionPath?.deleteRecursively()
82+
return File(baseDirectory, UUID.randomUUID().toString())
83+
.also { sessionPath = it }
84+
}
85+
7786
override fun loggedInStateFlow(): Flow<LoggedInState> {
7887
return sessionStore.isLoggedIn()
7988
}
@@ -117,8 +126,9 @@ class RustMatrixAuthenticationService @Inject constructor(
117126

118127
override suspend fun setHomeserver(homeserver: String): Result<Unit> =
119128
withContext(coroutineDispatchers.io) {
129+
val emptySessionPath = rotateSessionPath()
120130
runCatching {
121-
val client = getBaseClientBuilder()
131+
val client = getBaseClientBuilder(emptySessionPath)
122132
.serverNameOrHomeserverUrl(homeserver)
123133
.build()
124134
currentClient = client
@@ -135,13 +145,14 @@ class RustMatrixAuthenticationService @Inject constructor(
135145
withContext(coroutineDispatchers.io) {
136146
runCatching {
137147
val client = currentClient ?: error("You need to call `setHomeserver()` first")
148+
val currentSessionPath = sessionPath ?: error("You need to call `setHomeserver()` first")
138149
client.login(username, password, "Element X Android", null)
139150
val sessionData = client.session()
140151
.toSessionData(
141152
isTokenValid = true,
142153
loginType = LoginType.PASSWORD,
143154
passphrase = pendingPassphrase,
144-
sessionPath = sessionPath,
155+
sessionPath = currentSessionPath.absolutePath,
145156
)
146157
clear()
147158
sessionStore.storeData(sessionData)
@@ -185,13 +196,14 @@ class RustMatrixAuthenticationService @Inject constructor(
185196
return withContext(coroutineDispatchers.io) {
186197
runCatching {
187198
val client = currentClient ?: error("You need to call `setHomeserver()` first")
199+
val currentSessionPath = sessionPath ?: error("You need to call `setHomeserver()` first")
188200
val urlForOidcLogin = pendingOidcAuthorizationData ?: error("You need to call `getOidcUrl()` first")
189201
client.loginWithOidcCallback(urlForOidcLogin, callbackUrl)
190202
val sessionData = client.session().toSessionData(
191203
isTokenValid = true,
192204
loginType = LoginType.OIDC,
193205
passphrase = pendingPassphrase,
194-
sessionPath = sessionPath,
206+
sessionPath = currentSessionPath.absolutePath,
195207
)
196208
clear()
197209
pendingOidcAuthorizationData?.close()
@@ -206,9 +218,10 @@ class RustMatrixAuthenticationService @Inject constructor(
206218

207219
override suspend fun loginWithQrCode(qrCodeData: MatrixQrCodeLoginData, progress: (QrCodeLoginStep) -> Unit) =
208220
withContext(coroutineDispatchers.io) {
221+
val emptySessionPath = rotateSessionPath()
209222
runCatching {
210223
val client = rustMatrixClientFactory.getBaseClientBuilder(
211-
sessionPath = sessionPath,
224+
sessionPath = emptySessionPath.absolutePath,
212225
passphrase = pendingPassphrase,
213226
slidingSyncProxy = AuthenticationConfig.SLIDING_SYNC_PROXY_URL,
214227
slidingSync = ClientBuilderSlidingSync.Discovered,
@@ -229,7 +242,7 @@ class RustMatrixAuthenticationService @Inject constructor(
229242
isTokenValid = true,
230243
loginType = LoginType.QR,
231244
passphrase = pendingPassphrase,
232-
sessionPath = sessionPath,
245+
sessionPath = emptySessionPath.absolutePath,
233246
)
234247
sessionStore.storeData(sessionData)
235248
SessionId(sessionData.userId)
@@ -246,11 +259,13 @@ class RustMatrixAuthenticationService @Inject constructor(
246259
}
247260
Timber.e(throwable, "Failed to login with QR code")
248261
}
249-
}
262+
}
250263

251-
private fun getBaseClientBuilder() = rustMatrixClientFactory
264+
private fun getBaseClientBuilder(
265+
sessionPath: File,
266+
) = rustMatrixClientFactory
252267
.getBaseClientBuilder(
253-
sessionPath = sessionPath,
268+
sessionPath = sessionPath.absolutePath,
254269
passphrase = pendingPassphrase,
255270
slidingSyncProxy = AuthenticationConfig.SLIDING_SYNC_PROXY_URL,
256271
slidingSync = ClientBuilderSlidingSync.Discovered,

0 commit comments

Comments
 (0)