I'm trying to create an HMAC signature over a payl...
# ktor
b
I'm trying to create an HMAC signature over a payload in ktor client; right now I've used http interceptors but when implementing the JS parts, I had to migrate to suspending callbacks and I'm now running into youtrack.jetbrains.com/projects/…/KT-47562/Support-suspend-functional-types-in… is there another way to do that? serialize the request body before and set it as text? or is there a generic callback mechanism that allows me to change the request before submitting it that does not build upon http interceptors? is there a way to reuse the JSON encoder from the client?
a
Can you please share some code to illustrate your issue?
b
Copy code
class OrchestrationEngineEventClient(
    val secret: String,
    val client: HttpClient,
    val eventEndpointUrl: String,
    val source: String,
) {

    suspend fun <T : Any> publish(event: OrchestrationEngineEvent<T>): HttpResponse {
        return client.post(eventEndpointUrl) {
            accept(ContentType.Any)
            setBody(event.payload, event.typeInfo)
            contentType(ContentType.Application.Json)
            header("ce-source", source)
            header("ce-id", event.id)
            header("ce-type", event.type)
            header("ce-specversion", "1.0")
            beforeRequest { request ->
                val body = request.body as TextContent
                val hmac = hmacSignatureB64(secret, body.text)
                val b64Hmac = Base64.encode(hmac)
                request.headers.append("x-emporix-hmac", b64Hmac)
            }
        }
    }
}

fun HttpClient.registerBeforeRequest() {
    plugin(HttpSend).intercept {
        it.attributes
            .getOrNull(AttributeKey<(HttpRequestBuilder) -> Unit>("beforeRequest"))
            ?.invoke(it)
        execute(it)
    }
}

fun HttpRequestBuilder.beforeRequest(callback: (HttpRequestBuilder) -> Unit) =
    attributes.put(AttributeKey<(HttpRequestBuilder) -> Unit>("beforeRequest"), callback)
that's what I've got right now, but I need to migrate from (HttpRequestBuilder) -> Unit to suspend (HttpRequestBuilder) -> Unit
due to JS having an async hmac crypto lib
changing the type fails with a compile error
Copy code
[TYPEOF_SUSPEND_TYPE] Suspend functional types are not supported in typeOf
so rather than going for a request interceptor, I thought about trying to compute it directly in the post request by serializing the data class first
so I'm looking for a way to do that by reusing the configured JSON serializer
a
So, do you need a different
beforeRequest
handler for each request?
b
yes
only a fraction require a signature over the text body added as http parameter
root issue is that I need the serialized body to compute the hmac for the header
a
You can write a plugin that will create the signature if some attribute is present in the request.
b
that's basically what I did, right?
a
No. You can have just a Boolean flag, which will decide if the signature generation is required.
So the plugin will be installed on the client, the interceptor will fire for each request, and the signature will be generated based on the attribute value.
b
got it
it's a bit unfortunate to make it more specific