From 3436c8f28c086390dd09995b1483658bed5a656c Mon Sep 17 00:00:00 2001 From: Nicholas Souphandavong Date: Wed, 6 Jun 2018 16:57:14 -0400 Subject: [PATCH 1/2] Allow empty parameters. --- lib/oauth/client/helper.rb | 8 ++++++-- lib/oauth/version.rb | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/oauth/client/helper.rb b/lib/oauth/client/helper.rb index 6c8b19b2..4998d517 100644 --- a/lib/oauth/client/helper.rb +++ b/lib/oauth/client/helper.rb @@ -27,7 +27,7 @@ def timestamp end def oauth_parameters - { + out = { 'oauth_body_hash' => options[:body_hash], 'oauth_callback' => options[:oauth_callback], 'oauth_consumer_key' => options[:consumer].key, @@ -38,7 +38,11 @@ def oauth_parameters 'oauth_verifier' => options[:oauth_verifier], 'oauth_version' => (options[:oauth_version] || '1.0'), 'oauth_session_handle' => options[:oauth_session_handle] - }.reject { |k,v| v.to_s == "" } + } + if !options[:allow_empty_params] + out.reject! { |k,v| v.to_s == '' } + end + out end def signature(extra_options = {}) diff --git a/lib/oauth/version.rb b/lib/oauth/version.rb index 5fdd439d..3ce42460 100644 --- a/lib/oauth/version.rb +++ b/lib/oauth/version.rb @@ -1,3 +1,3 @@ module OAuth - VERSION = "0.5.4" + VERSION = "0.5.5" end From de65c6467fcbf682d18bcf293affa92370efe857 Mon Sep 17 00:00:00 2001 From: Nicholas Souphandavong <39563467+souphan-adsk@users.noreply.github.com> Date: Fri, 8 Jun 2018 09:57:43 -0400 Subject: [PATCH 2/2] Allow empty parameters (#1) --- lib/oauth/client/helper.rb | 6 +- test/units/test_client_helper.rb | 147 +++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 test/units/test_client_helper.rb diff --git a/lib/oauth/client/helper.rb b/lib/oauth/client/helper.rb index 4998d517..5fe2be24 100644 --- a/lib/oauth/client/helper.rb +++ b/lib/oauth/client/helper.rb @@ -39,9 +39,11 @@ def oauth_parameters 'oauth_version' => (options[:oauth_version] || '1.0'), 'oauth_session_handle' => options[:oauth_session_handle] } - if !options[:allow_empty_params] - out.reject! { |k,v| v.to_s == '' } + allowed_empty_params = options[:allow_empty_params] + if allowed_empty_params != true && !allowed_empty_params.kind_of?(Array) + allowed_empty_params = allowed_empty_params == false ? [] : [allowed_empty_params] end + out.select! { |k,v| v.to_s != '' || allowed_empty_params == true || allowed_empty_params.include?(k) } out end diff --git a/test/units/test_client_helper.rb b/test/units/test_client_helper.rb new file mode 100644 index 00000000..d6ed973a --- /dev/null +++ b/test/units/test_client_helper.rb @@ -0,0 +1,147 @@ +require File.expand_path('../../test_helper', __FILE__) + +require 'oauth/client' + +class ClientHelperTest < Minitest::Test + + def setup + @consumer=OAuth::Consumer.new( + 'consumer_key_86cad9', '5888bf0345e5d237', + { + :site=>"http://blabla.bla", + :proxy=>"http://user:password@proxy.bla:8080", + :request_token_path=>"/oauth/example/request_token.php", + :access_token_path=>"/oauth/example/access_token.php", + :authorize_path=>"/oauth/example/authorize.php", + :scheme=>:header, + :http_method=>:get + }) + end + + def test_oauth_parameters_allow_empty_params_default + helper = OAuth::Client::Helper.new(nil, { + :consumer => @consumer + }) + helper.stub :timestamp, '0' do + helper.stub :nonce, 'nonce' do + expected = { + "oauth_consumer_key"=>"consumer_key_86cad9", + "oauth_signature_method"=>"HMAC-SHA1", + "oauth_timestamp"=>"0", + "oauth_nonce"=>"nonce", + "oauth_version"=>"1.0" + } + assert_equal expected, helper.oauth_parameters + end + end + end + + def test_oauth_parameters_allow_empty_params_true + input = true + helper = OAuth::Client::Helper.new(nil, { + :consumer => @consumer, + :allow_empty_params => input + }) + helper.stub :timestamp, '0' do + helper.stub :nonce, 'nonce' do + expected = { + "oauth_body_hash"=>nil, + "oauth_callback"=>nil, + "oauth_consumer_key"=>"consumer_key_86cad9", + "oauth_token"=>"", + "oauth_signature_method"=>"HMAC-SHA1", + "oauth_timestamp"=>"0", + "oauth_nonce"=>"nonce", + "oauth_verifier"=>nil, + "oauth_version"=>"1.0", + "oauth_session_handle"=>nil + } + assert_equal expected, helper.oauth_parameters + end + end + end + + def test_oauth_parameters_allow_empty_params_false + input = false + helper = OAuth::Client::Helper.new(nil, { + :consumer => @consumer, + :allow_empty_params => input + }) + helper.stub :timestamp, '0' do + helper.stub :nonce, 'nonce' do + expected = { + "oauth_consumer_key"=>"consumer_key_86cad9", + "oauth_signature_method"=>"HMAC-SHA1", + "oauth_timestamp"=>"0", + "oauth_nonce"=>"nonce", + "oauth_version"=>"1.0" + } + assert_equal expected, helper.oauth_parameters + end + end + end + + def test_oauth_parameters_allow_empty_params_only_oauth_token_as_string + input = 'oauth_token' + helper = OAuth::Client::Helper.new(nil, { + :consumer => @consumer, + :allow_empty_params => input + }) + helper.stub :timestamp, '0' do + helper.stub :nonce, 'nonce' do + expected = { + "oauth_consumer_key"=>"consumer_key_86cad9", + "oauth_token"=>"", + "oauth_signature_method"=>"HMAC-SHA1", + "oauth_timestamp"=>"0", + "oauth_nonce"=>"nonce", + "oauth_version"=>"1.0", + } + assert_equal expected, helper.oauth_parameters + end + end + end + + def test_oauth_parameters_allow_empty_params_only_oauth_token_as_array + input = ['oauth_token'] + helper = OAuth::Client::Helper.new(nil, { + :consumer => @consumer, + :allow_empty_params => input + }) + helper.stub :timestamp, '0' do + helper.stub :nonce, 'nonce' do + expected = { + "oauth_consumer_key"=>"consumer_key_86cad9", + "oauth_token"=>"", + "oauth_signature_method"=>"HMAC-SHA1", + "oauth_timestamp"=>"0", + "oauth_nonce"=>"nonce", + "oauth_version"=>"1.0", + } + assert_equal expected, helper.oauth_parameters + end + end + end + + def test_oauth_parameters_allow_empty_params_oauth_token_and_oauth_session_handle + input = ['oauth_token', 'oauth_session_handle'] + helper = OAuth::Client::Helper.new(nil, { + :consumer => @consumer, + :allow_empty_params => input + }) + helper.stub :timestamp, '0' do + helper.stub :nonce, 'nonce' do + expected = { + "oauth_consumer_key"=>"consumer_key_86cad9", + "oauth_token"=>"", + "oauth_signature_method"=>"HMAC-SHA1", + "oauth_timestamp"=>"0", + "oauth_nonce"=>"nonce", + "oauth_version"=>"1.0", + "oauth_session_handle"=>nil + } + assert_equal expected, helper.oauth_parameters + end + end + end +end