|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require "em-http" |
4 | 3 | require "oauth/helper" |
5 | | -require "oauth/request_proxy/em_http_request" |
| 4 | +require "oauth/optional" |
6 | 5 |
|
7 | | -# Extensions for em-http so that we can use consumer.sign! with an EventMachine::HttpClient |
8 | | -# instance. This is purely syntactic sugar. |
9 | | -module EventMachine |
10 | | - class HttpClient |
11 | | - attr_reader :oauth_helper |
| 6 | +if OAuth::Optional.em_http_available? |
| 7 | + require "oauth/request_proxy/em_http_request" |
12 | 8 |
|
13 | | - # Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting |
14 | | - # this may add a header, additional query string parameters, or additional POST body parameters. |
15 | | - # The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
16 | | - # header. |
17 | | - # |
18 | | - # * http - Configured Net::HTTP instance, ignored in this scenario except for getting host. |
19 | | - # * consumer - OAuth::Consumer instance |
20 | | - # * token - OAuth::Token instance |
21 | | - # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
22 | | - # +signature_method+, +nonce+, +timestamp+) |
23 | | - # |
24 | | - # This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version. |
25 | | - # |
26 | | - # See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1] |
27 | | - def oauth!(http, consumer = nil, token = nil, options = {}) |
28 | | - options = { |
29 | | - request_uri: normalized_oauth_uri(http), |
30 | | - consumer: consumer, |
31 | | - token: token, |
32 | | - scheme: "header", |
33 | | - signature_method: nil, |
34 | | - nonce: nil, |
35 | | - timestamp: nil, |
36 | | - }.merge(options) |
| 9 | + # Extensions for em-http so that we can use consumer.sign! with an EventMachine::HttpClient |
| 10 | + # instance. This is purely syntactic sugar. |
| 11 | + module EventMachine |
| 12 | + class HttpClient |
| 13 | + attr_reader :oauth_helper |
37 | 14 |
|
38 | | - @oauth_helper = OAuth::Client::Helper.new(self, options) |
39 | | - __send__(:"set_oauth_#{options[:scheme]}") |
40 | | - end |
| 15 | + # Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting |
| 16 | + # this may add a header, additional query string parameters, or additional POST body parameters. |
| 17 | + # The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
| 18 | + # header. |
| 19 | + # |
| 20 | + # * http - Configured Net::HTTP instance, ignored in this scenario except for getting host. |
| 21 | + # * consumer - OAuth::Consumer instance |
| 22 | + # * token - OAuth::Token instance |
| 23 | + # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
| 24 | + # +signature_method+, +nonce+, +timestamp+) |
| 25 | + # |
| 26 | + # This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version. |
| 27 | + # |
| 28 | + # See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1] |
| 29 | + def oauth!(http, consumer = nil, token = nil, options = {}) |
| 30 | + options = { |
| 31 | + request_uri: normalized_oauth_uri(http), |
| 32 | + consumer: consumer, |
| 33 | + token: token, |
| 34 | + scheme: "header", |
| 35 | + signature_method: nil, |
| 36 | + nonce: nil, |
| 37 | + timestamp: nil, |
| 38 | + }.merge(options) |
41 | 39 |
|
42 | | - # Create a string suitable for signing for an HTTP request. This process involves parameter |
43 | | - # normalization as specified in the OAuth specification. The exact normalization also depends |
44 | | - # on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request |
45 | | - # itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
46 | | - # header. |
47 | | - # |
48 | | - # * http - Configured Net::HTTP instance |
49 | | - # * consumer - OAuth::Consumer instance |
50 | | - # * token - OAuth::Token instance |
51 | | - # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
52 | | - # +signature_method+, +nonce+, +timestamp+) |
53 | | - # |
54 | | - # See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1] |
55 | | - def signature_base_string(http, consumer = nil, token = nil, options = {}) |
56 | | - options = { |
57 | | - request_uri: normalized_oauth_uri(http), |
58 | | - consumer: consumer, |
59 | | - token: token, |
60 | | - scheme: "header", |
61 | | - signature_method: nil, |
62 | | - nonce: nil, |
63 | | - timestamp: nil, |
64 | | - }.merge(options) |
| 40 | + @oauth_helper = OAuth::Client::Helper.new(self, options) |
| 41 | + __send__(:"set_oauth_#{options[:scheme]}") |
| 42 | + end |
65 | 43 |
|
66 | | - OAuth::Client::Helper.new(self, options).signature_base_string |
67 | | - end |
| 44 | + # Create a string suitable for signing for an HTTP request. This process involves parameter |
| 45 | + # normalization as specified in the OAuth specification. The exact normalization also depends |
| 46 | + # on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request |
| 47 | + # itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
| 48 | + # header. |
| 49 | + # |
| 50 | + # * http - Configured Net::HTTP instance |
| 51 | + # * consumer - OAuth::Consumer instance |
| 52 | + # * token - OAuth::Token instance |
| 53 | + # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
| 54 | + # +signature_method+, +nonce+, +timestamp+) |
| 55 | + # |
| 56 | + # See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1] |
| 57 | + def signature_base_string(http, consumer = nil, token = nil, options = {}) |
| 58 | + options = { |
| 59 | + request_uri: normalized_oauth_uri(http), |
| 60 | + consumer: consumer, |
| 61 | + token: token, |
| 62 | + scheme: "header", |
| 63 | + signature_method: nil, |
| 64 | + nonce: nil, |
| 65 | + timestamp: nil, |
| 66 | + }.merge(options) |
68 | 67 |
|
69 | | - # This code was lifted from the em-http-request because it was removed from |
70 | | - # the gem June 19, 2010 |
71 | | - # see: https://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b |
72 | | - def normalize_uri |
73 | | - @normalized_uri ||= begin |
74 | | - uri = @conn.dup |
75 | | - encoded_query = encode_query(@conn, @req[:query]) |
76 | | - path, query = encoded_query.split("?", 2) |
77 | | - uri.query = query unless encoded_query.empty? |
78 | | - uri.path = path |
79 | | - uri |
| 68 | + OAuth::Client::Helper.new(self, options).signature_base_string |
80 | 69 | end |
81 | | - end |
82 | 70 |
|
83 | | - protected |
| 71 | + # This code was lifted from the em-http-request because it was removed from |
| 72 | + # the gem June 19, 2010 |
| 73 | + # see: https://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b |
| 74 | + def normalize_uri |
| 75 | + @normalized_uri ||= begin |
| 76 | + uri = @conn.dup |
| 77 | + encoded_query = encode_query(@conn, @req[:query]) |
| 78 | + path, query = encoded_query.split("?", 2) |
| 79 | + uri.query = query unless encoded_query.empty? |
| 80 | + uri.path = path |
| 81 | + uri |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + protected |
84 | 86 |
|
85 | | - def combine_query(path, query, uri_query) |
86 | | - combined_query = if query.is_a?(Hash) |
87 | | - query.map { |k, v| encode_param(k, v) }.join("&") |
88 | | - else |
89 | | - query.to_s |
| 87 | + def combine_query(path, query, uri_query) |
| 88 | + combined_query = if query.is_a?(Hash) |
| 89 | + query.map { |k, v| encode_param(k, v) }.join("&") |
| 90 | + else |
| 91 | + query.to_s |
| 92 | + end |
| 93 | + combined_query = [combined_query, uri_query].reject(&:empty?).join("&") unless uri_query.to_s.empty? |
| 94 | + combined_query.to_s.empty? ? path : "#{path}?#{combined_query}" |
90 | 95 | end |
91 | | - combined_query = [combined_query, uri_query].reject(&:empty?).join("&") unless uri_query.to_s.empty? |
92 | | - combined_query.to_s.empty? ? path : "#{path}?#{combined_query}" |
93 | | - end |
94 | 96 |
|
95 | | - # Since we expect to get the host etc details from the http instance (...), |
96 | | - # we create a fake url here. Surely this is a horrible, horrible idea? |
97 | | - def normalized_oauth_uri(http) |
98 | | - uri = URI.parse(normalize_uri.path) |
99 | | - uri.host = http.address |
100 | | - uri.port = http.port |
| 97 | + # Since we expect to get the host etc details from the http instance (...), |
| 98 | + # we create a fake url here. Surely this is a horrible, horrible idea? |
| 99 | + def normalized_oauth_uri(http) |
| 100 | + uri = URI.parse(normalize_uri.path) |
| 101 | + uri.host = http.address |
| 102 | + uri.port = http.port |
101 | 103 |
|
102 | | - uri.scheme = if http.respond_to?(:use_ssl?) && http.use_ssl? |
103 | | - "https" |
104 | | - else |
105 | | - "http" |
| 104 | + uri.scheme = if http.respond_to?(:use_ssl?) && http.use_ssl? |
| 105 | + "https" |
| 106 | + else |
| 107 | + "http" |
| 108 | + end |
| 109 | + uri.to_s |
106 | 110 | end |
107 | | - uri.to_s |
108 | | - end |
109 | 111 |
|
110 | | - def set_oauth_header |
111 | | - req[:head] ||= {} |
112 | | - req[:head].merge!("Authorization" => @oauth_helper.header) |
113 | | - end |
| 112 | + def set_oauth_header |
| 113 | + req[:head] ||= {} |
| 114 | + req[:head].merge!("Authorization" => @oauth_helper.header) |
| 115 | + end |
114 | 116 |
|
115 | | - def set_oauth_body |
116 | | - raise NotImplementedError, "please use the set_oauth_header method instead" |
117 | | - end |
| 117 | + def set_oauth_body |
| 118 | + raise NotImplementedError, "please use the set_oauth_header method instead" |
| 119 | + end |
118 | 120 |
|
119 | | - def set_oauth_query_string |
120 | | - raise NotImplementedError, "please use the set_oauth_header method instead" |
| 121 | + def set_oauth_query_string |
| 122 | + raise NotImplementedError, "please use the set_oauth_header method instead" |
| 123 | + end |
121 | 124 | end |
122 | 125 | end |
123 | 126 | end |
0 commit comments