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

dave08

03/04/2024, 2:20 PM
Is there any way to use ktor's authentication with a token provided in a query parameter instead of a header?
a

Aleksei Tirman [JB]

03/04/2024, 2:51 PM
For the bearer provider, you can use the
authHeader
method to configure the token's source. Here is an example:
Copy code
install(Authentication) {
    bearer {
        authHeader { 
            val auth = it.request.queryParameters["auth"] ?: throw BadRequestException("No auth query parameter given")
            parseAuthorizationHeader(auth)
        }
    }
}
👍🏼 1
d

dave08

03/04/2024, 3:03 PM
Thanks, that's perfect! Just funny that it's called
authHeader
...?
Since there we're really setting the auth source, shouldn't it be
authSource { }
?
a

Aleksei Tirman [JB]

03/04/2024, 5:17 PM
Not exactly, since the source also has to be parsed.
d

dave08

03/12/2024, 10:47 AM
It's not working @Aleksei Tirman [JB]...?
As a result to that parse function, I get the token in the authScheme of the HttpHeader... is that what's supposed to happen?
It seems to skip the
authenticate { }
block in
bearer {  }
too...
Copy code
install(Authentication) {
    bearer {
        authHeader { 
            val auth = it.request.queryParameters["auth"] ?: throw BadRequestException("No auth query parameter given")
            parseAuthorizationHeader("Bearer " + auth)
        }
    }
}
this works... I need to add
Bearer
to the auth token.
a

Aleksei Tirman [JB]

03/12/2024, 11:18 AM
It depends on the format of the value in the header.
3 Views