Skip to content

Commit 4dc77b7

Browse files
Display mfa warnings on gem signin
Co-authored-by: Jenny Shen <jenny.shen@shopify.com>
1 parent 42dc010 commit 4dc77b7

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

lib/rubygems/gemcutter_utilities.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,12 @@ def sign_in(sign_in_host = nil, scope: nil)
163163

164164
key_name = get_key_name(scope)
165165
scope_params = get_scope_params(scope)
166-
mfa_params = get_mfa_params(email, password)
166+
profile = get_user_profile(email, password)
167+
mfa_params = get_mfa_params(profile)
167168
all_params = scope_params.merge(mfa_params)
169+
warning = profile["warning"]
170+
171+
say "#{warning}\n" if warning
168172

169173
response = rubygems_api_request(:post, "api/v1/api_key",
170174
sign_in_host, scope: scope) do |request|
@@ -273,29 +277,28 @@ def default_host?
273277
self.host == Gem::DEFAULT_HOST
274278
end
275279

276-
def get_mfa_params(email, password)
280+
def get_user_profile(email, password)
277281
return {} unless default_host?
278282

279-
mfa_level = get_user_mfa_level(email, password)
280-
params = {}
281-
if mfa_level == "ui_only" || mfa_level == "ui_and_gem_signin"
282-
selected = ask_yes_no("Would you like to enable MFA for this key? (strongly recommended)")
283-
params["mfa"] = true if selected
284-
end
285-
params
286-
end
287-
288-
def get_user_mfa_level(email, password)
289283
response = rubygems_api_request(:get, "api/v1/profile/me.yaml") do |request|
290284
request.basic_auth email, password
291285
end
292286

293287
with_response response do |resp|
294-
body = Gem::SafeYAML.load clean_text(resp.body)
295-
body["mfa"]
288+
Gem::SafeYAML.load clean_text(resp.body)
296289
end
297290
end
298291

292+
def get_mfa_params(profile)
293+
mfa_level = profile["mfa"]
294+
params = {}
295+
if mfa_level == "ui_only" || mfa_level == "ui_and_gem_signin"
296+
selected = ask_yes_no("Would you like to enable MFA for this key? (strongly recommended)")
297+
params["mfa"] = true if selected
298+
end
299+
params
300+
end
301+
299302
def get_key_name(scope)
300303
hostname = Socket.gethostname || "unknown-host"
301304
user = ENV["USER"] || ENV["USERNAME"] || "unknown-user"

test/rubygems/test_gem_commands_signin_command.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ def test_execute_with_key_name_scope_and_mfa_level_of_gem_signin
159159
assert_equal api_key, credentials[:rubygems_api_key]
160160
end
161161

162+
def test_execute_with_warnings
163+
email = "you@example.com"
164+
password = "secret"
165+
api_key = "1234"
166+
fetcher = Gem::RemoteFetcher.fetcher
167+
mfa_level = "disabled"
168+
warning = "/[WARNING/] For protection of your account and gems"
169+
170+
key_name_ui = Gem::MockGemUi.new "#{email}\n#{password}\ntest-key\n\ny\n\n\n\n\n\ny"
171+
util_capture(key_name_ui, nil, api_key, fetcher, mfa_level, warning) { @cmd.execute }
172+
173+
assert_match warning, key_name_ui.output
174+
end
175+
162176
def test_execute_on_gemserver_without_profile_me_endpoint
163177
host = "http://some-gemcutter-compatible-host.org"
164178

@@ -193,10 +207,10 @@ def test_execute_on_gemserver_without_profile_me_endpoint
193207

194208
# Utility method to capture IO/UI within the block passed
195209

196-
def util_capture(ui_stub = nil, host = nil, api_key = nil, fetcher = Gem::FakeFetcher.new, mfa_level = "disabled")
210+
def util_capture(ui_stub = nil, host = nil, api_key = nil, fetcher = Gem::FakeFetcher.new, mfa_level = "disabled", warning = nil)
197211
api_key ||= "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
198212
response = [api_key, 200, "OK"]
199-
profile_response = [ "mfa: #{mfa_level}\n" , 200, "OK"]
213+
profile_response = [ "mfa: #{mfa_level}\nwarning: #{warning}" , 200, "OK"]
200214
email = "you@example.com"
201215
password = "secret"
202216

0 commit comments

Comments
 (0)