Class: OAuth::Consumer
- Inherits:
-
Object
- Object
- OAuth::Consumer
- Defined in:
- lib/oauth/consumer.rb
Constant Summary collapse
- CA_FILE =
nil- @@default_options =
{ # Signature method used by server. Defaults to HMAC-SHA1 signature_method: "HMAC-SHA1", # 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", proxy: nil, # How do we send the oauth values to the server see # https://oauth.net/core/1.0/#consumer_req_param for more info # # Possible values: # # :header - via the Authorize header (Default) ( option 1. in spec) # :body - url form encoded in body of POST request ( option 2. in spec) # :query_string - via the query part of the url ( option 3. in spec) scheme: :header, # Default http method used for OAuth Token Requests (defaults to :post) http_method: :post, # Add a custom ca_file for consumer # :ca_file => '/etc/certs.pem' # Possible values: # # nil, false - no debug output # true - uses $stdout # some_value - uses some_value debug_output: nil, # Defaults to producing a body_hash as part of the signature but # can be disabled since it's not officially part of the OAuth 1.0 # spec. Possible values are true and false body_hash_enabled: true, oauth_version: "1.0" }
Instance Attribute Summary collapse
-
#http ⇒ Object
The HTTP object for the site.
-
#key ⇒ Object
Returns the value of attribute key.
-
#options ⇒ Object
Returns the value of attribute options.
-
#secret ⇒ Object
Returns the value of attribute secret.
- #site ⇒ Object
Instance Method Summary collapse
- #access_token_path ⇒ Object
- #access_token_url ⇒ Object
- #access_token_url? ⇒ Boolean
- #authenticate_path ⇒ Object
- #authenticate_url ⇒ Object
- #authenticate_url? ⇒ Boolean
- #authorize_path ⇒ Object
- #authorize_url ⇒ Object
- #authorize_url? ⇒ Boolean
-
#create_signed_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates and signs an http request.
- #debug_output ⇒ Object
- #get_access_token(request_token, request_options = {}, *arguments, &block) ⇒ Object
-
#get_request_token(request_options = {}, *arguments, &block) ⇒ Object
Makes a request to the service for a new OAuth::RequestToken.
-
#http_method ⇒ Object
The default http method.
-
#initialize(consumer_key, consumer_secret, options = {}) ⇒ Consumer
constructor
Create a new consumer instance by passing it a configuration hash:.
- #proxy ⇒ Object
-
#request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates, signs and performs an http request.
- #request_endpoint ⇒ Object
- #request_token_path ⇒ Object
-
#request_token_url ⇒ Object
TODO: this is ugly, rewrite.
- #request_token_url? ⇒ Boolean
- #scheme ⇒ Object
-
#sign!(request, token = nil, request_options = {}) ⇒ Object
Sign the Request object.
-
#signature_base_string(request, token = nil, request_options = {}) ⇒ Object
Return the signature_base_string.
-
#token_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates a request and parses the result as url_encoded.
-
#uri(custom_uri = nil) ⇒ Object
Contains the root URI for this site.
Constructor Details
#initialize(consumer_key, consumer_secret, options = {}) ⇒ Consumer
Create a new consumer instance by passing it a configuration hash:
@consumer = OAuth::Consumer.new(key, secret, {
:site => "http://term.ie",
:scheme => :header,
:http_method => :post,
:request_token_path => "/oauth/example/request_token.php",
:access_token_path => "/oauth/example/access_token.php",
:authorize_path => "/oauth/example/authorize.php",
:body_hash_enabled => false
})
Start the process by requesting a token
@request_token = @consumer.get_request_token
session[:request_token] = @request_token
redirect_to @request_token.
When user returns create an access_token
@access_token = @request_token.get_access_token
@photos=@access_token.get('/photos.xml')
101 102 103 104 105 106 107 |
# File 'lib/oauth/consumer.rb', line 101 def initialize(consumer_key, consumer_secret, = {}) @key = consumer_key @secret = consumer_secret # ensure that keys are symbols = .merge(.transform_keys(&:to_sym)) end |
Instance Attribute Details
#http ⇒ Object
The HTTP object for the site. The HTTP Object is what you get when you do Net::HTTP.new
125 126 127 |
# File 'lib/oauth/consumer.rb', line 125 def http @http ||= create_http end |
#key ⇒ Object
Returns the value of attribute key.
75 76 77 |
# File 'lib/oauth/consumer.rb', line 75 def key @key end |
#options ⇒ Object
Returns the value of attribute options.
75 76 77 |
# File 'lib/oauth/consumer.rb', line 75 def end |
#secret ⇒ Object
Returns the value of attribute secret.
75 76 77 |
# File 'lib/oauth/consumer.rb', line 75 def secret @secret end |
#site ⇒ Object
283 284 285 |
# File 'lib/oauth/consumer.rb', line 283 def site [:site].to_s end |
Instance Method Details
#access_token_path ⇒ Object
309 310 311 |
# File 'lib/oauth/consumer.rb', line 309 def access_token_path [:access_token_path] end |
#access_token_url ⇒ Object
338 339 340 |
# File 'lib/oauth/consumer.rb', line 338 def access_token_url [:access_token_url] || (site + access_token_path) end |
#access_token_url? ⇒ Boolean
342 343 344 |
# File 'lib/oauth/consumer.rb', line 342 def access_token_url? .key?(:access_token_url) end |
#authenticate_path ⇒ Object
301 302 303 |
# File 'lib/oauth/consumer.rb', line 301 def authenticate_path [:authenticate_path] end |
#authenticate_url ⇒ Object
322 323 324 |
# File 'lib/oauth/consumer.rb', line 322 def authenticate_url [:authenticate_url] || (site + authenticate_path) end |
#authenticate_url? ⇒ Boolean
326 327 328 |
# File 'lib/oauth/consumer.rb', line 326 def authenticate_url? .key?(:authenticate_url) end |
#authorize_path ⇒ Object
305 306 307 |
# File 'lib/oauth/consumer.rb', line 305 def [:authorize_path] end |
#authorize_url ⇒ Object
330 331 332 |
# File 'lib/oauth/consumer.rb', line 330 def [:authorize_url] || (site + ) end |
#authorize_url? ⇒ Boolean
334 335 336 |
# File 'lib/oauth/consumer.rb', line 334 def .key?(:authorize_url) end |
#create_signed_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates and signs an http request. It’s recommended to use the Token classes to set this up correctly
228 229 230 231 232 |
# File 'lib/oauth/consumer.rb', line 228 def create_signed_request(http_method, path, token = nil, = {}, *arguments) request = create_http_request(http_method, path, *arguments) sign!(request, token, ) request end |
#debug_output ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/oauth/consumer.rb', line 114 def debug_output @debug_output ||= case [:debug_output] when nil, false when true $stdout else [:debug_output] end end |
#get_access_token(request_token, request_options = {}, *arguments, &block) ⇒ Object
139 140 141 142 143 |
# File 'lib/oauth/consumer.rb', line 139 def get_access_token(request_token, = {}, *arguments, &block) response = token_request(http_method, (access_token_url? ? access_token_url : access_token_path), request_token, , *arguments, &block) OAuth::AccessToken.from_hash(self, response) end |
#get_request_token(request_options = {}, *arguments, &block) ⇒ Object
Makes a request to the service for a new OAuth::RequestToken
@request_token = @consumer.get_request_token
To include OAuth parameters:
@request_token = @consumer.get_request_token \
:oauth_callback => "http://example.com/cb"
To include application-specific parameters:
@request_token = @consumer.get_request_token({}, :foo => "bar")
TODO oauth_callback should be a mandatory parameter
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/oauth/consumer.rb', line 159 def get_request_token( = {}, *arguments, &block) # if oauth_callback wasn't provided, it is assumed that oauth_verifiers # will be exchanged out of band unless [:exclude_callback] [:oauth_callback] ||= OAuth::OUT_OF_BAND end response = if block token_request( http_method, (request_token_url? ? request_token_url : request_token_path), nil, , *arguments, &block ) else token_request(http_method, (request_token_url? ? request_token_url : request_token_path), nil, , *arguments) end OAuth::RequestToken.from_hash(self, response) end |
#http_method ⇒ Object
The default http method
110 111 112 |
# File 'lib/oauth/consumer.rb', line 110 def http_method @http_method ||= [:http_method] || :post end |
#proxy ⇒ Object
346 347 348 |
# File 'lib/oauth/consumer.rb', line 346 def proxy [:proxy] end |
#request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates, signs and performs an http request. It’s recommended to use the OAuth::Token classes to set this up correctly. request_options take precedence over consumer-wide options when signing
a request.
arguments are POST and PUT bodies (a Hash, string-encoded parameters, or
absent), followed by additional HTTP headers.
@consumer.request(:get, '/people', @token, { :scheme => :query_string })
@consumer.request(:post, '/people', @token, {}, @person.to_xml, { 'Content-Type' => 'application/xml' })
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/oauth/consumer.rb', line 192 def request(http_method, path, token = nil, = {}, *arguments) unless %r{^/}.match?(path) @http = create_http(path) _uri = URI.parse(path) path = "#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}" end # override the request with your own, this is useful for file uploads which Net::HTTP does not do req = create_signed_request(http_method, path, token, , *arguments) return nil if block_given? && (yield(req) == :done) rsp = http.request(req) # check for an error reported by the Problem Reporting extension # (https://wiki.oauth.net/ProblemReporting) # note: a 200 may actually be an error; check for an oauth_problem key to be sure if !(headers = rsp.to_hash["www-authenticate"]).nil? && (h = headers.grep(/^OAuth /)).any? && h.first.include?("oauth_problem") # puts "Header: #{h.first}" # TODO: doesn't handle broken responses from api.login.yahoo.com # remove debug code when done params = OAuth::Helper.parse_header(h.first) # puts "Params: #{params.inspect}" # puts "Body: #{rsp.body}" raise OAuth::Problem.new(params.delete("oauth_problem"), rsp, params) end rsp end |
#request_endpoint ⇒ Object
287 288 289 290 291 |
# File 'lib/oauth/consumer.rb', line 287 def request_endpoint return nil if [:request_endpoint].nil? [:request_endpoint].to_s end |
#request_token_path ⇒ Object
297 298 299 |
# File 'lib/oauth/consumer.rb', line 297 def request_token_path [:request_token_path] end |
#request_token_url ⇒ Object
TODO: this is ugly, rewrite
314 315 316 |
# File 'lib/oauth/consumer.rb', line 314 def request_token_url [:request_token_url] || (site + request_token_path) end |
#request_token_url? ⇒ Boolean
318 319 320 |
# File 'lib/oauth/consumer.rb', line 318 def request_token_url? .key?(:request_token_url) end |
#scheme ⇒ Object
293 294 295 |
# File 'lib/oauth/consumer.rb', line 293 def scheme [:scheme] end |
#sign!(request, token = nil, request_options = {}) ⇒ Object
Sign the Request object. Use this if you have an externally generated http request object you want to sign.
274 275 276 |
# File 'lib/oauth/consumer.rb', line 274 def sign!(request, token = nil, = {}) request.oauth!(http, self, token, .merge()) end |
#signature_base_string(request, token = nil, request_options = {}) ⇒ Object
Return the signature_base_string
279 280 281 |
# File 'lib/oauth/consumer.rb', line 279 def signature_base_string(request, token = nil, = {}) request.signature_base_string(http, self, token, .merge()) end |
#token_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates a request and parses the result as url_encoded. This is used internally for the RequestToken and AccessToken requests.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/oauth/consumer.rb', line 235 def token_request(http_method, path, token = nil, = {}, *arguments) [:token_request] ||= true response = request(http_method, path, token, , *arguments) case response.code.to_i when (200..299) if block_given? yield response.body else # symbolize keys # TODO this could be considered unexpected behavior; symbols or not? # TODO this also drops subsequent values from multi-valued keys CGI.parse(response.body).each_with_object({}) do |(k, v), h| h[k.strip.to_sym] = v.first h[k.strip] = v.first end end when (300..399) # Parse redirect to follow uri = URI.parse(response["location"]) our_uri = URI.parse(site) # Guard against infinite redirects response.error! if uri.path == path && our_uri.host == uri.host if uri.path == path && our_uri.host != uri.host [:site] = "#{uri.scheme}://#{uri.host}" @http = create_http end token_request(http_method, uri.path, token, , arguments) when (400..499) raise OAuth::, response else response.error! end end |
#uri(custom_uri = nil) ⇒ Object
Contains the root URI for this site
130 131 132 133 134 135 136 137 |
# File 'lib/oauth/consumer.rb', line 130 def uri(custom_uri = nil) if custom_uri @uri = custom_uri @http = create_http # yike, oh well. less intrusive this way else # if no custom passed, we use existing, which, if unset, is set to site uri @uri ||= URI.parse(site) end end |