Skip to content

Commit 138c978

Browse files
authored
2020-03 patch (#145)
* handle invalid option exceptions * deprecate xauth * fix #142 * supress undefined method error #149
1 parent 3c78a05 commit 138c978

File tree

5 files changed

+21
-30
lines changed

5 files changed

+21
-30
lines changed

lib/twurl/cli.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def parse_options(args)
6767

6868
o.section "Authorization options:" do
6969
username
70-
password
7170
consumer_key
7271
consumer_secret
7372
access_token
@@ -96,7 +95,15 @@ def parse_options(args)
9695
end
9796
end
9897

99-
arguments = option_parser.parse!(args)
98+
begin
99+
arguments = option_parser.parse!(args)
100+
rescue OptionParser::InvalidOption
101+
CLI.puts "ERROR: undefined option"
102+
exit
103+
rescue
104+
CLI.puts "ERROR: invalid argument"
105+
exit
106+
end
100107
Twurl.options.command = extract_command!(arguments)
101108
Twurl.options.path = extract_path!(arguments)
102109
Twurl.options.subcommands = arguments
@@ -219,12 +226,6 @@ def username
219226
end
220227
end
221228

222-
def password
223-
on('-p', '--password [password]', 'Password of account to authorize (required)') do |password|
224-
options.password = password ? password : CLI.prompt_for('Password')
225-
end
226-
end
227-
228229
def trace
229230
on('-t', '--[no-]trace', 'Trace request/response traffic (default: --no-trace)') do |trace|
230231
options.trace = trace

lib/twurl/oauth_client.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ def rcfile(reload = false)
99
end
1010

1111
def load_from_options(options)
12-
if rcfile.has_oauth_profile_for_username_with_consumer_key?(options.username, options.consumer_key)
12+
if options.command == 'request' && has_oauth_options?(options)
13+
load_new_client_from_oauth_options(options)
14+
elsif options.command == 'request' && options.app_only && options.consumer_key
15+
load_client_for_non_profile_app_only_auth(options)
16+
elsif rcfile.has_oauth_profile_for_username_with_consumer_key?(options.username, options.consumer_key)
1317
load_client_for_username_and_consumer_key(options.username, options.consumer_key)
1418
elsif options.username
1519
load_client_for_username(options.username)
1620
elsif options.command == 'authorize' && options.app_only
1721
load_client_for_app_only_auth(options, options.consumer_key)
1822
elsif options.command == 'authorize'
1923
load_new_client_from_options(options)
20-
elsif options.command == 'request' && has_oauth_options?(options)
21-
load_new_client_from_oauth_options(options)
22-
elsif options.command == 'request' && options.app_only && options.consumer_key
23-
load_client_for_non_profile_app_only_auth(options)
2424
else
2525
load_default_client(options)
2626
end
@@ -52,7 +52,7 @@ def load_client_for_username(username)
5252
end
5353

5454
def load_new_client_from_options(options)
55-
new(options.oauth_client_options.merge('password' => options.password))
55+
new(options.oauth_client_options)
5656
end
5757

5858
def load_new_client_from_oauth_options(options)
@@ -106,10 +106,9 @@ def load_default_client(options)
106106

107107
OAUTH_CLIENT_OPTIONS = %w[username consumer_key consumer_secret token secret]
108108
attr_reader *OAUTH_CLIENT_OPTIONS
109-
attr_reader :username, :password
109+
attr_reader :username
110110
def initialize(options = {})
111111
@username = options['username']
112-
@password = options['password']
113112
@consumer_key = options['consumer_key']
114113
@consumer_secret = options['consumer_secret']
115114
@token = options['token']
@@ -192,18 +191,14 @@ def user_agent
192191

193192
def exchange_credentials_for_access_token
194193
response = begin
195-
consumer.token_request(:post, consumer.access_token_path, nil, {}, client_auth_parameters)
194+
consumer.token_request(:post, consumer.access_token_path, nil, {})
196195
rescue OAuth::Unauthorized
197196
perform_pin_authorize_workflow
198197
end
199198
@token = response[:oauth_token]
200199
@secret = response[:oauth_token_secret]
201200
end
202201

203-
def client_auth_parameters
204-
{'x_auth_username' => username, 'x_auth_password' => password, 'x_auth_mode' => 'client_auth'}
205-
end
206-
207202
def perform_pin_authorize_workflow
208203
@request_token = consumer.get_request_token
209204
CLI.puts("Go to #{generate_authorize_url} and paste in the supplied PIN")

lib/twurl/rcfile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def alias(name, path)
7272
end
7373

7474
def aliases
75-
data['aliases']
75+
data['aliases'] ||= {}
7676
end
7777

7878
def bearer_token(consumer_key, bearer_token)

test/oauth_client_test.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ def setup
166166
@new_client = Twurl::OAuthClient.load_new_client_from_options(options)
167167
end
168168

169-
def test_password_is_included
170-
assert_equal options.password, new_client.password
171-
end
172-
173169
def test_oauth_options_are_passed_through
174170
assert_equal client.to_hash, new_client.to_hash
175171
end
@@ -248,15 +244,15 @@ def test_successful_exchange_parses_token_and_secret_from_response_body
248244
parsed_response = {:oauth_token => "123456789",
249245
:oauth_token_secret => "abcdefghi",
250246
:user_id => "3191321",
251-
:screen_name => "noradio",
252-
:x_auth_expires => "0"}
247+
:screen_name => "noradio"
248+
}
253249

254250
mock(client.consumer).
255251
token_request(:post,
256252
client.consumer.access_token_path,
257253
nil,
258254
{},
259-
client.client_auth_parameters) { parsed_response }
255+
) { parsed_response }
260256

261257
assert client.needs_to_authorize?
262258
client.exchange_credentials_for_access_token

test/test_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class << self
1717
def test_exemplar
1818
options = new
1919
options.username = 'exemplar_user_name'
20-
options.password = 'secret'
2120
options.consumer_key = '123456789'
2221
options.consumer_secret = '987654321'
2322
options.subcommands = []

0 commit comments

Comments
 (0)