File: em_http.rb

package info (click to toggle)
ruby-oauth 0.5.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 584 kB
  • sloc: ruby: 4,070; makefile: 4
file content (119 lines) | stat: -rw-r--r-- 4,573 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require 'em-http'
require 'oauth/helper'
require 'oauth/request_proxy/em_http_request'

# Extensions for em-http so that we can use consumer.sign! with an EventMachine::HttpClient
# instance. This is purely syntactic sugar.
class EventMachine::HttpClient

  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, ignored in this scenario except for getting host.
  # * 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      => normalized_oauth_uri(http),
                :consumer         => consumer,
                :token            => token,
                :scheme           => 'header',
                :signature_method => nil,
                :nonce            => nil,
                :timestamp        => nil }.merge(options)

    @oauth_helper = OAuth::Client::Helper.new(self, options)
    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      => normalized_oauth_uri(http),
                :consumer         => consumer,
                :token            => token,
                :scheme           => 'header',
                :signature_method => nil,
                :nonce            => nil,
                :timestamp        => nil }.merge(options)

    OAuth::Client::Helper.new(self, options).signature_base_string
  end

  # This code was lifted from the em-http-request because it was removed from
  # the gem June 19, 2010
  # see: http://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b
  def normalize_uri
    @normalized_uri ||= begin
      uri = @uri.dup
      encoded_query = encode_query(@uri, @options[:query])
      path, query = encoded_query.split("?", 2)
      uri.query = query unless encoded_query.empty?
      uri.path  = path
      uri
    end
  end

  protected

  def combine_query(path, query, uri_query)
    combined_query = if query.kind_of?(Hash)
      query.map { |k, v| encode_param(k, v) }.join('&')
    else
      query.to_s
    end
    if !uri_query.to_s.empty?
      combined_query = [combined_query, uri_query].reject {|part| part.empty?}.join("&")
    end
    combined_query.to_s.empty? ? path : "#{path}?#{combined_query}"
  end

  # Since we expect to get the host etc details from the http instance (...),
  # we create a fake url here. Surely this is a horrible, horrible idea?
  def normalized_oauth_uri(http)
    uri = URI.parse(normalize_uri.path)
    uri.host = http.address
    uri.port = http.port

    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
    headers = (self.options[:head] ||= {})
    headers['Authorization'] = @oauth_helper.header
  end

  def set_oauth_body
    raise NotImplementedError, 'please use the set_oauth_header method instead'
  end

  def set_oauth_query_string
    raise NotImplementedError, 'please use the set_oauth_header method instead'
  end

end