I'm trying to write an `HttpClient` feature for ha...
# ktor
t
I'm trying to write an
HttpClient
feature for handling OAuth authentication. Here is how it should operate : 1. On each request, if a valid auth token is available, then perform the request by adding the
Authorization: Bearer <token>
2. If no token is available or the token expired, then perform a request on a configurable authentication endpoint with provided credentials. If authentication succeeds, save the received token and perform the initial request with the
Authorization
header. I've seen that this kind of features can be implemented using
HttpClient
pipelines, but can't find how to use them with my use case. What pipeline/phase should I use ? How to perform the authentication request with the same config (for example JSON deserialization) as the initial request ?
t
Thanks for your quick reply. From what I've read, I can implement a custom AuthProvider for OAuth authentication that sets the Authorization header. But since I'd like to perform an internal request for obtaining an access token, I have to create a custom feature. So the correct way to do that would be to intercept
Before
in the request pipeline and use the
HttpSend
feature to send the authentication request, then apply the headers by intercepting
State
?
e
Yep 🙂