public static AuthResult Authorize(AuthData data) { Keys keys = GetKeys(); if (keys != null && keys.Public.Equals(data.PublicKey)) { var privateKey = keys.Private; var hashCheck = General.Sha1Hash(data.Data + privateKey + data.PublicKey); if (hashCheck.Equals(data.Hash)) { var newToken = General.Sha1Hash(privateKey + hashCheck + GetDateTimeFormatted()); var computersJsonFile = GetFile("~/App_Data/", "computers.json"); var computers = new List<ComputerData>(); try { computers = JsonConvert.DeserializeObject<List<ComputerData>>(GetFileContents(computersJsonFile)); } catch { } var computerData = new ComputerData() { Name = data.HostName, IpExternal = data.IpExternal, IpInternal = data.IpInternal, LastActive = DateTime.Now, FileUploaded = null, BytesUploaded = 0, Hash = null }; computerData.Hash = Transmitter.GetComputerHash(computerData); if (computers.Where(c => c.Hash == computerData.Hash).FirstOrDefault() == null) { computers.Add(computerData); } var computersJson = JsonConvert.SerializeObject(computers); File.WriteAllText(computersJsonFile, computersJson); return new AuthResult() { Token = newToken, IpExternal = data.IpExternal }; } } return null; }
public static bool UpdateLastActive(AuthData data) { Keys keys = GetKeys(); if (keys != null && keys.Public.Equals(data.PublicKey)) { var privateKey = keys.Private; var hashCheck = General.Sha1Hash(data.Data + privateKey + data.PublicKey); if (hashCheck.Equals(data.Hash)) { //var newToken = General.Sha1Hash(privateKey + hashCheck + GetDateTimeFormatted()); var computersJsonFile = GetFile("~/App_Data/", "computers.json"); var computers = new List<ComputerData>(); try { computers = JsonConvert.DeserializeObject<List<ComputerData>>(GetFileContents(computersJsonFile)); } catch { } var computerData = new ComputerData() { Name = data.HostName, IpExternal = data.IpExternal, IpInternal = data.IpInternal, LastActive = DateTime.Now, FileUploaded = null, BytesUploaded = 0, Hash = null }; computerData.Hash = Transmitter.GetComputerHash(computerData); var computer = computers.Find(c => c.Hash == computerData.Hash); if(computer != null) { computer.LastActive = DateTime.Now; } var computersJson = JsonConvert.SerializeObject(computers); System.Threading.Thread.Sleep(250); File.WriteAllText(computersJsonFile, computersJson); return true; } } return false; }
//todo: refactor public static string GetComputerHash(ComputerData data) { if (data == null) return null; return General.Sha1Hash(data.Name + data.IpExternal + data.IpInternal); }