diff options
author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2020-12-18 12:13:33 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2020-12-18 13:33:18 +0900 |
commit | 0e40cc9b194a5e46024d32b85a61e651372a65cb (patch) | |
tree | d91f781ca34d78897d8cea9fb3403e47f891df5a /lib/rubygems/gemcutter_utilities.rb | |
parent | 34f06062174882a98ebef998c50ad8d4f7fc0f2e (diff) |
Merge RubyGems 3.2.2 and Bundler 2.2.2
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3930
Diffstat (limited to 'lib/rubygems/gemcutter_utilities.rb')
-rw-r--r-- | lib/rubygems/gemcutter_utilities.rb | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb index d021f47e24..8751a79d63 100644 --- a/lib/rubygems/gemcutter_utilities.rb +++ b/lib/rubygems/gemcutter_utilities.rb @@ -94,20 +94,16 @@ module Gem::GemcutterUtilities end 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) + response = request_with_otp(method, uri, &block) if mfa_unauthorized?(response) - response = Gem::RemoteFetcher.fetcher.request(uri, request_method) do |req| - req.add_field "OTP", get_otp - block.call(req) - end + ask_otp + response = request_with_otp(method, uri, &block) end if api_key_forbidden?(response) update_scope(scope) - Gem::RemoteFetcher.fetcher.request(uri, request_method, &block) + request_with_otp(method, uri, &block) else response end @@ -117,11 +113,6 @@ module Gem::GemcutterUtilities 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 - def update_scope(scope) sign_in_host = self.host pretty_host = pretty_host(sign_in_host) @@ -135,7 +126,7 @@ module Gem::GemcutterUtilities response = rubygems_api_request(:put, "api/v1/api_key", sign_in_host, scope: scope) do |request| request.basic_auth email, password - request.add_field "OTP", options[:otp] if options[:otp] + request["OTP"] = options[:otp] if options[:otp] request.body = URI.encode_www_form({:api_key => api_key }.merge(update_scope_params)) end @@ -168,7 +159,7 @@ module Gem::GemcutterUtilities response = rubygems_api_request(:post, "api/v1/api_key", sign_in_host, scope: scope) do |request| request.basic_auth email, password - request.add_field "OTP", options[:otp] if options[:otp] + request["OTP"] = options[:otp] if options[:otp] request.body = URI.encode_www_form({ name: key_name }.merge(scope_params)) end @@ -229,6 +220,20 @@ module Gem::GemcutterUtilities private + def request_with_otp(method, uri, &block) + request_method = Net::HTTP.const_get method.to_s.capitalize + + Gem::RemoteFetcher.fetcher.request(uri, request_method) do |req| + req["OTP"] = options[:otp] if options[:otp] + block.call(req) + end + end + + def ask_otp + say 'You have enabled multi-factor authentication. Please enter OTP code.' + options[:otp] = ask 'Code: ' + end + def pretty_host(host) if Gem::DEFAULT_HOST == host 'RubyGems.org' |