Jason Toms
06/25/2024, 1:58 PMClientPlugin
to replace an OkHttp Interceptor
in Android that implements the headers described in the DPoP spec. It all seems very straightforward, but I can't seem to get the raw request body to add to my DPoP token. The body I get from onRequest
appears to be the data class, and I can see that I have the bodyType
as well but I cannot figure out how to use this to get a serialized version of the request body. I feel like I am missing something really obvious or I am doing something fundamentally wrong. I am using Kotlin Serialization and the OkHttp engine, but I really want to convert my OkHttp interceptors so I can leverage coroutines and get the headers into the ktor logging plugin.Jason Toms
06/25/2024, 5:43 PMSendingRequest
, which let me get things a little later. Not entirely sure this is a good idea but it works!
on(SendingRequest) { request, content ->
val bodyContent = (content as TextContent).text
val dpop = makeDPoPTokenForKtor(
requestBody = bodyContent,
httpMethod = request.method.value,
url = request.url.build(),
accessToken = keyHandler.accessToken,
currentTime = clockSync.now(),
)
val signedDPoPToken = signDPoPToken(
dPoPToken = dpop,
privateKey = keyHandler.privateKey,
publicKey = keyHandler.publicKey,
)
request.headers.append("DPoP", signedDPoPToken)
}
Aleksei Tirman [JB]
06/26/2024, 7:40 AMSend
hook where the raw request body is available. Here is an example:
val client = HttpClient(CIO) {
install(createClientPlugin("plugin") {
on(Send) { request ->
val bodyBytes = (request.body as OutgoingContent).toByteArray()
request.header("custom", bodyBytes.size.toString())
proceed(request)
}
})
}