Пример #1
0
        private string RetrieveApiKey()
        {
            const string regex            = "^(hibp-?|haveibeenpwned)apikey$";
            var          candidates       = new PwObjectList <PwEntry>();
            var          searchParameters = new SearchParameters()
            {
                SearchInTitles = true,
                SearchString   = "apikey",
            };

            this.pluginHost.Database.RootGroup.SearchEntries(searchParameters, candidates);

            var apiKeys = candidates.Where(x => Regex.IsMatch(x.Strings.ReadSafe(PwDefs.TitleField), regex, RegexOptions.IgnoreCase));

            if (apiKeys.Count() > 1)
            {
                throw new ApiKeyException(string.Format("Found more than one api key matching the pattern: {0}", regex));
            }
            else if (apiKeys.Any() == false)
            {
                throw new ApiKeyException("No API Key found. Please create an entry named \"hibp-apikey\" in your database\n" +
                                          "and set its password to the API key obtained from https://haveibeenpwned.com/API/Key");
            }
            else
            {
                var key = apiKeys.Single();
                return(key.Strings.ReadSafe(PwDefs.PasswordField));
            }
        }
Пример #2
0
        /// <summary>
        /// Finds all entries with a given group and tag (or multiple)
        /// </summary>
        /// <param name="sourceDb">Database to search for the entries.</param>
        /// <param name="groups">Groups to search for (multiple separated by ,).</param>
        /// <param name="tags">Tag to search for (multiple separated by ,).</param>
        /// <returns>A PwObjectList with all metching entries.</returns>
        private static PwObjectList <PwEntry> FindEntriesByGroupAndTag(PwDatabase sourceDb, string groups, string tags)
        {
            PwObjectList <PwEntry> entries = new PwObjectList <PwEntry>();

            // Tag and group export
            foreach (string group in groups.Split(',').Select(x => x.Trim()))
            {
                PwGroup groupToExport = sourceDb.RootGroup.GetFlatGroupList().FirstOrDefault(g => g.Name == group);

                if (groupToExport == null)
                {
                    throw new ArgumentException("No group with the name of the Group-Setting found.");
                }

                foreach (string tag in tags.Split(',').Select(x => x.Trim()))
                {
                    PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                    groupToExport.FindEntriesByTag(tag, tagEntries, true);
                    // Prevent duplicated entries
                    IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                    List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                    entries.Add(entriesToAdd);
                }
            }

            return(entries);
        }
Пример #3
0
        private static void SelectEntriesAndDelete(PwObjectList <PwEntry> masterList, PwObjectList <PwEntry> slaveList, PwDatabase targetDb)
        {
            //Find entries in slaveList not in masterList to delete
            IEnumerable <PwUuid> masterUuid = masterList.Select(u => u.Uuid);
            var toDelete = slaveList.Where(e => !masterUuid.Contains(e.Uuid)).ToList();

            DeleteEntries(toDelete, targetDb);
        }
Пример #4
0
        /// <summary>
        /// Finds all entries with a given tag (or multiple)
        /// </summary>
        /// <param name="sourceDb">Database to search for the entries.</param>
        /// <param name="tags">Tag to search for (multiple separated by ,).</param>
        /// <returns>A PwObjectList with all metching entries.</returns>
        private static PwObjectList <PwEntry> FindEntriesByTag(PwDatabase sourceDb, string tags)
        {
            PwObjectList <PwEntry> entries = new PwObjectList <PwEntry>();

            foreach (string tag in tags.Split(',').Select(x => x.Trim()))
            {
                PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                sourceDb.RootGroup.FindEntriesByTag(tag, tagEntries, true);
                // Prevent duplicated entries
                IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                entries.Add(entriesToAdd);
            }

            return(entries);
        }
Пример #5
0
        private static PwObjectList <PwEntry> GetMatching(PwDatabase sourceDb, Settings settings)
        {
            PwObjectList <PwEntry> entries = new PwObjectList <PwEntry>();

            if (!string.IsNullOrEmpty(settings.Tag) && string.IsNullOrEmpty(settings.Group))
            {
                // Tag only export
                // Support multiple tag (Tag1,Tag2)
                foreach (string tag in settings.Tag.Split(','))
                {
                    PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                    sourceDb.RootGroup.FindEntriesByTag(tag, tagEntries, true);
                    // Prevent duplicated entries
                    IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                    List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                    entries.Add(entriesToAdd);
                }
            }
            else if (string.IsNullOrEmpty(settings.Tag) && !string.IsNullOrEmpty(settings.Group))
            {
                // Support multiple group (Group1,Group2)
                foreach (string group in settings.Group.Split(','))
                {
                    // Tag and group export
                    PwGroup groupToExport = sourceDb.RootGroup.GetFlatGroupList().FirstOrDefault(g => g.Name == group);

                    if (groupToExport == null)
                    {
                        throw new ArgumentException("No group with the name of the Group-Setting found.");
                    }

                    PwObjectList <PwEntry> groupEntries = groupToExport.GetEntries(true);
                    // Prevent duplicated entries
                    IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                    List <PwEntry>       entriesToAdd  = groupEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                    entries.Add(entriesToAdd);
                }
            }
            else if (!string.IsNullOrEmpty(settings.Tag) && !string.IsNullOrEmpty(settings.Group))
            {
                // Tag and group export
                foreach (string group in settings.Group.Split(','))
                {
                    PwGroup groupToExport = sourceDb.RootGroup.GetFlatGroupList().FirstOrDefault(g => g.Name == group);

                    if (groupToExport == null)
                    {
                        throw new ArgumentException("No group with the name of the Group-Setting found.");
                    }

                    foreach (string tag in settings.Tag.Split(','))
                    {
                        PwObjectList <PwEntry> tagEntries = new PwObjectList <PwEntry>();
                        groupToExport.FindEntriesByTag(tag, tagEntries, true);
                        // Prevent duplicated entries
                        IEnumerable <PwUuid> existingUuids = entries.Select(x => x.Uuid);
                        List <PwEntry>       entriesToAdd  = tagEntries.Where(x => !existingUuids.Contains(x.Uuid)).ToList();
                        entries.Add(entriesToAdd);
                    }
                }
            }
            else
            {
                throw new ArgumentException("At least one of Tag or ExportFolderName must be set.");
            }

            return(entries);
        }