Skip to content

Commit

Permalink
2020-03 patch (#145)
Browse files Browse the repository at this point in the history
* handle invalid option exceptions

* deprecate xauth

* fix #142

* supress undefined method error #149
  • Loading branch information
smaeda-ks authored Aug 27, 2020
1 parent 3c78a05 commit 138c978
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 30 deletions.
17 changes: 9 additions & 8 deletions lib/twurl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def parse_options(args)

o.section "Authorization options:" do
username
password
consumer_key
consumer_secret
access_token
Expand Down Expand Up @@ -96,7 +95,15 @@ def parse_options(args)
end
end

arguments = option_parser.parse!(args)
begin
arguments = option_parser.parse!(args)
rescue OptionParser::InvalidOption
CLI.puts "ERROR: undefined option"
exit
rescue
CLI.puts "ERROR: invalid argument"
exit
end
Twurl.options.command = extract_command!(arguments)
Twurl.options.path = extract_path!(arguments)
Twurl.options.subcommands = arguments
Expand Down Expand Up @@ -219,12 +226,6 @@ def username
end
end

def password
on('-p', '--password [password]', 'Password of account to authorize (required)') do |password|
options.password = password ? password : CLI.prompt_for('Password')
end
end

def trace
on('-t', '--[no-]trace', 'Trace request/response traffic (default: --no-trace)') do |trace|
options.trace = trace
Expand Down
21 changes: 8 additions & 13 deletions lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ def rcfile(reload = false)
end

def load_from_options(options)
if rcfile.has_oauth_profile_for_username_with_consumer_key?(options.username, options.consumer_key)
if options.command == 'request' && has_oauth_options?(options)
load_new_client_from_oauth_options(options)
elsif options.command == 'request' && options.app_only && options.consumer_key
load_client_for_non_profile_app_only_auth(options)
elsif rcfile.has_oauth_profile_for_username_with_consumer_key?(options.username, options.consumer_key)
load_client_for_username_and_consumer_key(options.username, options.consumer_key)
elsif options.username
load_client_for_username(options.username)
elsif options.command == 'authorize' && options.app_only
load_client_for_app_only_auth(options, options.consumer_key)
elsif options.command == 'authorize'
load_new_client_from_options(options)
elsif options.command == 'request' && has_oauth_options?(options)
load_new_client_from_oauth_options(options)
elsif options.command == 'request' && options.app_only && options.consumer_key
load_client_for_non_profile_app_only_auth(options)
else
load_default_client(options)
end
Expand Down Expand Up @@ -52,7 +52,7 @@ def load_client_for_username(username)
end

def load_new_client_from_options(options)
new(options.oauth_client_options.merge('password' => options.password))
new(options.oauth_client_options)
end

def load_new_client_from_oauth_options(options)
Expand Down Expand Up @@ -106,10 +106,9 @@ def load_default_client(options)

OAUTH_CLIENT_OPTIONS = %w[username consumer_key consumer_secret token secret]
attr_reader *OAUTH_CLIENT_OPTIONS
attr_reader :username, :password
attr_reader :username
def initialize(options = {})
@username = options['username']
@password = options['password']
@consumer_key = options['consumer_key']
@consumer_secret = options['consumer_secret']
@token = options['token']
Expand Down Expand Up @@ -192,18 +191,14 @@ def user_agent

def exchange_credentials_for_access_token
response = begin
consumer.token_request(:post, consumer.access_token_path, nil, {}, client_auth_parameters)
consumer.token_request(:post, consumer.access_token_path, nil, {})
rescue OAuth::Unauthorized
perform_pin_authorize_workflow
end
@token = response[:oauth_token]
@secret = response[:oauth_token_secret]
end

def client_auth_parameters
{'x_auth_username' => username, 'x_auth_password' => password, 'x_auth_mode' => 'client_auth'}
end

def perform_pin_authorize_workflow
@request_token = consumer.get_request_token
CLI.puts("Go to #{generate_authorize_url} and paste in the supplied PIN")
Expand Down
2 changes: 1 addition & 1 deletion lib/twurl/rcfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def alias(name, path)
end

def aliases
data['aliases']
data['aliases'] ||= {}
end

def bearer_token(consumer_key, bearer_token)
Expand Down
10 changes: 3 additions & 7 deletions test/oauth_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ def setup
@new_client = Twurl::OAuthClient.load_new_client_from_options(options)
end

def test_password_is_included
assert_equal options.password, new_client.password
end

def test_oauth_options_are_passed_through
assert_equal client.to_hash, new_client.to_hash
end
Expand Down Expand Up @@ -248,15 +244,15 @@ def test_successful_exchange_parses_token_and_secret_from_response_body
parsed_response = {:oauth_token => "123456789",
:oauth_token_secret => "abcdefghi",
:user_id => "3191321",
:screen_name => "noradio",
:x_auth_expires => "0"}
:screen_name => "noradio"
}

mock(client.consumer).
token_request(:post,
client.consumer.access_token_path,
nil,
{},
client.client_auth_parameters) { parsed_response }
) { parsed_response }

assert client.needs_to_authorize?
client.exchange_credentials_for_access_token
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class << self
def test_exemplar
options = new
options.username = 'exemplar_user_name'
options.password = 'secret'
options.consumer_key = '123456789'
options.consumer_secret = '987654321'
options.subcommands = []
Expand Down

0 comments on commit 138c978

Please sign in to comment.