public string Login(string msg, int clientId) { string[] fields = msg.Split("$$", StringSplitOptions.RemoveEmptyEntries); string username = fields[0].Split(':', StringSplitOptions.RemoveEmptyEntries)[1]; string password = fields[1].Split(':', StringSplitOptions.RemoveEmptyEntries)[1]; string passwordHash = ""; DbMethods dbConnection = new DbMethods(); lock (activeUsers[clientId]) { dbConnection = activeUsers[clientId].dbConnection; } try { passwordHash = dbConnection.GetFromUser("password_hash", username); } catch { return(TransmisionProtocol.CreateServerMessage(ErrorCodes.USER_NOT_FOUND, Options.LOGIN)); } if (Security.VerifyPassword(passwordHash, password)) { lock (activeUsers) { foreach (User u in activeUsers) { if (u != null) { if (u.userName == username && u.logged) { return(TransmisionProtocol.CreateServerMessage(ErrorCodes.USER_ALREADY_LOGGED_IN, Options.LOGIN)); } } } activeUsers[clientId].logged = true; activeUsers[clientId].userName = username; activeUsers[clientId].userId = dbConnection.GetUserId(username); } return(TransmisionProtocol.CreateServerMessage(ErrorCodes.NO_ERROR, Options.LOGIN, dbConnection.GetFromUser("iv_to_decrypt_user_key", username), dbConnection.GetFromUser("user_key_hash", username))); } else { return(TransmisionProtocol.CreateServerMessage(ErrorCodes.INCORRECT_PASSWORD, Options.LOGIN)); } }
public string Login(string msg, int clientId) { // Get message as object Login login = MessageProccesing.DeserializeObject(msg) as Login; // Get password hash from DB string passwordHash; DbMethods dbConnection = new DbMethods(); lock (activeUsers[clientId]) { dbConnection = activeUsers[clientId].dbConnection; } try { passwordHash = dbConnection.GetFromUser("password_hash", login.username); } catch { return(MessageProccesing.CreateMessage(ErrorCodes.USER_NOT_FOUND)); } // Verify password if (Security.VerifyPassword(passwordHash, login.passwordHash)) { lock (activeUsers) { // Check if user isnt already logged in foreach (User u in activeUsers) { if (u != null) { if (u.username == login.username && u.logged) { return(MessageProccesing.CreateMessage(ErrorCodes.USER_ALREADY_LOGGED_IN)); } } } // If user isnt already logged in, add data to activeUsers activeUsers[clientId].username = login.username; activeUsers[clientId].logged = true; activeUsers[clientId].userId = dbConnection.GetUserId(login.username); } // Start async thread eventHandlers[activeUsers[clientId].username] = new EventWaitHandle(false, EventResetMode.ManualReset); userLoginHandler[clientId].Set(); if (!whichFunction.ContainsKey(activeUsers[clientId].username)) { whichFunction[activeUsers[clientId].username] = new List <Tuple <Options, string> >(); } List <string> friends = activeUsers[clientId].dbConnection.GetFriendsNames(activeUsers[clientId].username); foreach (var key in friends) { // Check if friend is active if (activeUsers.Contains(new User { username = key })) { // Send to active friend information about activity of user lock (whichFunction[key]) { whichFunction[key].Add(new Tuple <Options, string>(Options.ACTIVE_FRIENDS, activeUsers[clientId].username)); eventHandlers[key].Set(); } } } // Send invitations lock (whichFunction[activeUsers[clientId].username]) whichFunction[activeUsers[clientId].username].Add(new Tuple <Options, string>(Options.FRIEND_INVITATIONS, activeUsers[clientId].username)); eventHandlers[activeUsers[clientId].username].Set(); return(MessageProccesing.CreateMessage(ErrorCodes.NO_ERROR)); } else { return(MessageProccesing.CreateMessage(ErrorCodes.INCORRECT_PASSWORD)); } }