lib/oauth/client/net_http.rb in pelle-oauth-0.3.1 vs lib/oauth/client/net_http.rb in pelle-oauth-0.3.5
- old
+ new
@@ -3,47 +3,78 @@
require 'oauth/request_proxy/net_http'
class Net::HTTPRequest
include OAuth::Helper
+ attr_reader :oauth_helper
+
+ # Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting
+ # this may add a header, additional query string parameters, or additional POST body parameters.
+ # The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
+ # header.
+ #
+ # * http - Configured Net::HTTP instance
+ # * consumer - OAuth::Consumer instance
+ # * token - OAuth::Token instance
+ # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
+ # +signature_method+, +nonce+, +timestamp+)
+ #
+ # This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version.
+ #
+ # See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1]
def oauth!(http, consumer = nil, token = nil, options = {})
- options = { :request_uri => oauth_full_request_uri(http),
- :consumer => consumer,
- :token => token,
- :scheme => 'header',
+ options = { :request_uri => oauth_full_request_uri(http),
+ :consumer => consumer,
+ :token => token,
+ :scheme => 'header',
:signature_method => nil,
- :nonce => nil,
- :timestamp => nil }.merge(options)
+ :nonce => nil,
+ :timestamp => nil }.merge(options)
@oauth_helper = OAuth::Client::Helper.new(self, options)
+ @oauth_helper.amend_user_agent_header(self)
self.send("set_oauth_#{options[:scheme]}")
end
+ # Create a string suitable for signing for an HTTP request. This process involves parameter
+ # normalization as specified in the OAuth specification. The exact normalization also depends
+ # on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request
+ # itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
+ # header.
+ #
+ # * http - Configured Net::HTTP instance
+ # * consumer - OAuth::Consumer instance
+ # * token - OAuth::Token instance
+ # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
+ # +signature_method+, +nonce+, +timestamp+)
+ #
+ # See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1]
def signature_base_string(http, consumer = nil, token = nil, options = {})
- options = { :request_uri => oauth_full_request_uri(http),
- :consumer => consumer,
- :token => token,
- :scheme => 'header',
+ options = { :request_uri => oauth_full_request_uri(http),
+ :consumer => consumer,
+ :token => token,
+ :scheme => 'header',
:signature_method => nil,
- :nonce => nil,
- :timestamp => nil }.merge(options)
+ :nonce => nil,
+ :timestamp => nil }.merge(options)
OAuth::Client::Helper.new(self, options).signature_base_string
end
-
- def oauth_helper
- @oauth_helper
- end
- private
+private
+
def oauth_full_request_uri(http)
uri = URI.parse(self.path)
uri.host = http.address
uri.port = http.port
- if http.respond_to?(:use_ssl?)
- uri.scheme = http.use_ssl? ? 'https' : 'http'
+
+ if http.respond_to?(:use_ssl?) && http.use_ssl?
+ uri.scheme = "https"
+ else
+ uri.scheme = "http"
end
+
uri.to_s
end
def set_oauth_header
self['Authorization'] = @oauth_helper.header
@@ -57,19 +88,19 @@
params_with_sig = @oauth_helper.parameters.merge(:oauth_signature => @oauth_helper.signature)
self.set_form_data(params_with_sig)
end
def set_oauth_query_string
- oauth_params_str = @oauth_helper.oauth_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
+ oauth_params_str = @oauth_helper.oauth_parameters.map { |k,v| [escape(k), escape(v)] * "=" }.join("&")
uri = URI.parse(path)
- if !uri.query || uri.query == ''
+ if uri.query.to_s == ""
uri.query = oauth_params_str
else
uri.query = uri.query + "&" + oauth_params_str
end
@path = uri.to_s
- @path << "&oauth_signature=#{escape(@oauth_helper.signature)}"
+ @path << "&oauth_signature=#{escape(oauth_helper.signature)}"
end
end