@@ -60,7 +60,7 @@ import javax.inject.Inject
60
60
@ContributesBinding(AppScope ::class )
61
61
@SingleIn(AppScope ::class )
62
62
class RustMatrixAuthenticationService @Inject constructor(
63
- baseDirectory : File ,
63
+ private val baseDirectory : File ,
64
64
private val coroutineDispatchers : CoroutineDispatchers ,
65
65
private val sessionStore : SessionStore ,
66
66
private val rustMatrixClientFactory : RustMatrixClientFactory ,
@@ -70,10 +70,19 @@ class RustMatrixAuthenticationService @Inject constructor(
70
70
// Passphrase which will be used for new sessions. Existing sessions will use the passphrase
71
71
// stored in the SessionData.
72
72
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
74
77
private var currentClient: Client ? = null
75
78
private var currentHomeserver = MutableStateFlow <MatrixHomeServerDetails ?>(null )
76
79
80
+ private fun rotateSessionPath (): File {
81
+ sessionPath?.deleteRecursively()
82
+ return File (baseDirectory, UUID .randomUUID().toString())
83
+ .also { sessionPath = it }
84
+ }
85
+
77
86
override fun loggedInStateFlow (): Flow <LoggedInState > {
78
87
return sessionStore.isLoggedIn()
79
88
}
@@ -117,8 +126,9 @@ class RustMatrixAuthenticationService @Inject constructor(
117
126
118
127
override suspend fun setHomeserver (homeserver : String ): Result <Unit > =
119
128
withContext(coroutineDispatchers.io) {
129
+ val emptySessionPath = rotateSessionPath()
120
130
runCatching {
121
- val client = getBaseClientBuilder()
131
+ val client = getBaseClientBuilder(emptySessionPath )
122
132
.serverNameOrHomeserverUrl(homeserver)
123
133
.build()
124
134
currentClient = client
@@ -135,13 +145,14 @@ class RustMatrixAuthenticationService @Inject constructor(
135
145
withContext(coroutineDispatchers.io) {
136
146
runCatching {
137
147
val client = currentClient ? : error(" You need to call `setHomeserver()` first" )
148
+ val currentSessionPath = sessionPath ? : error(" You need to call `setHomeserver()` first" )
138
149
client.login(username, password, " Element X Android" , null )
139
150
val sessionData = client.session()
140
151
.toSessionData(
141
152
isTokenValid = true ,
142
153
loginType = LoginType .PASSWORD ,
143
154
passphrase = pendingPassphrase,
144
- sessionPath = sessionPath ,
155
+ sessionPath = currentSessionPath.absolutePath ,
145
156
)
146
157
clear()
147
158
sessionStore.storeData(sessionData)
@@ -185,13 +196,14 @@ class RustMatrixAuthenticationService @Inject constructor(
185
196
return withContext(coroutineDispatchers.io) {
186
197
runCatching {
187
198
val client = currentClient ? : error(" You need to call `setHomeserver()` first" )
199
+ val currentSessionPath = sessionPath ? : error(" You need to call `setHomeserver()` first" )
188
200
val urlForOidcLogin = pendingOidcAuthorizationData ? : error(" You need to call `getOidcUrl()` first" )
189
201
client.loginWithOidcCallback(urlForOidcLogin, callbackUrl)
190
202
val sessionData = client.session().toSessionData(
191
203
isTokenValid = true ,
192
204
loginType = LoginType .OIDC ,
193
205
passphrase = pendingPassphrase,
194
- sessionPath = sessionPath ,
206
+ sessionPath = currentSessionPath.absolutePath ,
195
207
)
196
208
clear()
197
209
pendingOidcAuthorizationData?.close()
@@ -206,9 +218,10 @@ class RustMatrixAuthenticationService @Inject constructor(
206
218
207
219
override suspend fun loginWithQrCode (qrCodeData : MatrixQrCodeLoginData , progress : (QrCodeLoginStep ) -> Unit ) =
208
220
withContext(coroutineDispatchers.io) {
221
+ val emptySessionPath = rotateSessionPath()
209
222
runCatching {
210
223
val client = rustMatrixClientFactory.getBaseClientBuilder(
211
- sessionPath = sessionPath ,
224
+ sessionPath = emptySessionPath.absolutePath ,
212
225
passphrase = pendingPassphrase,
213
226
slidingSyncProxy = AuthenticationConfig .SLIDING_SYNC_PROXY_URL ,
214
227
slidingSync = ClientBuilderSlidingSync .Discovered ,
@@ -229,7 +242,7 @@ class RustMatrixAuthenticationService @Inject constructor(
229
242
isTokenValid = true ,
230
243
loginType = LoginType .QR ,
231
244
passphrase = pendingPassphrase,
232
- sessionPath = sessionPath ,
245
+ sessionPath = emptySessionPath.absolutePath ,
233
246
)
234
247
sessionStore.storeData(sessionData)
235
248
SessionId (sessionData.userId)
@@ -246,11 +259,13 @@ class RustMatrixAuthenticationService @Inject constructor(
246
259
}
247
260
Timber .e(throwable, " Failed to login with QR code" )
248
261
}
249
- }
262
+ }
250
263
251
- private fun getBaseClientBuilder () = rustMatrixClientFactory
264
+ private fun getBaseClientBuilder (
265
+ sessionPath : File ,
266
+ ) = rustMatrixClientFactory
252
267
.getBaseClientBuilder(
253
- sessionPath = sessionPath,
268
+ sessionPath = sessionPath.absolutePath ,
254
269
passphrase = pendingPassphrase,
255
270
slidingSyncProxy = AuthenticationConfig .SLIDING_SYNC_PROXY_URL ,
256
271
slidingSync = ClientBuilderSlidingSync .Discovered ,
0 commit comments