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);
    }