Exemplo n.º 1
0
        public async Task <ModifyUserResponse> ChangePasswordByUser(string param)
        {
            User user = null;

            try
            {
                user = JsonConvert.DeserializeObject <User>(param);
            }
            catch (JsonReaderException)
            {
                return(new ModifyUserResponse(ModifyUserResult.JSON_ERROR, null, null, null));
            }
            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                if (!CheckPasswordRules(user.Password))
                {
                    return new ModifyUserResponse(ModifyUserResult.PASSWORD_GUIDELINES_ERROR, null, besteUserSettings?.PasswordRules, user);
                }
                user.TableId = TABLE_ID;
                User dbUser = await AmazonDynamoDBFactory.Context.LoadAsync <User>(user);
                if (dbUser == null || dbUser.Equals(new User()))
                {
                    return new ModifyUserResponse(ModifyUserResult.USER_UNKNOWN, null, null, user);
                }
                else
                {
                    dbUser.MustChangePassword = user.MustChangePassword;
                    dbUser.SaltValue = random.Next(0, 1000000);
                    dbUser.Password = Sha256WithSaltAndPepper(user.Password, dbUser.SaltValue.ToString());
                    dbUser.WrongPasswordCounter = 0;
                    await AmazonDynamoDBFactory.Context.SaveAsync(dbUser);
                    return new ModifyUserResponse(ModifyUserResult.SUCCESS, null, null, user);
                }
            }));
        }
Exemplo n.º 2
0
        public async Task <GetUsersResponse> GetUser(string param)
        {
            User user = null;

            try
            {
                user = JsonConvert.DeserializeObject <User>(param);
            }
            catch (JsonReaderException)
            {
                return(new GetUsersResponse(GetUsersResult.JSON_ERROR, null));
            }
            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                user.TableId = TABLE_ID;
                User dbUser = await AmazonDynamoDBFactory.Context.LoadAsync <User>(user);
                if (dbUser == null)
                {
                    return new GetUsersResponse(GetUsersResult.USER_UNKNOWN, null);
                }
                List <User> users = new List <User> {
                    user
                };
                users.ForEach((item) =>
                {
                    item.Password = null;
                    item.SaltValue = null;
                });
                return new GetUsersResponse(GetUsersResult.SUCCESS, users);
            }));
        }
Exemplo n.º 3
0
        public async Task <ModifyUserResponse> DeleteUser(string param)
        {
            User user = null;

            try
            {
                user = JsonConvert.DeserializeObject <User>(param);
            }
            catch (JsonReaderException)
            {
                return(new ModifyUserResponse(ModifyUserResult.JSON_ERROR, null, null, null));
            }
            try
            {
                return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
                {
                    user.TableId = TABLE_ID;
                    User dbUser = await AmazonDynamoDBFactory.Context.LoadAsync <User>(user);

                    if (dbUser == null || dbUser.Equals(new User()))
                    {
                        return new ModifyUserResponse(ModifyUserResult.USER_UNKNOWN, null, null, user);
                    }
                    await AmazonDynamoDBFactory.Context.DeleteAsync(dbUser);
                    return new ModifyUserResponse(ModifyUserResult.SUCCESS, null, null, user);
                }));
            }
            catch (Exception)
            {
                return(new ModifyUserResponse(ModifyUserResult.EXCEPTION, null, null, user));
            }
        }
 private static async Task <int> GenerateNewServerSettingsId()
 {
     return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
     {
         Random random = new Random();
         for (int tries = 0; tries < 5; tries++)
         {
             byte[] buf = new byte[4];
             random.NextBytes(buf);
             int generatedId = BitConverter.ToInt32(buf, 0);
             ServerSetting serverSetting = new ServerSetting
             {
                 TableId = TABLE_ID,
                 Id = generatedId
             };
             serverSetting = await context.LoadAsync(serverSetting);
             //QueryResponse response = await GetServerSettingBySpecificProperty("Id", generatedId);
             if (serverSetting == null)
             {
                 return generatedId;
             }
         }
         throw new Exception("Was not able to generate a Unique Id for ServerSetting!");
     }));
 }
Exemplo n.º 5
0
        internal static async Task CreateInitialSettingsAndRights()
        {
            User adminUser = new User
            {
                TableId  = TABLE_ID,
                Username = "******"
            };

            adminUser = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                return(await context.LoadAsync(adminUser));
            });

            await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                await context.SaveAsync(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.ServerSettings",
                    Operation        = "Edit",
                    RecourceModule   = "ServerSetting",
                    RecourceId       = null,
                    Authorized       = true,
                    LegitimationUuid = adminUser.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                });
            });
        }
Exemplo n.º 6
0
 private async Task <List <PureRight> > GetPureRights(string legitimationUuid)
 {
     return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
     {
         List <PureRight> pureRights = new List <PureRight>();
         var request = new QueryRequest
         {
             TableName = BesteRightsAuthorization.TableName,
             //ScanIndexForward = false,
             KeyConditions = new Dictionary <string, Condition>
             {
                 { "TableId", new Condition()
                   {
                       ComparisonOperator = ComparisonOperator.EQ,
                       AttributeValueList = new List <AttributeValue>
                       {
                           new AttributeValue {
                               N = TABLE_ID.ToString()
                           }
                       }
                   } }
             },
             ExpressionAttributeNames = new Dictionary <string, string>
             {
                 { "#namespace", "Namespace" },
                 { "#legitimationuuid", "LegitimationUuid" }
             },
             ExpressionAttributeValues = new Dictionary <string, AttributeValue>
             {
                 { ":namespace", new AttributeValue {
                       S = BesteRightsNamespace
                   } },
                 { ":legitimationuuid", new AttributeValue {
                       S = legitimationUuid
                   } }
             },
             FilterExpression = "#namespace = :namespace and #legitimationuuid = :legitimationuuid"
         };
         var response = await AmazonDynamoDBFactory.Client.QueryAsync(request);
         foreach (var item in response.Items)
         {
             PureRight pureRight = new PureRight
             {
                 RecourceModule = item["RecourceModule"].S,
                 Authorized = item["Authorized"].N == "1" ? true : false,
                 Operation = item["Operation"].S
             };
             if (item.ContainsKey("RecourceId"))
             {
                 pureRight.RecourceId = Convert.ToInt32(item["RecourceId"].N);
             }
             pureRights.Add(pureRight);
         }
         return pureRights;
     }));
 }
 private static async Task DeleteAllRightsByServerSettingId(int serverSettingId)
 {
     await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
     {
         var request = new QueryRequest
         {
             TableName = BesteRightsAuthorization.TableName,
             //ScanIndexForward = false,
             KeyConditions = new Dictionary <string, Condition>
             {
                 { "TableId", new Condition()
                   {
                       ComparisonOperator = ComparisonOperator.EQ,
                       AttributeValueList = new List <AttributeValue>
                       {
                           new AttributeValue {
                               N = TABLE_ID.ToString()
                           }
                       }
                   } }
             },
             ExpressionAttributeNames = new Dictionary <string, string>
             {
                 { "#Namespace", "Namespace" },
                 { "#RecourceModule", "RecourceModule" },
                 { "#RecourceId", "RecourceId" }
             },
             ExpressionAttributeValues = new Dictionary <string, AttributeValue>
             {
                 { ":Namespace", new AttributeValue {
                       S = "Beste.GameServer.SDaysTDie.ServerSettings"
                   } },
                 { ":RecourceModule", new AttributeValue {
                       S = "ServerSetting"
                   } },
                 { ":RecourceId", new AttributeValue {
                       N = serverSettingId.ToString()
                   } }
             },
             FilterExpression = "#Namespace = :Namespace and" +
                                "#RecourceModule = :RecourceModule and" +
                                "#RecourceId = :RecourceId"
         };
         var response = await client.QueryAsync(request);
         foreach (var item in response.Items)
         {
             await context.DeleteAsync(new BesteRightsAuthorization
             {
                 TableId = TABLE_ID,
                 Uuid    = item["Uuid"].S
             });
         }
     });
 }
        private static async Task <ModifySettingsResponse> EditServerSetting(ServerSetting serverSettings, User user, string token)
        {
            ServerSetting dbServerSetting = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                serverSettings.TableId = TABLE_ID;
                return(await context.LoadAsync(serverSettings));
            });

            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                try
                {
                    if (dbServerSetting == null)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.SETTING_NOT_FOUND);
                    }
                    if (SDaysTDieServerHandler.IsServerRunningBySeed(dbServerSetting.WorldGenSeed))
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.SERVER_MUST_BE_STOPPED);
                    }
                    QueryResponse response = await GetServerSettingBySpecificProperty("WorldGenSeed", serverSettings.WorldGenSeed);
                    if (response.Items.Count > 0 && Convert.ToInt32(response.Items[0]["Id"].N.ToString()) != dbServerSetting.Id)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.GAME_SEED_ALREADY_EXISTS);
                    }
                    string oldUserId = dbServerSetting.UserUuid;
                    GetUsersResponse getUsersResponse = await BesteUser.GetUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    }));
                    if (getUsersResponse.Result != GetUsersResult.SUCCESS)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.USER_NOT_FOUND);
                    }
                    User dbUser = getUsersResponse.Users[0];
                    serverSettings.UserUuid = dbUser.Uuid;
                    serverSettings.CopyAllButId(dbServerSetting);
                    if (oldUserId != getUsersResponse.Users[0].Uuid)
                    {
                        await AddOrUpdateServerSettingRight("EditServerSettings", serverSettings.Id, dbUser.Uuid, token);
                    }
                    await context.SaveAsync(dbServerSetting);
                    return new ModifySettingsResponse(ModifySettingsResult.SETTING_EDITED);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString() + "\n" + ex.StackTrace);
                    return new ModifySettingsResponse(ModifySettingsResult.EXCEPTION);
                }
            }));
        }
Exemplo n.º 9
0
        public async Task EditNotExistingServerSettingsInDatabase()
        {
            ClientWebSocket  webSocket        = new ClientWebSocket();
            WebSocketHandler webSocketHandler = new WebSocketHandler(webSocket);
            await webSocket.ConnectAsync(new Uri("ws://localhost:80/ws"), CancellationToken.None);

            await Task.Delay(50);

            await TestHelper.Login("User", "Passwort1$", webSocket, webSocketHandler);

            ServerSetting serverSettings = TestHelper.GenerateNewServerSetting();

            serverSettings.GameName = "Edited!";
            serverSettings.Id       = 532345823;

            await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                BesteRightsAuthorization besteRightsAuthorization = new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.ServerSettings",
                    Operation        = "EditServerSettings",
                    RecourceModule   = "ServerSetting",
                    RecourceId       = serverSettings.Id,
                    Authorized       = true,
                    LegitimationUuid = webSocketHandler.User.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                };
                await AmazonDynamoDBFactory.Context.SaveAsync(besteRightsAuthorization);
            });

            await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, webSocketHandler.Result.CloseStatusDescription, CancellationToken.None);

            webSocket        = new ClientWebSocket();
            webSocketHandler = new WebSocketHandler(webSocket);
            await webSocket.ConnectAsync(new Uri("ws://localhost:80/ws"), CancellationToken.None);

            await TestHelper.Login("User", "Passwort1$", webSocket, webSocketHandler);



            Command command = new Command("EditServerSettings", serverSettings);
            await TestHelper.ExecuteCommandAndAwaitResponse(webSocket, webSocketHandler, command);

            ModifySettingsResponse modifyResponse = JsonConvert.DeserializeObject <ModifySettingsResponse>(webSocketHandler.ReceivedCommand.CommandData.ToString());

            TestHelper.ValiateResponse(modifyResponse, ModifySettingsResult.SETTING_NOT_FOUND);
        }
Exemplo n.º 10
0
        public async Task WriteInTestTableFunctionalProgramming_User()
        {
            User.User user = await AmazonDynamoDBFactory.ExecuteInTransactionContext(AddTestUser);

            async Task checkUserExists(IAmazonDynamoDB client, IDynamoDBContext context)
            {
                User.User dbUser = await AmazonDynamoDBFactory.Context.LoadAsync <User.User>(user);

                if (!dbUser.Equals(user))
                {
                    Assert.Fail();
                }
            }

            await AmazonDynamoDBFactory.ExecuteInTransactionContext(checkUserExists);
        }
        internal static async Task <GetSettingsResponse> GetServerSettingsByIdAndUser(User user, int serverSettingId)
        {
            ServerSetting dbServerSetting = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                ServerSetting serverSetting = new ServerSetting
                {
                    TableId = TABLE_ID,
                    Id      = serverSettingId
                };
                return(await context.LoadAsync(serverSetting));
            });

            List <ServerSetting> serverSettings = new List <ServerSetting> {
                dbServerSetting
            };

            return(new GetSettingsResponse(GetSettingsResult.OK, serverSettings));
        }
        private static async Task <ModifySettingsResponse> AddServerSetting(ServerSetting serverSettings, User user, string token)
        {
            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                try
                {
                    QueryResponse response = await GetServerSettingBySpecificProperty("WorldGenSeed", serverSettings.WorldGenSeed);
                    if (response.Items.Count > 0)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.GAME_SEED_ALREADY_EXISTS);
                    }
                    GetUsersResponse getUsersResponse = await BesteUser.GetUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    }));
                    if (getUsersResponse.Result != GetUsersResult.SUCCESS)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.USER_NOT_FOUND);
                    }
                    serverSettings.TableId = TABLE_ID;
                    serverSettings.UserUuid = getUsersResponse.Users[0].Uuid;
                    serverSettings.ServerConfigFilepath = "";
                    serverSettings.ServerPort = 0;
                    serverSettings.TelnetPassword = "";
                    serverSettings.TelnetPort = 0;
                    serverSettings.TerminalWindowEnabled = false;
                    serverSettings.Id = await GenerateNewServerSettingsId();

                    await CreateRightsForNewServerSettings(serverSettings, user, token);

                    await context.SaveAsync(serverSettings);
                    return new ModifySettingsResponse(ModifySettingsResult.SETTING_ADDED);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString() + "\n" + ex.StackTrace);
                    return new ModifySettingsResponse(ModifySettingsResult.EXCEPTION);
                }
            }));
        }
Exemplo n.º 13
0
        public async Task <ModifyUserResponse> CreateUser(string param)
        {
            User user = null;

            try
            {
                user = JsonConvert.DeserializeObject <User>(param);
            }
            catch (JsonReaderException)
            {
                return(new ModifyUserResponse(ModifyUserResult.JSON_ERROR, null, null, null));
            }
            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                if (!CheckMandatoryUserParameters(user))
                {
                    return new ModifyUserResponse(ModifyUserResult.MISSING_USER_PARAMS, besteUserSettings?.MandatoryUserParams, null, user);
                }
                if (!CheckPasswordRules(user.Password))
                {
                    return new ModifyUserResponse(ModifyUserResult.PASSWORD_GUIDELINES_ERROR, null, besteUserSettings?.PasswordRules, user);
                }

                user.TableId = TABLE_ID;
                User dbUser = await AmazonDynamoDBFactory.Context.LoadAsync <User>(user);
                if (dbUser != null)
                {
                    return new ModifyUserResponse(ModifyUserResult.USER_ALREADY_EXISTS, null, null, user);
                }
                user.Uuid = Guid.NewGuid().ToString();
                user.WrongPasswordCounter = 0;
                user.MustChangePassword = true;
                user.SaltValue = random.Next(0, 1000000);
                user.Password = Sha256WithSaltAndPepper(user.Password, user.SaltValue.ToString());
                await AmazonDynamoDBFactory.Context.SaveAsync(user);
                return new ModifyUserResponse(ModifyUserResult.SUCCESS, null, null, user);
            }));
        }
Exemplo n.º 14
0
        public async Task WriteInTestTable_User()
        {
            User.User user = null;
            user = new User.User
            {
                TableId            = TABLE_ID,
                Firstname          = "Firstname",
                Lastname           = "Lastname",
                Username           = "******",
                Email              = "Email",
                MustChangePassword = true,
                Password           = "******",
                SaltValue          = 1,
                Uuid = Guid.NewGuid().ToString(),
                WrongPasswordCounter = 1
            };
            await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                await context.SaveAsync(user);
            });

            User.User dbUser = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                return(await context.LoadAsync(user));
            });

            dbUser = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                dbUser.TableId = TABLE_ID;
                return(await context.LoadAsync(dbUser));
            });

            if (!dbUser.Equals(user))
            {
                Assert.Fail();
            }
        }
 private static async Task <QueryResponse> GetServerSettingBySpecificProperty(string propertyName, int propertyValue)
 {
     return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
     {
         var request = new QueryRequest
         {
             TableName = ServerSetting.TableName,
             //ScanIndexForward = false,
             KeyConditions = new Dictionary <string, Condition>
             {
                 { "TableId", new Condition()
                   {
                       ComparisonOperator = ComparisonOperator.EQ,
                       AttributeValueList = new List <AttributeValue>
                       {
                           new AttributeValue {
                               N = TABLE_ID.ToString()
                           }
                       }
                   } }
             },
             ExpressionAttributeNames = new Dictionary <string, string>
             {
                 { "#Property", propertyName }
             },
             ExpressionAttributeValues = new Dictionary <string, AttributeValue>
             {
                 { ":Property", new AttributeValue {
                       N = propertyValue.ToString()
                   } }
             },
             FilterExpression = "#Property = :Property"
         };
         var response = await client.QueryAsync(request);
         return response;
     }));
 }
        private static async Task <ModifySettingsResponse> DeleteServerSetting(ServerSetting serverSettings, User user, string connectedUserToken)
        {
            ServerSetting dbServerSetting = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                serverSettings.TableId = TABLE_ID;
                return(await context.LoadAsync(serverSettings));
            });

            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                try
                {
                    if (dbServerSetting == null)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.SETTING_NOT_FOUND);
                    }
                    if (SDaysTDieServerHandler.IsServerRunningBySeed(dbServerSetting.WorldGenSeed))
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.SERVER_MUST_BE_STOPPED);
                    }
                    await AmazonDynamoDBFactory.ExecuteInTransactionContext(async(clientDelete, contextDelete) =>
                    {
                        await contextDelete.DeleteAsync(dbServerSetting);
                    });
                    await AmazonDynamoDBFactory.ExecuteInTransactionContext(async(clientDelete, contextDelete) =>
                    {
                        await DeleteAllRightsByServerSettingId(dbServerSetting.Id);
                    });
                    return new ModifySettingsResponse(ModifySettingsResult.SETTING_DELETED);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString() + "\n" + ex.StackTrace);
                    return new ModifySettingsResponse(ModifySettingsResult.EXCEPTION);
                }
            }));
        }
 private static async Task <QueryResponse> GetServerSettingByWorldGenSeed(string worldGenSeed)
 {
     return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
     {
         var request = new QueryRequest
         {
             TableName = ServerSetting.TableName,
             //ScanIndexForward = false,
             KeyConditions = new Dictionary <string, Condition>
             {
                 { "TableId", new Condition()
                   {
                       ComparisonOperator = ComparisonOperator.EQ,
                       AttributeValueList = new List <AttributeValue>
                       {
                           new AttributeValue {
                               N = TABLE_ID.ToString()
                           }
                       }
                   } }
             },
             ExpressionAttributeNames = new Dictionary <string, string>
             {
                 { "#WorldGenSeed", "WorldGenSeed" }
             },
             ExpressionAttributeValues = new Dictionary <string, AttributeValue>
             {
                 { ":WorldGenSeed", new AttributeValue {
                       S = worldGenSeed
                   } }
             },
             FilterExpression = "#WorldGenSeed = :WorldGenSeed"
         };
         return await client.QueryAsync(request);
     }));
 }
Exemplo n.º 18
0
        public async Task <BesteUserAuthentificationResponse> Authenticate(string param)
        {
            User user = null;

            try
            {
                user = JsonConvert.DeserializeObject <User>(param);
            }
            catch (JsonReaderException)
            {
                return(new BesteUserAuthentificationResponse(BesteUserAuthentificationResult.JSON_ERROR, null));
            }
            if (user.Username == null ||
                user.Username == "" ||
                user.Password == null ||
                user.Password == "")
            {
                return(new BesteUserAuthentificationResponse(BesteUserAuthentificationResult.WRONG_PARAMETER, null));
            }

            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                user.TableId = TABLE_ID;
                User dbUser = await AmazonDynamoDBFactory.Context.LoadAsync <User>(user);
                if (dbUser == null || dbUser.Equals(new User()))
                {
                    return new BesteUserAuthentificationResponse(BesteUserAuthentificationResult.USER_UNKNOWN, null);
                }
                else
                {
                    string hashedPw = Sha256WithSaltAndPepper(user.Password, dbUser.SaltValue.ToString());
                    if (hashedPw == dbUser.Password)
                    {
                        if (dbUser.WrongPasswordCounter <= 10)
                        {
                            dbUser.WrongPasswordCounter = 0;
                            await AmazonDynamoDBFactory.Context.SaveAsync(dbUser);
                            dbUser.WrongPasswordCounter = null;
                            dbUser.Password = null;
                            dbUser.SaltValue = null;
                            if (dbUser.MustChangePassword == true)
                            {
                                return new BesteUserAuthentificationResponse(BesteUserAuthentificationResult.MUST_CHANGE_PASSWORT, dbUser);
                            }
                            else
                            {
                                return new BesteUserAuthentificationResponse(BesteUserAuthentificationResult.SUCCESS, dbUser);
                            }
                        }
                        else
                        {
                            dbUser.WrongPasswordCounter = null;
                            dbUser.Password = null;
                            dbUser.SaltValue = null;
                            dbUser.Firstname = null;
                            dbUser.Lastname = null;
                            dbUser.SaltValue = null;
                            dbUser.TableId = 0;
                            return new BesteUserAuthentificationResponse(BesteUserAuthentificationResult.WRONG_PASSWORD_COUNTER_TOO_HIGH, dbUser);
                        }
                    }
                    else
                    {
                        dbUser.WrongPasswordCounter++;
                        await AmazonDynamoDBFactory.Context.SaveAsync(dbUser);
                        dbUser.WrongPasswordCounter = null;
                        dbUser.Password = null;
                        dbUser.SaltValue = null;
                        dbUser.Firstname = null;
                        dbUser.Lastname = null;
                        dbUser.SaltValue = null;
                        dbUser.TableId = 0;
                        return new BesteUserAuthentificationResponse(BesteUserAuthentificationResult.WRONG_PASSWORD, dbUser);
                    }
                }
            }));
        }
        private static async Task AddOrUpdateServerSettingRight(string operation, int serverSettingsId, string userUuid, string token)
        {
            BesteRightsAuthorization besteRightsAuthorizationDb = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                var request = new QueryRequest
                {
                    TableName = BesteRightsAuthorization.TableName,
                    //ScanIndexForward = false,
                    KeyConditions = new Dictionary <string, Condition>
                    {
                        { "TableId", new Condition()
                          {
                              ComparisonOperator = ComparisonOperator.EQ,
                              AttributeValueList = new List <AttributeValue>
                              {
                                  new AttributeValue {
                                      N = TABLE_ID.ToString()
                                  }
                              }
                          } }
                    },
                    ExpressionAttributeNames = new Dictionary <string, string>
                    {
                        { "#Namespace", "Namespace" },
                        { "#Operation", "Operation" },
                        { "#RecourceModule", "RecourceModule" },
                        { "#RecourceId", "RecourceId" }
                    },
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>
                    {
                        { ":Namespace", new AttributeValue {
                              S = "Beste.GameServer.SDaysTDie.ServerSettings"
                          } },
                        { ":Operation", new AttributeValue {
                              S = operation
                          } },
                        { ":RecourceModule", new AttributeValue {
                              S = "ServerSetting"
                          } },
                        { ":RecourceId", new AttributeValue {
                              N = serverSettingsId.ToString()
                          } }
                    },
                    FilterExpression = "#Namespace = :Namespace and" +
                                       "#Operation = :Operation and" +
                                       "#RecourceModule = :RecourceModule and" +
                                       "#RecourceId = :RecourceId"
                };
                var response = await client.QueryAsync(request);
                if (response.Items.Count > 0)
                {
                    return(new BesteRightsAuthorization
                    {
                        TableId = TABLE_ID,
                        Uuid = response.Items[0]["Uuid"].S
                    });
                }
                return(null);
            });

            await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                BesteRightsAuthorization besteRightsAuthorization = new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.ServerSettings",
                    Operation        = operation,
                    RecourceModule   = "ServerSetting",
                    RecourceId       = serverSettingsId,
                    Authorized       = true,
                    LegitimationUuid = userUuid,
                    Uuid             = besteRightsAuthorizationDb == null ?
                                       Guid.NewGuid().ToString() :
                                       besteRightsAuthorizationDb.Uuid
                };
                await context.SaveAsync(besteRightsAuthorization);
            });

            RightControl.Grant(token, operation, "ServerSetting", serverSettingsId);
        }
Exemplo n.º 20
0
        public async Task <GetUsersResponse> GetUsers(string param)
        {
            GetUsersParams getUsersParams;

            try
            {
                getUsersParams = JsonConvert.DeserializeObject <GetUsersParams>(param);
            }
            catch (JsonReaderException)
            {
                return(new GetUsersResponse(GetUsersResult.JSON_ERROR, null));
            }
            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                var request = new QueryRequest
                {
                    TableName = "user",
                    //ScanIndexForward = false,
                    KeyConditions = new Dictionary <string, Condition>
                    {
                        { "id", new Condition()
                          {
                              ComparisonOperator = ComparisonOperator.EQ,
                              AttributeValueList = new List <AttributeValue>
                              {
                                  new AttributeValue {
                                      N = TABLE_ID.ToString()
                                  }
                              }
                          } }
                    },
                    //AttributesToGet = new List<string> { "user_id" }
                };
                var response = await AmazonDynamoDBFactory.Client.QueryAsync(request);
                List <User> users = new List <User>();
                foreach (var item in response.Items)
                {
                    User user = new User()
                    {
                        TableId = Convert.ToInt32(item["id"].N),
                        Username = item["username"].S,
                        Uuid = item["Uuid"].S,
                        Firstname = item["Firstname"].S,
                        Lastname = item["Lastname"].S,
                        Email = item["Email"].S,
                        Password = item["Password"].S,
                        SaltValue = Convert.ToInt32(item["SaltValue"].N),
                        MustChangePassword = item.ContainsKey("MustChangePassword") ?
                                             item["MustChangePassword"].BOOL : true,
                        WrongPasswordCounter = Convert.ToInt32(item["WrongPasswordCounter"].N)
                    };
                    users.Add(user);
                }

                users.ForEach((user) =>
                {
                    user.Password = null;
                    user.SaltValue = null;
                });
                switch (getUsersParams.SortUsersBy)
                {
                case SortUsersBy.EMAIL:
                    users = users.OrderBy(u => u.Email).ToList();
                    break;

                case SortUsersBy.ID:
                    users = users.OrderBy(u => u.TableId).ToList();
                    break;

                case SortUsersBy.LASTNAME:
                    users = users.OrderBy(u => u.Lastname).ToList();
                    break;

                case SortUsersBy.USERNAME:
                    users = users.OrderBy(u => u.Username).ToList();
                    break;

                default:
                    break;
                }
                users = users.Skip(getUsersParams.Offset).Take(getUsersParams.Limit).ToList();

                return new GetUsersResponse(GetUsersResult.SUCCESS, users);
            }));
        }
Exemplo n.º 21
0
        private static async Task AddInitialRightsToDatabase()
        {
            var request = new QueryRequest
            {
                TableName     = BesteRightsAuthorization.TableName,
                KeyConditions = new Dictionary <string, Condition>
                {
                    { "TableId", new Condition()
                      {
                          ComparisonOperator = ComparisonOperator.EQ,
                          AttributeValueList = new List <AttributeValue>
                          {
                              new AttributeValue {
                                  N = TABLE_ID.ToString()
                              }
                          }
                      } }
                },
                //AttributesToGet = new List<string> { "user_id" }
            };
            var response = await AmazonDynamoDBFactory.Client.QueryAsync(request);

            foreach (var item in response.Items)
            {
                BesteRightsAuthorization user = new BesteRightsAuthorization()
                {
                    TableId = TABLE_ID,
                    Uuid    = item["Uuid"].S
                };
                await AmazonDynamoDBFactory.Context.DeleteAsync(user);
            }


            await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                List <BesteRightsAuthorization> besteRightsAuthorizations = new List <BesteRightsAuthorization>();
                besteRightsAuthorizations.Add(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "CheckRegister",
                    Operation        = "Delete",
                    RecourceModule   = "Authorizations",
                    Authorized       = false,
                    LegitimationUuid = "1",
                    Uuid             = Guid.NewGuid().ToString()
                });
                besteRightsAuthorizations.Add(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "CheckRegister",
                    Operation        = "Add",
                    RecourceModule   = "Authorizations",
                    Authorized       = true,
                    LegitimationUuid = "1",
                    Uuid             = Guid.NewGuid().ToString()
                });
                foreach (var item in besteRightsAuthorizations)
                {
                    await AmazonDynamoDBFactory.Context.SaveAsync(item);
                }
            });
        }
Exemplo n.º 22
0
        public static async Task CreateInitialUsersAndRights()
        {
            await CleanUpBesteRightsAuthorization();
            await CleanUpBesteUser();
            await CleanUpServerSettings();

            User adminUser = new User
            {
                Username  = "******",
                Lastname  = "Admin",
                Firstname = "Admin",
                Email     = "Email",
                Password  = "******"
            };
            ModifyUserResponse response = await BesteUser.CreateUser(JsonConvert.SerializeObject(adminUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            TestHelper.ValiateResponse(response, ModifyUserResult.SUCCESS);

            User user = new User
            {
                Username  = "******",
                Lastname  = "User",
                Firstname = "User",
                Email     = "Email",
                Password  = "******"
            };

            response = await BesteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            TestHelper.ValiateResponse(response, ModifyUserResult.SUCCESS);

            user = new User
            {
                Username  = "******",
                Lastname  = "User",
                Firstname = "User",
                Email     = "Email",
                Password  = "******"
            };
            response = await BesteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            TestHelper.ValiateResponse(response, ModifyUserResult.SUCCESS);



            adminUser = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                adminUser.TableId = TABLE_ID;
                return(await context.LoadAsync(adminUser));
            });

            await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                await context.SaveAsync(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.User",
                    Operation        = "ChangePassword",
                    RecourceModule   = "User",
                    RecourceId       = null,
                    Authorized       = true,
                    LegitimationUuid = adminUser.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                });
                await context.SaveAsync(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.User",
                    Operation        = "CreateUser",
                    RecourceModule   = "User",
                    RecourceId       = null,
                    Authorized       = true,
                    LegitimationUuid = adminUser.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                });
                await context.SaveAsync(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.User",
                    Operation        = "DeleteUser",
                    RecourceModule   = "User",
                    RecourceId       = null,
                    Authorized       = true,
                    LegitimationUuid = adminUser.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                });
                await context.SaveAsync(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.User",
                    Operation        = "EditUser",
                    RecourceModule   = "User",
                    RecourceId       = null,
                    Authorized       = true,
                    LegitimationUuid = adminUser.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                });
                await context.SaveAsync(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.User",
                    Operation        = "GetUsers",
                    RecourceModule   = "User",
                    RecourceId       = null,
                    Authorized       = true,
                    LegitimationUuid = adminUser.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                });
                await context.SaveAsync(new BesteRightsAuthorization
                {
                    TableId          = TABLE_ID,
                    Namespace        = "Beste.GameServer.SDaysTDie.User",
                    Operation        = "GetUser",
                    RecourceModule   = "User",
                    RecourceId       = null,
                    Authorized       = true,
                    LegitimationUuid = adminUser.Uuid,
                    Uuid             = Guid.NewGuid().ToString()
                });
            });
        }