https://kotlinlang.org logo
#http4k
Title
# http4k
d

dmcg

09/27/2022, 9:26 AM
I want to add basic auth headers in some tests, but not others. I can use postfix withBasicAuth
Copy code
val request = Request(GET, "/items?date=2021-01-02")
            .withBasicAuth(Credentials("testUser", "testUser-pass"), "Authorization")
but is there a way to do the same without specifying the header name?
d

dave

09/27/2022, 9:31 AM
can you expand a bit on this?
d

dmcg

09/27/2022, 9:33 AM
It looks like the ClientFilters.BasicAuth is used to decorate a client? I don’t want to decorate the client, but populate the request.
CustomBasicAuth has Request.withBasicAuth(creds, header), which is what I’m using there ^^. Am I missing a way to do the same with the other ClientFilters?
I’ve written `
Copy code
private fun Request.withBasicAuth(credentials: Credentials) = withBasicAuth(credentials, "Authorization")
which does the job, but I want to be as vanilla as possible
d

dave

09/27/2022, 9:38 AM
so you want a generic postfix method for adding a header? that's just
Request.header()
isn't it?
d

dmcg

09/27/2022, 9:40 AM
Not just any old header, I want a generic postfix method for encoding and adding the Authorization header
d

dave

09/27/2022, 9:43 AM
isn't the header name always Authorization?
a

Andrew O'Hara

09/30/2022, 3:08 AM
I think Duncan might be looking for something like a
BiDiLens<Request, Credentials>
for basic auth. I don't think it exists, but I can think of several uses for it.
d

dmcg

09/30/2022, 3:18 PM
That’s certainly the sort of thing! In the meantime, https://github.com/http4k/http4k/commit/02d38f8e94802a1b46349a180816a0d5b2be16b2 has done the job thanks @dave
a

Andrew O'Hara

09/30/2022, 7:22 PM
Oh I had no idea that existed.
d

dave

09/30/2022, 7:22 PM
that makes 2 of us (before Duncan told me about it! 😂 )
a

Andrew O'Hara

09/30/2022, 7:24 PM
A lens would still be useful. I just had to write this monstrosity yesterday.
Copy code
val (headerClientId, headerClientSecret) = request.header("Authorization")
                ?.trim()
                ?.takeIf { it.startsWith("Basic") }
                ?.substringAfter("Basic")
                ?.trim()
                ?.base64Decoded()
                ?.split(":", ignoreCase = false, limit = 2)
                .let { parts ->
                    val clientId = parts?.getOrNull(0)?.let(::ClientId)
                    val clientSecret = parts?.getOrNull(1)?.let(::ClientSecret)
                    clientId to clientSecret
                }
It's on my todo list, along with improvements to the BasicAuth ServerFilter and Security to accept a lookup rather than a single set of credentials.
@dmcg if you still have need for the BiDi lens mapping, it's coming in the next release
d

dave

10/01/2022, 6:08 PM
which is now in maven central.... 4.32.2.0 🙂
d

dmcg

10/02/2022, 7:02 PM
Thank you both. It may be too late for the Jax London session codebase, but watch out for the video
140 Views