zmalltalker-oauth 0.3.1.7

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 (80) hide show
  1. data/History.txt +61 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +79 -0
  4. data/README.rdoc +71 -0
  5. data/Rakefile +36 -0
  6. data/TODO +30 -0
  7. data/bin/oauth +5 -0
  8. data/examples/yql.rb +44 -0
  9. data/lib/oauth.rb +5 -0
  10. data/lib/oauth/cli.rb +231 -0
  11. data/lib/oauth/client.rb +4 -0
  12. data/lib/oauth/client/action_controller_request.rb +53 -0
  13. data/lib/oauth/client/helper.rb +71 -0
  14. data/lib/oauth/client/net_http.rb +80 -0
  15. data/lib/oauth/consumer.rb +253 -0
  16. data/lib/oauth/helper.rb +55 -0
  17. data/lib/oauth/oauth.rb +7 -0
  18. data/lib/oauth/oauth_test_helper.rb +25 -0
  19. data/lib/oauth/request_proxy.rb +24 -0
  20. data/lib/oauth/request_proxy/action_controller_request.rb +63 -0
  21. data/lib/oauth/request_proxy/base.rb +157 -0
  22. data/lib/oauth/request_proxy/jabber_request.rb +41 -0
  23. data/lib/oauth/request_proxy/mock_request.rb +44 -0
  24. data/lib/oauth/request_proxy/net_http.rb +65 -0
  25. data/lib/oauth/request_proxy/rack_request.rb +40 -0
  26. data/lib/oauth/server.rb +66 -0
  27. data/lib/oauth/signature.rb +28 -0
  28. data/lib/oauth/signature/base.rb +91 -0
  29. data/lib/oauth/signature/hmac/base.rb +16 -0
  30. data/lib/oauth/signature/hmac/md5.rb +9 -0
  31. data/lib/oauth/signature/hmac/rmd160.rb +9 -0
  32. data/lib/oauth/signature/hmac/sha1.rb +10 -0
  33. data/lib/oauth/signature/hmac/sha2.rb +9 -0
  34. data/lib/oauth/signature/md5.rb +13 -0
  35. data/lib/oauth/signature/plaintext.rb +23 -0
  36. data/lib/oauth/signature/rsa/sha1.rb +45 -0
  37. data/lib/oauth/signature/sha1.rb +13 -0
  38. data/lib/oauth/token.rb +7 -0
  39. data/lib/oauth/tokens/access_token.rb +68 -0
  40. data/lib/oauth/tokens/consumer_token.rb +32 -0
  41. data/lib/oauth/tokens/request_token.rb +28 -0
  42. data/lib/oauth/tokens/server_token.rb +9 -0
  43. data/lib/oauth/tokens/token.rb +17 -0
  44. data/lib/oauth/version.rb +3 -0
  45. data/oauth.gemspec +49 -0
  46. data/script/destroy +14 -0
  47. data/script/generate +14 -0
  48. data/script/txt2html +74 -0
  49. data/setup.rb +1585 -0
  50. data/tasks/deployment.rake +34 -0
  51. data/tasks/environment.rake +7 -0
  52. data/tasks/website.rake +17 -0
  53. data/test/cases/oauth_case.rb +19 -0
  54. data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
  55. data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
  56. data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
  57. data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
  58. data/test/keys/rsa.cert +11 -0
  59. data/test/keys/rsa.pem +16 -0
  60. data/test/test_access_token.rb +28 -0
  61. data/test/test_action_controller_request_proxy.rb +45 -0
  62. data/test/test_consumer.rb +331 -0
  63. data/test/test_helper.rb +19 -0
  64. data/test/test_hmac_sha1.rb +21 -0
  65. data/test/test_net_http_client.rb +174 -0
  66. data/test/test_net_http_request_proxy.rb +38 -0
  67. data/test/test_rack_request_proxy.rb +40 -0
  68. data/test/test_request_token.rb +53 -0
  69. data/test/test_rsa_sha1.rb +59 -0
  70. data/test/test_server.rb +40 -0
  71. data/test/test_signature.rb +11 -0
  72. data/test/test_signature_base.rb +32 -0
  73. data/test/test_signature_plain_text.rb +31 -0
  74. data/test/test_token.rb +14 -0
  75. data/website/index.html +87 -0
  76. data/website/index.txt +73 -0
  77. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  78. data/website/stylesheets/screen.css +138 -0
  79. data/website/template.rhtml +48 -0
  80. metadata +209 -0
@@ -0,0 +1,4 @@
1
+ module OAuth
2
+ module Client
3
+ end
4
+ end
@@ -0,0 +1,53 @@
1
+ require 'oauth/client/helper'
2
+ require 'oauth/request_proxy/action_controller_request'
3
+ require 'action_controller/test_process'
4
+
5
+ module ActionController
6
+ class Base
7
+ def process_with_oauth(request, response=nil)
8
+ request.apply_oauth!
9
+ process_without_oauth(request, response)
10
+ end
11
+
12
+ alias_method_chain :process, :oauth
13
+ end
14
+
15
+ class TestRequest
16
+ def self.use_oauth=(bool)
17
+ @use_oauth = bool
18
+ end
19
+
20
+ def self.use_oauth?
21
+ @use_oauth
22
+ end
23
+
24
+ def configure_oauth(consumer = nil, token = nil, options = {})
25
+ @oauth_options = { :consumer => consumer,
26
+ :token => token,
27
+ :scheme => 'header',
28
+ :signature_method => nil,
29
+ :nonce => nil,
30
+ :timestamp => nil }.merge(options)
31
+ end
32
+
33
+ def apply_oauth!
34
+ return unless ActionController::TestRequest.use_oauth? && @oauth_options
35
+
36
+ @oauth_helper = OAuth::Client::Helper.new(self, @oauth_options.merge(:request_uri => request_uri))
37
+
38
+ self.send("set_oauth_#{@oauth_options[:scheme]}")
39
+ end
40
+
41
+ def set_oauth_header
42
+ env['Authorization'] = @oauth_helper.header
43
+ end
44
+
45
+ def set_oauth_parameters
46
+ @query_parameters = @oauth_helper.parameters_with_oauth
47
+ @query_parameters.merge!(:oauth_signature => @oauth_helper.signature)
48
+ end
49
+
50
+ def set_oauth_query_string
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,71 @@
1
+ require 'oauth/client'
2
+ require 'oauth/consumer'
3
+ require 'oauth/helper'
4
+ require 'oauth/token'
5
+ require 'oauth/signature/hmac/sha1'
6
+
7
+ module OAuth::Client
8
+ class Helper
9
+ include OAuth::Helper
10
+
11
+ def initialize(request, options = {})
12
+ @request = request
13
+ @options = options
14
+ @options[:signature_method] ||= 'HMAC-SHA1'
15
+ end
16
+
17
+ def options
18
+ @options
19
+ end
20
+
21
+ def nonce
22
+ options[:nonce] ||= generate_key
23
+ end
24
+
25
+ def timestamp
26
+ options[:timestamp] ||= generate_timestamp
27
+ end
28
+
29
+ def oauth_parameters
30
+ {
31
+ 'oauth_consumer_key' => options[:consumer].key,
32
+ 'oauth_token' => options[:token] ? options[:token].token : '',
33
+ 'oauth_signature_method' => options[:signature_method],
34
+ 'oauth_timestamp' => timestamp,
35
+ 'oauth_nonce' => nonce,
36
+ 'oauth_version' => '1.0'
37
+ }.reject { |k,v| v.to_s == "" }
38
+ end
39
+
40
+ def signature(extra_options = {})
41
+ OAuth::Signature.sign(@request, { :uri => options[:request_uri],
42
+ :consumer => options[:consumer],
43
+ :token => options[:token] }.merge(extra_options) )
44
+ end
45
+
46
+ def signature_base_string(extra_options = {})
47
+ OAuth::Signature.signature_base_string(@request, { :uri => options[:request_uri],
48
+ :consumer => options[:consumer],
49
+ :token => options[:token],
50
+ :parameters => oauth_parameters}.merge(extra_options) )
51
+ end
52
+
53
+ def header
54
+ parameters = oauth_parameters
55
+ parameters.merge!('oauth_signature' => signature(options.merge(:parameters => parameters)))
56
+
57
+ header_params_str = parameters.map { |k,v| "#{k}=\"#{escape(v)}\"" }.join(', ')
58
+
59
+ realm = "realm=\"#{options[:realm]}\", " if options[:realm]
60
+ "OAuth #{realm}#{header_params_str}"
61
+ end
62
+
63
+ def parameters
64
+ OAuth::RequestProxy.proxy(@request).parameters
65
+ end
66
+
67
+ def parameters_with_oauth
68
+ oauth_parameters.merge(parameters)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,80 @@
1
+ require 'oauth/helper'
2
+ require 'oauth/client/helper'
3
+ require 'oauth/request_proxy/net_http'
4
+
5
+ class Net::HTTPRequest
6
+ include OAuth::Helper
7
+
8
+ def oauth!(http, consumer = nil, token = nil, options = {})
9
+ options = { :request_uri => oauth_full_request_uri(http),
10
+ :consumer => consumer,
11
+ :token => token,
12
+ :scheme => 'header',
13
+ :signature_method => nil,
14
+ :nonce => nil,
15
+ :timestamp => nil }.merge(options)
16
+
17
+ @oauth_helper = OAuth::Client::Helper.new(self, options)
18
+ self.send("set_oauth_#{options[:scheme]}")
19
+ end
20
+
21
+ def signature_base_string(http, consumer = nil, token = nil, options = {})
22
+ options = { :request_uri => oauth_full_request_uri(http),
23
+ :consumer => consumer,
24
+ :token => token,
25
+ :scheme => 'header',
26
+ :signature_method => nil,
27
+ :nonce => nil,
28
+ :timestamp => nil }.merge(options)
29
+
30
+ OAuth::Client::Helper.new(self, options).signature_base_string
31
+ end
32
+
33
+ def oauth_helper
34
+ @oauth_helper
35
+ end
36
+
37
+ private
38
+
39
+ def oauth_full_request_uri(http)
40
+ uri = URI.parse(self.path)
41
+ uri.host = http.address
42
+ uri.port = http.port
43
+
44
+ if http.respond_to?(:use_ssl?) && http.use_ssl?
45
+ uri.scheme = "https"
46
+ else
47
+ uri.scheme = "http"
48
+ end
49
+
50
+ uri.to_s
51
+ end
52
+
53
+ def set_oauth_header
54
+ self['Authorization'] = @oauth_helper.header
55
+ end
56
+
57
+ # FIXME: if you're using a POST body and query string parameters, using this
58
+ # method will convert those parameters on the query string into parameters in
59
+ # the body. this is broken, and should be fixed.
60
+ def set_oauth_body
61
+ self.set_form_data(@oauth_helper.parameters_with_oauth)
62
+ params_with_sig = @oauth_helper.parameters.merge('oauth_signature' => @oauth_helper.signature)
63
+ self.set_form_data(params_with_sig)
64
+ end
65
+
66
+ def set_oauth_query_string
67
+ oauth_params_str = @oauth_helper.oauth_parameters.map { |k,v| [escape(k), escape(v)] * "=" }.join("&")
68
+
69
+ uri = URI.parse(path)
70
+ if !uri.query || uri.query == ''
71
+ uri.query = oauth_params_str
72
+ else
73
+ uri.query = uri.query + "&" + oauth_params_str
74
+ end
75
+
76
+ @path = uri.to_s
77
+
78
+ @path << "&oauth_signature=#{escape(@oauth_helper.signature)}"
79
+ end
80
+ end
@@ -0,0 +1,253 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'oauth/client/net_http'
4
+ module OAuth
5
+ class Consumer
6
+
7
+ @@default_options = {
8
+ # Signature method used by server. Defaults to HMAC-SHA1
9
+ :signature_method => 'HMAC-SHA1',
10
+
11
+ # default paths on site. These are the same as the defaults set up by the generators
12
+ :request_token_path => '/oauth/request_token',
13
+ :authorize_path => '/oauth/authorize',
14
+ :access_token_path => '/oauth/access_token',
15
+
16
+ # How do we send the oauth values to the server see
17
+ # http://oauth.net/core/1.0/#consumer_req_param for more info
18
+ #
19
+ # Possible values:
20
+ #
21
+ # :header - via the Authorize header (Default) ( option 1. in spec)
22
+ # :body - url form encoded in body of POST request ( option 2. in spec)
23
+ # :query_string - via the query part of the url ( option 3. in spec)
24
+ :scheme => :header,
25
+
26
+ # Default http method used for OAuth Token Requests (defaults to :post)
27
+ :http_method => :post,
28
+
29
+ :oauth_version => "1.0"
30
+ }
31
+
32
+ attr_accessor :options, :key, :secret
33
+ attr_writer :site, :http
34
+
35
+ # Create a new consumer instance by passing it a configuration hash:
36
+ #
37
+ # @consumer = OAuth::Consumer.new(key, secret, {
38
+ # :site => "http://term.ie",
39
+ # :scheme => :header,
40
+ # :http_method => :post,
41
+ # :request_token_path => "/oauth/example/request_token.php",
42
+ # :access_token_path => "/oauth/example/access_token.php",
43
+ # :authorize_path => "/oauth/example/authorize.php"
44
+ # })
45
+ #
46
+ # Start the process by requesting a token
47
+ #
48
+ # @request_token = @consumer.get_request_token
49
+ # session[:request_token] = @request_token
50
+ # redirect_to @request_token.authorize_url
51
+ #
52
+ # When user returns create an access_token
53
+ #
54
+ # @access_token = @request_token.get_access_token
55
+ # @photos=@access_token.get('/photos.xml')
56
+ #
57
+ def initialize(consumer_key, consumer_secret, options = {})
58
+ @key = consumer_key
59
+ @secret = consumer_secret
60
+
61
+ # ensure that keys are symbols
62
+ @options = @@default_options.merge(options.inject({}) { |opts, (key, value)|
63
+ opts[key.to_sym] = value
64
+ opts
65
+ })
66
+ end
67
+
68
+ # The default http method
69
+ def http_method
70
+ @http_method ||= @options[:http_method] || :post
71
+ end
72
+
73
+ # The HTTP object for the site. The HTTP Object is what you get when you do Net::HTTP.new
74
+ def http
75
+ @http ||= create_http
76
+ end
77
+
78
+ # Contains the root URI for this site
79
+ def uri(custom_uri = nil)
80
+ if custom_uri
81
+ @uri = custom_uri
82
+ @http = create_http # yike, oh well. less intrusive this way
83
+ else # if no custom passed, we use existing, which, if unset, is set to site uri
84
+ @uri ||= URI.parse(site)
85
+ end
86
+ end
87
+
88
+ # Makes a request to the service for a new OAuth::RequestToken
89
+ #
90
+ # @request_token = @consumer.get_request_token
91
+ #
92
+ def get_request_token(request_options = {}, *arguments)
93
+ response = token_request(http_method, (request_token_url? ? request_token_url : request_token_path), nil, request_options, *arguments)
94
+ OAuth::RequestToken.new(self, response[:oauth_token], response[:oauth_token_secret])
95
+ end
96
+
97
+ # Creates, signs and performs an http request.
98
+ # It's recommended to use the OAuth::Token classes to set this up correctly.
99
+ # The arguments parameters are a hash or string encoded set of parameters if it's a post request as well as optional http headers.
100
+ #
101
+ # @consumer.request(:get, '/people', @token, { :scheme => :query_string })
102
+ # @consumer.request(:post, '/people', @token, {}, @person.to_xml, { 'Content-Type' => 'application/xml' })
103
+ #
104
+ def request(http_method, path, token = nil, request_options = {}, *arguments)
105
+ if path =~ /^\//
106
+ _http = http
107
+ else
108
+ _http = create_http(path)
109
+ _uri = URI.parse(path)
110
+ path = "#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}"
111
+ end
112
+
113
+ _http.request(create_signed_request(http_method, path, token, request_options, *arguments))
114
+ end
115
+
116
+ # Creates and signs an http request.
117
+ # It's recommended to use the Token classes to set this up correctly
118
+ def create_signed_request(http_method, path, token = nil,request_options = {}, *arguments)
119
+ request = create_http_request(http_method, path, *arguments)
120
+ sign!(request, token, request_options)
121
+ request
122
+ end
123
+
124
+ # Creates a request and parses the result as url_encoded. This is used internally for the RequestToken and AccessToken requests.
125
+ def token_request(http_method, path, token = nil, request_options = {}, *arguments)
126
+ response = request(http_method, path, token, request_options, *arguments)
127
+ if response.code == "200"
128
+ CGI.parse(response.body).inject({}) { |h,(k,v)| h[k.to_sym] = v.first; h }
129
+ else
130
+ response.error!
131
+ end
132
+ end
133
+
134
+ # Sign the Request object. Use this if you have an externally generated http request object you want to sign.
135
+ def sign!(request, token=nil, request_options = {})
136
+ request.oauth!(http, self, token, options.merge(request_options))
137
+ end
138
+
139
+ # Return the signature_base_string
140
+ def signature_base_string(request, token=nil, request_options = {})
141
+ request.signature_base_string(http, self, token, options.merge(request_options))
142
+ end
143
+
144
+ def site
145
+ @options[:site].to_s
146
+ end
147
+
148
+ def scheme
149
+ @options[:scheme]
150
+ end
151
+
152
+ def request_token_path
153
+ @options[:request_token_path]
154
+ end
155
+
156
+ def authorize_path
157
+ @options[:authorize_path]
158
+ end
159
+
160
+ def access_token_path
161
+ @options[:access_token_path]
162
+ end
163
+
164
+ # TODO this is ugly, rewrite
165
+ def request_token_url
166
+ @options[:request_token_url] || site + request_token_path
167
+ end
168
+
169
+ def request_token_url?
170
+ @options[:request_token_url]!=nil
171
+ end
172
+
173
+ def authorize_url
174
+ @options[:authorize_url] || site + authorize_path
175
+ end
176
+
177
+ def authorize_url?
178
+ @options[:authorize_url]!=nil
179
+ end
180
+
181
+ def access_token_url
182
+ @options[:access_token_url] || site + access_token_path
183
+ end
184
+
185
+ def access_token_url?
186
+ @options[:access_token_url]!=nil
187
+ end
188
+
189
+ protected
190
+
191
+ # Instantiates the http object
192
+ def create_http(_url = nil)
193
+ if _url.nil? || _url[0] =~ /^\//
194
+ our_uri = URI.parse(site)
195
+ else
196
+ our_uri = URI.parse(_url)
197
+ end
198
+
199
+ http_object = Net::HTTP.new(our_uri.host, our_uri.port)
200
+ if our_uri.scheme == 'https'
201
+ http_object.use_ssl = true
202
+ http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE
203
+ end
204
+ http_object.use_ssl = true if our_uri.scheme == "https"
205
+
206
+ http_object
207
+ end
208
+
209
+ # create the http request object for a given http_method and path
210
+ def create_http_request(http_method, path, *arguments)
211
+ http_method = http_method.to_sym
212
+
213
+ if [:post, :put].include?(http_method)
214
+ data = arguments.shift
215
+ end
216
+
217
+ headers = arguments.first.is_a?(Hash) ? arguments.shift : {}
218
+
219
+ case http_method
220
+ when :post
221
+ request = Net::HTTP::Post.new(path,headers)
222
+ request["Content-Length"] = 0 # Default to 0
223
+ when :put
224
+ request = Net::HTTP::Put.new(path,headers)
225
+ request["Content-Length"] = 0 # Default to 0
226
+ when :get
227
+ request = Net::HTTP::Get.new(path,headers)
228
+ when :delete
229
+ request = Net::HTTP::Delete.new(path,headers)
230
+ when :head
231
+ request = Net::HTTP::Head.new(path,headers)
232
+ else
233
+ raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}"
234
+ end
235
+
236
+ if data.is_a?(Hash)
237
+ request.set_form_data(data)
238
+ elsif data
239
+ request.body = data.to_s
240
+ request["Content-Length"] = request.body.length
241
+ end
242
+
243
+ request
244
+ end
245
+
246
+ # Unset cached http instance because it cannot be marshalled when
247
+ # it has already been used and use_ssl is set to true
248
+ def marshal_dump(*args)
249
+ @http = nil
250
+ self
251
+ end
252
+ end
253
+ end