Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal data class BlueskyService private constructor(
fun newBaseUrlService(baseUrl: String): BlueskyService = copy(baseUrlFlow = flowOf(baseUrl))
}

private class AtprotoProxyPlugin {
internal class AtprotoProxyPlugin {
companion object : HttpClientPlugin<Unit, AtprotoProxyPlugin> {
override val key = AttributeKey<AtprotoProxyPlugin>("AtprotoProxyPlugin")

Expand All @@ -102,11 +102,15 @@ private class AtprotoProxyPlugin {
scope: HttpClient,
) {
scope.requestPipeline.intercept(HttpRequestPipeline.State) {
if (context.url.pathSegments
.lastOrNull()
?.startsWith("chat.bsky.convo.") == true
) {
context.headers["Atproto-Proxy"] = "did:web:api.bsky.chat#bsky_chat"
val method = context.url.pathSegments.lastOrNull()
when {
method?.startsWith("app.bsky.") == true -> {
context.headers["Atproto-Proxy"] = "did:web:api.bsky.app#bsky_appview"
}

method?.startsWith("chat.bsky.convo.") == true -> {
context.headers["Atproto-Proxy"] = "did:web:api.bsky.chat#bsky_chat"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dev.dimension.flare.data.network.bluesky

import io.ktor.client.HttpClient
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.respond
import io.ktor.client.request.get
import io.ktor.http.Headers
import io.ktor.http.HttpHeaders
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class AtprotoProxyPluginTest {
@Test
fun addsAppViewProxyHeaderForAppBskyMethods() =
runTest {
assertEquals(
"did:web:api.bsky.app#bsky_appview",
proxyHeaderFor("https://pds.test/xrpc/app.bsky.feed.getTimeline"),
)
}

@Test
fun keepsChatProxyHeaderForChatMethods() =
runTest {
assertEquals(
"did:web:api.bsky.chat#bsky_chat",
proxyHeaderFor("https://pds.test/xrpc/chat.bsky.convo.listConvos"),
)
}

@Test
fun doesNotProxyComAtprotoMethods() =
runTest {
assertNull(proxyHeaderFor("https://pds.test/xrpc/com.atproto.server.describeServer"))
}

private suspend fun proxyHeaderFor(url: String): String? {
var proxyHeader: String? = null
val client =
HttpClient(
MockEngine { request ->
proxyHeader = request.headers["Atproto-Proxy"]
respond(
content = """{"ok":true}""",
headers =
Headers.build {
append(HttpHeaders.ContentType, "application/json")
},
)
},
) {
install(AtprotoProxyPlugin)
}

try {
client.get(url)
} finally {
client.close()
}

return proxyHeader
}
}
Loading