aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rubygems/gemcutter_utilities.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rubygems/gemcutter_utilities.rb')
-rw-r--r--lib/rubygems/gemcutter_utilities.rb43
1 files changed, 24 insertions, 19 deletions
diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb
index 877205b619..a125664ce5 100644
--- a/lib/rubygems/gemcutter_utilities.rb
+++ b/lib/rubygems/gemcutter_utilities.rb
@@ -1,11 +1,14 @@
# frozen_string_literal: true
require 'rubygems/remote_fetcher'
+require 'rubygems/text'
##
# Utility methods for using the RubyGems API.
module Gem::GemcutterUtilities
+ include Gem::Text
+
# TODO: move to Gem::Command
OptionParser.accept Symbol do |value|
value.to_sym
@@ -94,8 +97,22 @@ module Gem::GemcutterUtilities
uri = URI.parse "#{self.host}/#{path}"
request_method = Net::HTTP.const_get method.to_s.capitalize
+ response = Gem::RemoteFetcher.fetcher.request(uri, request_method, &block)
+ return response unless mfa_unauthorized?(response)
+
+ Gem::RemoteFetcher.fetcher.request(uri, request_method) do |req|
+ req.add_field "OTP", get_otp
+ block.call(req)
+ end
+ end
- Gem::RemoteFetcher.fetcher.request(uri, request_method, &block)
+ def mfa_unauthorized?(response)
+ response.kind_of?(Net::HTTPUnauthorized) && response.body.start_with?('You have enabled multifactor authentication')
+ end
+
+ def get_otp
+ say 'You have enabled multi-factor authentication. Please enter OTP code.'
+ ask 'Code: '
end
##
@@ -123,13 +140,7 @@ module Gem::GemcutterUtilities
response = rubygems_api_request(:get, "api/v1/api_key",
sign_in_host) do |request|
request.basic_auth email, password
- end
-
- if need_otp? response
- response = rubygems_api_request(:get, "api/v1/api_key", sign_in_host) do |request|
- request.basic_auth email, password
- request.add_field "OTP", options[:otp]
- end
+ request.add_field "OTP", options[:otp] if options[:otp]
end
with_response response do |resp|
@@ -164,30 +175,24 @@ module Gem::GemcutterUtilities
if block_given?
yield response
else
- say response.body
+ say clean_text(response.body)
end
else
message = response.body
message = "#{error_prefix}: #{message}" if error_prefix
- say message
+ say clean_text(message)
terminate_interaction 1 # TODO: question this
end
end
##
# Returns true when the user has enabled multifactor authentication from
- # +response+ text.
+ # +response+ text and no otp provided by options.
+
+
- def need_otp?(response)
- return unless response.kind_of?(Net::HTTPUnauthorized) &&
- response.body.start_with?('You have enabled multifactor authentication')
- return true if options[:otp]
- say 'You have enabled multi-factor authentication. Please enter OTP code.'
- options[:otp] = ask 'Code: '
- true
- end
def set_api_key(host, key)
if host == Gem::DEFAULT_HOST