oauth 0.5.5 → 0.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +483 -0
  3. data/CODE_OF_CONDUCT.md +84 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/LICENSE +18 -17
  6. data/README.md +190 -0
  7. data/SECURITY.md +16 -0
  8. data/TODO +0 -0
  9. data/bin/oauth +2 -2
  10. data/lib/oauth/cli/authorize_command.rb +0 -0
  11. data/lib/oauth/cli/base_command.rb +1 -1
  12. data/lib/oauth/cli/help_command.rb +0 -0
  13. data/lib/oauth/cli/query_command.rb +0 -0
  14. data/lib/oauth/cli/sign_command.rb +0 -0
  15. data/lib/oauth/cli/version_command.rb +0 -0
  16. data/lib/oauth/cli.rb +18 -18
  17. data/lib/oauth/client/action_controller_request.rb +7 -7
  18. data/lib/oauth/client/em_http.rb +99 -99
  19. data/lib/oauth/client/helper.rb +22 -22
  20. data/lib/oauth/client/net_http.rb +5 -5
  21. data/lib/oauth/client.rb +0 -0
  22. data/lib/oauth/consumer.rb +50 -39
  23. data/lib/oauth/errors/error.rb +0 -0
  24. data/lib/oauth/errors/problem.rb +0 -0
  25. data/lib/oauth/errors/unauthorized.rb +3 -1
  26. data/lib/oauth/errors.rb +3 -3
  27. data/lib/oauth/helper.rb +11 -7
  28. data/lib/oauth/oauth.rb +0 -0
  29. data/lib/oauth/oauth_test_helper.rb +4 -4
  30. data/lib/oauth/request_proxy/action_controller_request.rb +56 -53
  31. data/lib/oauth/request_proxy/action_dispatch_request.rb +8 -4
  32. data/lib/oauth/request_proxy/base.rb +136 -132
  33. data/lib/oauth/request_proxy/curb_request.rb +49 -43
  34. data/lib/oauth/request_proxy/em_http_request.rb +59 -49
  35. data/lib/oauth/request_proxy/jabber_request.rb +12 -9
  36. data/lib/oauth/request_proxy/mock_request.rb +4 -2
  37. data/lib/oauth/request_proxy/net_http.rb +63 -54
  38. data/lib/oauth/request_proxy/rack_request.rb +35 -31
  39. data/lib/oauth/request_proxy/rest_client_request.rb +53 -50
  40. data/lib/oauth/request_proxy/typhoeus_request.rb +51 -45
  41. data/lib/oauth/request_proxy.rb +0 -0
  42. data/lib/oauth/server.rb +2 -2
  43. data/lib/oauth/signature/base.rb +8 -6
  44. data/lib/oauth/signature/hmac/sha1.rb +4 -4
  45. data/lib/oauth/signature/hmac/sha256.rb +17 -0
  46. data/lib/oauth/signature/plaintext.rb +2 -2
  47. data/lib/oauth/signature/rsa/sha1.rb +3 -3
  48. data/lib/oauth/signature.rb +0 -0
  49. data/lib/oauth/token.rb +5 -5
  50. data/lib/oauth/tokens/access_token.rb +3 -3
  51. data/lib/oauth/tokens/consumer_token.rb +0 -0
  52. data/lib/oauth/tokens/request_token.rb +0 -0
  53. data/lib/oauth/tokens/server_token.rb +0 -0
  54. data/lib/oauth/tokens/token.rb +0 -0
  55. data/lib/oauth/version.rb +1 -1
  56. data/lib/oauth.rb +8 -6
  57. metadata +49 -88
  58. data/README.rdoc +0 -88
@@ -1,5 +1,5 @@
1
- require 'oauth/helper'
2
- require 'oauth/request_proxy/net_http'
1
+ require "oauth/helper"
2
+ require "oauth/request_proxy/net_http"
3
3
 
4
4
  class Net::HTTPGenericRequest
5
5
  include OAuth::Helper
@@ -58,7 +58,7 @@ private
58
58
  { :request_uri => oauth_full_request_uri(http,options),
59
59
  :consumer => consumer,
60
60
  :token => token,
61
- :scheme => 'header',
61
+ :scheme => "header",
62
62
  :signature_method => nil,
63
63
  :nonce => nil,
64
64
  :timestamp => nil }.merge(options)
@@ -71,7 +71,7 @@ private
71
71
 
72
72
  if options[:request_endpoint] && options[:site]
73
73
  is_https = options[:site].match(%r(^https://))
74
- uri.host = options[:site].gsub(%r(^https?://), '')
74
+ uri.host = options[:site].gsub(%r(^https?://), "")
75
75
  uri.port ||= is_https ? 443 : 80
76
76
  end
77
77
 
@@ -89,7 +89,7 @@ private
89
89
  end
90
90
 
91
91
  def set_oauth_header
92
- self['Authorization'] = @oauth_helper.header
92
+ self["Authorization"] = @oauth_helper.header
93
93
  end
94
94
 
95
95
  # FIXME: if you're using a POST body and query string parameters, this method
data/lib/oauth/client.rb CHANGED
File without changes
@@ -1,16 +1,16 @@
1
- require 'net/http'
2
- require 'net/https'
3
- require 'oauth/oauth'
4
- require 'oauth/client/net_http'
5
- require 'oauth/errors'
6
- require 'cgi'
1
+ require "net/http"
2
+ require "net/https"
3
+ require "oauth/oauth"
4
+ require "oauth/client/net_http"
5
+ require "oauth/errors"
6
+ require "cgi"
7
7
 
8
8
  module OAuth
9
9
  class Consumer
10
10
  # determine the certificate authority path to verify SSL certs
11
- if ENV['SSL_CERT_FILE']
12
- if File.exist?(ENV['SSL_CERT_FILE'])
13
- CA_FILE = ENV['SSL_CERT_FILE']
11
+ if ENV["SSL_CERT_FILE"]
12
+ if File.exist?(ENV["SSL_CERT_FILE"])
13
+ CA_FILE = ENV["SSL_CERT_FILE"]
14
14
  else
15
15
  raise "The SSL CERT provided does not exist."
16
16
  end
@@ -29,17 +29,17 @@ module OAuth
29
29
 
30
30
  @@default_options = {
31
31
  # Signature method used by server. Defaults to HMAC-SHA1
32
- :signature_method => 'HMAC-SHA1',
32
+ :signature_method => "HMAC-SHA1",
33
33
 
34
34
  # default paths on site. These are the same as the defaults set up by the generators
35
- :request_token_path => '/oauth/request_token',
36
- :authenticate_path => '/oauth/authenticate',
37
- :authorize_path => '/oauth/authorize',
38
- :access_token_path => '/oauth/access_token',
35
+ :request_token_path => "/oauth/request_token",
36
+ :authenticate_path => "/oauth/authenticate",
37
+ :authorize_path => "/oauth/authorize",
38
+ :access_token_path => "/oauth/access_token",
39
39
 
40
40
  :proxy => nil,
41
41
  # How do we send the oauth values to the server see
42
- # http://oauth.net/core/1.0/#consumer_req_param for more info
42
+ # https://oauth.net/core/1.0/#consumer_req_param for more info
43
43
  #
44
44
  # Possible values:
45
45
  #
@@ -157,11 +157,14 @@ module OAuth
157
157
  request_options[:oauth_callback] ||= OAuth::OUT_OF_BAND unless request_options[:exclude_callback]
158
158
 
159
159
  if block_given?
160
- response = token_request(http_method,
161
- (request_token_url? ? request_token_url : request_token_path),
162
- nil,
163
- request_options,
164
- *arguments, &block)
160
+ response = token_request(
161
+ http_method,
162
+ (request_token_url? ? request_token_url : request_token_path),
163
+ nil,
164
+ request_options,
165
+ *arguments,
166
+ &block
167
+ )
165
168
  else
166
169
  response = token_request(http_method, (request_token_url? ? request_token_url : request_token_path), nil, request_options, *arguments)
167
170
  end
@@ -190,7 +193,7 @@ module OAuth
190
193
  return nil if block_given? and yield(req) == :done
191
194
  rsp = http.request(req)
192
195
  # check for an error reported by the Problem Reporting extension
193
- # (http://wiki.oauth.net/ProblemReporting)
196
+ # (https://wiki.oauth.net/ProblemReporting)
194
197
  # note: a 200 may actually be an error; check for an oauth_problem key to be sure
195
198
  if !(headers = rsp.to_hash["www-authenticate"]).nil? &&
196
199
  (h = headers.select { |hdr| hdr =~ /^OAuth / }).any? &&
@@ -239,16 +242,18 @@ module OAuth
239
242
  end
240
243
  end
241
244
  when (300..399)
242
- # this is a redirect
243
- uri = URI.parse(response['location'])
245
+ # Parse redirect to follow
246
+ uri = URI.parse(response["location"])
244
247
  our_uri = URI.parse(site)
245
248
 
249
+ # Guard against infinite redirects
250
+ response.error! if uri.path == path && our_uri.host == uri.host
251
+
246
252
  if uri.path == path && our_uri.host != uri.host
247
253
  options[:site] = "#{uri.scheme}://#{uri.host}"
248
254
  @http = create_http
249
255
  end
250
256
 
251
- response.error! if uri.path == path && our_uri.host == uri.host # careful of those infinite redirects
252
257
  self.token_request(http_method, uri.path, token, request_options, arguments)
253
258
  when (400..499)
254
259
  raise OAuth::Unauthorized, response
@@ -338,18 +343,22 @@ module OAuth
338
343
  # Instantiates the http object
339
344
  def create_http(_url = nil)
340
345
 
341
-
342
346
  if !request_endpoint.nil?
343
347
  _url = request_endpoint
344
348
  end
345
349
 
346
-
347
- if _url.nil? || _url[0] =~ /^\//
348
- our_uri = URI.parse(site)
349
- else
350
- our_uri = URI.parse(_url)
351
- end
352
-
350
+ our_uri = if _url.nil? || _url[0] =~ /^\//
351
+ URI.parse(site)
352
+ else
353
+ your_uri = URI.parse(_url)
354
+ if your_uri.host.nil?
355
+ # If the _url is a path, missing the leading slash, then it won't have a host,
356
+ # and our_uri *must* have a host, so we parse site instead.
357
+ URI.parse(site)
358
+ else
359
+ your_uri
360
+ end
361
+ end
353
362
 
354
363
  if proxy.nil?
355
364
  http_object = Net::HTTP.new(our_uri.host, our_uri.port)
@@ -358,7 +367,7 @@ module OAuth
358
367
  http_object = Net::HTTP.new(our_uri.host, our_uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
359
368
  end
360
369
 
361
- http_object.use_ssl = (our_uri.scheme == 'https')
370
+ http_object.use_ssl = (our_uri.scheme == "https")
362
371
 
363
372
  if @options[:no_verify]
364
373
  http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -371,9 +380,11 @@ module OAuth
371
380
  http_object.verify_depth = 5
372
381
  end
373
382
 
374
- http_object.read_timeout = http_object.open_timeout = @options[:timeout] || 30
383
+ http_object.read_timeout = http_object.open_timeout = @options[:timeout] || 60
375
384
  http_object.open_timeout = @options[:open_timeout] if @options[:open_timeout]
376
385
  http_object.ssl_version = @options[:ssl_version] if @options[:ssl_version]
386
+ http_object.cert = @options[:ssl_client_cert] if @options[:ssl_client_cert]
387
+ http_object.key = @options[:ssl_client_key] if @options[:ssl_client_key]
377
388
  http_object.set_debug_output(debug_output) if debug_output
378
389
 
379
390
  http_object
@@ -391,20 +402,20 @@ module OAuth
391
402
  # only add if the site host matches the current http object's host
392
403
  # (in case we've specified a full url for token requests)
393
404
  uri = URI.parse(site)
394
- path = uri.path + path if uri.path && uri.path != '/' && uri.host == http.address
405
+ path = uri.path + path if uri.path && uri.path != "/" && uri.host == http.address
395
406
 
396
407
  headers = arguments.first.is_a?(Hash) ? arguments.shift : {}
397
408
 
398
409
  case http_method
399
410
  when :post
400
411
  request = Net::HTTP::Post.new(path,headers)
401
- request["Content-Length"] = '0' # Default to 0
412
+ request["Content-Length"] = "0" # Default to 0
402
413
  when :put
403
414
  request = Net::HTTP::Put.new(path,headers)
404
- request["Content-Length"] = '0' # Default to 0
415
+ request["Content-Length"] = "0" # Default to 0
405
416
  when :patch
406
417
  request = Net::HTTP::Patch.new(path,headers)
407
- request["Content-Length"] = '0' # Default to 0
418
+ request["Content-Length"] = "0" # Default to 0
408
419
  when :get
409
420
  request = Net::HTTP::Get.new(path,headers)
410
421
  when :delete
@@ -417,7 +428,7 @@ module OAuth
417
428
 
418
429
  if data.is_a?(Hash)
419
430
  request.body = OAuth::Helper.normalize(data)
420
- request.content_type = 'application/x-www-form-urlencoded'
431
+ request.content_type = "application/x-www-form-urlencoded"
421
432
  elsif data
422
433
  if data.respond_to?(:read)
423
434
  request.body_stream = data
File without changes
File without changes
@@ -6,7 +6,9 @@ module OAuth
6
6
  end
7
7
 
8
8
  def to_s
9
- [request.code, request.message] * " "
9
+ return "401 Unauthorized" if request.nil?
10
+
11
+ "#{request.code} #{request.message}"
10
12
  end
11
13
  end
12
14
  end
data/lib/oauth/errors.rb CHANGED
@@ -1,3 +1,3 @@
1
- require 'oauth/errors/error'
2
- require 'oauth/errors/unauthorized'
3
- require 'oauth/errors/problem'
1
+ require "oauth/errors/error"
2
+ require "oauth/errors/unauthorized"
3
+ require "oauth/errors/problem"
data/lib/oauth/helper.rb CHANGED
@@ -1,5 +1,5 @@
1
- require 'openssl'
2
- require 'base64'
1
+ require "openssl"
2
+ require "base64"
3
3
 
4
4
  module OAuth
5
5
  module Helper
@@ -19,13 +19,13 @@ module OAuth
19
19
  end
20
20
 
21
21
  def unescape(value)
22
- URI::DEFAULT_PARSER.unescape(value.gsub('+', '%2B'))
22
+ URI::DEFAULT_PARSER.unescape(value.gsub("+", "%2B"))
23
23
  end
24
24
 
25
25
  # Generate a random key of up to +size+ bytes. The value returned is Base64 encoded with non-word
26
26
  # characters removed.
27
27
  def generate_key(size=32)
28
- Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
28
+ Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, "")
29
29
  end
30
30
 
31
31
  alias_method :generate_nonce, :generate_key
@@ -47,8 +47,12 @@ module OAuth
47
47
  # make sure the array has an element so we don't lose the key
48
48
  values << nil if values.empty?
49
49
  # multiple values were provided for a single key
50
- values.sort.collect do |v|
51
- [escape(k),escape(v)] * "="
50
+ if values[0].is_a?(Hash)
51
+ normalize_nested_query(values, k)
52
+ else
53
+ values.sort.collect do |v|
54
+ [escape(k),escape(v)] * "="
55
+ end
52
56
  end
53
57
  elsif values.is_a?(Hash)
54
58
  normalize_nested_query(values, k)
@@ -58,7 +62,7 @@ module OAuth
58
62
  end * "&"
59
63
  end
60
64
 
61
- #Returns a string representation of the Hash like in URL query string
65
+ # Returns a string representation of the Hash like in URL query string
62
66
  # build_nested_query({:level_1 => {:level_2 => ['value_1','value_2']}}, 'prefix'))
63
67
  # #=> ["prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_1", "prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_2"]
64
68
  def normalize_nested_query(value, prefix = nil)
data/lib/oauth/oauth.rb CHANGED
File without changes
@@ -1,5 +1,5 @@
1
- require 'action_controller'
2
- require 'action_controller/test_process'
1
+ require "action_controller"
2
+ require "action_controller/test_process"
3
3
 
4
4
  module OAuth
5
5
  module OAuthTestHelper
@@ -8,7 +8,7 @@ module OAuth
8
8
  incoming.request_uri = request.path
9
9
  incoming.host = request.uri.host
10
10
  incoming.env["SERVER_PORT"] = request.uri.port
11
- incoming.env['REQUEST_METHOD'] = request.http_method
11
+ incoming.env["REQUEST_METHOD"] = request.http_method
12
12
  incoming
13
13
  end
14
14
 
@@ -18,7 +18,7 @@ module OAuth
18
18
  incoming.host = request.uri.host
19
19
  incoming.env["HTTP_AUTHORIZATION"] = request.to_auth_string
20
20
  incoming.env["SERVER_PORT"] = request.uri.port
21
- incoming.env['REQUEST_METHOD'] = request.http_method
21
+ incoming.env["REQUEST_METHOD"] = request.http_method
22
22
  incoming
23
23
  end
24
24
  end
@@ -1,22 +1,22 @@
1
- require 'active_support'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
2
4
  require "active_support/version"
3
- require 'action_controller'
4
- require 'uri'
5
+ require "action_controller"
6
+ require "uri"
5
7
 
6
- if
7
- Gem::Version.new(ActiveSupport::VERSION::STRING) < Gem::Version.new("3")
8
- then # rails 2.x
9
- require 'action_controller/request'
8
+ if Gem::Version.new(ActiveSupport::VERSION::STRING) < Gem::Version.new("3")
9
+ # rails 2.x
10
+ require "action_controller/request"
10
11
  unless ActionController::Request::HTTP_METHODS.include?("patch")
11
12
  ActionController::Request::HTTP_METHODS << "patch"
12
13
  ActionController::Request::HTTP_METHOD_LOOKUP["PATCH"] = :patch
13
14
  ActionController::Request::HTTP_METHOD_LOOKUP["patch"] = :patch
14
15
  end
15
16
 
16
- elsif
17
- Gem::Version.new(ActiveSupport::VERSION::STRING) < Gem::Version.new("4")
18
- then # rails 3.x
19
- require 'action_dispatch/http/request'
17
+ elsif Gem::Version.new(ActiveSupport::VERSION::STRING) < Gem::Version.new("4")
18
+ # rails 3.x
19
+ require "action_dispatch/http/request"
20
20
  unless ActionDispatch::Request::HTTP_METHODS.include?("patch")
21
21
  ActionDispatch::Request::HTTP_METHODS << "patch"
22
22
  ActionDispatch::Request::HTTP_METHOD_LOOKUP["PATCH"] = :patch
@@ -24,63 +24,66 @@ then # rails 3.x
24
24
  end
25
25
 
26
26
  else # rails 4.x and later - already has patch
27
- require 'action_dispatch/http/request'
27
+ require "action_dispatch/http/request"
28
28
  end
29
29
 
30
- module OAuth::RequestProxy
31
- class ActionControllerRequest < OAuth::RequestProxy::Base
32
- proxies(defined?(ActionDispatch::AbstractRequest) ? ActionDispatch::AbstractRequest : ActionDispatch::Request)
30
+ module OAuth
31
+ module RequestProxy
32
+ class ActionControllerRequest < OAuth::RequestProxy::Base
33
+ proxies(defined?(::ActionDispatch::AbstractRequest) ? ::ActionDispatch::AbstractRequest : ::ActionDispatch::Request)
33
34
 
34
- def method
35
- request.method.to_s.upcase
36
- end
35
+ def method
36
+ request.method.to_s.upcase
37
+ end
37
38
 
38
- def uri
39
- request.url
40
- end
39
+ def uri
40
+ request.url
41
+ end
41
42
 
42
- def parameters
43
- if options[:clobber_request]
44
- options[:parameters] || {}
45
- else
46
- params = request_params.merge(query_params).merge(header_params)
47
- params.stringify_keys! if params.respond_to?(:stringify_keys!)
48
- params.merge(options[:parameters] || {})
43
+ def parameters
44
+ if options[:clobber_request]
45
+ options[:parameters] || {}
46
+ else
47
+ params = request_params.merge(query_params).merge(header_params)
48
+ params.stringify_keys! if params.respond_to?(:stringify_keys!)
49
+ params.merge(options[:parameters] || {})
50
+ end
49
51
  end
50
- end
51
52
 
52
- # Override from OAuth::RequestProxy::Base to avoid roundtrip
53
- # conversion to Hash or Array and thus preserve the original
54
- # parameter names
55
- def parameters_for_signature
56
- params = []
57
- params << options[:parameters].to_query if options[:parameters]
53
+ # Override from OAuth::RequestProxy::Base to avoid roundtrip
54
+ # conversion to Hash or Array and thus preserve the original
55
+ # parameter names
56
+ def parameters_for_signature
57
+ params = []
58
+ params << options[:parameters].to_query if options[:parameters]
58
59
 
59
- unless options[:clobber_request]
60
- params << header_params.to_query
61
- params << request.query_string unless query_string_blank?
60
+ unless options[:clobber_request]
61
+ params << header_params.to_query
62
+ params << request.query_string unless query_string_blank?
62
63
 
63
- if request.post? && request.content_type.to_s.downcase.start_with?("application/x-www-form-urlencoded")
64
- params << request.raw_post
64
+ params << request.raw_post if raw_post_signature?
65
65
  end
66
+
67
+ params.
68
+ join("&").split("&").
69
+ reject { |s| s.match(/\A\s*\z/) }.
70
+ map { |p| p.split("=").map { |esc| CGI.unescape(esc) } }.
71
+ reject { |kv| kv[0] == "oauth_signature" }
66
72
  end
67
73
 
68
- params.
69
- join('&').split('&').
70
- reject { |s| s.match(/\A\s*\z/) }.
71
- map { |p| p.split('=').map{|esc| CGI.unescape(esc)} }.
72
- reject { |kv| kv[0] == 'oauth_signature'}
73
- end
74
+ def raw_post_signature?
75
+ (request.post? || request.put?) && request.content_type.to_s.downcase.start_with?("application/x-www-form-urlencoded")
76
+ end
74
77
 
75
- protected
78
+ protected
76
79
 
77
- def query_params
78
- request.query_parameters
79
- end
80
+ def query_params
81
+ request.query_parameters
82
+ end
80
83
 
81
- def request_params
82
- request.request_parameters
84
+ def request_params
85
+ request.request_parameters
86
+ end
83
87
  end
84
-
85
88
  end
86
89
  end
@@ -1,7 +1,11 @@
1
- require 'oauth/request_proxy/rack_request'
1
+ # frozen_string_literal: true
2
2
 
3
- module OAuth::RequestProxy
4
- class ActionDispatchRequest < OAuth::RequestProxy::RackRequest
5
- proxies ActionDispatch::Request
3
+ require "oauth/request_proxy/rack_request"
4
+
5
+ module OAuth
6
+ module RequestProxy
7
+ class ActionDispatchRequest < OAuth::RequestProxy::RackRequest
8
+ proxies ::ActionDispatch::Request
9
+ end
6
10
  end
7
11
  end