fix(android): signing out ends the server session, not only the local one (#8108)
signOut cleared the session cookie before calling /api/auth/logout, so the logout went without the cookie that names the session to drop, and the server session lived on until it expired. The app still shows signed-out at once; the logout now goes first with the cookie, bounded to 5 s and run in the application scope so leaving Settings can't cancel it, and the cookie is cleared after it, whether it worked or not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import kotlinx.serialization.json.Json
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.create
|
||||
@@ -107,18 +108,35 @@ class AuthController @Inject constructor(
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the local session immediately and best-effort hits
|
||||
* `/api/auth/logout` so the server can drop its session row.
|
||||
* Network errors are swallowed — the user is already locally
|
||||
* signed out and a stale server session times out on its own.
|
||||
* Signs out here at once and ends the session on the server too.
|
||||
*
|
||||
* The app shows signed-out straight away, then calls
|
||||
* `/api/auth/logout` while the session cookie is still set: the cookie
|
||||
* is what tells the server which session to drop, so the call has to go
|
||||
* before the cookie is cleared. The call waits at most
|
||||
* [LOGOUT_TIMEOUT_MS]; offline, or on an error, the cookie is cleared
|
||||
* anyway and the server session expires on its own. It runs in the
|
||||
* application scope, so the screen that asked closing as the app shows
|
||||
* signed-out can't cancel it halfway.
|
||||
*/
|
||||
suspend fun signOut() {
|
||||
currentUserState.value = null
|
||||
authStore.setSessionCookie(null)
|
||||
authStore.setUserJson(null)
|
||||
runCatching { api.logout() }
|
||||
scope.launch {
|
||||
try {
|
||||
withTimeoutOrNull(LOGOUT_TIMEOUT_MS) { runCatching { api.logout() } }
|
||||
} finally {
|
||||
authStore.setSessionCookie(null)
|
||||
}
|
||||
}.join()
|
||||
}
|
||||
|
||||
private fun decodeUser(raw: String): UserRef? =
|
||||
runCatching { json.decodeFromString(UserRef.serializer(), raw) }.getOrNull()
|
||||
|
||||
private companion object {
|
||||
// Long enough for a logout on a slow connection, short enough that
|
||||
// signing out offline doesn't seem to hang.
|
||||
const val LOGOUT_TIMEOUT_MS = 5_000L
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
@@ -23,6 +24,7 @@ import retrofit2.Retrofit
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class AuthControllerTest {
|
||||
private lateinit var server: MockWebServer
|
||||
@@ -80,6 +82,57 @@ class AuthControllerTest {
|
||||
assertEquals("/api/me", request.path)
|
||||
}
|
||||
|
||||
// Signing out ends the session on the server: the logout call goes out
|
||||
// while the cookie is still set, since the cookie is what names the
|
||||
// session to drop, and the cookie is cleared once it has.
|
||||
@Test
|
||||
fun `sign out sends the logout with the session cookie, then clears it`() {
|
||||
val vault = FakeSessionVault(stored = "session=stored")
|
||||
val row = AuthSessionEntity(baseUrl = server.url("/").toString())
|
||||
val dao =
|
||||
mockk<AuthSessionDao> {
|
||||
every { observe() } returns flowOf(row)
|
||||
coEvery { get() } returns row
|
||||
coEvery { upsert(any()) } returns Unit
|
||||
coEvery { setSessionCookie(any()) } returns Unit
|
||||
coEvery { setUserJson(any()) } returns Unit
|
||||
coEvery { setBaseUrl(any()) } returns Unit
|
||||
}
|
||||
val store = AuthStore(dao, vault, scope)
|
||||
val retrofit =
|
||||
Retrofit.Builder()
|
||||
.baseUrl(server.url("/"))
|
||||
.client(
|
||||
OkHttpClient.Builder()
|
||||
// Attaches the stored cookie, as AuthCookieInterceptor does.
|
||||
.addInterceptor { chain ->
|
||||
val cookie = store.sessionCookieForRequest()
|
||||
chain.proceed(
|
||||
chain.request().newBuilder().apply {
|
||||
if (!cookie.isNullOrEmpty()) header("Cookie", cookie)
|
||||
}.build(),
|
||||
)
|
||||
}
|
||||
.build(),
|
||||
)
|
||||
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
|
||||
.build()
|
||||
server.enqueue(MockResponse().setResponseCode(200).setBody("""{"id":"u1","username":"listener"}"""))
|
||||
server.enqueue(MockResponse().setResponseCode(204))
|
||||
|
||||
val controller = AuthController(store, json, scope, retrofit)
|
||||
val launch = server.takeRequest(REQUEST_WAIT_SECONDS, TimeUnit.SECONDS)
|
||||
assertEquals("/api/me", launch?.path)
|
||||
|
||||
runBlocking { controller.signOut() }
|
||||
|
||||
val logout = server.takeRequest(REQUEST_WAIT_SECONDS, TimeUnit.SECONDS)
|
||||
assertNotNull(logout, "the logout never reached the server")
|
||||
assertEquals("/api/auth/logout", logout.path)
|
||||
assertEquals("session=stored", logout.getHeader("Cookie"))
|
||||
assertNull(store.sessionCookie.value, "the cookie outlived the sign-out")
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val SLOW_VAULT_MS = 300L
|
||||
const val REQUEST_WAIT_SECONDS = 5L
|
||||
|
||||
Reference in New Issue
Block a user