public User Register([FromBody] User user) { _conn = new Connection(_connectionString); _conn.OpenConnection(); //Calculate MD5 hash MD5 md5 = MD5.Create(); byte[] inputBytes = Encoding.ASCII.GetBytes(user.Username + user.PasswordHash + DateTime.Now); byte[] hash = md5.ComputeHash(inputBytes); StringBuilder tokenString = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { tokenString.Append(hash[i].ToString("X2")); } bool result = _conn.Register(user.Username, user.PasswordHash, tokenString.ToString()); _conn.CloseConnection(); if (result) { user.Token = tokenString.ToString(); return user; } else { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Conflict)); } }
public IEnumerable<Document> SearchDocuments(string token, string pattern) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); return !string.IsNullOrEmpty(_conn.GetUsernameByToken(token)) ? _conn.SearchDocuments(pattern) : null; } finally { _conn.CloseConnection(); } }
public bool Delete([FromUri] string token, [FromUri] long id) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); return !string.IsNullOrEmpty(_conn.GetUsernameByToken(token)) && _conn.DeleteDocument(id); } finally { _conn.CloseConnection(); } }
public NetworkCredential GetFtpCredential([FromUri] string token) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); string username = _conn.GetUsernameByToken(token); return !string.IsNullOrEmpty(username) ? _accessCredential : null; } finally { _conn.CloseConnection(); } }
public User Login([FromBody] User user) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); string token = _conn.Login(user.Username, user.PasswordHash); if (!string.IsNullOrEmpty(token)) { user.Token = token; return user; } else { throw new Exception("Error login"); } } finally { _conn.CloseConnection(); } }
public Document UploadDocument(string token, string name, long size, string description) { _conn = new Connection(_connectionString); _conn.OpenConnection(); string username = _conn.GetUsernameByToken(token); try { if (!string.IsNullOrEmpty(username)) { Document doc = new Document() { Name = name, Size = size, Author = username, Path = _ftpUri + name, Description = description }; return _conn.UploadDocument(doc) ? doc : null; } else { return null; } } finally { _conn.CloseConnection(); } }