/// <summary> /// Send an object of type T1, @item, parsed as json string embedded with the /// authentication token, that is build using @user and @token, /// as an HTTP post request to server at address @url. /// This method reutens the server response as is. /// </summary> /// <typeparam name="T1">Type of the data object to send</typeparam> /// <param name="url">address of the server</param> /// <param name="user">username for authentication data</param> /// <param name="token">token for authentication data</param> /// <param name="item">the data item to send in the reuqest</param> /// <returns>the server response</returns> public string SendPostRequest <T1>(string url, string user, string token, T1 item, int nonce) { var auth = new { token, nonce, user }; JObject jsonItem = JObject.FromObject(item); jsonItem.Add("auth", JObject.FromObject(auth)); StringContent content = new StringContent(jsonItem.ToString()); string ans; using (var client = new HttpClient()) { var result = client.PostAsync(url, content).Result; var responseContent = result?.Content?.ReadAsStringAsync().Result; if (responseContent.Equals("Non unique nonce")) { nonce = new Random().Next(Int32.MinValue, Int32.MaxValue); return(SendPostRequest(url, user, token, item, nonce)); } else { string privateKey = System.IO.File.ReadAllText(KEY_PATH); ans = SimpleCtyptoLibrary.decrypt(responseContent, privateKey); } return(ans); } }
public void setToken(string KEY_PATH = @"..\..\..\private_key") { try { string privateKey = System.IO.File.ReadAllText(KEY_PATH); this.token = SimpleCtyptoLibrary.CreateToken(this.user, privateKey); } catch { throw new Exception("Error reading the private key file"); } }
public string createToken(int nonce) { try { return(SimpleCtyptoLibrary.CreateToken(user, privateKey, nonce)); } catch { StackFrame sf = new StackFrame(1, true); Logger.ErrorLog(sf.GetMethod(), sf.GetFileLineNumber(), "There's a problem with reading the private key"); throw new Exception("Error reading the private key file"); } }