Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
smaeda-ks committed Jan 20, 2021
1 parent c2c9d72 commit 17eb01f
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 24 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -2,10 +2,13 @@ language: ruby

rvm:
- "ruby-head"
- "2.7"
- "2.4.0"
- "2.3"
- "2.2"

matrix:
allow_failures:
- rvm: "ruby-head"
addons:
code_climate:
repo_token: 8f697ca756250f0c2c54170ae27e8a9c459d18a0236903b11291c88291b3aac9
Expand Down
7 changes: 7 additions & 0 deletions HISTORY
@@ -1,5 +1,12 @@
=== CURRENT

=== 0.5.5 2020-01-19

* Allow redirect to different host but same path
* Add :allow_empty_params option (#155)
* Fixes ssl-noverify
* Various cleanups

=== 0.5.4 2017-12-08

* Fixes UnknownRequestType on Rails 5.1 for ActionDispatch::Request (xprazak2)
Expand Down
42 changes: 42 additions & 0 deletions examples/twitter.rb
@@ -0,0 +1,42 @@
#!/usr/bin/env ruby -r rubygems
#
# ./twitter.rb --consumer-key <key> --consumer-secret <secret> <tweet_id>

require 'oauth'
require 'optparse'
require 'json'
require 'pp'

options = {}

option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] <query>"

opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
options[:consumer_key] = v
end

opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
options[:consumer_secret] = v
end
end

option_parser.parse!
query = ARGV.pop
query = STDIN.read if query == "-"

if options[:consumer_key].nil? || options[:consumer_secret].nil? || query.nil?
puts option_parser.help
exit 1
end

consumer = OAuth::Consumer.new \
options[:consumer_key],
options[:consumer_secret],
:site => "https://api.twitter.com"

access_token = OAuth::AccessToken.new(consumer)

response = access_token.request(:get, "/1.1/statuses/show/#{OAuth::Helper.escape(query)}.json")
rsp = JSON.parse(response.body)
pp rsp
2 changes: 1 addition & 1 deletion examples/yql.rb
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby -rubygems
#!/usr/bin/env ruby -r rubygems

# Sample queries:
# ./yql.rb --consumer-key <key> --consumer-secret <secret> "show tables"
Expand Down
10 changes: 8 additions & 2 deletions lib/oauth/client/helper.rb
Expand Up @@ -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,
Expand All @@ -38,7 +38,13 @@ 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 == "" }
}
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

def signature(extra_options = {})
Expand Down
53 changes: 43 additions & 10 deletions lib/oauth/consumer.rb
Expand Up @@ -8,11 +8,21 @@
module OAuth
class Consumer
# determine the certificate authority path to verify SSL certs
CA_FILES = %W(#{ENV['SSL_CERT_FILE']} /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt /usr/share/curl/curl-ca-bundle.crt)
CA_FILES.each do |ca_file|
if File.exist?(ca_file)
CA_FILE = ca_file
break
if ENV['SSL_CERT_FILE']
if File.exist?(ENV['SSL_CERT_FILE'])
CA_FILE = ENV['SSL_CERT_FILE']
else
raise "The SSL CERT provided does not exist."
end
end

if !defined?(CA_FILE)
CA_FILES = %W(/etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt /usr/share/curl/curl-ca-bundle.crt)
CA_FILES.each do |ca_file|
if File.exist?(ca_file)
CA_FILE = ca_file
break
end
end
end
CA_FILE = nil unless defined?(CA_FILE)
Expand All @@ -23,6 +33,7 @@ class Consumer

# default paths on site. These are the same as the defaults set up by the generators
:request_token_path => '/oauth/request_token',
:authenticate_path => '/oauth/authenticate',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',

Expand Down Expand Up @@ -230,7 +241,14 @@ def token_request(http_method, path, token = nil, request_options = {}, *argumen
when (300..399)
# this is a redirect
uri = URI.parse(response['location'])
response.error! if uri.path == path # careful of those infinite redirects
our_uri = URI.parse(site)

if uri.path == path && our_uri.host != uri.host
options[:site] = "#{uri.scheme}://#{uri.host}"
@http = create_http
end

response.error! if uri.path == path && our_uri.host == uri.host # careful of those infinite redirects
self.token_request(http_method, uri.path, token, request_options, arguments)
when (400..499)
raise OAuth::Unauthorized, response
Expand Down Expand Up @@ -266,6 +284,10 @@ def request_token_path
@options[:request_token_path]
end

def authenticate_path
@options[:authenticate_path]
end

def authorize_path
@options[:authorize_path]
end
Expand All @@ -283,6 +305,14 @@ def request_token_url?
@options.has_key?(:request_token_url)
end

def authenticate_url
@options[:authenticate_url] || site + authenticate_path
end

def authenticate_url?
@options.has_key?(:authenticate_url)
end

def authorize_url
@options[:authorize_url] || site + authorize_path
end
Expand Down Expand Up @@ -330,12 +360,15 @@ def create_http(_url = nil)

http_object.use_ssl = (our_uri.scheme == 'https')

if @options[:ca_file] || CA_FILE
http_object.ca_file = @options[:ca_file] || CA_FILE
if @options[:no_verify]
http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE
else
ca_file = @options[:ca_file] || CA_FILE
if ca_file
http_object.ca_file = ca_file
end
http_object.verify_mode = OpenSSL::SSL::VERIFY_PEER
http_object.verify_depth = 5
else
http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

http_object.read_timeout = http_object.open_timeout = @options[:timeout] || 60
Expand Down
13 changes: 10 additions & 3 deletions lib/oauth/tokens/request_token.rb
Expand Up @@ -8,7 +8,14 @@ def authorize_url(params = nil)
return nil if self.token.nil?

params = (params || {}).merge(:oauth_token => self.token)
build_authorize_url(consumer.authorize_url, params)
build_url(consumer.authorize_url, params)
end

def authenticate_url(params = nil)
return nil if self.token.nil?

params = (params || {}).merge(:oauth_token => self.token)
build_url(consumer.authenticate_url, params)
end

def callback_confirmed?
Expand All @@ -23,8 +30,8 @@ def get_access_token(options = {}, *arguments)

protected

# construct an authorization url
def build_authorize_url(base_url, params)
# construct an authorization or authentication url
def build_url(base_url, params)
uri = URI.parse(base_url.to_s)
queries = {}
queries = Hash[URI.decode_www_form(uri.query)] if uri.query
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth/version.rb
@@ -1,3 +1,3 @@
module OAuth
VERSION = "0.5.4"
VERSION = "0.5.5"
end
2 changes: 1 addition & 1 deletion oauth.gemspec
Expand Up @@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency("iconv")
spec.add_development_dependency("rack", "~> 2.0")
spec.add_development_dependency("rack-test")
spec.add_development_dependency("mocha", ">= 0.9.12")
spec.add_development_dependency("mocha", ">= 0.9.12", "<=1.1.0")
spec.add_development_dependency("typhoeus", ">= 0.1.13")
spec.add_development_dependency("em-http-request", "0.2.11")
spec.add_development_dependency("curb")
Expand Down
147 changes: 147 additions & 0 deletions 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

0 comments on commit 17eb01f

Please sign in to comment.