bool _passwordCheck(string password, ref AccountCreationFailFlags failFlags) { // Keeping it simple for now. Complex passwords introduce insecurity. if (password.Length < 6) { failFlags = failFlags | AccountCreationFailFlags.PasswordTooShort; return(false); } return(true); }
/// <summary> /// Performs a bunch of checks and returns a created account if succesful /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <param name="persistAccount">If false, account will not be stored in the manager or the db</param> /// <returns>Check flags if returned account is null</returns> public async Task <Tuple <Account, AccountCreationFailFlags> > CreateAccountAsync(string username, string password, bool persistAccount = true) { AccountCreationFailFlags failFlags = 0; // Checks _usernameCharCheck(username, ref failFlags); _passwordCheck(password, ref failFlags); var res = await _usernameExistsAsync(username, failFlags); failFlags = res.Item2; Account retAccount = null; if (failFlags == 0) { #if DEBUG //ConsoleManager.WriteToFreeLine("Creating Account with username " + username); #endif retAccount = new Account(username, password, _accountIDManager.PopFreeID(), false); if (persistAccount) { await _databaseManager.SaveAsync(retAccount); var writeResult = await _usernameExistsAsync(username, 0); if (writeResult.Item1) { #if DEBUG ConsoleManager.WriteLine("DB write failed when creating a new account."); #endif _accountIDManager.PushFreeID(retAccount.Id); retAccount = null; failFlags = failFlags | AccountCreationFailFlags.DBWriteFailed; } } } if (persistAccount && retAccount != null) { RegisterAccount(retAccount); } return(new Tuple <Account, AccountCreationFailFlags>(retAccount, failFlags)); }
bool _usernameCharCheck(string username, ref AccountCreationFailFlags failFlags) { List <char> acceptableUsernameChars = new List <char> { '_', '-', '=' }; // We should move this if we want to keep it. foreach (char c in username) { if (!(char.IsLetterOrDigit(c) || acceptableUsernameChars.Contains(c))) { failFlags = failFlags | AccountCreationFailFlags.InvalidCharactersUsername; return(false); } } return(true); //return username.All( // c=>{return char.IsLetterOrDigit(c) || acceptableUsernameChars.Contains(c); // }); }
/// <summary> /// returned value .Item2 adds to any flags in argument message. /// </summary> /// <param name="username"></param> /// <param name="message"></param> /// <returns></returns> async Task <Tuple <bool, AccountCreationFailFlags> > _usernameExistsAsync(string username, AccountCreationFailFlags message) { var loadedAccount = await _databaseManager.GetAccountAsync(username); if (loadedAccount != null) { return(new Tuple <bool, AccountCreationFailFlags>(false, message | AccountCreationFailFlags.UsernameExists)); } return(new Tuple <bool, AccountCreationFailFlags>(true, message)); }