/// <summary>
        /// Asks the user whether they trust a given key for a specific domain. May automatically accept based on policy.
        /// </summary>
        /// <param name="uri">The URI the signed file originally came from.</param>
        /// <param name="fingerprint">The fingerprint of the key to trust.</param>
        /// <param name="domain">The domain to trust the key for.</param>
        /// <returns><c>true</c> if the user decided to trust the key, <c>false</c> if they decided not to trust the key.</returns>
        /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
        private bool AskKeyApproval(FeedUri uri, string fingerprint, Domain domain)
        {
            (bool goodVote, string?keyInformation) = GetKeyInformation(fingerprint);

            // Automatically trust key for _new_ feeds if voted good by key server
            if (_config.AutoApproveKeys && goodVote && !_feedCache.Contains(uri))
            {
                Log.Info("Auto-approving key for " + uri.ToStringRfc());
                return(true);
            }

            // Otherwise ask user
            return(_handler.Ask(
                       string.Format(Resources.AskKeyTrust, uri.ToStringRfc(), fingerprint, keyInformation ?? Resources.NoKeyInfoServerData, domain),
                       defaultAnswer: false, alternateMessage: Resources.UntrustedKeys));
        }
        //--------------------//

        #region Get feed
        /// <inheritdoc/>
        public Feed GetFeed(FeedUri feedUri)
        {
            #region Sanity checks
            if (feedUri == null)
            {
                throw new ArgumentNullException("feedUri");
            }
            #endregion

            if (feedUri.IsFile)
            {
                return(LoadLocal(feedUri));
            }
            else
            {
                try
                {
                    if (Refresh)
                    {
                        Download(feedUri);
                    }
                    else if (!_feedCache.Contains(feedUri))
                    {
                        // Do not download in offline mode
                        if (_config.NetworkUse == NetworkLevel.Offline)
                        {
                            throw new IOException(string.Format(Resources.FeedNotCachedOffline, feedUri));
                        }

                        // Try to download missing feed
                        Download(feedUri);
                    }

                    return(LoadCached(feedUri));
                }
                #region Error handling
                catch (KeyNotFoundException ex)
                {
                    // Wrap exception since only certain exception types are allowed
                    throw new IOException(ex.Message, ex);
                }
                #endregion
            }
        }
Exemple #3
0
        /// <summary>
        /// Returns a specific <see cref="Feed"/>. Automatically handles downloading and caching. Updates the <see cref="Stale"/> indicator.
        /// </summary>
        /// <param name="feedUri">The canonical ID used to identify the feed.</param>
        /// <returns>The parsed <see cref="Feed"/> object.</returns>
        /// <remarks><see cref="Feed"/>s are always served from the <see cref="IFeedCache"/> if possible, unless <see cref="Refresh"/> is set to <c>true</c>.</remarks>
        /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
        /// <exception cref="WebException">A problem occured while fetching the feed file.</exception>
        /// <exception cref="IOException">A problem occured while reading the feed file.</exception>
        /// <exception cref="UnauthorizedAccessException">Access to the cache is not permitted.</exception>
        /// <exception cref="SignatureException">The signature data of a remote feed file could not be verified.</exception>
        /// <exception cref="InvalidDataException"><see cref="Feed.Uri"/> is missing or does not match <paramref name="feedUri"/>.</exception>
        private Feed GetFeed([NotNull] FeedUri feedUri)
        {
            if (feedUri.IsFromDistribution)
            {
                throw new ArgumentException($"{feedUri.ToStringRfc()} is a virtual feed URI and therefore cannot be downloaded.");
            }
            if (feedUri.IsFile)
            {
                return(XmlStorage.LoadXml <Feed>(feedUri.LocalPath));
            }
            else
            {
                try
                {
                    if (Refresh)
                    {
                        Download(feedUri);
                    }
                    else if (!_feedCache.Contains(feedUri))
                    {
                        // Do not download in offline mode
                        if (_config.NetworkUse == NetworkLevel.Offline)
                        {
                            throw new IOException(string.Format(Resources.FeedNotCachedOffline, feedUri));
                        }

                        // Try to download missing feed
                        Download(feedUri);
                    }

                    return(LoadCached(feedUri));
                }
                #region Error handling
                catch (KeyNotFoundException ex)
                {
                    // Wrap exception since only certain exception types are allowed
                    throw new IOException(ex.Message, ex);
                }
                #endregion
            }
        }
Exemple #4
0
        //--------------------//

        #region Contains
        /// <inheritdoc/>
        public bool Contains(FeedUri feedUri)
        {
            return(_backingCache.Contains(feedUri));
        }