public void DataExtensions_CreatePassword_Passwords_Match()
        {
            string password1 = "password1";
            string password2 = "password1";

            DataExtensions.CreatePasswordHash(password1, out byte[] hash, out byte[] salt);
            bool result = DataExtensions.IsPasswordCorrect(password2, hash, salt);

            Assert.IsTrue(result);
        }
예제 #2
0
        protected User MockUser(string pass = "******")
        {
            DataExtensions.CreatePasswordHash(pass, out byte[] hash, out byte[] salt);

            var user = _fixture.Build <User>()
                       .With(t => t.PasswordHash, hash)
                       .With(t => t.PasswordSalt, salt)
                       .Create();

            _userRepositoryMock
            .Setup(t => t.FindOneAsync(It.IsAny <Expression <Func <User, bool> > >(), It.IsAny <string[]>()))
            .ReturnsAsync(user);

            return(user);
        }
예제 #3
0
        public async Task UserService_ChangeUserPasswordAsync_OK()
        {
            string pass       = "******";
            var    user       = MockUser(pass);
            Guid   modifierId = Guid.NewGuid();
            string newPass    = "******";

            DataExtensions.CreatePasswordHash(newPass, out byte[] newHash, out byte[] newSalt);

            var changeModel = _fixture.Build <ChangePasswordModel>()
                              .With(u => u.Email, user.Email)
                              .With(t => t.OldPassword, pass)
                              .With(t => t.NewPassword, newPass)
                              .Create();

            await _userService.ChangeUserPasswordAsync(changeModel);

            _userRepositoryMock.Verify(t => t.InsertOrUpdateAsync(It.Is <User>(u => u.Id == user.Id), modifierId));
        }
        public async Task <UserModel> SignUpAsync(SignUpModel model)
        {
            var userByEmail = await _userRepository.FindOneAsync(r => r.Email == model.Email);

            if (userByEmail != null)
            {
                throw userByEmail.EmailAlreadyTakenException();
            }

            var newUser = _mapper.Map <SignUpModel, User>(model);

            DataExtensions.CreatePasswordHash(model.Password, out var hash, out var salt);
            newUser.PasswordHash = hash;
            newUser.PasswordSalt = salt;

            await _userRepository.InsertOrUpdateAsync(newUser, newUser.Id);

            var createdUserModel = _mapper.Map <User, UserModel>(newUser);

            return(createdUserModel);
        }
예제 #5
0
        public async Task <UserModel> ChangeUserPasswordAsync(ChangePasswordModel model)
        {
            var user = await _userRepository.FindOneAsync(t => t.Email == model.Email);

            if (user == null)
            {
                throw model.Email.EmailNotFoundException();
            }

            if (!DataExtensions.IsPasswordCorrect(model.OldPassword, user.PasswordHash, user.PasswordSalt))
            {
                throw user.PasswordIsIncorrectException();
            }

            DataExtensions.CreatePasswordHash(model.NewPassword, out byte[] newPassHash, out byte[] newSaltHash);

            user.PasswordHash = newPassHash;
            user.PasswordSalt = newSaltHash;

            var updatedUser = await _userRepository.InsertOrUpdateAsync(user, user.CreatedBy);

            return(_mapper.Map <User, UserModel>(updatedUser));
        }