コード例 #1
0
        /// <summary>
        /// Saves a catalog.
        /// </summary>
        /// <param name="catalog">The catalog to save.</param>
        /// <exception cref="IOException">A file could not be read or written or the GnuPG could not be launched or the catalog file could not be written.</exception>
        /// <exception cref="UnauthorizedAccessException">Read or write access to a catalog file is not permitted.</exception>
        /// <exception cref="KeyNotFoundException">An OpenPGP key could not be found.</exception>
        private void SaveCatalog(Catalog catalog)
        {
            if (_xmlSign)
            {
                var openPgp       = OpenPgpFactory.CreateDefault();
                var signedCatalog = new SignedCatalog(catalog, openPgp.GetSecretKey(_key));

                while (true)
                {
                    try
                    {
                        signedCatalog.Save(_catalogFile, _openPgpPassphrase);
                        break; // Exit loop if passphrase is correct
                    }
                    catch (WrongPassphraseException ex)
                    {
                        // Continue loop if passhrase is incorrect
                        if (!string.IsNullOrEmpty(_openPgpPassphrase))
                        {
                            Log.Error(ex);
                        }
                    }

                    // Ask for passphrase to unlock secret key if we were unable to save without it
                    _openPgpPassphrase = CliUtils.ReadPassword(string.Format(Resources.AskForPassphrase, signedCatalog.SecretKey));
                }
            }
            else
            {
                catalog.SaveXml(_catalogFile);
            }
        }
コード例 #2
0
        /// <summary>
        /// Saves a feed.
        /// </summary>
        /// <exception cref="IOException">A file could not be read or written or the GnuPG could not be launched or the feed file could not be read or written.</exception>
        /// <exception cref="UnauthorizedAccessException">Read or write access to a feed file is not permitted.</exception>
        /// <exception cref="KeyNotFoundException">An OpenPGP key could not be found.</exception>
        private void SaveFeed(FeedEditing feedEditing)
        {
            if (_unsign)
            {
                // Remove any existing signatures
                feedEditing.SignedFeed.SecretKey = null;
            }
            else
            {
                var openPgp = OpenPgpFactory.CreateDefault();
                if (_xmlSign)
                {     // Signing explicitly requested
                    if (feedEditing.SignedFeed.SecretKey == null)
                    { // No previous signature
                        // Use user-specified key or default key
                        feedEditing.SignedFeed.SecretKey = openPgp.GetSecretKey(_key);
                    }
                    else
                    {                                    // Existing siganture
                        if (!string.IsNullOrEmpty(_key)) // Use new user-specified key
                        {
                            feedEditing.SignedFeed.SecretKey = openPgp.GetSecretKey(_key);
                        }
                        //else resign implied
                    }
                }
                //else resign implied
            }

            // If no signing or unsigning was explicitly requested and the content did not change
            // there is no need to overwrite (and potentiall resign) the file
            if (!_xmlSign && !_unsign && !feedEditing.Changed)
            {
                return;
            }

            while (true)
            {
                try
                {
                    Debug.Assert(feedEditing.Path != null);
                    feedEditing.SignedFeed.Save(feedEditing.Path, _openPgpPassphrase);
                    break; // Exit loop if passphrase is correct
                }
                catch (WrongPassphraseException ex)
                {
                    // Continue loop if passhrase is incorrect
                    if (!string.IsNullOrEmpty(_openPgpPassphrase))
                    {
                        Log.Error(ex);
                    }
                }

                // Ask for passphrase to unlock secret key if we were unable to save without it
                _openPgpPassphrase = CliUtils.ReadPassword(string.Format(Resources.AskForPassphrase, feedEditing.SignedFeed.SecretKey));
            }
        }
コード例 #3
0
        /// <inheritdoc/>
        public override NetworkCredential GetCredential(Uri uri, string authType)
        {
            #region Sanity checks
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }
            #endregion

            if (!Interactive)
            {
                return(null);
            }

            Log.Debug("Prompt for credentials on command-line: " + uri.ToStringRfc());
            if (WasReportedInvalid(uri))
            {
                Log.Error(string.Format(Resources.InvalidCredentials, uri.ToStringRfc()));
            }
            Console.Error.WriteLine(Resources.PleasEnterCredentials, uri.ToStringRfc());
            return(new NetworkCredential(
                       CliUtils.ReadString(Resources.UserName),
                       CliUtils.ReadPassword(Resources.Password)));
        }