1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-10 09:26:22 +00:00

Added support for credential reading from (file)stream

This commit is contained in:
Jan Korf 2019-05-13 15:40:01 +02:00
parent 2382dbed2d
commit 292341823b

View File

@ -1,5 +1,8 @@
using System;
using System.IO;
using System.Security;
using System.Text;
using Newtonsoft.Json.Linq;
namespace CryptoExchange.Net.Authentication
{
@ -8,12 +11,12 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// The api key to authenticate requests
/// </summary>
public SecureString Key { get; }
public SecureString Key { get; private set; }
/// <summary>
/// The api secret to authenticate requests
/// </summary>
public SecureString Secret { get; }
public SecureString Secret { get; private set; }
/// <summary>
/// The private key to authenticate requests
@ -50,17 +53,49 @@ namespace CryptoExchange.Net.Authentication
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret))
throw new ArgumentException("Key and secret can't be null/empty");
var secureApiKey = new SecureString();
foreach (var c in key)
secureApiKey.AppendChar(c);
secureApiKey.MakeReadOnly();
Key = secureApiKey;
Key = CreateSecureString(key);
Secret = CreateSecureString(secret);
}
var secureApiSecret = new SecureString();
foreach (var c in secret)
secureApiSecret.AppendChar(c);
secureApiSecret.MakeReadOnly();
Secret = secureApiSecret;
/// <summary>
/// Create Api credentials providing a stream containing json data. The json data should include two values: apiKey and apiSecret
/// </summary>
/// <param name="inputStream">The stream containing the json data</param>
/// <param name="identifierKey">A key to identify the credentials for the API. For example, when set to `binanceKey` the json data should contain a value for the property `binanceKey`. Defaults to 'apiKey'.</param>
/// <param name="identifierSecret">A key to identify the credentials for the API. For example, when set to `binanceSecret` the json data should contain a value for the property `binanceSecret`. Defaults to 'apiSecret'.</param>
public ApiCredentials(Stream inputStream, string identifierKey = null, string identifierSecret = null)
{
using (var reader = new StreamReader(inputStream, Encoding.ASCII, false, 512, true))
{
var stringData = reader.ReadToEnd();
var jsonData = JToken.Parse(stringData);
var key = TryGetValue(jsonData, identifierKey ?? "apiKey");
var secret = TryGetValue(jsonData, identifierSecret ?? "apiSecret");
if (key == null || secret == null)
throw new ArgumentException("apiKey or apiSecret value not found in Json credential file");
Key = CreateSecureString(key);
Secret = CreateSecureString(secret);
}
inputStream.Seek(0, SeekOrigin.Begin);
}
protected string TryGetValue(JToken data, string key)
{
if (data[key] == null)
return null;
return (string) data[key];
}
protected SecureString CreateSecureString(string source)
{
var secureString = new SecureString();
foreach (var c in source)
secureString.AppendChar(c);
secureString.MakeReadOnly();
return secureString;
}
public void Dispose()