예제 #1
0
        public void PasswordMatch_should_return_false_when_password_do_not_match()
        {
            PasswordService service  = new PasswordService();
            string          password = "******";

            Assert.IsFalse(service.ValidatePassword(password + "b", service.CreateHash(password)));
        }
        public override async Task <Developer> Do()
        {
            await Check(() => IsUserNotExist(_email), DevelopersErrorCodes.EmailAlreadyExists);
            await Check(() => IsUsernameAvailable(_displayname), DevelopersErrorCodes.DisplayNameNotAvailable);

            byte[] passwordBytes = Encoding.UTF8.GetBytes(_password);
            byte[] saltBytes     = _passwordService.CreateSalt();
            byte[] hashBytes     = _passwordService.CreateHash(passwordBytes, saltBytes);

            Developer developer = new Developer
            {
                Email                 = _email,
                DisplayName           = _displayname,
                Salt                  = Convert.ToBase64String(saltBytes),
                PasswordHash          = Convert.ToBase64String(hashBytes),
                IsActive              = true,
                IsPending             = true,
                PendingRegistrationId = Guid.NewGuid(),
                Created               = DateTime.UtcNow
            };

            Context.Developers.Add(developer);

            return(developer);
        }
예제 #3
0
        public void PasswordMatch_should_return_true_when_password_match()
        {
            PasswordService service  = new PasswordService();
            string          password = "******";

            Assert.IsTrue(service.ValidatePassword(password, service.CreateHash(password)));
        }
예제 #4
0
        public override async Task <bool> Do()
        {
            Product product = await Context.Products
                              .FirstOrDefaultAsync(p => p.Id == _productId);

            if (product == null || !product.IsActive || !product.IsPublic)
            {
                return(false);
            }

            User user = await Context.Users
                        .FirstOrDefaultAsync(u => u.Email == _email && u.ProductId == product.Id);

            if (user == null || user.IsPending || !user.IsActive)
            {
                return(false);
            }

            byte[] passwordBytes = Encoding.UTF8.GetBytes(_password);
            byte[] saltBytes     = Convert.FromBase64String(user.Salt);
            byte[] hashBytes     = _passwordService.CreateHash(passwordBytes, saltBytes);
            string hash          = Convert.ToBase64String(hashBytes);

            return(hash == user.PasswordHash);
        }
예제 #5
0
        public async Task AddLogin(User user, string password, bool enabled = true)
        {
            var login = new Login()
            {
                UserId       = user.Id,
                PasswordHash = passwordService.CreateHash(password),
                Enabled      = enabled
            };

            dataContext.Logins.Add(login);
            await dataContext.SaveChangesAsync();
        }
예제 #6
0
        public override async Task <bool> Do()
        {
            Developer user = await Context.Developers.FirstOrDefaultAsync(u => u.Email == _email);

            if (user == null || user.IsPending || !user.IsActive)
            {
                return(false);
            }

            byte[] passwordBytes = Encoding.UTF8.GetBytes(_password);
            byte[] saltBytes     = Convert.FromBase64String(user.Salt);
            byte[] hashBytes     = _passwordService.CreateHash(passwordBytes, saltBytes);
            string hash          = Convert.ToBase64String(hashBytes);

            return(user.PasswordHash == hash);
        }
예제 #7
0
        public override async Task <User> Do()
        {
            await Check(() => IsUserExist(), AccountErrorCodes.EmailAlreadyExists);
            await Check(() => IsUsernameAvailable(), AccountErrorCodes.UsernameNotAvailable);

            Product product = await Context.Products
                              .Include(p => p.Policies)
                              .FirstOrDefaultAsync(p => p.Id == _productId);

            byte[] passwordBytes = Encoding.UTF8.GetBytes(_password);
            byte[] saltBytes     = _passwordService.CreateSalt();
            byte[] hashBytes     = _passwordService.CreateHash(passwordBytes, saltBytes);

            User user = new User
            {
                ProductId             = product.Id,
                Email                 = _email,
                Username              = _username,
                Salt                  = Convert.ToBase64String(saltBytes),
                PasswordHash          = Convert.ToBase64String(hashBytes),
                IsPending             = true,
                PendingRegistrationId = Guid.NewGuid(),
                IsActive              = true,
                IsExternal            = false
            };

            Context.Users.Add(user);

            Policy defaultPolicy = product.Policies.FirstOrDefault(p => p.Default);

            if (defaultPolicy != null)
            {
                user.Policies.Add(defaultPolicy);
            }

            return(user);
        }