Ejemplo n.º 1
0
        public void SaveOrUpdatePassword(Password password, bool addToDatabase)
        {
            if (registeredPasswords.ContainsKey(password.ParentId))
            {
                registeredPasswords[password.ParentId] = password;
            }
            else
            {
                registeredPasswords.Add(password.ParentId, password);
            }

            if (addToDatabase)
            {
                using (var context = new PasswordContext())
                {
                    var existingPass = context.Objects.FirstOrDefault(pass => pass.Id == password.Id);

                    if (existingPass != null)
                    {
                        context.Objects.Remove(existingPass);
                    }

                    context.Objects.Add(password);

                    context.SaveChanges();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize the service.
        /// </summary>
        public void Initialize()
        {
            try
            {
                using (var context = new PasswordContext())
                {
                    var allPasswords = context.Objects.ToList();

                    foreach (var password in allPasswords)
                    {
                        var realPassword = (Password)password.CreateFromProxy(password);

                        registeredPasswords.Add(realPassword.ParentId, realPassword);
                    }
                }

                $"Cached {registeredPasswords.Count} passwords".WriteToConsole();
                logger.Info($"Initialized { nameof(PasswordService) }");
            }
            catch (Exception ex)
            {
                string message = $"Failed to initialize {nameof(PasswordService)}";
                message.WriteToConsole();
                logger.Error(ex, message);
            }
        }
 public async Task <IEnumerable <string> > GetUserNamesAsync(string host, int port)
 {
     using (PasswordContext dbContext = new PasswordContext())
     {
         var entries   = dbContext.Passwords.Where(x => x.Host == host && x.Port == port);
         var userNames = entries.Select(x => x.UserName);
         return(await userNames.ToArrayAsync());
     }
 }
 public async Task <IEnumerable <string> > GetUserNamesAsync(string host, int port, string startsWith)
 {
     using (PasswordContext dbContext = new PasswordContext())
     {
         var entries = from Password item in dbContext.Passwords
                       where item.Host == host
                       where item.Port == port
                       where item.UserName.StartsWith(startsWith)
                       select item.UserName;
         return(await entries.ToArrayAsync());
     }
 }
Ejemplo n.º 5
0
        // note: this class is only used here in order to simplify the test app deployment, in a real prod environment we would use migrations
        public static void Seed(PasswordContext ctx)
        {
            var rules = new List <PasswordRule>()
            {
                new PasswordRule()
                {
                    Name             = "Contains Numbers",
                    ImplementingType = "YetAnotherPasswordChecker.BLL.PasswordRules.ContainsNumbers",
                    IsActive         = true,
                    Importance       = 1
                },
                new PasswordRule()
                {
                    Name             = "Contains Special Characters",
                    ImplementingType = "YetAnotherPasswordChecker.BLL.PasswordRules.ContainsSpecialCharacters",
                    IsActive         = true,
                    Importance       = 2
                },
                new PasswordRule()
                {
                    Name             = "Contains Upper Case",
                    ImplementingType = "YetAnotherPasswordChecker.BLL.PasswordRules.ContainsUpperCase",
                    IsActive         = true,
                    Importance       = 1
                },
                new PasswordRule()
                {
                    Name                       = "Length Valid",
                    ImplementingType           = "YetAnotherPasswordChecker.BLL.PasswordRules.LengthValid",
                    IsActive                   = true,
                    Importance                 = 4,
                    PasswordRuleConfigurations = new List <PasswordRuleConfiguration>()
                    {
                        new PasswordRuleConfiguration()
                        {
                            Name  = "MaxLength",
                            Value = "100"
                        },
                        new PasswordRuleConfiguration()
                        {
                            Name  = "MinLength",
                            Value = "10"
                        }
                    }
                },
            };

            ctx.PasswordRules.AddRange(rules);
            ctx.SaveChanges();
        }
        public async Task <string> GetPasswordAsync(string host, int port, string userName)
        {
            using (PasswordContext dbContext = new PasswordContext())
            {
                var entry = await dbContext.Passwords.FirstOrDefaultAsync(
                    x => x.Host == host && x.Port == port && x.UserName == userName);

                if (entry == null)
                {
                    return(null);
                }
                string password = await DecryptAsync(entry.EncryptedPassword);

                return(password);
            }
        }
Ejemplo n.º 7
0
        private static PasswordContext ParseContext(string line)
        {
            var context = new PasswordContext();

            var parts = line.Split(' ');

            var range = parts[0];

            var rangeValues = range
                              .Split('-')
                              .Select(v => int.Parse(v))
                              .ToList();

            context.MinPosition = rangeValues[0];
            context.MaxPosition = rangeValues[1];

            context.Character = parts[1][0];

            context.Password = parts[2];

            return(context);
        }
        public async Task SavePasswordAsync(string host, int port, string userName, string password)
        {
            using (PasswordContext dbContext = new PasswordContext())
            {
                byte[] encryptedPassword = await EncryptAsync(password);

                bool createNew = false;

                var oldEntries    = dbContext.Passwords.Where(x => x.Host == host && x.Port == port && x.UserName == userName);
                var firstOldEntry = await oldEntries.FirstOrDefaultAsync();

                if (firstOldEntry != null)
                {
                    firstOldEntry.EncryptedPassword = encryptedPassword;
                }
                else
                {
                    createNew = true;
                }
                dbContext.RemoveRange(oldEntries.Skip(1));

                if (createNew)
                {
                    var newEntry = new Password
                    {
                        Host              = host,
                        Port              = port,
                        UserName          = userName,
                        EncryptedPassword = encryptedPassword
                    };
                    await dbContext.AddAsync(newEntry);
                }

                await dbContext.SaveChangesAsync();
            }
        }
Ejemplo n.º 9
0
 public void ChangePassword(CSEntry csentry, SecureString oldPassword, SecureString newPassword, PasswordContext context)
 {
     AsyncHelper.RunSync(((OktaConnectionContext)context.ConnectionContext).Client.Users.ChangePasswordAsync(csentry.DN.ToString(),
                                                                                                             new ChangePasswordOptions()
     {
         NewPassword     = newPassword.ConvertToUnsecureString(),
         CurrentPassword = oldPassword.ConvertToUnsecureString()
     }));
 }
Ejemplo n.º 10
0
        public void SetPassword(CSEntry csentry, SecureString newPassword, PasswordOptions options, PasswordContext context)
        {
            User u = new User
            {
                Credentials = new UserCredentials()
                {
                    Password = new PasswordCredential()
                    {
                        Value = newPassword.ConvertToUnsecureString()
                    }
                }
            };

            AsyncHelper.RunSync(((OktaConnectionContext)context.ConnectionContext).Client.Users.UpdateUserAsync(u, csentry.DN.ToString()));
        }