Пример #1
0
        public async Task Update(User userParam, string password = null)
        {
            var user = await _context.Users.SingleOrDefaultAsync(x => x.Id == userParam.Id);

            if (user == null)
            {
                throw new ApplicationException(Properties.resultMessages.UserNotFound);
            }

            if (userParam.Username != user.Username)
            {
                if (await _context.Users.AnyAsync(x => x.Username == userParam.Username))
                {
                    throw new ApplicationException(Properties.resultMessages.UsernameExists);
                }
            }

            user.Username = userParam.Username;

            if (!string.IsNullOrWhiteSpace(password))
            {
                PasswordHelpers.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;
            }

            _context.Users.Update(user);
            await _context.SaveChangesAsync();
        }
Пример #2
0
        public Room Create(Room room, string password)
        {
            if (string.IsNullOrWhiteSpace(room.Title))
            {
                throw new ApplicationException(Properties.resultMessages.TitleNull);
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ApplicationException(Properties.resultMessages.PasswordNull);
            }

            room.Id = 0;

            byte[] passwordHash, passwordSalt;
            PasswordHelpers.CreatePasswordHash(password, out passwordHash, out passwordSalt);

            room.PasswordHash = passwordHash;
            room.PasswordSalt = passwordSalt;

            _context.Rooms.Add(room);
            _context.SaveChanges();

            return(room);
        }
Пример #3
0
        public User Create(User user, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ApplicationException(Properties.resultMessages.PasswordNull);
            }

            if (string.IsNullOrWhiteSpace(user.Username))
            {
                throw new ApplicationException(Properties.resultMessages.UsernameNull);
            }

            if (_context.Users.Any(x => x.Username == user.Username))
            {
                throw new ApplicationException(Properties.resultMessages.UsernameExists);
            }

            user.Id = 0;

            byte[] passwordHash, passwordSalt;
            PasswordHelpers.CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _context.Users.Add(user);
            _context.SaveChanges();

            return(user);
        }
        public void CreatePasswordHash_Test()
        {
            // Arrange
            const string password = "******";

            // Act
            var(passwordHashResult, passwordSaltResult) = PasswordHelpers.CreatePasswordHash(password);

            // Assert
            var expectedPasswordHash = new HMACSHA512(passwordSaltResult).ComputeHash(UTF8.GetBytes(password));

            Assert.Equal(expectedPasswordHash, passwordHashResult);
        }
Пример #5
0
        public void Authenticate_NullOrWhiteSpacesCredentials_ThrowsCredentialsInvalidError(string username, string password)
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var users = new List <User> {
                new User()
                {
                    Id           = 1,
                    Username     = "******",
                    PasswordHash = passwordHash,
                    PasswordSalt = passwordSalt
                }
            };
            var userService = CreateService(users);

            var exception = Assert.Throws <ApplicationException>(
                () => userService.Authenticate(username, password));

            Assert.Equal(DiceApi.Properties.resultMessages.CredentialsInvalid, exception.Message);
        }
Пример #6
0
        public void Authenticate_NullOrWhiteSpacesPassoword_ThrowsCredentialsInvalidError(string password)
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var rooms = new List <Room> {
                new Room()
                {
                    Id           = 1,
                    Title        = "Room123",
                    PasswordHash = passwordHash,
                    PasswordSalt = passwordSalt
                }
            };
            var roomService = CreateService(rooms);
            int id          = 1;

            var exception = Assert.Throws <ApplicationException>(
                () => roomService.Authenticate(id, password));

            Assert.Equal(DiceApi.Properties.resultMessages.CredentialsInvalid, exception.Message);
        }
Пример #7
0
        public void Authenticate_ValidObjectPassed_ReturnsRoom()
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var rooms = new List <Room> {
                new Room()
                {
                    Id           = 1,
                    Title        = "Room123",
                    PasswordHash = passwordHash,
                    PasswordSalt = passwordSalt
                }
            };
            var    roomService = CreateService(rooms);
            int    id          = 1;
            string password    = "******";

            var result = roomService.Authenticate(id, password);

            Assert.IsType <Room>(result);
        }
        public User RegisterUser(UserRegisterDto userRegisterDto)
        {
            if (_context.Users.Any(x => x.Username == userRegisterDto.Username))
            {
                throw new AppException("Username \"" + userRegisterDto.Username + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            PasswordHelpers.CreatePasswordHash(userRegisterDto.Password, out passwordHash, out passwordSalt);

            var user = _mapper.Map <User>(userRegisterDto);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _context.Users.Add(user);
            _context.SaveChanges();

            return(user);
        }
Пример #9
0
        public void Authenticate_ValidObject_ReturnsUser()
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var users = new List <User> {
                new User()
                {
                    Id           = 1,
                    Username     = "******",
                    PasswordHash = passwordHash,
                    PasswordSalt = passwordSalt
                }
            };
            var    userService = CreateService(users);
            string username    = "******";
            string password    = "******";

            var result = userService.Authenticate(username, password);

            Assert.IsType <User>(result);
        }
Пример #10
0
        public void Update_ValidParams_DataUpdated()
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var rooms = new List <Room>
            {
                new Room {
                    Id = 1, Title = "Room1", PasswordHash = passwordHash, PasswordSalt = passwordSalt
                }
            };
            var  roomService = CreateService(rooms);
            Room roomParam   = new Room {
                Id = 1, Title = "NewRoom1"
            };
            string password = "******";

            roomService.Update(roomParam, password);

            var room = roomService.GetById(1);

            Assert.Equal("NewRoom1", room.Title);
            Assert.True(PasswordHelpers.VerifyPasswordHash("newPassword123", room.PasswordHash, room.PasswordSalt));
        }
Пример #11
0
        public void Update_ValidParams_DataUpdated()
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var users = new List <User>
            {
                new User {
                    Id = 1, Username = "******", PasswordHash = passwordHash, PasswordSalt = passwordSalt
                }
            };
            var  userService = CreateService(users);
            User userParam   = new User {
                Id = 1, Username = "******"
            };
            string password = "******";

            userService.Update(userParam, password);

            var user = userService.GetById(1);

            Assert.Equal("NewUser1", user.Username);
            Assert.True(PasswordHelpers.VerifyPasswordHash("newPassword123", user.PasswordHash, user.PasswordSalt));
        }
Пример #12
0
        public async Task <User> Create(string username, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ApplicationException(Properties.resultMessages.PasswordNull);
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ApplicationException(Properties.resultMessages.UsernameNull);
            }

            if (_context.Users.Any(x => x.Username == username))
            {
                throw new ApplicationException(Properties.resultMessages.UsernameExists);
            }

            PasswordHelpers.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);

            var folder = new Page
            {
                Name = username + " pages"
            };

            var user = new User
            {
                Username     = username,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt,
                MainPage     = folder
            };

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(user);
        }
Пример #13
0
        public static void CreateEntities(
            out List <User> users)
        {
            PasswordHelpers.CreatePasswordHash("User101Password",
                                               out byte[] user101PasswordHash, out byte[] user101PasswordSalt);
            var user101 = new User
            {
                Id           = 101,
                Username     = "******",
                PasswordHash = user101PasswordHash,
                PasswordSalt = user101PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("User102Password",
                                               out byte[] user102PasswordHash, out byte[] user102PasswordSalt);
            var user102 = new User
            {
                Id           = 102,
                Username     = "******",
                PasswordHash = user102PasswordHash,
                PasswordSalt = user102PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("User103Password",
                                               out byte[] user103PasswordHash, out byte[] user103PasswordSalt);
            var user103 = new User
            {
                Id           = 103,
                Username     = "******",
                PasswordHash = user103PasswordHash,
                PasswordSalt = user103PasswordSalt
            };

            users = new List <User> {
                user101, user102, user103
            };
        }
Пример #14
0
        public void Update(Room roomParam, string password = null)
        {
            var room = _context.Rooms.SingleOrDefault(x => x.Id == roomParam.Id);

            if (room == null)
            {
                throw new ApplicationException(Properties.resultMessages.RoomNotFound);
            }

            room.Title     = roomParam.Title;
            room.RoomUsers = roomParam.RoomUsers;

            if (!string.IsNullOrWhiteSpace(password))
            {
                byte[] passwordHash, passwordSalt;
                PasswordHelpers.CreatePasswordHash(password, out passwordHash, out passwordSalt);

                room.PasswordHash = passwordHash;
                room.PasswordSalt = passwordSalt;
            }

            _context.Rooms.Update(room);
            _context.SaveChanges();
        }
Пример #15
0
        public static void CreateEntities(
            out List <User> users,
            out List <Room> rooms,
            out List <UserRoom> userRooms)
        {
            PasswordHelpers.CreatePasswordHash("User101Password",
                                               out byte[] user101PasswordHash, out byte[] user101PasswordSalt);
            var user101 = new User
            {
                Id           = 101,
                Username     = "******",
                PasswordHash = user101PasswordHash,
                PasswordSalt = user101PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("User102Password",
                                               out byte[] user102PasswordHash, out byte[] user102PasswordSalt);
            var user102 = new User
            {
                Id           = 102,
                Username     = "******",
                PasswordHash = user102PasswordHash,
                PasswordSalt = user102PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("User103Password",
                                               out byte[] user103PasswordHash, out byte[] user103PasswordSalt);
            var user103 = new User
            {
                Id           = 103,
                Username     = "******",
                PasswordHash = user103PasswordHash,
                PasswordSalt = user103PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("Room101Password",
                                               out byte[] room101PasswordHash, out byte[] room101PasswordSalt);
            var room101 = new Room
            {
                Id           = 101,
                Title        = "Room101",
                PasswordHash = room101PasswordHash,
                PasswordSalt = room101PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("Room102Password",
                                               out byte[] room102PasswordHash, out byte[] room102PasswordSalt);
            var room102 = new Room
            {
                Id           = 102,
                Title        = "Room102",
                PasswordHash = room102PasswordHash,
                PasswordSalt = room102PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("Room103Password",
                                               out byte[] room103PasswordHash, out byte[] room103PasswordSalt);
            var room103 = new Room
            {
                Id           = 103,
                Title        = "Room103",
                PasswordHash = room103PasswordHash,
                PasswordSalt = room103PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("Room104Password",
                                               out byte[] room104PasswordHash, out byte[] room104PasswordSalt);
            var room104 = new Room
            {
                Id           = 104,
                Title        = "Room104",
                PasswordHash = room104PasswordHash,
                PasswordSalt = room104PasswordSalt
            };

            PasswordHelpers.CreatePasswordHash("Room105Password",
                                               out byte[] room105PasswordHash, out byte[] room105PasswordSalt);
            var room105 = new Room
            {
                Id           = 105,
                Title        = "Room105",
                PasswordHash = room105PasswordHash,
                PasswordSalt = room105PasswordSalt
            };

            var user101Room101 = new UserRoom {
                User = user101, Room = room101, Owner = false
            };
            var user101Room102 = new UserRoom {
                User = user101, Room = room102, Owner = false
            };
            var user101Room103 = new UserRoom {
                User = user101, Room = room103, Owner = true
            };
            var user102Room101 = new UserRoom {
                User = user102, Room = room101, Owner = true
            };
            var user102Room104 = new UserRoom {
                User = user102, Room = room104, Owner = true
            };
            var user102Room105 = new UserRoom {
                User = user102, Room = room105, Owner = true
            };

            user101.UserRooms = new List <UserRoom> {
                user101Room101, user101Room102, user101Room103
            };
            user102.UserRooms = new List <UserRoom> {
                user102Room101, user102Room104, user102Room105
            };
            room101.RoomUsers = new List <UserRoom> {
                user101Room101, user102Room101
            };
            room102.RoomUsers = new List <UserRoom> {
                user101Room102
            };
            room103.RoomUsers = new List <UserRoom> {
                user101Room103
            };
            room104.RoomUsers = new List <UserRoom> {
                user102Room104
            };
            room105.RoomUsers = new List <UserRoom> {
                user102Room105
            };

            users = new List <User> {
                user101, user102, user103
            };
            rooms = new List <Room> {
                room101, room102, room103, room104, room105
            };
            userRooms = new List <UserRoom> {
                user101Room101, user101Room102, user101Room103, user102Room101, user102Room104, user102Room105
            };
        }