Exemple #1
0
        public void OneTimeSetUp()
        {
            var option = new DbContextOptionsBuilder <PasswordManagerContext>().UseInMemoryDatabase(databaseName: "InMemoryDb")
                         .Options;

            _context = new PasswordManagerContext(option);
            _sut     = new MasterPasswordService(_context);

            _context.Users.Add(new User()
            {
                FirstName    = "John",
                LastName     = "Doe",
                EmailAddress = "*****@*****.**",
            });
            _context.SaveChanges();
            _context.Users.Add(new User()
            {
                FirstName    = "Jane",
                LastName     = "Doe",
                EmailAddress = "*****@*****.**",
            });
            _context.SaveChanges();

            var salt1         = Hash.GenerateSalt(20);
            var hashPassword1 = Hash.GenerateHash(Encoding.ASCII.GetBytes("Password123"), salt1, 1000, 16);

            var salt2         = Hash.GenerateSalt(20);
            var hashPassword2 = Hash.GenerateHash(Encoding.ASCII.GetBytes("Password!_"), salt2, 1000, 16);

            _sut.Create(hashPassword1, salt1, 1000, _context.Users.Where(u => u.EmailAddress == "*****@*****.**").FirstOrDefault().Id);
            _sut.Create(hashPassword2, salt2, 1000, _context.Users.Where(u => u.EmailAddress == "*****@*****.**").FirstOrDefault().Id);
        }
Exemple #2
0
        public void Setup()
        {
            using (var db = new PasswordManagerContext())
            {
                var selectedUser =
                    from u in db.Users
                    where u.EmailAddress == "*****@*****.**"
                    select u;

                db.Users.RemoveRange(selectedUser);
                db.SaveChanges();

                db.Users.Add(new User()
                {
                    FirstName = "Unit", LastName = "Test", EmailAddress = "*****@*****.**"
                });
                db.SaveChanges();

                _testUser = db.Users.Where(u => u.EmailAddress == "*****@*****.**").FirstOrDefault();


                var salt         = Hash.GenerateSalt(20);
                var hashPassword = Hash.GenerateHash(Encoding.ASCII.GetBytes("Password123"), salt, 1000, 16);

                db.MasterPasswords.Add(new MasterPassword()
                {
                    Hash = hashPassword, Salt = salt, Iterations = 1000, UserId = _testUser.Id
                });
                db.SaveChanges();
                _testMPassword = db.MasterPasswords.Where(mp => mp.UserId == _testUser.Id).FirstOrDefault();
            }
        }
Exemple #3
0
        private void SeedUserRoles()
        {
            if (PmContext.UserRoles.Count() > 1)
            {
                return;
            }
            var userRoles = new[]
            {
                new UserRole()
                {
                    RoleName = "Admin", Username = "******"
                },
                new UserRole()
                {
                    RoleName = "SystemUser", Username = "******"
                },
                new UserRole()
                {
                    RoleName = "Admin", Username = "******"
                },
                new UserRole()
                {
                    RoleName = "Admin", Username = "******"
                },
                new UserRole()
                {
                    RoleName = "SystemUser", Username = "******"
                }
            };

            PmContext.UserRoles.AddRange(userRoles);
            PmContext.SaveChanges();
        }
        public void Create(byte[] hash, byte[] salt, int iterations, int userId)
        {
            _context.MasterPasswords.Add(new MasterPassword()
            {
                Hash       = hash,
                Salt       = salt,
                Iterations = iterations,
                UserId     = userId
            });

            _context.SaveChanges();
        }
        public void TearDown()
        {
            using (var db = new PasswordManagerContext())
            {
                var selectedUser =
                    from u in db.Users
                    where u.EmailAddress == "*****@*****.**"
                    select u;

                db.Users.RemoveRange(selectedUser);
                db.SaveChanges();

                db.Users.RemoveRange(db.Users.Where(u => u.EmailAddress == "*****@*****.**"));
                db.SaveChanges();
            }
        }
Exemple #6
0
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            InitializeComponent();
            UnhandledException += App_UnhandledException;
            Suspending         += OnSuspending;

            using (var db = new PasswordManagerContext()) {
                try {
                    db.Database.Migrate();
                }
                catch (Exception ex) {
                    //Log.Error(ex.Message, ex);
                }
                try {
                    var t = db.Settings.FirstOrDefaultAsync(x => x.Name == AppSettings.MASTER_PASSWORD_KEY).Result;
                    if (t == null)
                    {
                        db.Settings.Add(new Setting {
                            Name = AppSettings.MASTER_PASSWORD_KEY
                        });
                        db.SaveChanges();
                    }
                }
                catch (Exception ex) {
                    //Log.Error(ex.Message, ex);
                }
            }
        }
 public void Execute(DeleteProfileCommand command)
 {
     using (var db = new PasswordManagerContext()) {
         var profile = db.Profiles.Find(command.Id);
         db.Remove(profile);
         db.SaveChanges();
     }
 }
        public void TearDown()
        {
            using (var db = new PasswordManagerContext())
            {
                db.MasterPasswords.RemoveRange(db.MasterPasswords.Where(mp => mp.UserId == _testUser.Id).FirstOrDefault());
                db.SaveChanges();

                if (db.Websites.Where(w => w.UserId == _testUser.Id).FirstOrDefault() != null)
                {
                    db.Websites.RemoveRange(db.Websites.Where(w => w.UserId == _testUser.Id).FirstOrDefault());
                    db.SaveChanges();
                }

                var user = db.Users.Find(_testUser.Id);
                db.Users.RemoveRange(user);
                db.SaveChanges();
            }
        }
Exemple #9
0
 public void Execute(CreateMasterPasswordCommand command)
 {
     using (var db = new PasswordManagerContext()) {
         var mp = db.Settings.Single(x => x.Name == AppSettings.MASTER_PASSWORD_KEY);
         mp.Value = SecurePasswordHasher.Hash(command.Password);
         db.Update(mp);
         db.SaveChanges();
         AppSettings.MasterPassword = command.Password;
     }
 }
        public void Setup()
        {
            using (var db = new PasswordManagerContext())
            {
                var selectedUser =
                    from u in db.Users
                    where u.EmailAddress == "*****@*****.**"
                    select u;

                db.Users.RemoveRange(selectedUser);
                db.SaveChanges();

                db.Users.Add(new User()
                {
                    FirstName = "Unit", LastName = "Test", EmailAddress = "*****@*****.**"
                });
                db.SaveChanges();
            }
        }
        public void Setup()
        {
            using (var db = new PasswordManagerContext())
            {
                var selectedUser =
                    from u in db.Users
                    where u.EmailAddress == "*****@*****.**"
                    select u;

                db.Users.RemoveRange(selectedUser);
                db.SaveChanges();

                db.Users.Add(new User()
                {
                    FirstName = "Unit", LastName = "Test", EmailAddress = "*****@*****.**"
                });
                db.SaveChanges();

                _testUser = db.Users.Where(u => u.EmailAddress == "*****@*****.**").FirstOrDefault();

                var salt         = Hash.GenerateSalt(20);
                var hashPassword = Hash.GenerateHash(Encoding.ASCII.GetBytes("Password123"), salt, 1000, 16);

                db.MasterPasswords.Add(new MasterPassword()
                {
                    Hash = hashPassword, Salt = salt, Iterations = 1000, UserId = _testUser.Id
                });
                db.SaveChanges();
                var testMPassword = db.MasterPasswords.Where(mp => mp.UserId == _testUser.Id).FirstOrDefault();
                _masterPasswordId = testMPassword.Id;

                _encryptedPassword = SymmetricEncryption.Encrypt(Convert.ToBase64String(hashPassword), "YouTubePassword1");

                db.Websites.Add(new Website()
                {
                    Name = "YouTube", Username = "******", Password = _encryptedPassword, Url = $"https://www.youtube.com/", UserId = _testUser.Id
                });
                db.SaveChanges();
                _testWebsite = db.Websites.Where(w => w.UserId == _testUser.Id).FirstOrDefault();
            };
        }
 public void Execute(UpdateProfileCommand command)
 {
     using (var db = new PasswordManagerContext()) {
         var pwd     = Cryptographer.Encrypt(command.Password, AppSettings.MasterPassword);
         var profile = db.Profiles.Find(command.Id);
         profile.Name     = command.Profile;
         profile.Account  = command.Account;
         profile.Password = pwd.EncryptedPassword;
         profile.IV       = pwd.IV;
         profile.Salt     = pwd.Salt;
         db.SaveChanges();
     }
 }
Exemple #13
0
        public void TearDown()
        {
            using (var db = new PasswordManagerContext())
            {
                var selectedUser =
                    from u in db.Users
                    where u.EmailAddress == "*****@*****.**"
                    select u;

                db.MasterPasswords.RemoveRange(db.MasterPasswords.Where(mp => mp.UserId == _testUser.Id));
                db.Users.RemoveRange(selectedUser);
                db.SaveChanges();
            }
        }
 public void Execute(CreateProfileCommand command)
 {
     using (var db = new PasswordManagerContext()) {
         var pwd = Cryptographer.Encrypt(command.Password, AppSettings.MasterPassword);
         db.Profiles.Add(new Profile {
             Name     = command.Profile,
             Password = pwd.EncryptedPassword,
             Account  = command.Account,
             IV       = pwd.IV,
             Salt     = pwd.Salt
         });
         db.SaveChanges();
     }
 }
Exemple #15
0
        public void WhenAPasswordIsCreatedTheDatabaseIsUpdated()
        {
            var masterPasswordManager = new MasterPasswordManager();

            using (var db = new PasswordManagerContext())
            {
                db.MasterPasswords.RemoveRange(db.MasterPasswords.Where(mp => mp.UserId == _testUser.Id));
                db.SaveChanges();
                var numberOfUsersBefore = db.MasterPasswords.Count();
                masterPasswordManager.Create(_testUser.Id, "ThisIsAStrongPassword123");
                var numberOfUsersAfter = db.MasterPasswords.Count();

                Assert.AreEqual(numberOfUsersBefore + 1, numberOfUsersAfter);
            }
        }
        public void WhenAWebsiteIsCreatedTheDatabaseIsUpdated()
        {
            var websiteManager = new WebsiteManager();

            using (var db = new PasswordManagerContext())
            {
                var website = db.Websites.Find(_testWebsite.Id);
                db.Websites.RemoveRange(website);
                db.SaveChanges();

                var numberOfWebsitesBefore = db.Websites.Count();
                websiteManager.Create(_testUser.Id, "Google", _encryptedPassword, "Username1", $"https://www.youtube.com/");
                var numberOfWebsitesAfter = db.Websites.Count();

                Assert.AreEqual(numberOfWebsitesBefore + 1, numberOfWebsitesAfter);
                _testWebsite = db.Websites.Where(w => w.UserId == _testUser.Id).FirstOrDefault();
            }
        }
Exemple #17
0
        public void GivenANewUser_CreateUserAddItToDataBase()
        {
            var numberOfUserBefore = _context.Users.Count();

            var newUser = new User
            {
                FirstName    = "James",
                LastName     = "Smith",
                EmailAddress = "*****@*****.**"
            };

            _sut.CreateUser(newUser);

            var userInDb = _sut.GetByEmail("*****@*****.**");

            Assert.That(_context.Users.Count(), Is.EqualTo(numberOfUserBefore + 1));
            Assert.That(userInDb.EmailAddress, Is.EqualTo("*****@*****.**"));

            _context.Users.Remove(userInDb);
            _context.SaveChanges();
        }
Exemple #18
0
 public void Create(Website website)
 {
     _context.Websites.Add(website);
     _context.SaveChanges();
 }
Exemple #19
0
 public void CreateUser(User u)
 {
     _context.Add(u);
     _context.SaveChanges();
 }