From 138c978ba1ce83f5a0e6fb6751cdc3483d3f07a1 Mon Sep 17 00:00:00 2001 From: Shohei Maeda Date: Thu, 27 Aug 2020 22:59:14 +0900 Subject: [PATCH] 2020-03 patch (#145) * handle invalid option exceptions * deprecate xauth * fix #142 * supress undefined method error #149 --- lib/twurl/cli.rb | 17 +++++++++-------- lib/twurl/oauth_client.rb | 21 ++++++++------------- lib/twurl/rcfile.rb | 2 +- test/oauth_client_test.rb | 10 +++------- test/test_helper.rb | 1 - 5 files changed, 21 insertions(+), 30 deletions(-) diff --git a/lib/twurl/cli.rb b/lib/twurl/cli.rb index 304a883..60b01db 100644 --- a/lib/twurl/cli.rb +++ b/lib/twurl/cli.rb @@ -67,7 +67,6 @@ def parse_options(args) o.section "Authorization options:" do username - password consumer_key consumer_secret access_token @@ -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 @@ -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 diff --git a/lib/twurl/oauth_client.rb b/lib/twurl/oauth_client.rb index 02ebc59..482f90c 100644 --- a/lib/twurl/oauth_client.rb +++ b/lib/twurl/oauth_client.rb @@ -9,7 +9,11 @@ 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) @@ -17,10 +21,6 @@ def load_from_options(options) 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 @@ -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) @@ -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'] @@ -192,7 +191,7 @@ 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 @@ -200,10 +199,6 @@ def exchange_credentials_for_access_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") diff --git a/lib/twurl/rcfile.rb b/lib/twurl/rcfile.rb index 25baf98..d6173dd 100644 --- a/lib/twurl/rcfile.rb +++ b/lib/twurl/rcfile.rb @@ -72,7 +72,7 @@ def alias(name, path) end def aliases - data['aliases'] + data['aliases'] ||= {} end def bearer_token(consumer_key, bearer_token) diff --git a/test/oauth_client_test.rb b/test/oauth_client_test.rb index 4c739e6..6d86c05 100644 --- a/test/oauth_client_test.rb +++ b/test/oauth_client_test.rb @@ -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 @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 47c422c..782b26e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 = []