/// <summary>
        /// Get the whole list of passwords for a user
        /// </summary>
        /// <param name="userId">User id</param>
        /// <returns>The list of active passwords</returns>
        internal List <PasswordModel> GetList(int userId)
        {
            List <PasswordModel> passwords = new List <PasswordModel>();

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                List <Password> pwds = (from pass in context.Passwords
                                        where pass.IsActive && pass.UserId == userId
                                        select pass).ToList();

                BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);
                engine.SetPadding(new Pkcs7Padding());

                foreach (Password pwd in pwds)
                {
                    passwords.Add(new PasswordModel()
                    {
                        Id           = pwd.Id,
                        DisplayName  = pwd.DisplayName,
                        Login        = GetDecryptedString(engine, pwd.Login),
                        Password     = GetDecryptedString(engine, pwd.Password1),
                        Url          = GetDecryptedString(engine, pwd.Url),
                        Notes        = GetDecryptedString(engine, pwd.Notes),
                        CreationDate = pwd.CreationDate,
                        IsActive     = pwd.IsActive,
                        UserId       = pwd.UserId
                    });
                }
            }

            return(passwords);
        }
        /// <summary>
        /// Delete a specify connection
        /// </summary>
        /// <param name="tmpConnectionToSet">connection to delete</param>
        internal void DeleteConnection(ConnectionModel tmpConnectionToSet)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            string encComputerMacAddress = engine.Encrypt(string.Concat(Salt, tmpConnectionToSet.MachineMacAddress, Pepper), Key256Bits);
            string encComputerName       = engine.Encrypt(string.Concat(Salt, tmpConnectionToSet.MachineName, Pepper), Key256Bits);
            string encComputerUserName   = engine.Encrypt(string.Concat(Salt, tmpConnectionToSet.MachineUserName, Pepper), Key256Bits);

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                // Delete any previous row which match
                List <ConnectionTemp> existingConnections = (from conn in context.ConnectionTemps
                                                             where conn.ComputerMacAddress.Equals(encComputerMacAddress) &&
                                                             conn.ComputerName.Equals(encComputerName) &&
                                                             conn.ComputerUserName.Equals(encComputerUserName)
                                                             select conn).ToList();

                if (existingConnections.Count > 0)
                {
                    context.ConnectionTemps.RemoveRange(existingConnections);
                    context.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Add a custom field for a password
        /// </summary>
        /// <param name="fieldType">Type of the custom field</param>
        /// <param name="pwdId">Password id</param>
        /// <param name="webControlId">Id of the HTML control</param>
        internal void AddOrUpdateCustomField(Tools.TypeField fieldType, int pwdId, string webControlId)
        {
            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                CustomField cField = (from field in context.CustomFields
                                      where field.IdPassword == pwdId && field.IdCustomFieldType == (int)fieldType
                                      select field).FirstOrDefault();

                if (cField == null)
                {
                    cField = new CustomField()
                    {
                        IdCustomFieldType = Convert.ToInt32(fieldType),
                        IdPassword        = pwdId,
                        ControlId         = webControlId
                    };

                    context.CustomFields.Add(cField);
                }
                else
                {
                    cField.ControlId = webControlId;
                }

                context.SaveChanges();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Get a user
        /// </summary>
        /// <param name="login">Login</param>
        /// <param name="password">Password</param>
        /// <returns>The user who matches with the login/password</returns>
        internal UserModel GetUser(string login, string password)
        {
            UserModel user = null;

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                User currentUser = (from usr in context.Users
                                    where usr.Login.ToLower().Equals(login.ToLower()) && usr.IsActive
                                    select usr).FirstOrDefault();

                PasswordHasher hasher = new PasswordHasher();

                if (currentUser != null && hasher.Verify(currentUser.PasswordHash, string.Concat(Salt, password, Pepper)))
                {
                    user = new UserModel()
                    {
                        Id           = currentUser.Id,
                        Login        = currentUser.Login,
                        Password     = password,
                        DisplayName  = currentUser.DisplayName,
                        IsActive     = currentUser.IsActive,
                        CreationDate = currentUser.CreationDate
                    };
                }
            }

            return(user);
        }
Esempio n. 5
0
 /// <summary>
 /// Count how much login exist for a given login
 /// </summary>
 /// <param name="login">User login to control</param>
 /// <returns>Occurs of the login in database</returns>
 internal int GetUserCount(string login)
 {
     using (PasswordKeeperEntities context = new PasswordKeeperEntities())
     {
         return((from usr in context.Users
                 where usr.Login.ToLower().Equals(login.ToLower()) && usr.IsActive
                 select usr).Count());
     }
 }
        /// <summary>
        /// Delete a password
        /// </summary>
        /// <param name="pwdId">Password id</param>
        internal void Delete(int pwdId)
        {
            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                Password pass = GetPassword(context, pwdId);

                pass.IsActive = false;

                context.SaveChanges();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Delete a user
        /// </summary>
        /// <param name="userId">User id</param>
        internal void Delete(int userId)
        {
            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                User userToDelete = GetUser(context, userId);

                userToDelete.IsActive = false;

                context.SaveChanges();
            }
        }
        /// <summary>
        /// Delete all custom fields stored for a password
        /// </summary>
        /// <param name="pwdId">Password Id</param>
        internal void Delete(int pwdId)
        {
            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                IEnumerable <CustomField> cFields = from field in context.CustomFields
                                                    where field.IdPassword == pwdId
                                                    select field;

                cFields.ToList().ForEach(field => context.CustomFields.Remove(field));

                context.SaveChanges();
            }
        }
        /// <summary>
        /// Clean all connections
        /// </summary>
        internal void SwipeOldConnections()
        {
            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                // Delete any previous row older than an hour
                List <ConnectionTemp> existingConnections = (from conn in context.ConnectionTemps
                                                             where DbFunctions.AddHours(conn.ConnexionDate, 1) < DateTime.Now
                                                             select conn).ToList();

                if (existingConnections.Count > 0)
                {
                    context.ConnectionTemps.RemoveRange(existingConnections);
                    context.SaveChanges();
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Update existing user
        /// </summary>
        /// <param name="userToUpdate">User to update</param>
        internal void Update(UserModel userToUpdate)
        {
            PasswordHasher hasher = new PasswordHasher();

            string passwordHash = hasher.Hash(string.Concat(Salt, userToUpdate.Password, Pepper));

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                User currentUser = GetUser(context, userToUpdate.Id);

                currentUser.PasswordHash = passwordHash;
                currentUser.DisplayName  = userToUpdate.DisplayName;

                context.SaveChanges();
            }
        }
        /// <summary>
        /// Update a password
        /// </summary>
        /// <param name="pwdToUpdate">Password to update</param>
        internal void Update(PasswordModel pwdToUpdate)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                Password pass = GetPassword(context, pwdToUpdate.Id);

                pass.DisplayName = pwdToUpdate.DisplayName;
                pass.Login       = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Login, Pepper), Key256Bits);
                pass.Password1   = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Password, Pepper), Key256Bits);
                pass.Url         = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Url, Pepper), Key256Bits);
                pass.Notes       = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Notes, Pepper), Key256Bits);

                context.SaveChanges();
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Create new user
        /// </summary>
        /// <param name="userToCreate">User to create</param>
        internal void Create(UserModel userToCreate)
        {
            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                PasswordHasher hasher = new PasswordHasher();

                string passwordHash = hasher.Hash(string.Concat(Salt, userToCreate.Password, Pepper));

                User newUser = new User()
                {
                    Login        = userToCreate.Login,
                    PasswordHash = passwordHash,
                    DisplayName  = userToCreate.DisplayName,
                    CreationDate = userToCreate.CreationDate,
                    IsActive     = userToCreate.IsActive
                };

                context.Users.Add(newUser);
                context.SaveChanges();
            }
        }
        /// <summary>
        /// Get the last user id for the given connection details
        /// </summary>
        /// <param name="tmpConnection">Connection data</param>
        /// <returns>The user id</returns>
        internal int?GetConnectionTempUserId(ConnectionModel tmpConnection)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            string encComputerMacAddress = engine.Encrypt(string.Concat(Salt, tmpConnection.MachineMacAddress, Pepper), Key256Bits);
            string encComputerName       = engine.Encrypt(string.Concat(Salt, tmpConnection.MachineName, Pepper), Key256Bits);
            string encComputerUserName   = engine.Encrypt(string.Concat(Salt, tmpConnection.MachineUserName, Pepper), Key256Bits);

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                int usrIdLogin = (from conn in context.ConnectionTemps
                                  where conn.ComputerMacAddress.Equals(encComputerMacAddress) &&
                                  conn.ComputerName.Equals(encComputerName) &&
                                  conn.ComputerUserName.Equals(encComputerUserName)
                                  select conn.IdUser).FirstOrDefault();

                return(usrIdLogin == 0 ? new int?() : usrIdLogin);
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Get a user
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns>The user who matches with the user ID</returns>
        internal UserModel GetUser(int userId)
        {
            UserModel user;

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                User currentUser = (from usr in context.Users
                                    where usr.Id == userId && usr.IsActive
                                    select usr).FirstOrDefault();

                user = new UserModel()
                {
                    Id           = currentUser.Id,
                    Login        = currentUser.Login,
                    DisplayName  = currentUser.DisplayName,
                    IsActive     = currentUser.IsActive,
                    CreationDate = currentUser.CreationDate
                };
            }

            return(user);
        }
        /// <summary>
        /// Get the list of customs fields for a given password entry
        /// </summary>
        /// <param name="pwdId">Id of the password entry</param>
        /// <returns>List of the custom fields of the password</returns>
        internal List <CustomFieldModel> GetCustomFields(int pwdId)
        {
            List <CustomFieldModel> customFields = new List <CustomFieldModel>();

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                IEnumerable <CustomField> cFields = from field in context.CustomFields
                                                    where field.IdPassword == pwdId
                                                    select field;

                foreach (CustomField field in cFields)
                {
                    customFields.Add(new CustomFieldModel()
                    {
                        PasswordId   = field.IdPassword,
                        Type         = (Tools.TypeField)field.IdCustomFieldType,
                        WebControlId = field.ControlId
                    });
                }
            }

            return(customFields);
        }
        /// <summary>
        /// Create a new password
        /// </summary>
        /// <param name="pwdToCreate">Password to create</param>
        internal void Create(PasswordModel pwdToCreate)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                Password newPwd = new Password()
                {
                    Login        = engine.Encrypt(string.Concat(Salt, pwdToCreate.Login, Pepper), Key256Bits),
                    Password1    = engine.Encrypt(string.Concat(Salt, pwdToCreate.Password, Pepper), Key256Bits),
                    DisplayName  = pwdToCreate.DisplayName,
                    Url          = engine.Encrypt(string.Concat(Salt, pwdToCreate.Url, Pepper), Key256Bits),
                    Notes        = engine.Encrypt(string.Concat(Salt, pwdToCreate.Notes, Pepper), Key256Bits),
                    CreationDate = pwdToCreate.CreationDate,
                    IsActive     = pwdToCreate.IsActive,
                    UserId       = pwdToCreate.UserId
                };

                context.Passwords.Add(newPwd);
                context.SaveChanges();
            }
        }
Esempio n. 17
0
 /// <summary>
 /// Get a user from database
 /// </summary>
 /// <param name="ctx">Entity context</param>
 /// <param name="passwordId">Id of the user</param>
 /// <returns>The user matching with the id</returns>
 private User GetUser(PasswordKeeperEntities ctx, int usrId)
 {
     return((from user in ctx.Users
             where user.Id == usrId
             select user).First());
 }
 /// <summary>
 /// Get a password from database
 /// </summary>
 /// <param name="ctx">Entity context</param>
 /// <param name="passwordId">Id of the password</param>
 /// <returns>The password matching with the id</returns>
 private Password GetPassword(PasswordKeeperEntities ctx, int passwordId)
 {
     return((from pwd in ctx.Passwords
             where pwd.Id == passwordId
             select pwd).First());
 }