Skip to content

Commit

Permalink
OAuthHandler: Removed default value for InnerHandler
Browse files Browse the repository at this point in the history
This is required for compatability with HttpClientFactory.
  • Loading branch information
Bastian Eicher committed Mar 6, 2020
1 parent 26f0c5c commit 9d285c9
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/TypedRest.OAuth/Http/OAuthHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ namespace TypedRest.Http
public class OAuthHandler : DelegatingHandler
{
private readonly OAuthOptions _oAuthOptions;
private readonly HttpClient _httpClient;
private readonly Lazy<HttpClient> _httpClient;

/// <summary>
/// Creates a new OAuth handler.
/// </summary>
/// <param name="oAuthOptions">Options for OAuth 2.0 / OpenID Connect authentication.</param>
/// <param name="innerHandler">An optional inner HTTP message handler to delegate to.</param>
public OAuthHandler(OAuthOptions oAuthOptions, HttpMessageHandler? innerHandler = null)
: base(innerHandler ?? new HttpClientHandler())
{
if (oAuthOptions == null) throw new ArgumentNullException(nameof(oAuthOptions));
if (oAuthOptions.Uri == null) throw new ArgumentException($"{nameof(OAuthOptions)}.{nameof(OAuthOptions.Uri)} must not be null.", nameof(oAuthOptions));
if (string.IsNullOrEmpty(oAuthOptions.ClientId)) throw new ArgumentException($"{nameof(OAuthOptions)}.{nameof(OAuthOptions.ClientId)} must not be null or empty.", nameof(oAuthOptions));
if (string.IsNullOrEmpty(oAuthOptions.ClientSecret)) throw new ArgumentException($"{nameof(OAuthOptions)}.{nameof(OAuthOptions.ClientSecret)} must not be null or empty.", nameof(oAuthOptions));

_oAuthOptions = oAuthOptions;
_httpClient = new HttpClient(InnerHandler);

if (innerHandler != null) InnerHandler = innerHandler;
_httpClient = new Lazy<HttpClient>(() => new HttpClient(InnerHandler ?? new HttpClientHandler()));
}

private AccessToken? _accessToken;
Expand All @@ -45,7 +45,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

private async Task<AccessToken> RequestAccessTokenAsync(CancellationToken cancellationToken)
{
var response = await _httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
var response = await _httpClient.Value.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = await DiscoverTokenEndpointAsync(cancellationToken),
ClientId = _oAuthOptions.ClientId,
Expand All @@ -62,7 +62,7 @@ private async Task<AccessToken> RequestAccessTokenAsync(CancellationToken cancel

private async Task<string> DiscoverTokenEndpointAsync(CancellationToken cancellationToken)
{
var response = await _httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
var response = await _httpClient.Value.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
{
RequestUri = _oAuthOptions.Uri
}, cancellationToken);
Expand Down

0 comments on commit 9d285c9

Please sign in to comment.