summaryrefslogtreecommitdiff
path: root/lib/rubygems/gemcutter_utilities.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-01 11:01:00 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-01 11:01:00 +0000
commit7a46a3b94121339cbead211c4497142bee82fddb (patch)
treebc6e9f11a1b60f8c4258e4780b18f952b0a8ec35 /lib/rubygems/gemcutter_utilities.rb
parent5cae104e51be9cbf524b7d953b33d0909c46d006 (diff)
Merge rubygems-3.0.0.beta3.
* [GSoC] Multi-factor feature for RubyGems https://github.com/rubygems/rubygems/pull/2369 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66118 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rubygems/gemcutter_utilities.rb')
-rw-r--r--lib/rubygems/gemcutter_utilities.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb
index 6fe8455498..3607b61529 100644
--- a/lib/rubygems/gemcutter_utilities.rb
+++ b/lib/rubygems/gemcutter_utilities.rb
@@ -25,6 +25,16 @@ module Gem::GemcutterUtilities
end
##
+ # Add the --otp option
+
+ def add_otp_option
+ add_option('--otp CODE',
+ 'Digit code for multifactor authentication') do |value, options|
+ options[:otp] = value
+ end
+ end
+
+ ##
# The API key from the command options or from the user's configuration.
def api_key
@@ -113,6 +123,13 @@ module Gem::GemcutterUtilities
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
+ end
+
with_response response do |resp|
say "Signed in."
set_api_key host, resp.body
@@ -156,6 +173,20 @@ module Gem::GemcutterUtilities
end
end
+ ##
+ # Returns true when the user has enabled multifactor authentication from
+ # +response+ text.
+
+ 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
Gem.configuration.rubygems_api_key = key