1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# frozen_string_literal: true
require_relative "helper"
require "rubygems"
require "rubygems/command"
require "rubygems/gemcutter_utilities"
require "rubygems/config_file"
class TestGemGemcutterUtilities < Gem::TestCase
def setup
super
credential_setup
# below needed for random testing, class property
Gem.configuration.disable_default_gem_server = nil
ENV["RUBYGEMS_HOST"] = nil
ENV["GEM_HOST_OTP_CODE"] = nil
Gem.configuration.rubygems_api_key = nil
@cmd = Gem::Command.new "", "summary"
@cmd.extend Gem::GemcutterUtilities
end
def teardown
ENV["RUBYGEMS_HOST"] = nil
ENV["GEM_HOST_OTP_CODE"] = nil
Gem.configuration.rubygems_api_key = nil
credential_teardown
super
end
def test_alternate_key_alternate_host
keys = {
:rubygems_api_key => "KEY",
"http://rubygems.engineyard.com" => "EYKEY",
}
File.open Gem.configuration.credentials_path, "w" do |f|
f.write Gem::ConfigFile.dump_with_rubygems_yaml(keys)
end
ENV["RUBYGEMS_HOST"] = "http://rubygems.engineyard.com"
Gem.configuration.load_api_keys
assert_equal "EYKEY", @cmd.api_key
end
def test_api_key
keys = { :rubygems_api_key => "KEY" }
File.open Gem.configuration.credentials_path, "w" do |f|
f.write Gem::ConfigFile.dump_with_rubygems_yaml(keys)
end
Gem.configuration.load_api_keys
assert_equal "KEY", @cmd.api_key
end
def test_api_key_override
keys = { :rubygems_api_key => "KEY", :other => "OTHER" }
File.open Gem.configuration.credentials_path, "w" do |f|
f.write Gem::ConfigFile.dump_with_rubygems_yaml(keys)
end
Gem.configuration.load_api_keys
@cmd.add_key_option
@cmd.handle_options %w[--key other]
assert_equal "OTHER", @cmd.api_key
end
def test_host
assert_equal "https://rubygems.org", @cmd.host
end
def test_host_RUBYGEMS_HOST
ENV["RUBYGEMS_HOST"] = "https://other.example"
assert_equal "https://other.example", @cmd.host
end
def test_host_RUBYGEMS_HOST_empty
ENV["RUBYGEMS_HOST"] = ""
assert_equal "https://rubygems.org", @cmd.host
end
def test_sign_in
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output)
assert @fetcher.last_request["authorization"]
assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_sign_in_with_host
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), "http://example.com", ["http://example.com"]
assert_match "Enter your http://example.com credentials.",
@sign_in_ui.output
assert @fetcher.last_request["authorization"]
assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials["http://example.com"]
end
def test_sign_in_with_host_nil
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), nil, [nil]
assert_match "Enter your RubyGems.org credentials.",
@sign_in_ui.output
assert @fetcher.last_request["authorization"]
assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
end
def test_sign_in_with_host_ENV
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), "http://example.com"
assert_match "Enter your http://example.com credentials.",
@sign_in_ui.output
assert @fetcher.last_request["authorization"]
assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials["http://example.com"]
end
def test_sign_in_skips_with_existing_credentials
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
Gem.configuration.rubygems_api_key = api_key
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_equal "", @sign_in_ui.output
end
def test_sign_in_skips_with_key_override
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
Gem.configuration.api_keys[:KEY] = "other"
@cmd.options[:key] = :KEY
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_equal "", @sign_in_ui.output
end
def test_sign_in_with_other_credentials_doesnt_overwrite_other_keys
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
other_api_key = "f46dbb18bb6a9c97cdc61b5b85c186a17403cdcbf"
config = Hash[:other_api_key, other_api_key]
File.open Gem.configuration.credentials_path, "w" do |f|
f.write Gem::ConfigFile.dump_with_rubygems_yaml(config)
end
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output)
assert_match(/Signed in./, @sign_in_ui.output)
credentials = load_yaml_file Gem.configuration.credentials_path
assert_equal api_key, credentials[:rubygems_api_key]
assert_equal other_api_key, credentials[:other_api_key]
end
def test_sign_in_with_bad_credentials
assert_raise Gem::MockGemUi::TermError do
util_sign_in HTTPResponseFactory.create(body: "Access Denied.", code: 403, msg: "Forbidden")
end
assert_match(/Enter your RubyGems.org credentials./, @sign_in_ui.output)
assert_match(/Access Denied./, @sign_in_ui.output)
end
def test_signin_with_env_otp_code
ENV["GEM_HOST_OTP_CODE"] = "111111"
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_match "Signed in with API key:", @sign_in_ui.output
assert_equal "111111", @fetcher.last_request["OTP"]
end
def test_sign_in_with_correct_otp_code
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
response_fail = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
util_sign_in(proc do
@call_count ||= 0
if (@call_count += 1).odd?
HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized")
else
HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
end
end, nil, [], "111111\n")
assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output
assert_match "Code: ", @sign_in_ui.output
assert_match "Signed in with API key:", @sign_in_ui.output
assert_equal "111111", @fetcher.last_request["OTP"]
end
def test_sign_in_with_incorrect_otp_code
response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
assert_raise Gem::MockGemUi::TermError do
util_sign_in HTTPResponseFactory.create(body: response, code: 401, msg: "Unauthorized"), nil, [], "111111\n"
end
assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output
assert_match "Code: ", @sign_in_ui.output
assert_match response, @sign_in_ui.output
assert_equal "111111", @fetcher.last_request["OTP"]
end
def test_sign_in_with_webauthn_enabled
webauthn_verification_url = "rubygems.org/api/v1/webauthn_verification/odow34b93t6aPCdY"
response_fail = "You have enabled multifactor authentication"
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
port = 5678
server = TCPServer.new(port)
TCPServer.stub(:new, server) do
Gem::WebauthnListener.stub(:wait_for_otp_code, "Uvh6T57tkWuUnWYo") do
util_sign_in(proc do
@call_count ||= 0
if (@call_count += 1).odd?
HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized")
else
HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
end
end, nil, [], "", webauthn_verification_url)
end
ensure
server.close
end
url_with_port = "#{webauthn_verification_url}?port=#{port}"
assert_match "You have enabled multi-factor authentication. Please visit #{url_with_port} to authenticate via security device. If you can't verify using WebAuthn but have OTP enabled, you can re-run the gem signin command with the `--otp [your_code]` option.", @sign_in_ui.output
assert_match "You are verified with a security device. You may close the browser window.", @sign_in_ui.output
assert_equal "Uvh6T57tkWuUnWYo", @fetcher.last_request["OTP"]
end
def test_sign_in_with_webauthn_enabled_with_error
webauthn_verification_url = "rubygems.org/api/v1/webauthn_verification/odow34b93t6aPCdY"
response_fail = "You have enabled multifactor authentication"
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
port = 5678
server = TCPServer.new(port)
raise_error = ->(*_args) { raise Gem::WebauthnVerificationError, "Something went wrong" }
error = assert_raise Gem::MockGemUi::TermError do
TCPServer.stub(:new, server) do
Gem::WebauthnListener.stub(:wait_for_otp_code, raise_error) do
util_sign_in(proc do
@call_count ||= 0
if (@call_count += 1).odd?
HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized")
else
HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
end
end, nil, [], "", webauthn_verification_url)
end
ensure
server.close
end
end
assert_equal 1, error.exit_code
url_with_port = "#{webauthn_verification_url}?port=#{port}"
assert_match "You have enabled multi-factor authentication. Please visit #{url_with_port} to authenticate via security device. If you can't verify using WebAuthn but have OTP enabled, you can re-run the gem signin command with the `--otp [your_code]` option.", @sign_in_ui.output
assert_match "ERROR: Security device verification failed: Something went wrong", @sign_in_ui.error
refute_match "You are verified with a security device. You may close the browser window.", @sign_in_ui.output
refute_match "Signed in with API key:", @sign_in_ui.output
end
def util_sign_in(response, host = nil, args = [], extra_input = "", webauthn_url = nil)
email = "you@example.com"
password = "secret"
profile_response = HTTPResponseFactory.create(body: "mfa: disabled\n", code: 200, msg: "OK")
webauthn_response =
if webauthn_url
HTTPResponseFactory.create(body: webauthn_url, code: 200, msg: "OK")
else
HTTPResponseFactory.create(body: "You don't have any security devices", code: 422, msg: "Unprocessable Entity")
end
if host
ENV["RUBYGEMS_HOST"] = host
else
host = Gem.host
end
@fetcher = Gem::FakeFetcher.new
@fetcher.data["#{host}/api/v1/api_key"] = response
@fetcher.data["#{host}/api/v1/profile/me.yaml"] = profile_response
@fetcher.data["#{host}/api/v1/webauthn_verification"] = webauthn_response
Gem::RemoteFetcher.fetcher = @fetcher
@sign_in_ui = Gem::MockGemUi.new("#{email}\n#{password}\n\n\n\n\n\n\n\n\n" + extra_input)
use_ui @sign_in_ui do
if args.length > 0
@cmd.sign_in(*args)
else
@cmd.sign_in
end
end
end
def test_verify_api_key
keys = { :other => "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" }
File.open Gem.configuration.credentials_path, "w" do |f|
f.write Gem::ConfigFile.dump_with_rubygems_yaml(keys)
end
Gem.configuration.load_api_keys
assert_equal "a5fdbb6ba150cbb83aad2bb2fede64cf040453903",
@cmd.verify_api_key(:other)
end
def test_verify_missing_api_key
assert_raise Gem::MockGemUi::TermError do
@cmd.verify_api_key :missing
end
end
end
|