1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-11 16:32:57 +00:00

Authentication update (#275)

Updated API credential logic, exchange implementation are expected to provide their own credentials implementation with ApiCredentials as base class
Removed ApiCredentials implementation used by most exchanges
Removed ApiCredentialsType Enum
Added CredentialSet base class and implementations for defining different API credentials
Added optional type param to AuthenticationProvider for the specific API credential type to improve type safety
Moved AuthenticationProvider/ApiCredentials from BaseApiClient to RestApiClient/SocketApiClient base classes
Added optional type params to RestApiClient/SocketApiClient base class to specify the AuthenticationProvider type and credentials type to improve type safety
Moved SetOptions/SetApiCredentials from BaseApiClient to RestApiClient/SocketApiClient
Extracted LibraryOptions<TRestOptions, TSocketOptions, TEnvironment> without TApiCredentials for libraries without API credentials
Removed ApiCredentials from ApiOptions, credentials can only be configured at library, rest or socket level
Added EnvironmentName to RestApiClient/SocketApiClient property
Added Unknown enum value to Shared interfaces SharedOrderStatus, SharedTransferStatus and SharedTriggerOrderStatus enums
Updated Enum converter to map value to an undefined Enum value instead of the first Enum value
Added support for checking for missing fields on RestIntegrationTest
Added BytesToHexString and HexToBytesString to ExchangeHelpers static class
Fixed bug where WebSocket connections are not reconnected when configuring Proxy with SetUpdates
Removed legacy CryptoBaseClient, CryptoRestClient and CryptoSocketClient
This commit is contained in:
Jan Korf
2026-03-23 14:28:00 +01:00
committed by GitHub
parent 875696a73a
commit 2f9f97f4e8
47 changed files with 1615 additions and 771 deletions
+2 -1
View File
@@ -130,7 +130,8 @@ namespace CryptoExchange.Net.Objects
/// <summary>
/// Default error info
/// </summary>
protected static readonly ErrorInfo _errorInfo = new ErrorInfo(ErrorType.MissingCredentials, false, "No credentials provided for private endpoint");
protected static readonly ErrorInfo _errorInfo = new ErrorInfo(ErrorType.MissingCredentials, false,
"No credentials provided for private endpoint, set the `ApiCredentials` option in the client configuration");
/// <summary>
/// ctor
@@ -1,6 +1,4 @@
using CryptoExchange.Net.Authentication;
namespace CryptoExchange.Net.Objects.Options
namespace CryptoExchange.Net.Objects.Options
{
/// <summary>
/// Options for API usage
@@ -17,10 +15,5 @@ namespace CryptoExchange.Net.Objects.Options
/// Note that this comes at a performance cost
/// </summary>
public bool? OutputOriginalData { get; set; }
/// <summary>
/// The api credentials used for signing requests to this API. Overrides API credentials provided in the client options
/// </summary>
public ApiCredentials? ApiCredentials { get; set; }
}
}
@@ -28,11 +28,6 @@ namespace CryptoExchange.Net.Objects.Options
/// </summary>
public TimeSpan RequestTimeout { get; set; } = TimeSpan.FromSeconds(20);
/// <summary>
/// The api credentials used for signing requests to this API.
/// </summary>
public ApiCredentials? ApiCredentials { get; set; }
/// <summary>
/// Whether or not client side rate limiting should be applied
/// </summary>
@@ -45,7 +40,7 @@ namespace CryptoExchange.Net.Objects.Options
/// <inheritdoc />
public override string ToString()
{
return $"RequestTimeout: {RequestTimeout}, Proxy: {(Proxy == null ? "-" : "set")}, ApiCredentials: {(ApiCredentials == null ? "-" : "set")}";
return $"RequestTimeout: {RequestTimeout}, Proxy: {(Proxy == null ? "-" : "set")}";
}
}
}
@@ -6,15 +6,10 @@ namespace CryptoExchange.Net.Objects.Options
/// <summary>
/// Library options
/// </summary>
/// <typeparam name="TRestOptions"></typeparam>
/// <typeparam name="TSocketOptions"></typeparam>
/// <typeparam name="TApiCredentials"></typeparam>
/// <typeparam name="TEnvironment"></typeparam>
public class LibraryOptions<TRestOptions, TSocketOptions, TApiCredentials, TEnvironment>
where TRestOptions: RestExchangeOptions, new()
where TSocketOptions: SocketExchangeOptions, new()
where TApiCredentials: ApiCredentials
where TEnvironment: TradeEnvironment
public class LibraryOptions<TRestOptions, TSocketOptions, TEnvironment>
where TRestOptions : RestExchangeOptions<TEnvironment>, new()
where TSocketOptions : SocketExchangeOptions<TEnvironment>, new()
where TEnvironment : TradeEnvironment
{
/// <summary>
/// Rest client options
@@ -31,11 +26,6 @@ namespace CryptoExchange.Net.Objects.Options
/// </summary>
public TEnvironment? Environment { get; set; }
/// <summary>
/// The api credentials used for signing requests.
/// </summary>
public TApiCredentials? ApiCredentials { get; set; }
/// <summary>
/// The DI service lifetime for the socket client
/// </summary>
@@ -44,9 +34,8 @@ namespace CryptoExchange.Net.Objects.Options
/// <summary>
/// Copy values from these options to the target options
/// </summary>
public T Set<T>(T targetOptions) where T: LibraryOptions<TRestOptions, TSocketOptions, TApiCredentials, TEnvironment>
public T Set<T>(T targetOptions) where T : LibraryOptions<TRestOptions, TSocketOptions, TEnvironment>
{
targetOptions.ApiCredentials = (TApiCredentials?)ApiCredentials?.Copy();
targetOptions.Environment = Environment;
targetOptions.SocketClientLifeTime = SocketClientLifeTime;
targetOptions.Rest = Rest.Set(targetOptions.Rest);
@@ -55,4 +44,29 @@ namespace CryptoExchange.Net.Objects.Options
return targetOptions;
}
}
/// <summary>
/// Library options
/// </summary>
public class LibraryOptions<TRestOptions, TSocketOptions, TApiCredentials, TEnvironment> : LibraryOptions<TRestOptions, TSocketOptions, TEnvironment>
where TRestOptions: RestExchangeOptions<TEnvironment, TApiCredentials>, new()
where TSocketOptions: SocketExchangeOptions<TEnvironment, TApiCredentials>, new()
where TApiCredentials: ApiCredentials
where TEnvironment: TradeEnvironment
{
/// <summary>
/// The api credentials used for signing requests.
/// </summary>
public TApiCredentials? ApiCredentials { get; set; }
/// <summary>
/// Copy values from these options to the target options
/// </summary>
public new T Set<T>(T targetOptions) where T: LibraryOptions<TRestOptions, TSocketOptions, TApiCredentials, TEnvironment>
{
targetOptions = base.Set(targetOptions);
targetOptions.ApiCredentials = (TApiCredentials?)ApiCredentials?.Copy();
return targetOptions;
}
}
}
@@ -18,27 +18,10 @@ namespace CryptoExchange.Net.Objects.Options
/// </summary>
public T Set<T>(T item) where T : RestApiOptions, new()
{
item.ApiCredentials = ApiCredentials?.Copy();
item.OutputOriginalData = OutputOriginalData;
item.AutoTimestamp = AutoTimestamp;
item.TimestampRecalculationInterval = TimestampRecalculationInterval;
return item;
}
}
/// <summary>
/// Http API options
/// </summary>
/// <typeparam name="TApiCredentials"></typeparam>
public class RestApiOptions<TApiCredentials>: RestApiOptions where TApiCredentials: ApiCredentials
{
/// <summary>
/// The api credentials used for signing requests to this API.
/// </summary>
public new TApiCredentials? ApiCredentials
{
get => (TApiCredentials?)base.ApiCredentials;
set => base.ApiCredentials = value;
}
}
}
@@ -6,8 +6,9 @@ namespace CryptoExchange.Net.Objects.Options
/// <summary>
/// Options for a rest exchange client
/// </summary>
public class RestExchangeOptions: ExchangeOptions
public class RestExchangeOptions : ExchangeOptions
{
/// <summary>
/// How often the timestamp adjustment between client and server is recalculated. If you need a very small TimeSpan here you're probably better of syncing your server time more often
/// </summary>
@@ -64,7 +65,6 @@ namespace CryptoExchange.Net.Objects.Options
item.OutputOriginalData = OutputOriginalData;
item.AutoTimestamp = AutoTimestamp;
item.TimestampRecalculationInterval = TimestampRecalculationInterval;
item.ApiCredentials = ApiCredentials?.Copy();
item.Proxy = Proxy;
item.RequestTimeout = RequestTimeout;
item.RateLimiterEnabled = RateLimiterEnabled;
@@ -83,11 +83,9 @@ namespace CryptoExchange.Net.Objects.Options
}
}
/// <summary>
/// Options for a rest exchange client
/// </summary>
/// <typeparam name="TEnvironment"></typeparam>
public class RestExchangeOptions<TEnvironment> : RestExchangeOptions where TEnvironment : TradeEnvironment
/// <inheritdoc />
public class RestExchangeOptions<TEnvironment> : RestExchangeOptions
where TEnvironment : TradeEnvironment
{
/// <summary>
/// Trade environment. Contains info about URL's to use to connect to the API. To swap environment select another environment for
@@ -108,20 +106,32 @@ namespace CryptoExchange.Net.Objects.Options
}
}
/// <summary>
/// Options for a rest exchange client
/// </summary>
/// <typeparam name="TEnvironment"></typeparam>
/// <typeparam name="TApiCredentials"></typeparam>
public class RestExchangeOptions<TEnvironment, TApiCredentials> : RestExchangeOptions<TEnvironment> where TEnvironment : TradeEnvironment where TApiCredentials : ApiCredentials
/// <inheritdoc />
public class RestExchangeOptions<TEnvironment, TApiCredentials> : RestExchangeOptions<TEnvironment>
where TEnvironment : TradeEnvironment
where TApiCredentials : ApiCredentials
{
/// <summary>
/// The api credentials used for signing requests to this API.
/// </summary>
public new TApiCredentials? ApiCredentials
public TApiCredentials? ApiCredentials { get; set; }
/// <summary>
/// Set the values of this options on the target options
/// </summary>
public new T Set<T>(T item) where T : RestExchangeOptions<TEnvironment, TApiCredentials>, new()
{
get => (TApiCredentials?)base.ApiCredentials;
set => base.ApiCredentials = value;
base.Set(item);
item.ApiCredentials = (TApiCredentials?)ApiCredentials?.Copy();
return item;
}
/// <inheritdoc />
public override string ToString()
{
return $"{base.ToString()}, ApiCredentials: {(ApiCredentials == null ? "-" : "set")}";
}
}
}
@@ -24,7 +24,6 @@ namespace CryptoExchange.Net.Objects.Options
/// </summary>
public T Set<T>(T item) where T : SocketApiOptions, new()
{
item.ApiCredentials = ApiCredentials?.Copy();
item.OutputOriginalData = OutputOriginalData;
item.SocketNoDataTimeout = SocketNoDataTimeout;
item.AutoTimestamp = AutoTimestamp;
@@ -32,20 +31,4 @@ namespace CryptoExchange.Net.Objects.Options
return item;
}
}
/// <summary>
/// Socket API options
/// </summary>
/// <typeparam name="TApiCredentials"></typeparam>
public class SocketApiOptions<TApiCredentials> : SocketApiOptions where TApiCredentials : ApiCredentials
{
/// <summary>
/// The api credentials used for signing requests to this API.
/// </summary>
public new TApiCredentials? ApiCredentials
{
get => (TApiCredentials?)base.ApiCredentials;
set => base.ApiCredentials = value;
}
}
}
@@ -91,7 +91,6 @@ namespace CryptoExchange.Net.Objects.Options
/// <returns></returns>
public T Set<T>(T item) where T : SocketExchangeOptions, new()
{
item.ApiCredentials = ApiCredentials?.Copy();
item.AutoTimestamp = AutoTimestamp;
item.OutputOriginalData = OutputOriginalData;
item.ReconnectPolicy = ReconnectPolicy;
@@ -110,12 +109,11 @@ namespace CryptoExchange.Net.Objects.Options
}
}
/// <summary>
/// Options for a socket exchange client
/// </summary>
/// <typeparam name="TEnvironment"></typeparam>
public class SocketExchangeOptions<TEnvironment> : SocketExchangeOptions where TEnvironment : TradeEnvironment
/// <inheritdoc />
public class SocketExchangeOptions<TEnvironment> : SocketExchangeOptions
where TEnvironment : TradeEnvironment
{
/// <summary>
/// Trade environment. Contains info about URL's to use to connect to the API. To swap environment select another environment for
/// the exchange's environment list or create a custom environment using either `[Exchange]Environment.CreateCustom()` or `[Exchange]Environment.[Environment]`, for example `KucoinEnvironment.TestNet` or `BinanceEnvironment.Live`
@@ -138,17 +136,29 @@ namespace CryptoExchange.Net.Objects.Options
/// <summary>
/// Options for a socket exchange client
/// </summary>
/// <typeparam name="TEnvironment"></typeparam>
/// <typeparam name="TApiCredentials"></typeparam>
public class SocketExchangeOptions<TEnvironment, TApiCredentials> : SocketExchangeOptions<TEnvironment> where TEnvironment : TradeEnvironment where TApiCredentials : ApiCredentials
public class SocketExchangeOptions<TEnvironment, TApiCredentials> : SocketExchangeOptions<TEnvironment>
where TEnvironment : TradeEnvironment
where TApiCredentials : ApiCredentials
{
/// <summary>
/// The api credentials used for signing requests to this API.
/// </summary>
public new TApiCredentials? ApiCredentials
public TApiCredentials? ApiCredentials { get; set; }
/// <summary>
/// Set the values of this options on the target options
/// </summary>
public new T Set<T>(T item) where T : SocketExchangeOptions<TEnvironment, TApiCredentials>, new()
{
get => (TApiCredentials?)base.ApiCredentials;
set => base.ApiCredentials = value;
base.Set(item);
item.ApiCredentials = (TApiCredentials?)ApiCredentials?.Copy();
return item;
}
/// <inheritdoc />
public override string ToString()
{
return $"{base.ToString()}, ApiCredentials: {(ApiCredentials == null ? "-" : "set")}";
}
}
}
@@ -6,7 +6,7 @@ namespace CryptoExchange.Net.Objects.Options
/// <summary>
/// Options to update
/// </summary>
public class UpdateOptions<T> where T : ApiCredentials
public class UpdateOptions<TApiCredentials> where TApiCredentials : ApiCredentials
{
/// <summary>
/// Proxy setting. Note that if this is not provided any previously set proxy will be reset
@@ -15,13 +15,10 @@ namespace CryptoExchange.Net.Objects.Options
/// <summary>
/// Api credentials
/// </summary>
public T? ApiCredentials { get; set; }
public TApiCredentials? ApiCredentials { get; set; }
/// <summary>
/// Request timeout
/// </summary>
public TimeSpan? RequestTimeout { get; set; }
}
/// <inheritdoc />
public class UpdateOptions : UpdateOptions<ApiCredentials> { }
}