diff --git a/android/app/src/main/java/com/fabledsword/minstrel/auth/AuthController.kt b/android/app/src/main/java/com/fabledsword/minstrel/auth/AuthController.kt index 07dc321a..ad7046b5 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/auth/AuthController.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/auth/AuthController.kt @@ -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 + } } diff --git a/android/app/src/test/java/com/fabledsword/minstrel/auth/AuthControllerTest.kt b/android/app/src/test/java/com/fabledsword/minstrel/auth/AuthControllerTest.kt index ba1ecf7d..9ce9015d 100644 --- a/android/app/src/test/java/com/fabledsword/minstrel/auth/AuthControllerTest.kt +++ b/android/app/src/test/java/com/fabledsword/minstrel/auth/AuthControllerTest.kt @@ -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 { + 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