Example #1
0
    public Model_Account LoginAccount(string usernameOrEmail, string password, int cnnId, string token)
    {
        Model_Account accountModel = null;
        Query         q            = null;

        if (AccountUtils.IsEmail(usernameOrEmail))
        {
            // login via email
            q = new Query(Controller_Account.Enum.account)
            {
                Filter = Filter.Equal(Controller_Account.Enum.email, usernameOrEmail)
            };
        }
        else
        {
            // login with username + discriminator
            string[] userDiscriminator = usernameOrEmail.Split('#');
            if (userDiscriminator[1] != null)
            {
                q = new Query(Controller_Account.Enum.account)
                {
                    Filter = Filter.And(
                        Filter.Equal(Controller_Account.Enum.username, userDiscriminator[0]),
                        Filter.Equal(Controller_Account.Enum.discriminator, userDiscriminator[1]))
                };
            }
        }
        // perform query to find the account
        Entity accountEntity = GetOneResult(q);

        if (accountEntity != null)
        {
            accountModel = Controller_Account.BuildController(accountEntity).model;
            if (!BCryptImplementation.ValidatePassword(accountModel, password))
            {
                return(null);
            }

            // perform login
            accountModel.ActiveConnection = cnnId;
            accountModel.Token            = token;
            accountModel.Status           = 1; // status of 1 means logged in
            accountModel.LastLogin        = System.DateTime.Now;
            StoreEntity(Controller_Account.BuildEntity(accountModel));
        }
        return(accountModel);
    }
Example #2
0
    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);
    }
Example #3
0
    public void submitLoginRequest()
    {
        string usernameOrEmail = loginUsernameOrEmailField.text;
        string password        = loginPasswordField.text;

        // validate username or email
        if (!AccountUtils.IsUsernameAndDiscriminator(usernameOrEmail) && !AccountUtils.IsEmail(usernameOrEmail))
        {
            loginInfoText.text  = "Please user your email or username#0000 to sign in";
            loginInfoText.color = Color.red;
            return;
        }

        Net_LoginRequest lr = new Net_LoginRequest();

        lr.UsernameOrEmail = usernameOrEmail;
        lr.Password        = password;
        Client.Instance.SendServer(lr);
    }
Example #4
0
    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");
        }
    }