HTTP-client auth with Bearer token
See original GitHub issueSubsystem Client/Server, particular relevant ktor module(s) if applicable. ktor HTTP-client, auth
Is your feature request related to a problem? Please describe. Currently, Ktor HTTP-client supports only 2 types of providers: Basic and Digest. But in a real-life project is a standard to use Bearer token pair - access and refresh. So it will be great to add support of Bearer tokens to multiplatform Ktor’s HTTP-client. So the problem itself can be split into 4 sub-tasks:
- Persist token pair in a safe place
- Add them to auth headers
- In case when the request with access token returns 401, 3.1. pause all present requests 3.2 refresh it with refresh token… and so on, regular flow. 3.3 restart interrupted requests from 3.1
Describe the solution you’d like Create BearerAuthConfig, BearerAuthProvider like its Basic and Digest versions. Update Auth class to be able to save, load and refresh tokens. Something like this:
fun provide(): HttpClient = HttpClient {
install(Auth) {
bearer {
refresh = TODO("lambda function that get new access token and return a string"),
revoke = TODO("lambda function that removes tokens")
}
}
}
Motivation to include to ktor Add support of Bearer tokens for clients.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:18
- Comments:21 (13 by maintainers)
Top Results From Across the Web
Setting Authorization Header of HttpClient - Stack Overflow
I was setting the bearer token httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);.
Read more >How to set the Authorization Header of HttpClient in C# ...
A C# code example that shows how to set the Authorization Header in HttpClient. ... new AuthenticationHeaderValue("Bearer", "Your Oauth token");.
Read more >Posting with HttpClient and Bearer Token - gists · GitHub
Posting with HttpClient and Bearer Token. GitHub Gist: instantly share code, ... var token = <some valid token>. using (var client = new...
Read more >How To Consume RestAPI Using HttpClient In C# - C# Corner
Here Authorization contains the generated token with Bearer as the prefix. And we need to pass the Body with the JSON Data as...
Read more >C#/.NET | How do I Send a GET Request with Bearer Token ...
HTTP authentication is performed by sending authentication credentials in the authorization header to access the protected resource. Bearer ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Merged in main. Will be available in Ktor 1.6.0
Thanks for the feature request! What you suggest is actually 2 features:
The first one should be added as a part of Auth feature and we’ll be working on that. And the second one (token renewal) is currently a bit out of our scope but we’ll keep our eye on it in future.