public byte CreateAccount(string username, string password, string email) { if (!AccountUtils.IsEmail(email)) { return(CreateAccountResponseCode.invalidEmail); } if (!AccountUtils.IsUsername(username)) { return(CreateAccountResponseCode.invalidUsername); } if (FindAccountByEmail(email) != null) // if account already exists { return(CreateAccountResponseCode.emailAlreadyUsed); } // account credentials are valid string salt = BCryptImplementation.GetRandomSalt(); string hashedPassword = BCryptImplementation.HashPassword(password, salt); // roll for a unique discriminator int rollCount = 0; string discriminator = "0000"; while (FindAccount(username, discriminator) != null) { discriminator = Random.Range(0, 9999).ToString("000"); rollCount++; if (rollCount > 100) { Debug.Log("Rolled over 100 times for account"); return(CreateAccountResponseCode.overUsedUsername); } } Model_Account model = new Model_Account(); model.Username = username; model.Discriminator = discriminator; model.Email = email; model.Salt = salt; model.HashedPassword = hashedPassword; StoreEntity(Controller_Account.BuildEntity(model)); return(CreateAccountResponseCode.success); }
public void submitCreateAccount() { string username = createUsernameField.text; string password1 = createPasswordField.text; string password2 = createPasswordConfirmationField.text; string email = createEmailField.text; // password validation if (password1 != password2) { Debug.Log("Password mismatch"); if (createAccountInfoText != null) { createAccountInfoText.text = "Passwords do not match"; } return; } if (password1.Length < 8) { Debug.Log("Password must be at least 8 characters"); createAccountInfoText.text = "Password must be at least 8 characters"; return; } if (password1.Length > 64) { Debug.Log("Maximum password length is 64 characters"); createAccountInfoText.text = "Maximum password length is 64 characters"; return; } // username validation if (username.Length < 4) { Debug.Log("Username must be at least 4 characters"); createAccountInfoText.text = "Username must be at least 4 characters"; return; } if (username.Length > 16) { Debug.Log("Username must be 16 characters or less"); createAccountInfoText.text = "Username must be 16 characters or less"; return; } if (!AccountUtils.IsUsername(username)) { Debug.Log("Username contains invalid characters. Only use a-z (capital or lowercase) and 0-9"); createAccountInfoText.text = "Username contains invalid characters. Only use a-z (capital or lowercase) and 0-9"; return; } // email validation if (!AccountUtils.IsEmail(email)) { Debug.Log(email + " is not an email"); createAccountInfoText.text = email + " is not an email"; return; } // all validation is done so can send server a message and lock the UI if (Client.Instance.getIsStarted()) // make sure we're connected to the internet first { lockUi(); Net_CreateAccount netMsg = new Net_CreateAccount(); netMsg.Email = email; netMsg.Password = password1; netMsg.Username = username; Client.Instance.SendServer(netMsg); } else { throw new Exception("Client is not connected to server"); } }