public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary <string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, out SortedDictionary <string, object> uriParameters, out SortedDictionary <string, object> bodyParameters, out Dictionary <string, string> headers) { var uriParam = new SortedDictionary <string, object>(); bodyParameters = new(); uriParameters = new(); headers = new(); if (parameterPosition == HttpMethodParameterPosition.InBody && method == HttpMethod.Post) { bodyParameters = new SortedDictionary <string, object>(providedParameters); } if (parameterPosition == HttpMethodParameterPosition.InUri && method == HttpMethod.Get) { uriParam = new SortedDictionary <string, object>(providedParameters); } if (auth) { if (uri.PathAndQuery.Contains("v3")) { headers = AddAuthenticationToHeaders(uri.ToString(), method, providedParameters, auth, parameterPosition, arraySerialization); } else { var uriAuthParam = AddAuthenticationToParameters(uri.ToString(), method, providedParameters, auth, parameterPosition, arraySerialization); foreach (var p in uriAuthParam) { uriParam.Add(p.Key, p.Value); } } } uriParameters = uriParam; }
public ThodexAuthenticationProvider(ApiCredentials credentials, ArrayParametersSerialization arraySerialization) : base(credentials) { if (credentials.Secret == null) { throw new ArgumentException("No valid API credentials provided. Key/Secret needed."); } this.arraySerialization = arraySerialization; }
public BtcTurkAuthenticationProvider(ApiCredentials credentials, ArrayParametersSerialization arraySerialization) : base(credentials) { if (credentials.Secret == null) { throw new ArgumentException("No valid API credentials provided. Key/Secret needed."); } encryptor = new HMACSHA256(Encoding.ASCII.GetBytes(credentials.Secret.GetString())); this.arraySerialization = arraySerialization; }
public OkexAuthenticationProvider(ApiCredentials credentials, string passPhrase, bool signPublicRequests, ArrayParametersSerialization arraySerialization) : base(credentials) { if (credentials == null || credentials.Secret == null) { throw new ArgumentException("No valid API credentials provided. Key/Secret needed."); } this.PassPhrase = passPhrase.ToSecureString(); this.encryptor = new HMACSHA256(Encoding.ASCII.GetBytes(credentials.Secret.GetString())); this.signPublicRequests = signPublicRequests; this.arraySerialization = arraySerialization; }
/// <summary> /// Authenticate a request. Output parameters should include the providedParameters input /// </summary> /// <param name="apiClient">The Api client sending the request</param> /// <param name="uri">The uri for the request</param> /// <param name="method">The method of the request</param> /// <param name="providedParameters">The request parameters</param> /// <param name="auth">If the requests should be authenticated</param> /// <param name="arraySerialization">Array serialization type</param> /// <param name="parameterPosition">The position where the providedParameters should go</param> /// <param name="uriParameters">Parameters that need to be in the Uri of the request. Should include the provided parameters if they should go in the uri</param> /// <param name="bodyParameters">Parameters that need to be in the body of the request. Should include the provided parameters if they should go in the body</param> /// <param name="headers">The headers that should be send with the request</param> public abstract void AuthenticateRequest( RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary <string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, out SortedDictionary <string, object> uriParameters, out SortedDictionary <string, object> bodyParameters, out Dictionary <string, string> headers );
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(new Dictionary <string, string>()); } if (Credentials.Key == null) { throw new ArgumentException("ApiKey/Secret needed"); } var result = new Dictionary <string, string>(); lock (locker) result.Add("Api-Key", Credentials.Key.GetString()); result.Add("Api-Timestamp", Math.Round((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(CultureInfo.InvariantCulture)); var jsonContent = ""; if (method != HttpMethod.Get && method != HttpMethod.Delete) { jsonContent = JsonConvert.SerializeObject(parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value)); } result.Add("Api-Content-Hash", ByteToString(encryptor.ComputeHash(Encoding.UTF8.GetBytes(jsonContent))).ToLower(CultureInfo.InvariantCulture)); uri = WebUtility.UrlDecode(uri); // Sign needs the query parameters to not be encoded var sign = result["Api-Timestamp"] + uri + method + result["Api-Content-Hash"] + ""; result.Add("Api-Signature", ByteToString(encryptorHmac.ComputeHash(Encoding.UTF8.GetBytes(sign)))); return(result); }
public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary <string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, out SortedDictionary <string, object> uriParameters, out SortedDictionary <string, object> bodyParameters, out Dictionary <string, string> headers) { uriParameters = parameterPosition == HttpMethodParameterPosition.InUri ? new SortedDictionary <string, object>(providedParameters) : new SortedDictionary <string, object>(); bodyParameters = parameterPosition == HttpMethodParameterPosition.InBody ? new SortedDictionary <string, object>(providedParameters) : new SortedDictionary <string, object>(); headers = new Dictionary <string, string>(); if (!auth) { return; } var parameters = parameterPosition == HttpMethodParameterPosition.InUri ? uriParameters: bodyParameters; parameters.Add("access_id", Credentials.Key !.GetString()); parameters.Add("tonce", _nonceProvider.GetNonce()); headers.Add("Authorization", SignMD5(uri.SetParameters(parameters).Query.Replace("?", "") + "&secret_key=" + Credentials.Secret !.GetString())); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, HttpMethodParameterPosition parameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(new Dictionary <string, string>()); } if (Credentials.Key == null) { throw new ArgumentException("ApiKey/Secret needed"); } var nonce = parameters.Single(n => n.Key == "nonce").Value; var paramList = parameters.OrderBy(o => o.Key != "nonce"); var pars = string.Join("&", paramList.Select(p => $"{p.Key}={p.Value}")); var result = new Dictionary <string, string> { { "API-Key", Credentials.Key.GetString() } }; var np = nonce + pars; byte[] nonceParamsBytes; using (var sha = SHA256.Create()) nonceParamsBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(np)); var pathBytes = Encoding.UTF8.GetBytes(uri.Split(new[] { ".com" }, StringSplitOptions.None)[1].Split('?')[0]); var allBytes = pathBytes.Concat(nonceParamsBytes).ToArray(); var sign = encryptor.ComputeHash(allBytes); result.Add("API-Sign", Convert.ToBase64String(sign)); return(result); }
public override Dictionary <string, object> AddAuthenticationToParameters(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, HttpMethodParameterPosition parameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(parameters); } parameters.Add("nonce", _nonceProvider.GetNonce()); return(parameters); }
public Dictionary <string, object> AddAuthenticationToParameters(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, HttpMethodParameterPosition postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(parameters); } if (uri.Contains("v2")) { parameters.Add("access_key", Credentials.Key.GetString()); var n = Nonce; parameters.Add("tonce", n); parameters = parameters.OrderBy(p => p.Key).ToDictionary(k => k.Key, v => v.Value); var paramString = parameters.CreateParamString(false, ArrayParametersSerialization.MultipleValues); var signData = method + "|"; signData += uri + "|"; signData += paramString; byte[] signBytes; lock (encryptLock) signBytes = encryptor.ComputeHash(Encoding.UTF8.GetBytes(signData)); parameters.Add("signature", ByteArrayToString(signBytes)); } return(parameters); }
public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary <string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, out SortedDictionary <string, object> uriParameters, out SortedDictionary <string, object> bodyParameters, out Dictionary <string, string> headers) { uriParameters = parameterPosition == HttpMethodParameterPosition.InUri ? new SortedDictionary <string, object>(providedParameters) : new SortedDictionary <string, object>(); bodyParameters = parameterPosition == HttpMethodParameterPosition.InBody ? new SortedDictionary <string, object>(providedParameters) : new SortedDictionary <string, object>(); headers = new Dictionary <string, string>(); if (!auth) { return; } // Auth requests are always POST if (uri.AbsolutePath.Contains("v1")) { bodyParameters.Add("request", uri.AbsolutePath); bodyParameters.Add("nonce", _nonceProvider.GetNonce().ToString()); var signature = JsonConvert.SerializeObject(bodyParameters); var payload = Convert.ToBase64String(Encoding.ASCII.GetBytes(signature)); var signedData = Sign(payload); headers.Add("X-BFX-APIKEY", Credentials.Key !.GetString()); headers.Add("X-BFX-PAYLOAD", payload); headers.Add("X-BFX-SIGNATURE", signedData.ToLower(CultureInfo.InvariantCulture)); } else if (uri.AbsolutePath.Contains("v2")) { var json = JsonConvert.SerializeObject(bodyParameters); var n = _nonceProvider.GetNonce().ToString(); var signature = $"/api{uri.AbsolutePath}{n}{json}"; var signedData = SignHMACSHA384(signature); headers.Add("bfx-apikey", Credentials.Key !.GetString()); headers.Add("bfx-nonce", n); headers.Add("bfx-signature", signedData.ToLower(CultureInfo.InvariantCulture)); } }
//Create hmac signature with secretKey as a key and sign request "method+timestamp+url+body" //Like as basic authorization add header "Authorization: HS256 " + base64(publicKey+":"+timestamp+":"+signature hex encoded) public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameters, ArrayParametersSerialization arrayParametersSerialization) { // var time = TimeStamp; //if (!signed) // return new Dictionary<string, string>(); //var result = new Dictionary<string, string>(); //string additionalData = String.Empty; //if (parameters != null && parameters.Any() && method != HttpMethod.Delete && method != HttpMethod.Get) //{ // additionalData = JsonConvert.SerializeObject(parameters.ToDictionary(p => p.Key, p => p.Value)); //} //var dataToSign = CreateAuthPayload(method, time, uri.Split(new[] { ".com" }, StringSplitOptions.None)[1], additionalData); //var signedData = Sign(dataToSign); //var b64 = Base64Encode($"{Credentials.Key.GetString()}:{time}:{signedData}"); //result.Add("Authorization", $"HS256 {b64}"); //return result; if (!signed) { return(new Dictionary <string, string>()); } if (Credentials.Key == null) { throw new ArgumentException("ApiKey/Secret needed"); } if (Credentials.Secret == null) { throw new ArgumentException("ApiKey/Secret needed"); } var result = new Dictionary <string, string>(); var encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(Credentials.Key.GetString() + ":" + Credentials.Secret.GetString())); result.Add("Authorization", "Basic " + encoded); return(result); }
public override Dictionary <string, object> AddAuthenticationToParameters(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameters, ArrayParametersSerialization arraySerialization) { return(base.AddAuthenticationToParameters(uri, method, parameters, signed, postParameters, arraySerialization)); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(new Dictionary <string, string>()); } var result = new Dictionary <string, string>(); lock (locker) { parameters.Add("nonce", nonce); result.Add("Key", Credentials.Key.GetString()); var lDataToSign = ConstructDataToSign(parameters); var lSignedValue = ByteToString(encryptor.ComputeHash(Encoding.UTF8.GetBytes(lDataToSign))); result.Add("Sign", lSignedValue); } return(result); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameters, ArrayParametersSerialization arrayParametersSerialization) { var _nonce = Util.Util.GetMillisecondsFromEpochStart(); if (!signed) { return(new Dictionary <string, string>()); } var result = new Dictionary <string, string>(); result.Add("FTX-KEY", Credentials.Key.GetString()); result.Add("FTX-TS", _nonce.ToString()); string additionalData = String.Empty; if (parameters != null && parameters.Any() && method != HttpMethod.Delete && method != HttpMethod.Get) { additionalData = JsonConvert.SerializeObject(parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value)); } var dataToSign = CreateAuthPayload(method, uri.Split(new[] { ".com" }, StringSplitOptions.None)[1], _nonce, additionalData); var signedData = Sign(dataToSign); result.Add("FTX-SIGN", signedData); result.AddOptionalParameter("FTX-SUBACCOUNT", SubAccount); return(result); }
protected virtual IRequest BtcTurkConstructRequest(Uri uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postPosition, ArrayParametersSerialization arraySerialization, int requestId) { if (parameters == null) { parameters = new Dictionary <string, object>(); } var uriString = uri.ToString(); if (authProvider != null) { parameters = authProvider.AddAuthenticationToParameters(uriString, method, parameters, signed, postPosition, arraySerialization); } if ((method == HttpMethod.Get || method == HttpMethod.Delete || postParametersPosition == PostParameters.InUri) && parameters?.Any() == true) { uriString += "?" + parameters.CreateParamString(true, ArrayParametersSerialization.MultipleValues); } if (method == HttpMethod.Post && signed) { var uriParamNames = new[] { "AccessKeyId", "SignatureMethod", "SignatureVersion", "Timestamp", "Signature" }; var uriParams = parameters.Where(p => uriParamNames.Contains(p.Key)).ToDictionary(k => k.Key, k => k.Value); uriString += "?" + uriParams.CreateParamString(true, ArrayParametersSerialization.MultipleValues); parameters = parameters.Where(p => !uriParamNames.Contains(p.Key)).ToDictionary(k => k.Key, k => k.Value); } var request = RequestFactory.Create(method, uriString, requestId); var contentType = requestBodyFormat == RequestBodyFormat.Json ? Constants.JsonContentHeader : Constants.FormContentHeader; request.Accept = Constants.JsonContentHeader; var headers = new Dictionary <string, string>(); if (authProvider != null) { headers = authProvider.AddAuthenticationToHeaders(uriString, method, parameters !, signed, postPosition, arraySerialization); } foreach (var header in headers) { request.AddHeader(header.Key, header.Value); } if ((method == HttpMethod.Post || method == HttpMethod.Put) && postParametersPosition != PostParameters.InUri) { if (parameters?.Any() == true) { WriteParamBody(request, parameters, contentType); } else { request.SetContent("{}", contentType); } } return(request); }
protected override IRequest ConstructRequest(Uri uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postPosition, ArrayParametersSerialization arraySerialization, int requestId) { return(this.BtcTurkConstructRequest(uri, method, parameters, signed, postPosition, arraySerialization, requestId)); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(new Dictionary <string, string>()); } if (Credentials.Key == null) { throw new ArgumentException("No valid API credentials provided. Key/Secret needed."); } var query = CreateParamOrderedString(parameters, true, arraySerialization); return(new Dictionary <string, string> { { "Api-Key", Credentials.Key.GetString() }, { "Sign", ByteToString(encryptor.ComputeHash(Encoding.UTF8.GetBytes(query))) } }); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(new Dictionary <string, string>()); } var result = new Dictionary <string, string>(); lock (locker) result.Add("apisign", ByteToString(encryptor.ComputeHash(Encoding.UTF8.GetBytes(uri)))); return(result); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(new Dictionary <string, string>()); } if (ExanteCredentials.ApplicationId is null || ExanteCredentials.ClientId is null || ExanteCredentials.SharedKey is null) { throw new ArgumentException("No valid API credentials"); } var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ExanteCredentials.SharedKey)); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); var payload = new JwtPayload(ExanteCredentials.ClientId, null, new[] { new Claim(JwtRegisteredClaimNames.Sub, ExanteCredentials.ApplicationId), new Claim(JwtRegisteredClaimNames.Iss, ExanteCredentials.ClientId), }, new Dictionary <string, object> { { JwtRegisteredClaimNames.Aud, tokenPermissions } }, null, DateTime.UtcNow.AddHours(1), DateTime.UtcNow ); var securityToken = new JwtSecurityToken(new JwtHeader(credentials), payload); var token = new JwtSecurityTokenHandler().WriteToken(securityToken); return(new Dictionary <string, string> { { "Authorization", $"Bearer {token}" } }); }
public Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, HttpMethodParameterPosition postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(new Dictionary <string, string>()); } var result = new Dictionary <string, string>(); if (uri.Contains("v3")) { var json = JsonConvert.SerializeObject(parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value)); if (string.IsNullOrEmpty(json) && method == HttpMethod.Post) { json = "{}"; } var n = Nonce; var signature = $"{uri.Split(new[] { ".io" }, StringSplitOptions.None)[1]}{n}{json}"; var signedData = SignV3(signature); result.Add("kun-apikey", Credentials.Key.GetString()); result.Add("kun-nonce", n); result.Add("kun-signature", signedData.ToLower()); } return(result); }
public override Dictionary <string, object> AddAuthenticationToParameters(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed && !signPublicRequests) { return(parameters); } if (Credentials.Key == null) { throw new ArgumentException("ApiKey/secret not provided"); } var uriObj = new Uri(uri); var signParameters = new Dictionary <string, object> { { "AccessKeyId", Credentials.Key.GetString() }, { "SignatureMethod", "HmacSHA256" }, { "SignatureVersion", 2 } }; if (!parameters.ContainsKey("Timestamp") || method != HttpMethod.Get) { signParameters.Add("Timestamp", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss")); } if (method == HttpMethod.Get) { foreach (var kvp in parameters) { signParameters.Add(kvp.Key, kvp.Value); } } signParameters = signParameters.OrderBy(kv => Encoding.UTF8.GetBytes(WebUtility.UrlEncode(kv.Key)), new ByteOrderComparer()).ToDictionary(k => k.Key, k => k.Value); var paramString = signParameters.CreateParamString(true, ArrayParametersSerialization.MultipleValues); signParameters = signParameters.OrderBy(kv => kv.Key).ToDictionary(k => k.Key, k => k.Value); var absolutePath = uriObj.AbsolutePath; if (absolutePath.StartsWith("/api")) { // Russian api has /api prefix which shouldn't be part of the signature absolutePath = absolutePath.Substring(4); } var signData = method + "\n"; signData += uriObj.Host + "\n"; signData += absolutePath + "\n"; signData += paramString; byte[] signBytes; lock (encryptLock) signBytes = encryptor.ComputeHash(Encoding.UTF8.GetBytes(signData)); signParameters.Add("Signature", Convert.ToBase64String(signBytes)); if (method != HttpMethod.Get) { foreach (var kvp in parameters) { signParameters.Add(kvp.Key, kvp.Value); } } return(signParameters); }
public BinanceAuthenticationProvider(ApiCredentials credentials, ArrayParametersSerialization arraySerialization) : base(credentials) { encryptor = new HMACSHA256(Encoding.ASCII.GetBytes(credentials.Secret.GetString())); this.arraySerialization = arraySerialization; }
/// <summary> /// Create a query string of the specified parameters /// </summary> /// <param name="parameters">The parameters to use</param> /// <param name="urlEncodeValues">Whether or not the values should be url encoded</param> /// <param name="serializationType">How to serialize array parameters</param> /// <returns></returns> private static string CreateParamOrderedString(Dictionary <string, object> parameters, bool urlEncodeValues, ArrayParametersSerialization serializationType) { var uriString = ""; foreach (var entry in parameters.OrderBy(kp => kp.Key).ToList()) { if (entry.Value.GetType().IsArray) { if (serializationType == ArrayParametersSerialization.Array) { uriString += $"{string.Join("&", ((object[])(urlEncodeValues ? Uri.EscapeDataString(entry.Value.ToString()) : entry.Value)).Select(v => $"{entry.Key}[]={v}"))}&"; } else { var array = (Array)entry.Value; uriString += string.Join("&", array.OfType <object>().Select(a => $"{entry.Key}={Uri.EscapeDataString(a.ToString())}")); uriString += "&"; } } else { uriString += $"{entry.Key}={(urlEncodeValues ? Uri.EscapeDataString(entry.Value.ToString()) : entry.Value)}"; uriString += "&"; } } uriString = uriString.TrimEnd('&'); return(uriString); }
/// <summary> /// Create a query string of the specified parameters /// </summary> /// <param name="parameters">The parameters to use</param> /// <param name="urlEncodeValues">Whether or not the values should be url encoded</param> /// <param name="serializationType">How to serialize array parameters</param> /// <returns></returns> public static string CreateParamString(this Dictionary <string, object> parameters, bool urlEncodeValues, ArrayParametersSerialization serializationType) { var uriString = ""; var arraysParameters = parameters.Where(p => p.Value.GetType().IsArray).ToList(); foreach (var arrayEntry in arraysParameters) { if (serializationType == ArrayParametersSerialization.Array) { uriString += $"{string.Join("&", ((object[])(urlEncodeValues ? WebUtility.UrlEncode(arrayEntry.Value.ToString()) : arrayEntry.Value)).Select(v => $"{arrayEntry.Key}[]={v}"))}&"; } else { var array = (Array)arrayEntry.Value; uriString += string.Join("&", array.OfType <object>().Select(a => $"{arrayEntry.Key}={WebUtility.UrlEncode(a.ToString())}")); uriString += "&"; } } uriString += $"{string.Join("&", parameters.Where(p => !p.Value.GetType().IsArray).Select(s => $"{s.Key}={(urlEncodeValues ? WebUtility.UrlEncode(s.Value.ToString()) : s.Value)}"))}"; uriString = uriString.TrimEnd('&'); return(uriString); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed && !signPublicRequests) { return(new Dictionary <string, string>()); } if (Credentials == null || Credentials.Key == null || this.PassPhrase == null) { throw new ArgumentException("No valid API credentials provided. Key/Secret/PassPhrase needed."); } var time = (DateTime.UtcNow.ToUnixTimeMilliSeconds() / 1000.0m).ToString(CultureInfo.InvariantCulture); var signtext = time + method.Method.ToUpper() + uri.Replace("https://www.okex.com", "").Trim('?'); if (method == HttpMethod.Post) { var bodyString = JsonConvert.SerializeObject(parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value)); signtext = signtext + bodyString; } var signature = OkexAuthenticationProvider.Base64Encode(encryptor.ComputeHash(Encoding.UTF8.GetBytes(signtext))); return(new Dictionary <string, string> { { "OK-ACCESS-KEY", Credentials.Key.GetString() }, { "OK-ACCESS-SIGN", signature }, { "OK-ACCESS-TIMESTAMP", time }, { "OK-ACCESS-PASSPHRASE", this.PassPhrase.GetString() }, }); }
public override Dictionary <string, object> AddAuthenticationToParameters(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, HttpMethodParameterPosition parameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed && !signPublicRequests) { return(parameters); } return(SignRequest(uri, method, parameters, "AccessKeyId", "SignatureMethod", "SignatureVersion", "Timestamp", "Signature", 2)); }
public override Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (Credentials.Key == null) { throw new ArgumentException("ApiKey/Secret needed"); } var result = new Dictionary <string, string>(); if (!signed) { return(result); } if (uri.Contains("v1")) { var signature = JsonConvert.SerializeObject(parameters); var payload = Convert.ToBase64String(Encoding.ASCII.GetBytes(signature)); var signedData = Sign(payload); result.Add("X-BFX-APIKEY", Credentials.Key.GetString()); result.Add("X-BFX-PAYLOAD", payload); result.Add("X-BFX-SIGNATURE", signedData.ToLower(CultureInfo.InvariantCulture)); } else if (uri.Contains("v2")) { var json = JsonConvert.SerializeObject(parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value)); var n = BitfinexSocketClient.Nonce; var signature = $"/api{uri.Split(new[] { ".com" }, StringSplitOptions.None)[1]}{n}{json}"; var signedData = Sign(signature); result.Add("bfx-apikey", Credentials.Key.GetString()); result.Add("bfx-nonce", n); result.Add("bfx-signature", signedData.ToLower(CultureInfo.InvariantCulture)); } return(result); }
/// <summary> /// Add authentication to the header dictionary /// </summary> /// <param name="uri"></param> /// <param name="method"></param> /// <param name="parameters"></param> /// <param name="signed"></param> /// <param name="postParameterPosition"></param> /// <param name="arraySerialization"></param> /// <returns></returns> public virtual Dictionary <string, string> AddAuthenticationToHeaders(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { return(new Dictionary <string, string>()); }
public override Dictionary <string, object> AddAuthenticationToParameters(string uri, HttpMethod method, Dictionary <string, object> parameters, bool signed, PostParameters postParameterPosition, ArrayParametersSerialization arraySerialization) { if (!signed) { return(parameters); } if (uri.Contains("v1")) { parameters.Add("request", uri.Split(new[] { ".com" }, StringSplitOptions.None)[1]); parameters.Add("nonce", BitfinexSocketClient.Nonce); } else if (uri.Contains("v2")) { // Nothing } return(parameters); }