public async Task <User> RegisterAsync(UserBase model)
        {
            model.ValidateIsNotNull(nameof(model));
            var userExists = await UserExistsAsync(model.Username);

            var emailInUse = await EmailInUseAsync(model.Email);

            if (userExists || emailInUse)
            {
                return(null);
            }

            var passwordSalt = CryptographyService.CreateSalt();
            var passwordHash = CryptographyService.CreateHash(model.Password, passwordSalt);

            var entity = new Entities.User();

            entity.PasswordHash = passwordHash;
            entity.PasswordSalt = passwordSalt;
            entity.FirstName    = model.FirstName;
            entity.LastName     = model.LastName;
            entity.Username     = model.Username;

            entity.DateOfBirth = model.DateOfBirth;
            entity.Email       = model.Email;

            await _genericRepository.InsertAsync(entity);

            await _databaseScope.SaveChangesAsync();

            return(_mapper.Map <User>(entity));
        }
Beispiel #2
0
        public void CreateHash_EmptyData_ThrowsArgumentNullException()
        {
            var factory = Substitute.For <ICryptographyProcessorFactory>();
            var svc     = new CryptographyService(factory);

            var e = Assert.Throws <ArgumentNullException>(() => svc.CreateHash(string.Empty, out var x, out var y));

            Assert.That(e.Message, Does.Contain("data"));
        }
Beispiel #3
0
        public void CreateHash_ValidData_HashingProcessorReceivedCall()
        {
            var factory       = Substitute.For <ICryptographyProcessorFactory>();
            var hashProcessor = Substitute.For <IHashingProcessor>();

            factory.GetHashingProcessor().Returns(hashProcessor);

            var svc = new CryptographyService(factory);

            svc.CreateHash("Test Data", out var hashResult, out var saltResult);

            hashProcessor.Received().CreateHash("Test Data", out Arg.Any <byte[]>(), out Arg.Any <byte[]>());
        }
        public async Task <User> LoginAsync(string username, string password)
        {
            var entity = await _genericRepository.All.FirstOrDefaultAsync(user => user.Username == username);

            entity.ValidateIsNotNull(nameof(entity));

            var passwordHash = CryptographyService.CreateHash(password, entity.PasswordSalt);

            if (entity.PasswordHash != passwordHash)
            {
                return(null);
            }

            return(_mapper.Map <User>(entity));
        }