/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http.Headers;
namespace QuantConnect.Brokerages.Authentication
{
///
/// Provides base functionality for token-based HTTP request handling,
/// including automatic retries and token refresh on unauthorized responses.
///
public abstract class TokenHandler : DelegatingHandler
{
///
/// The maximum number of retry attempts for an authenticated request.
///
private readonly int _maxRetryCount = 3;
///
/// The time interval to wait between retry attempts for an authenticated request.
///
private readonly TimeSpan _retryInterval;
///
/// A delegate used to construct an from a token type and access token string.
///
private readonly Func _createAuthHeader;
///
/// Initializes a new instance of the class.
///
///
/// An optional delegate for creating an
/// from the token type and access token. If not provided, a default implementation is used.
///
///
/// An optional time interval to wait between retry attempts when fetching the token or retrying a failed request.
/// If null, the default interval of 5 seconds is used.
///
protected TokenHandler(Func createAuthHeader = null, TimeSpan? retryInterval = null)
: base(new HttpClientHandler())
{
_createAuthHeader = createAuthHeader ?? ((tokenType, accessToken) => new AuthenticationHeaderValue(tokenType.ToString(), accessToken));
_retryInterval = retryInterval ?? TimeSpan.FromSeconds(5);
}
///
/// Retrieves a valid access token for authenticating HTTP requests.
/// Must be implemented by derived classes to provide token type and value,
/// with optional support for caching and refresh logic.
///
/// A cancellation token that can be used to cancel the token retrieval operation.
///
/// A instance containing the token type and access token string.
///
public abstract TokenCredentials GetAccessToken(CancellationToken cancellationToken);
///
/// Sends an HTTP request asynchronously by internally invoking the synchronous method.
/// This is useful for compatibility with components that require an asynchronous pipeline, even though the core logic is synchronous.
///
/// The HTTP request message to send.
/// A cancellation token to cancel the operation.
///
/// A task representing the asynchronous operation, containing the HTTP response message.
///
protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Task.FromResult(Send(request, cancellationToken));
}
///
/// Sends an HTTP request synchronously with retry support.
/// This override includes token-based authentication and refresh logic on 401 Unauthorized responses.
///
/// The HTTP request message to send.
/// A cancellation token to cancel operation.
/// The HTTP response message.
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = default;
for (var retryCount = 0; retryCount <= _maxRetryCount; retryCount++)
{
var accessToken = default(TokenCredentials);
try
{
accessToken = GetAccessToken(cancellationToken);
}
catch when (retryCount < _maxRetryCount)
{
if (cancellationToken.WaitHandle.WaitOne(_retryInterval))
{
throw new OperationCanceledException($"{nameof(TokenHandler)}.{nameof(Send)}: Token fetch canceled during wait.", cancellationToken);
}
continue;
}
catch
{
throw;
}
request.Headers.Authorization = _createAuthHeader(accessToken.TokenType, accessToken.AccessToken);
response = base.Send(request, cancellationToken);
if (response.IsSuccessStatusCode)
{
break;
}
if (response.StatusCode != HttpStatusCode.Unauthorized)
{
break;
}
if (cancellationToken.WaitHandle.WaitOne(_retryInterval))
{
break;
}
}
return response;
}
}
}