Ejemplo n.º 1
0
    public bool InsertAccount(string username, string password, string email)
    {
        //check valid email
        if (!Utility.IsEmail(email))
        {
            Debug.Log(email + " is not a valid");
            return(false);
        }

        //check valid username
        if (!Utility.IsUsername(username))
        {
            Debug.Log(username + " is not a valid");
            return(false);
        }

        //check if the account already exist
        if (FindAccountByEmail(email) != null)
        {
            Debug.Log(email + " already being used");
            return(false);
        }

        Model_Account newAccount = new Model_Account();

        newAccount.Username    = username;
        newAccount.ShaPassword = password;
        newAccount.Email       = email;

        accountsCollection.InsertOne(newAccount);

        return(true);
    }
Ejemplo n.º 2
0
    public Model_Account LoginAccount(string usernameOrEmail, string password, int cnnId, string token)
    {
        Model_Account accountForLoggining = null;

        //Find account
        if (Utility.IsEmail(usernameOrEmail))
        {
            //if looging use email
            accountForLoggining = accountsCollection.Find(u => u.Email.Equals(usernameOrEmail) && u.ShaPassword.Equals(password)).FirstOrDefault();
        }
        else
        {
            //if logging use username
            accountForLoggining = accountsCollection.Find(u => u.Username.Equals(usernameOrEmail) && u.ShaPassword.Equals(password)).FirstOrDefault();
        }

        if (accountForLoggining != null)
        {
            accountForLoggining.ActiveConnection = cnnId;
            accountForLoggining.Token            = token;
            accountForLoggining.Status           = 1;
            accountForLoggining.LastLogin        = System.DateTime.Now;

            accountsCollection.FindOneAndReplace(u => u.Username.Equals(accountForLoggining.Username), accountForLoggining);
        }
        else
        {
        }

        return(accountForLoggining);
    }
    public static Controller_Account BuildController(Entity e)
    {
        Controller_Account controller = new Controller_Account();
        Model_Account      model      = new Model_Account();
        Value v;

        model.Username       = e.Properties[Enum.username].StringValue;
        model.HashedPassword = e.Properties[Enum.hashedPassword].StringValue;
        model.Discriminator  = e.Properties[Enum.discriminator].StringValue;

        if (e.Properties.TryGetValue(Enum.activeConnection, out v))
        {
            model.ActiveConnection = (int)v.IntegerValue;
        }
        if (e.Properties.TryGetValue(Enum.status, out v))
        {
            model.Status = (byte)v.IntegerValue;
        }
        if (e.Properties.TryGetValue(Enum.token, out v))
        {
            model.Token = v.StringValue;
        }
        if (e.Properties.TryGetValue(Enum.lastLogin, out v))
        {
            model.LastLogin = new DateTime(v.IntegerValue);
        }
        if (e.Properties.TryGetValue(Enum.salt, out v))
        {
            model.Salt = v.StringValue;
        }


        controller.model = model;
        return(controller);
    }
Ejemplo n.º 4
0
    private void LoginRequest(int cnnId, int channelId, int recHostId, Net_LoginRequest lr)
    {
        // Debug.Log(string.Format("{0},{1}",lr.UsernameOrEmail,lr.Password));

        string             randomToken = Utility.GenerateRandom(4); //Change to 256 on production
        Model_Account      account     = db.LoginAccount(lr.UsernameOrEmail, lr.Password, cnnId, randomToken);
        Net_OnLoginRequest olr         = new Net_OnLoginRequest();

        if (account != null)
        {
            olr.Success     = 1;
            olr.Information = "You have been logged in as" + account.Username;

            olr.Username      = account.Username;
            olr.Discriminator = account.Username;
            olr.Token         = account.Token;
            olr.ConnectionId  = cnnId;
        }
        else
        {
            olr.Success = 0;
        }


        SendClient(recHostId, cnnId, olr);
    }
Ejemplo n.º 5
0
    public Model_Account InsertAccount(string username, string password)
    {
        Model_Account newAccount = null;

        //Checkea si el username es valido
        if (!Utility.IsUsername(username))
        {
            Debug.Log(username + "is not an username");
            return(newAccount);
        }

        //Checkea si la cuenta existe
        newAccount = FindAccountByUsername(username);
        if (newAccount.Discriminator == "failed")
        {
            Debug.Log(username + "has no discriminator avaiable, please change your username");
            return(newAccount);
        }
        newAccount.ShaPassword  = Utility.Sha256FromString(password);
        newAccount.isGameMaster = false;
        newAccount.LastLogin    = System.DateTime.Now;
        //newAccount.Email = email;
        accounts.InsertOne(newAccount);
        //accounts.Insert(newAccount);

        return(newAccount);
    }
Ejemplo n.º 6
0
    public Model_Account LoginAccount(string usernameOrEmail, string password)
    {
        Model_Account myAccount = null;

        string[] data = usernameOrEmail.Split('#');

        //Find my account
        if (!Utility.isEmail(usernameOrEmail)) //Si me he loggeado usando el username#Discriminator
        {
            if (data.Length > 1)               //Si existe el Discriminator,continuamos
            {
                myAccount = accounts.Find(u => u.Username.Equals(data[0]) && u.Discriminator.Equals(data[1]) && u.ShaPassword.Equals(password)).SingleOrDefault();
            }
        }

        if (myAccount != null) //Si hemos encontrado la cuenta
        {
            accounts.UpdateOne(u => u.Username.Equals(data[0]) && u.Discriminator.Equals(data[1]) && u.ShaPassword.Equals(password), Builders <Model_Account> .Update.Set(u => u.LastLogin, System.DateTime.Now));
        }
        else
        {
            //No hemos encontrado la cuenta
        }

        return(myAccount);
    }
Ejemplo n.º 7
0
    private void LoginRequest(int cnnId, int channelId, int recHostId, Net_LoginRequest msg)
    {
        string        randomToken = AccountUtils.GenerateRandomString(256);
        Model_Account account     = db.LoginAccount(msg.UsernameOrEmail, msg.Password, cnnId, randomToken);

        Net_LoginRequestResponse response = new Net_LoginRequestResponse();

        if (account != null)
        {
            // login was successful
            response.success       = 0;
            response.information   = "Login was successful!";
            response.connectionId  = cnnId;
            response.token         = randomToken;
            response.username      = account.Username;
            response.discriminator = account.Discriminator;
        }
        else
        {
            // login was not successful
            response.success     = 1;
            response.information = "Login attempt failed";
        }

        SendClient(recHostId, cnnId, channelId, response); // let the client know if their login attempt was successful or not
    }
Ejemplo n.º 8
0
    private void  DisconnectEvent(int recHostId, int cnnId)
    {
        Debug.Log(string.Format("User {0} has Disconnected..", cnnId));

        //  get a reference to the connected account
        Model_Account account = db.FindAccountByConnectionId(cnnId);

        if (account == null)
        {
            // making sure athenticated
            return;
        }
        db.UpdateAccountAfterDisconnection(account.Email);

        // Prepare and send update message

        Net_UpdateFollow fu             = new Net_UpdateFollow();
        Model_Account    updatedAccount = db.FindAccountByEmail(account.Email);

        fu.Follow = updatedAccount.GetAccount();

        foreach (var f in db.FindAllFollowBy(account.Email))
        {
            if (f.ActiveConnection == 0)
            {
                continue;
            }

            SendClient(recHostId, f.ActiveConnection, fu);
        }
    }
    public bool InsertFollow(string token, string emailOrUsername)
    {
        Model_Followers newfollow = new Model_Followers();

        newfollow.Sender = new MongoDBRef("account", FindAccountByToken(token)._id);

        // Startby getting reference

        if (!Utility.IsEmail(emailOrUsername))
        {
            // if its username discriminator
            string[] data = emailOrUsername.Split('#');
            if (data[1] != null)
            {
                Model_Account follow = FindAccountByUsernameAndDiscriminator(data[0], data[1]);
                if (follow != null)
                {
                    newfollow.Receiver = new MongoDBRef("account", follow._id);
                }
                else
                {
                    return(false);
                }
            }
        }
        else
        {
            //  If its email
            Model_Account follow = FindAccountByEmail(emailOrUsername);
            if (follow != null)
            {
                newfollow.Receiver = new MongoDBRef("account", follow._id);
            }
            else
            {
                return(false);
            }
        }

        if (newfollow.Receiver != newfollow.Sender)
        {
            //  Does already exist
            Model_Followers modelfollow = follows.Find(u => u.Sender.Equals(newfollow.Sender) && u.Receiver.Equals(newfollow.Receiver)).SingleOrDefault();

            //  if no create one retuen true
            if (modelfollow == null)
            {
                follows.InsertOne(newfollow);
            }
            return(true);
        }

        return(false);
    }
Ejemplo n.º 10
0
    public bool InsertAccount(string username, string password, string email)
    {
        Model_Account newAccount = new Model_Account();
        newAccount.Username = username;
        newAccount.ShaPassword = password;
        newAccount.Email = email;
        newAccount.Discriminator = "0000";

        accounts.Insert(newAccount);

        return true;
    }
    public void UpdateAccountAfterDisconnection(string email)
    {
        // Model_Account myAccount = new Model_Account();
        Model_Account myAccount = accounts.Find(em => em.Email.Equals(email)).SingleOrDefault();

        myAccount.Token            = null;
        myAccount.ActiveConnection = 0;
        myAccount.Status           = 0;

        var filter = Builders <Model_Account> .Filter.Eq("Email", email);

        accounts.FindOneAndReplace(filter, myAccount);
    }
    public bool InsertAccount(string username, string password, string email)
    {
        // Check if email is valid
        if (!Utility.IsEmail(email))
        {
            Debug.Log(email + " is not an Email");
            return(false);
        }

        // Check if Username is valid
        if (!Utility.IsUsername(username))
        {
            Debug.Log(username + " is not valid Username");
            return(false);
        }

        // Check if account already exists
        if (FindAccountByEmail(email) != null)
        {
            Debug.Log(email + " is already being used");
            return(false);
        }


        Model_Account newAccount = new Model_Account();

        newAccount.Username      = username;
        newAccount.ShaPassword   = password;
        newAccount.Email         = email;
        newAccount.Discriminator = "0000";

        // roll for unique discriminator
        int rollCount = 0;

        while (FindAccountByUsernameAndDiscriminator(newAccount.Username, newAccount.Discriminator) != null)
        {
            newAccount.Discriminator = Random.Range(0, 9999).ToString("0000");

            rollCount++;
            if (rollCount > 1000)
            {
                Debug.Log("We rolled too many times, suggest username change!");
                return(false);
            }
        }

        accounts.InsertOne(newAccount);
        return(true);
    }
Ejemplo n.º 13
0
    public Model_Account FindAccountByUsernameAndDiscriminator(string username, string discriminator)
    {
        Model_Account        userMA    = null;
        List <Model_Account> usersList = accounts.Find(user => user.Username.Equals(username)).ToList();

        foreach (Model_Account u in usersList)
        {
            if (u.Discriminator == discriminator)
            {
                userMA = u;
                return(userMA);
            }
        }

        return(userMA);
    }
Ejemplo n.º 14
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);
    }
Ejemplo n.º 15
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);
    }
Ejemplo n.º 16
0
    public Model_Account LoginAccount(string usernameOrEmail, string password, int cnnId, string token)
    {
        Model_Account myAccount = null;


        // find my Account
        if (Utility.IsEmail(usernameOrEmail))
        {
            // if logged in using email
            myAccount = accounts.Find(u => u.Email.Equals(usernameOrEmail) &&
                                      u.ShaPassword.Equals(password)).SingleOrDefault();
        }
        else
        {
            // if logged in using username discriminator
            string[] data = usernameOrEmail.Split('#');
            if (data[1] != null)
            {
                myAccount = accounts.Find(u => u.Username.Equals(data[0]) &&
                                          u.Discriminator.Equals(data[1]) &&
                                          u.ShaPassword.Equals(password)).SingleOrDefault();
            }
        }

        if (myAccount != null)
        {
            // We found the account lets login
            myAccount.ActiveConnection = cnnId;
            myAccount.Token            = token;
            myAccount.Status           = 1;
            myAccount.LastLogin        = System.DateTime.Now;

            var filter = Builders <Model_Account> .Filter.Eq("_id", myAccount._id);

            var update = Builders <Model_Account> .Update.Set("class_id", 483);

            accounts.FindOneAndReplace(filter, myAccount);
        }
        else
        {
            // Invalid Credentials.. didn't find anything
        }

        return(myAccount);
    }
Ejemplo n.º 17
0
    public void LogIn()
    {
        string username = LogInInUsernameInputText.text;
        string password = Utility.Sha256FromString(LogInPasswordInputText.text);

        userAccount = db.LoginAccount(username, password);

        if (userAccount != null) //SI hemos encontrado la cuenta
        {
            PersistentData.account = userAccount;
            PersistentData.isGM    = userAccount.isGameMaster;
            Debug.Log("Login as: " + userAccount.Username + "#" + userAccount.Discriminator + ". Welcome back!");
            lobby.Connect();
        }
        else
        {
            Debug.Log("Wrong username or password, please try to log in with other credentials");
        }
    }
Ejemplo n.º 18
0
    private void LoginRequest(int cnnId, int channelId, int recHostId, Net_LoginRequest lr)
    {
        // Debug.Log(string.Format("{0},{1}",lr.UsernameOrEmail,lr.Password));

        string             randomToken = Utility.GenerateRandom(4); //Change to 256 on production
        Model_Account      account     = db.LoginAccount(lr.UsernameOrEmail, lr.Password, cnnId, randomToken);
        Net_OnLoginRequest olr         = new Net_OnLoginRequest();

        if (account != null)
        {
            olr.Success     = 1;
            olr.Information = "You have been logged in as" + account.Username;

            olr.Username      = account.Username;
            olr.Discriminator = account.Discriminator;
            olr.Token         = account.Token;
            olr.ConnectionId  = cnnId;

            // Here

            // Prepare and send update message
            Net_UpdateFollow fu = new Net_UpdateFollow();
            fu.Follow = account.GetAccount();

            foreach (var f in db.FindAllFollowBy(account.Email))
            {
                if (f.ActiveConnection == 0)
                {
                    continue;
                }

                SendClient(recHostId, f.ActiveConnection, fu);
            }
        }
        else
        {
            olr.Success = 0;
        }


        SendClient(recHostId, cnnId, olr);
    }
Ejemplo n.º 19
0
    public static Entity BuildEntity(Model_Account model)
    {
        Key key = Datastore.ds.keyBuilder(Enum.account).SetId(model.Email).Build();

        var e = new Entity
        {
            Key                     = key,
            [Enum.username]         = model.Username,
            [Enum.email]            = model.Email,
            [Enum.discriminator]    = model.Discriminator,
            [Enum.activeConnection] = model.ActiveConnection,
            [Enum.status]           = model.Status,
            [Enum.token]            = model.Token,
            [Enum.lastLogin]        = model.LastLogin.Ticks,
            [Enum.hashedPassword]   = model.HashedPassword,
            [Enum.salt]             = model.Salt
        };

        return(e);
    }
Ejemplo n.º 20
0
    private void LoginRequest(int connectionId, int channelId, int receiveHostId, Net_LoginRequest loginRequest)
    {
        string             randomToken    = Utility.GenerateRandom(256);
        Model_Account      modelAccount   = dataBase.LoginAccount(loginRequest.UsernameOrEmail, loginRequest.Password, connectionId, randomToken);
        Net_OnLoginRequest onLoginRequest = new Net_OnLoginRequest();

        if (modelAccount != null)
        {
            onLoginRequest.Success      = 1;
            onLoginRequest.Information  = "You've been logged in as " + modelAccount.Username;
            onLoginRequest.Username     = modelAccount.Username;
            onLoginRequest.Token        = randomToken;
            onLoginRequest.ConnectionId = connectionId;
        }
        else
        {
            onLoginRequest.Success     = 0;
            onLoginRequest.Information = "Fail login";
        }
        SendClient(receiveHostId, connectionId, onLoginRequest);
    }
Ejemplo n.º 21
0
    //Inserta una nueva cuenta en la base de datos
    public void InsertNewAccount()
    {
        string username         = SignInUsernameInputText.text;
        string password         = SignInPasswordInputText.text;
        string passwordRepeated = SignInRepeatPasswordInputText.text;

        if (password.Equals(passwordRepeated)) //Si ambas contraseñas están bien escritas, se intenta insertar
        {
            userAccount = db.InsertAccount(username, password);
            if (userAccount != null)
            {
                Debug.Log("New account created, your username is: " + userAccount.Username + "#" + userAccount.Discriminator + ". Use it to login!");
            }
            else
            {
                Debug.Log("Cannot create the account ");
            }
        }
        else
        {
            Debug.Log("Passwords mismatched, write it again!");
        }
    }
Ejemplo n.º 22
0
    public Model_Account FindAccountByUsername(string username)
    {
        List <Model_Account> usersList = accounts.Find(user => user.Username.Equals(username)).ToList();
        //Generar Discriminator aleatorio
        Model_Account newAccount = new Model_Account();

        newAccount.Username      = username;
        newAccount.Discriminator = "0000";
        int rollCount = 0;

        while (FindAccountByUsernameAndDiscriminator(newAccount.Username, newAccount.Discriminator) != null) //Mientras haya alguien con ese Discriminator, creamos uno nuevo y volvemos a buscar
        {
            newAccount.Discriminator = Random.Range(0, 9999).ToString("0000");
            rollCount++;
            if (rollCount > 1000)
            {
                Debug.Log("We rolled to many time, suggest username change!");
                newAccount.Discriminator = "failed";
                return(newAccount);
            }
        }

        return(newAccount);
    }
    public Model_Account FindAccountByToken(string token)
    {
        Model_Account modelAccount = accounts.Find(em => em.Token.Equals(token)).SingleOrDefault();

        return(modelAccount);
    }
 public static bool ValidatePassword(Model_Account account, string password)
 {
     password = HashPassword(password, account.Salt);
     return(ValidatePassword(password, account.HashedPassword));
 }
    public Model_Account FindAccountByObjectId(ObjectId id)
    {
        Model_Account modelAccount = accounts.Find(em => em._id.Equals(id)).SingleOrDefault();

        return(modelAccount);
    }
    public Model_Account FindAccountByEmail(string email)
    {
        Model_Account modelAccount = accounts.Find(em => em.Email.Equals(email)).SingleOrDefault();

        return(modelAccount);
    }
    public Model_Account FindAccountByUsernameAndDiscriminator(string username, string discriminator)
    {
        Model_Account modelAccount = accounts.Find(u => u.Username.Equals(username) && u.Discriminator.Equals(discriminator)).SingleOrDefault();

        return(modelAccount);
    }
    public Model_Account FindAccountByConnectionId(int connectionId)
    {
        Model_Account modelAccount = accounts.Find(em => em.ActiveConnection.Equals(connectionId)).SingleOrDefault();

        return(modelAccount);
    }