Table of Contents

Authentication

The authentication component plays a key role in ensuring secure interaction with a number of exchange APIs. It is responsible for storing API keys, generating signatures for requests, and other security aspects.

Main Functions

  1. Storing API keys (public key, secret key, passphrase).
  2. Generating signatures for requests according to the requirements of a specific exchange.
  3. Adding necessary authentication headers to HTTP requests.

Implementation Example

Below is an example of the Authenticator class for working with authentication:

class Authenticator : Disposable
{
    private readonly HashAlgorithm _hasher;

    public Authenticator(bool canSign, SecureString key, SecureString secret, SecureString passphrase)
    {
        CanSign = canSign;
        Key = key;
        Secret = secret;
        Passphrase = passphrase;

        // Create a hashing algorithm based on the secret key
        _hasher = secret.IsEmpty() ? null : new HMACSHA256(secret.UnSecure().Base64());
    }

    protected override void DisposeManaged()
    {
        // Dispose of the hashing algorithm resources
        _hasher?.Dispose();
        base.DisposeManaged();
    }

    // Flag indicating whether the authenticator can create signatures
    public bool CanSign { get; }
    
    // API public key
    public SecureString Key { get; }
    
    // API secret key
    public SecureString Secret { get; }
    
    // Passphrase (if required by the exchange)
    public SecureString Passphrase { get; }

    // Method for creating a request signature
    public string MakeSign(string url, Method method, string parameters, out string timestamp)
    {
        // Generate a timestamp
        timestamp = DateTime.UtcNow.ToUnix().ToString("F0");

        // Create a signature based on the timestamp, method, URL, and parameters
        return _hasher
            .ComputeHash((timestamp + method.ToString().ToUpperInvariant() + url + parameters).UTF8())
            .Base64();
    }
}

Recommendations

  • Use SecureString for storing sensitive data, such as keys and passphrases.
  • Implement the IDisposable interface for proper resource disposal, especially if cryptographic primitives are used.
  • Ensure that signature generation methods correspond to the latest version of the exchange's API documentation.

With proper implementation of the authentication component, you will ensure secure interaction with the exchange and simplify the process of authorizing requests in other parts of the connector.