-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Refit's AuthenticationHandler issue. See reactiveui/refit#1761
- Loading branch information
1 parent
58aa429
commit f8cc614
Showing
2 changed files
with
31 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Net.Http.Headers; | ||
|
||
namespace matrix_dotnet; | ||
|
||
// <summary> | ||
// This is taken directly from Refit's source, because the class is private | ||
// See <see href="https://github.com/reactiveui/refit/issues/1761"> | ||
// </summary> | ||
class AuthenticatedHttpClientHandler : DelegatingHandler { | ||
readonly Func<HttpRequestMessage, CancellationToken, Task<string>> getToken; | ||
|
||
public AuthenticatedHttpClientHandler(Func<HttpRequestMessage, CancellationToken, Task<string>> getToken, HttpMessageHandler? innerHandler = null) : base(innerHandler ?? new HttpClientHandler()) { | ||
this.getToken = getToken ?? throw new ArgumentNullException(nameof(getToken)); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { | ||
// See if the request has an authorize header | ||
var auth = request.Headers.Authorization; | ||
if (auth != null) { | ||
var token = await getToken(request, cancellationToken).ConfigureAwait(false); | ||
request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token); | ||
} | ||
|
||
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters