question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTTP-client auth with Bearer token

See original GitHub issue

Subsystem 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:

  1. Persist token pair in a safe place
  2. Add them to auth headers
  3. 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:closed
  • Created 3 years ago
  • Reactions:18
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

9reactions
e5lcommented, May 6, 2021

Merged in main. Will be available in Ktor 1.6.0

6reactions
Ololoshechkincommented, Jun 25, 2020

Thanks for the feature request! What you suggest is actually 2 features:

  1. Bearer (jwt) support in HttpClient
  2. Automatic token refresh.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found