Esempio n. 1
0
        /// <summary>
        /// Sets the display name and creates a friends list entry
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="DisplayName"></param>
        /// <returns></returns>
        public async Task <bool> CreateNewAccount(string Id, string DisplayName)
        {
            // try to set the display name for the new user
            if (await displayNameHandler.SetDisplayName(Id, DisplayName) is false)
            {
                logger.LogError("Failed to set the display name of a new user, Name: {DisplayName} Id: {Id}", DisplayName, Id);
                return(false);
            }

            // try to get an available tag for the player
            short?result = await displayNameHandler.GetAvailableUniqueIdentifier(DisplayName);

            if (result is short UniqueIdentifier)
            {
                // since there was one available set it
                await displayNameHandler.SetUniqueIdentifier(Id, UniqueIdentifier);
            }
            else
            {
                // short was malformed, outside of bounds, or not available
                logger.LogError("Failed to get short to create a new user, Name: {DisplayName} Id: {Id} UniqueId: {UniqueIdentifier}", DisplayName, Id, result);
                return(false);
            }

            // create the friends list, blocked list, and request list for the user
            await db.ExecuteVoidProcedure(CreateNewUserProcedure, new { Id });

            // create the user in the message DB as well
            await messagesDb.ExecuteVoidProcedure(MessagesCreateNewUserProcedure, new { Id });

            // create identity keys
            //var (PublicKey, PrivateKey) = diffieHellmanHandler.GenerateKeys();

            // store the keys
            //await bundleHandler.SetKeys(Id, PublicKey, PrivateKey);

            logger.LogInformation("Finished creating new user {DisplayName}#{result} {Id}", DisplayName, result, Id);

            return(true);
        }
        private async Task <bool> SetAllStates(string Id, Dictionary <string, string> states)
        {
            try
            {
                // states are stored as a dictionary with friend ids and states
                string serializedStates = JsonConvert.SerializeObject(states);

                await db.ExecuteVoidProcedure <dynamic>(SetStateProcedureName, new { Id, EncryptionClientState = serializedStates });

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to set state for {Id} {Error}", Id, e);
                return(false);
            }
        }
Esempio n. 3
0
        public async Task <bool> SetUniqueIdentifier(string Id, short UniqueIdentifier)
        {
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(false);
            }
            try
            {
                await db.ExecuteVoidProcedure(SetUniqueIdentifiersProceduree, new { Id, UniqueIdentifier });

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to set unique identitfier for {id}#{num} Error: {Error}", Id, UniqueIdentifier, e);
                return(false);
            }
        }
Esempio n. 4
0
        public async Task <bool> SendMessage(string SenderId, string RecipientId, IMessageModel Message)
        {
            if (Helpers.FullVerifyGuid(ref SenderId, logger) is false)
            {
                return(false);
            }

            if (Helpers.FullVerifyGuid(ref RecipientId, logger) is false)
            {
                return(false);
            }

            try
            {
                // get the serialized messages list
                var result = await db.ExecuteSingleProcedure <string, dynamic>(GetMessagesProcedure, new { Id = RecipientId });

                // if the list is null just create a new one
                List <MessageModel> messages = result is null ? new() : Newtonsoft.Json.JsonConvert.DeserializeObject <List <MessageModel> >(result);

                // add it to the list
                if (Message is MessageModel mm)
                {
                    // add the message
                    if (messages.Contains(mm) is false)
                    {
                        messages.Add(mm);
                    }
                }

                // serialize the list for storage
                result = Newtonsoft.Json.JsonConvert.SerializeObject(messages);

                // save the messages
                await db.ExecuteVoidProcedure <dynamic>(SetMessagesProcedure, new { Id = RecipientId, Messages = result });

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to send message From {SenderId} to {RecipientId} Error: {Error}", SenderId, RecipientId, e);
                return(false);
            }
        }
Esempio n. 5
0
        public async Task <bool> SetAvatar(string Id, string Base64Avatar)
        {
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(false);
            }

            try
            {
                await db.ExecuteVoidProcedure(SetAvatarProcedure, new { Id, Avatar = Base64Avatar });

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to set Avatar for {Id} Error: {Error}", Id, e);
                return(false);
            }
        }
Esempio n. 6
0
        private async Task <bool> ExecuteTwoParamOAuthQuery(string Id, string OAuth, string ProcedureName)
        {
            // make sure to avoid possible Sql Injection
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(false);
            }

            // normally we would verify if the OAuth param contains invalid characters, however the OAuth ideally SHOULD be hashed with a salt using BCrypt and is never executed and therefor harmless... hopefully.

            try
            {
                await db.ExecuteVoidProcedure(ProcedureName, new { Id, OAuth });

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to {ProcedureName} OAuth for {Id} Oath: {OAuth} Error: {Error}", ProcedureName, Id, OAuth, e);
                return(false);
            }
        }