mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-12 10:26:27 +00:00
Added support for credential reading from (file)stream
This commit is contained in:
parent
2382dbed2d
commit
292341823b
@ -1,5 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Security;
|
using System.Security;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace CryptoExchange.Net.Authentication
|
namespace CryptoExchange.Net.Authentication
|
||||||
{
|
{
|
||||||
@ -8,12 +11,12 @@ namespace CryptoExchange.Net.Authentication
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The api key to authenticate requests
|
/// The api key to authenticate requests
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SecureString Key { get; }
|
public SecureString Key { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The api secret to authenticate requests
|
/// The api secret to authenticate requests
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SecureString Secret { get; }
|
public SecureString Secret { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The private key to authenticate requests
|
/// The private key to authenticate requests
|
||||||
@ -50,17 +53,49 @@ namespace CryptoExchange.Net.Authentication
|
|||||||
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret))
|
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret))
|
||||||
throw new ArgumentException("Key and secret can't be null/empty");
|
throw new ArgumentException("Key and secret can't be null/empty");
|
||||||
|
|
||||||
var secureApiKey = new SecureString();
|
Key = CreateSecureString(key);
|
||||||
foreach (var c in key)
|
Secret = CreateSecureString(secret);
|
||||||
secureApiKey.AppendChar(c);
|
}
|
||||||
secureApiKey.MakeReadOnly();
|
|
||||||
Key = secureApiKey;
|
|
||||||
|
|
||||||
var secureApiSecret = new SecureString();
|
/// <summary>
|
||||||
foreach (var c in secret)
|
/// Create Api credentials providing a stream containing json data. The json data should include two values: apiKey and apiSecret
|
||||||
secureApiSecret.AppendChar(c);
|
/// </summary>
|
||||||
secureApiSecret.MakeReadOnly();
|
/// <param name="inputStream">The stream containing the json data</param>
|
||||||
Secret = secureApiSecret;
|
/// <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()
|
public void Dispose()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user