From f218ea094ef17b60be69051ede09bb4849bd2fe6 Mon Sep 17 00:00:00 2001 From: Jason Monk Date: Sat, 11 Apr 2026 11:50:59 -0400 Subject: [PATCH] Fix #4: Add refresh token e2e test Add e2e tests verifying the full refresh token flow: exchanging a refresh token for new credentials, confirming the new access token works, verifying old access tokens are revoked on refresh, and ensuring used refresh tokens cannot be replayed (rotation). Also fix InMemoryTokenStore.refreshToken() to revoke old access tokens for the client on refresh, matching the RoomTokenStore behavior where the old row (containing both tokens) is deleted and replaced. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../mcp/core/InMemoryTokenStore.kt | 7 +- .../rousecontext/mcp/core/TokenStoreTest.kt | 16 ++ .../com/rousecontext/e2e/McpEndToEndTest.kt | 208 ++++++++++++++++++ 3 files changed, 230 insertions(+), 1 deletion(-) diff --git a/core/mcp/src/commonMain/kotlin/com/rousecontext/mcp/core/InMemoryTokenStore.kt b/core/mcp/src/commonMain/kotlin/com/rousecontext/mcp/core/InMemoryTokenStore.kt index 2e0193fb..59e4081c 100644 --- a/core/mcp/src/commonMain/kotlin/com/rousecontext/mcp/core/InMemoryTokenStore.kt +++ b/core/mcp/src/commonMain/kotlin/com/rousecontext/mcp/core/InMemoryTokenStore.kt @@ -109,8 +109,13 @@ class InMemoryTokenStore( if (stored.integrationId != integrationId) return null if (clock.currentTimeMillis() > stored.expiresAt) return null - // Rotate: invalidate old refresh token + // Rotate: invalidate old refresh token and revoke existing access tokens stored.revoked = true + tokens.filter { + it.integrationId == integrationId && + it.clientId == stored.clientId && + !it.revoked + }.forEach { it.revoked = true } return createTokenPair(integrationId, stored.clientId, stored.clientName) } diff --git a/core/mcp/src/jvmTest/kotlin/com/rousecontext/mcp/core/TokenStoreTest.kt b/core/mcp/src/jvmTest/kotlin/com/rousecontext/mcp/core/TokenStoreTest.kt index 6df84793..457dd1e8 100644 --- a/core/mcp/src/jvmTest/kotlin/com/rousecontext/mcp/core/TokenStoreTest.kt +++ b/core/mcp/src/jvmTest/kotlin/com/rousecontext/mcp/core/TokenStoreTest.kt @@ -155,6 +155,22 @@ class TokenStoreTest { assertNull(result) } + @Test + fun `refresh token revokes old access token`() { + val store = InMemoryTokenStore() + val pair = store.createTokenPair("health", "client-1") + + assertTrue(store.validateToken("health", pair.accessToken)) + + val newPair = store.refreshToken("health", pair.refreshToken) + assertNotNull(newPair) + + // Old access token should be revoked after refresh + assertFalse(store.validateToken("health", pair.accessToken)) + // New access token should work + assertTrue(store.validateToken("health", newPair!!.accessToken)) + } + @Test fun `unknown refresh token returns null`() { val store = InMemoryTokenStore() diff --git a/e2e/src/test/kotlin/com/rousecontext/e2e/McpEndToEndTest.kt b/e2e/src/test/kotlin/com/rousecontext/e2e/McpEndToEndTest.kt index a56f6875..a0917dbe 100644 --- a/e2e/src/test/kotlin/com/rousecontext/e2e/McpEndToEndTest.kt +++ b/e2e/src/test/kotlin/com/rousecontext/e2e/McpEndToEndTest.kt @@ -65,6 +65,7 @@ class McpEndToEndTest { private var codeChallenge: String? = null private var authCode: String? = null private var accessToken: String? = null + private var refreshToken: String? = null @BeforeAll fun setup() { @@ -337,8 +338,11 @@ class McpEndToEndTest { assertEquals(200, response.code, "Token exchange failed: $bodyText") val body = json.parseToJsonElement(bodyText).jsonObject accessToken = body["access_token"]?.jsonPrimitive?.content + refreshToken = body["refresh_token"]?.jsonPrimitive?.content assertNotNull(accessToken, "Missing access_token") + assertNotNull(refreshToken, "Missing refresh_token") println("Access token obtained: ${accessToken!!.take(10)}...") + println("Refresh token obtained: ${refreshToken!!.take(10)}...") } } @@ -453,6 +457,210 @@ class McpEndToEndTest { } } + @Test + @Order(9) + fun `09 - refresh token returns new access token`() { + val oldAccessToken = accessToken!! + + val formBody = FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", refreshToken!!) + .add("client_id", clientId!!) + .apply { clientSecret?.let { add("client_secret", it) } } + .build() + + val request = Request.Builder() + .url("$baseUrl/token") + .post(formBody) + .build() + + client.newCall(request).execute().use { response -> + val bodyText = response.body?.string() ?: "" + assertEquals(200, response.code, "Refresh token exchange failed: $bodyText") + val body = json.parseToJsonElement(bodyText).jsonObject + val newAccessToken = body["access_token"]?.jsonPrimitive?.content + val newRefreshToken = body["refresh_token"]?.jsonPrimitive?.content + assertNotNull(newAccessToken, "Missing access_token in refresh response") + assertNotNull(newRefreshToken, "Missing refresh_token in refresh response") + assertTrue( + newAccessToken != oldAccessToken, + "Refresh should return a different access token" + ) + accessToken = newAccessToken + refreshToken = newRefreshToken + println("New access token: ${newAccessToken!!.take(10)}...") + println("New refresh token: ${newRefreshToken!!.take(10)}...") + } + } + + @Test + @Order(10) + fun `10 - new access token works for MCP calls`() { + val callBody = buildJsonObject { + put("method", "tools/call") + put("jsonrpc", "2.0") + put("id", 4) + put( + "params", + buildJsonObject { + put("name", "echo") + put( + "arguments", + buildJsonObject { + put("message", "hello after refresh") + } + ) + } + ) + } + + val request = Request.Builder() + .url(mcpUrl) + .post(callBody.toString().toRequestBody("application/json".toMediaType())) + .header("Authorization", "Bearer $accessToken") + .build() + + client.newCall(request).execute().use { response -> + val bodyText = response.body?.string() ?: "" + assertEquals(200, response.code, "MCP call with new access token failed: $bodyText") + val body = json.parseToJsonElement(bodyText).jsonObject + val text = body["result"]?.jsonObject + ?.get("content")?.jsonArray + ?.get(0)?.jsonObject + ?.get("text")?.jsonPrimitive?.content + assertEquals("hello after refresh", text, "Echo tool returned wrong content") + println("Echo with refreshed token: $text") + } + } + + @Test + @Order(11) + fun `11 - old access token is revoked after refresh`() { + // We saved oldAccessToken in step 09 via the accessToken field before overwrite, + // but we need to use a token we know is old. Re-derive from the flow: + // After step 09, accessToken was updated to the new one. The old one was the + // value before the refresh. We need to track it separately. + // Actually, we don't have the old token saved — let's do a second refresh + // and verify the previous token stops working. + + // Current accessToken is from the refresh in step 09. + val tokenBeforeRefresh = accessToken!! + + // Do another refresh to get yet another new token + val formBody = FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", refreshToken!!) + .add("client_id", clientId!!) + .apply { clientSecret?.let { add("client_secret", it) } } + .build() + + val request = Request.Builder() + .url("$baseUrl/token") + .post(formBody) + .build() + + client.newCall(request).execute().use { response -> + val bodyText = response.body?.string() ?: "" + assertEquals(200, response.code, "Second refresh failed: $bodyText") + val body = json.parseToJsonElement(bodyText).jsonObject + accessToken = body["access_token"]?.jsonPrimitive?.content + refreshToken = body["refresh_token"]?.jsonPrimitive?.content + assertNotNull(accessToken, "Missing access_token") + assertNotNull(refreshToken, "Missing refresh_token") + } + + // Now try using the old access token — should be revoked + val callBody = buildJsonObject { + put("method", "tools/call") + put("jsonrpc", "2.0") + put("id", 5) + put( + "params", + buildJsonObject { + put("name", "echo") + put( + "arguments", + buildJsonObject { + put("message", "should fail") + } + ) + } + ) + } + + val oldRequest = Request.Builder() + .url(mcpUrl) + .post(callBody.toString().toRequestBody("application/json".toMediaType())) + .header("Authorization", "Bearer $tokenBeforeRefresh") + .build() + + client.newCall(oldRequest).execute().use { response -> + assertEquals( + 401, + response.code, + "Old access token should be revoked after refresh, but got ${response.code}" + ) + println("Old access token correctly rejected with 401") + } + } + + @Test + @Order(12) + fun `12 - used refresh token cannot be reused`() { + // The refresh token from step 09 was already used in step 11. + // We need to test with a known-consumed refresh token. + // The current refreshToken is from step 11's refresh. Let's use it, + // then try to use it again. + val currentRefresh = refreshToken!! + + // First use — should succeed + val formBody = FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", currentRefresh) + .add("client_id", clientId!!) + .apply { clientSecret?.let { add("client_secret", it) } } + .build() + + val request = Request.Builder() + .url("$baseUrl/token") + .post(formBody) + .build() + + client.newCall(request).execute().use { response -> + val bodyText = response.body?.string() ?: "" + assertEquals(200, response.code, "First refresh use failed: $bodyText") + val body = json.parseToJsonElement(bodyText).jsonObject + accessToken = body["access_token"]?.jsonPrimitive?.content + refreshToken = body["refresh_token"]?.jsonPrimitive?.content + } + + // Second use of same refresh token — should fail (rotation) + val replayBody = FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", currentRefresh) + .add("client_id", clientId!!) + .apply { clientSecret?.let { add("client_secret", it) } } + .build() + + val replayRequest = Request.Builder() + .url("$baseUrl/token") + .post(replayBody) + .build() + + client.newCall(replayRequest).execute().use { response -> + assertEquals( + 400, + response.code, + "Reused refresh token should be rejected, but got ${response.code}" + ) + val bodyText = response.body?.string() ?: "" + val body = json.parseToJsonElement(bodyText).jsonObject + val error = body["error"]?.jsonPrimitive?.content + assertEquals("invalid_grant", error, "Expected invalid_grant error") + println("Reused refresh token correctly rejected: $error") + } + } + // --- ADB helpers --- private fun adb(vararg args: String): String {