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.
- data/History.txt +61 -0
- data/License.txt +20 -0
- data/Manifest.txt +79 -0
- data/README.rdoc +71 -0
- data/Rakefile +36 -0
- data/TODO +30 -0
- data/bin/oauth +5 -0
- data/examples/yql.rb +44 -0
- data/lib/oauth.rb +5 -0
- data/lib/oauth/cli.rb +231 -0
- data/lib/oauth/client.rb +4 -0
- data/lib/oauth/client/action_controller_request.rb +53 -0
- data/lib/oauth/client/helper.rb +71 -0
- data/lib/oauth/client/net_http.rb +80 -0
- data/lib/oauth/consumer.rb +253 -0
- data/lib/oauth/helper.rb +55 -0
- data/lib/oauth/oauth.rb +7 -0
- data/lib/oauth/oauth_test_helper.rb +25 -0
- data/lib/oauth/request_proxy.rb +24 -0
- data/lib/oauth/request_proxy/action_controller_request.rb +63 -0
- data/lib/oauth/request_proxy/base.rb +157 -0
- data/lib/oauth/request_proxy/jabber_request.rb +41 -0
- data/lib/oauth/request_proxy/mock_request.rb +44 -0
- data/lib/oauth/request_proxy/net_http.rb +65 -0
- data/lib/oauth/request_proxy/rack_request.rb +40 -0
- data/lib/oauth/server.rb +66 -0
- data/lib/oauth/signature.rb +28 -0
- data/lib/oauth/signature/base.rb +91 -0
- data/lib/oauth/signature/hmac/base.rb +16 -0
- data/lib/oauth/signature/hmac/md5.rb +9 -0
- data/lib/oauth/signature/hmac/rmd160.rb +9 -0
- data/lib/oauth/signature/hmac/sha1.rb +10 -0
- data/lib/oauth/signature/hmac/sha2.rb +9 -0
- data/lib/oauth/signature/md5.rb +13 -0
- data/lib/oauth/signature/plaintext.rb +23 -0
- data/lib/oauth/signature/rsa/sha1.rb +45 -0
- data/lib/oauth/signature/sha1.rb +13 -0
- data/lib/oauth/token.rb +7 -0
- data/lib/oauth/tokens/access_token.rb +68 -0
- data/lib/oauth/tokens/consumer_token.rb +32 -0
- data/lib/oauth/tokens/request_token.rb +28 -0
- data/lib/oauth/tokens/server_token.rb +9 -0
- data/lib/oauth/tokens/token.rb +17 -0
- data/lib/oauth/version.rb +3 -0
- data/oauth.gemspec +49 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +74 -0
- data/setup.rb +1585 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- data/tasks/website.rake +17 -0
- data/test/cases/oauth_case.rb +19 -0
- data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
- data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
- data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
- data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
- data/test/keys/rsa.cert +11 -0
- data/test/keys/rsa.pem +16 -0
- data/test/test_access_token.rb +28 -0
- data/test/test_action_controller_request_proxy.rb +45 -0
- data/test/test_consumer.rb +331 -0
- data/test/test_helper.rb +19 -0
- data/test/test_hmac_sha1.rb +21 -0
- data/test/test_net_http_client.rb +174 -0
- data/test/test_net_http_request_proxy.rb +38 -0
- data/test/test_rack_request_proxy.rb +40 -0
- data/test/test_request_token.rb +53 -0
- data/test/test_rsa_sha1.rb +59 -0
- data/test/test_server.rb +40 -0
- data/test/test_signature.rb +11 -0
- data/test/test_signature_base.rb +32 -0
- data/test/test_signature_plain_text.rb +31 -0
- data/test/test_token.rb +14 -0
- data/website/index.html +87 -0
- data/website/index.txt +73 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +48 -0
- metadata +209 -0
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'oauth/request_proxy/action_controller_request.rb'
|
3
|
+
require 'action_controller'
|
4
|
+
require 'action_controller/test_process'
|
5
|
+
|
6
|
+
class ActionControllerRequestProxyTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def request_proxy(parameters={})
|
9
|
+
request = ActionController::TestRequest.new({}, parameters)
|
10
|
+
request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
|
11
|
+
yield request if block_given?
|
12
|
+
OAuth::RequestProxy.proxy(request)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_parameter_keys_should_preserve_brackets_from_hash
|
16
|
+
assert_equal(
|
17
|
+
[["message[body]", "This is a test"]],
|
18
|
+
request_proxy({ :message => { :body => 'This is a test' }}).parameters_for_signature
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_parameter_values_with_amps_should_not_break_parameter_parsing
|
23
|
+
assert_equal(
|
24
|
+
[['message[body]', 'http://foo.com/?a=b&c=d']],
|
25
|
+
request_proxy({ :message => { :body => 'http://foo.com/?a=b&c=d'}}).parameters_for_signature
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_parameter_keys_should_preserve_brackets_from_array
|
30
|
+
assert_equal(
|
31
|
+
[["foo[]", "123"], ["foo[]", "456"]],
|
32
|
+
request_proxy({ :foo => [123, 456] }).parameters_for_signature.sort
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_query_string_parameter_values_should_be_cgi_unescaped
|
37
|
+
request = request_proxy do |r|
|
38
|
+
r.env['QUERY_STRING'] = 'url=http%3A%2F%2Ffoo.com%2F%3Fa%3Db%26c%3Dd'
|
39
|
+
end
|
40
|
+
assert_equal(
|
41
|
+
[['url', 'http://foo.com/?a=b&c=d']],
|
42
|
+
request.parameters_for_signature.sort
|
43
|
+
)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,331 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require 'oauth/consumer'
|
3
|
+
require 'oauth/signature/rsa/sha1'
|
4
|
+
|
5
|
+
|
6
|
+
# This performs testing against Andy Smith's test server http://term.ie/oauth/example/
|
7
|
+
# Thanks Andy.
|
8
|
+
# This also means you have to be online to be able to run these.
|
9
|
+
class ConsumerTest < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@consumer=OAuth::Consumer.new(
|
12
|
+
'consumer_key_86cad9', '5888bf0345e5d237',
|
13
|
+
{
|
14
|
+
:site=>"http://blabla.bla",
|
15
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
16
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
17
|
+
:authorize_path=>"/oauth/example/authorize.php",
|
18
|
+
:scheme=>:header,
|
19
|
+
:http_method=>:get
|
20
|
+
})
|
21
|
+
@token = OAuth::ConsumerToken.new(@consumer,'token_411a7f', '3196ffd991c8ebdb')
|
22
|
+
@request_uri = URI.parse('http://example.com/test?key=value')
|
23
|
+
@request_parameters = { 'key' => 'value' }
|
24
|
+
@nonce = 225579211881198842005988698334675835446
|
25
|
+
@timestamp = "1199645624"
|
26
|
+
@consumer.http=Net::HTTP.new(@request_uri.host, @request_uri.port)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_initializer
|
30
|
+
assert_equal "consumer_key_86cad9",@consumer.key
|
31
|
+
assert_equal "5888bf0345e5d237",@consumer.secret
|
32
|
+
assert_equal "http://blabla.bla",@consumer.site
|
33
|
+
assert_equal "/oauth/example/request_token.php",@consumer.request_token_path
|
34
|
+
assert_equal "/oauth/example/access_token.php",@consumer.access_token_path
|
35
|
+
assert_equal "http://blabla.bla/oauth/example/request_token.php",@consumer.request_token_url
|
36
|
+
assert_equal "http://blabla.bla/oauth/example/access_token.php",@consumer.access_token_url
|
37
|
+
assert_equal "http://blabla.bla/oauth/example/authorize.php",@consumer.authorize_url
|
38
|
+
assert_equal :header,@consumer.scheme
|
39
|
+
assert_equal :get,@consumer.http_method
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_defaults
|
43
|
+
@consumer=OAuth::Consumer.new(
|
44
|
+
"key",
|
45
|
+
"secret",
|
46
|
+
{
|
47
|
+
:site=>"http://twitter.com"
|
48
|
+
})
|
49
|
+
assert_equal "key",@consumer.key
|
50
|
+
assert_equal "secret",@consumer.secret
|
51
|
+
assert_equal "http://twitter.com",@consumer.site
|
52
|
+
assert_equal "/oauth/request_token",@consumer.request_token_path
|
53
|
+
assert_equal "/oauth/access_token",@consumer.access_token_path
|
54
|
+
assert_equal "http://twitter.com/oauth/request_token",@consumer.request_token_url
|
55
|
+
assert_equal "http://twitter.com/oauth/access_token",@consumer.access_token_url
|
56
|
+
assert_equal "http://twitter.com/oauth/authorize",@consumer.authorize_url
|
57
|
+
assert_equal :header,@consumer.scheme
|
58
|
+
assert_equal :post,@consumer.http_method
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_override_paths
|
62
|
+
@consumer=OAuth::Consumer.new(
|
63
|
+
"key",
|
64
|
+
"secret",
|
65
|
+
{
|
66
|
+
:site=>"http://twitter.com",
|
67
|
+
:request_token_url=>"http://oauth.twitter.com/request_token",
|
68
|
+
:access_token_url=>"http://oauth.twitter.com/access_token",
|
69
|
+
:authorize_url=>"http://site.twitter.com/authorize"
|
70
|
+
})
|
71
|
+
assert_equal "key",@consumer.key
|
72
|
+
assert_equal "secret",@consumer.secret
|
73
|
+
assert_equal "http://twitter.com",@consumer.site
|
74
|
+
assert_equal "/oauth/request_token",@consumer.request_token_path
|
75
|
+
assert_equal "/oauth/access_token",@consumer.access_token_path
|
76
|
+
assert_equal "http://oauth.twitter.com/request_token",@consumer.request_token_url
|
77
|
+
assert_equal "http://oauth.twitter.com/access_token",@consumer.access_token_url
|
78
|
+
assert_equal "http://site.twitter.com/authorize",@consumer.authorize_url
|
79
|
+
assert_equal :header,@consumer.scheme
|
80
|
+
assert_equal :post,@consumer.http_method
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_that_signing_auth_headers_on_get_requests_works
|
84
|
+
request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
|
85
|
+
@token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
|
86
|
+
|
87
|
+
assert_equal 'GET', request.method
|
88
|
+
assert_equal '/test?key=value', request.path
|
89
|
+
assert_equal "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"1oO2izFav1GP4kEH2EskwXkCRFg%3D\", oauth_version=\"1.0\"".split(', ').sort, request['authorization'].split(', ').sort
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_that_setting_signature_method_on_consumer_effects_signing
|
93
|
+
require 'oauth/signature/plaintext'
|
94
|
+
request = Net::HTTP::Get.new(@request_uri.path)
|
95
|
+
consumer = @consumer.dup
|
96
|
+
consumer.options[:signature_method] = 'PLAINTEXT'
|
97
|
+
token = OAuth::ConsumerToken.new(consumer, 'token_411a7f', '3196ffd991c8ebdb')
|
98
|
+
token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
|
99
|
+
|
100
|
+
assert_no_match( /oauth_signature_method="HMAC-SHA1"/, request['authorization'])
|
101
|
+
assert_match( /oauth_signature_method="PLAINTEXT"/, request['authorization'])
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_that_setting_signature_method_on_consumer_effects_signature_base_string
|
105
|
+
require 'oauth/signature/plaintext'
|
106
|
+
request = Net::HTTP::Get.new(@request_uri.path)
|
107
|
+
consumer = @consumer.dup
|
108
|
+
consumer.options[:signature_method] = 'PLAINTEXT'
|
109
|
+
|
110
|
+
request = Net::HTTP::Get.new('/')
|
111
|
+
signature_base_string = consumer.signature_base_string(request)
|
112
|
+
|
113
|
+
assert_no_match( /HMAC-SHA1/, signature_base_string)
|
114
|
+
assert_equal( "#{consumer.secret}%26", signature_base_string)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_that_plaintext_signature_works
|
118
|
+
require 'oauth/signature/plaintext'
|
119
|
+
consumer = OAuth::Consumer.new("key", "secret",
|
120
|
+
:site => "http://term.ie", :signature_method => 'PLAINTEXT')
|
121
|
+
access_token = OAuth::AccessToken.new(consumer, 'accesskey', 'accesssecret')
|
122
|
+
response = access_token.get("/oauth/example/echo_api.php?echo=hello")
|
123
|
+
|
124
|
+
assert_equal 'echo=hello', response.body
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_that_signing_auth_headers_on_post_requests_works
|
128
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
129
|
+
request.set_form_data( @request_parameters )
|
130
|
+
@token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
|
131
|
+
# assert_equal "",request.oauth_helper.signature_base_string
|
132
|
+
|
133
|
+
assert_equal 'POST', request.method
|
134
|
+
assert_equal '/test', request.path
|
135
|
+
assert_equal 'key=value', request.body
|
136
|
+
assert_equal "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"26g7wHTtNO6ZWJaLltcueppHYiI%3D\", oauth_version=\"1.0\"".split(', ').sort, request['authorization'].split(', ').sort
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_that_signing_post_params_works
|
140
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
141
|
+
request.set_form_data( @request_parameters )
|
142
|
+
@token.sign!(request, {:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp})
|
143
|
+
|
144
|
+
assert_equal 'POST', request.method
|
145
|
+
assert_equal '/test', request.path
|
146
|
+
assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=iMZaUTbQof%2fHMFyIde%2bOIkhW5is%3d&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", request.body.split("&").sort.join("&")
|
147
|
+
assert_equal nil, request['authorization']
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_that_using_auth_headers_on_get_on_create_signed_requests_works
|
151
|
+
request=@consumer.create_signed_request(:get,@request_uri.path+ "?" + request_parameters_to_s,@token,{:nonce => @nonce, :timestamp => @timestamp},@request_parameters)
|
152
|
+
|
153
|
+
assert_equal 'GET', request.method
|
154
|
+
assert_equal '/test?key=value', request.path
|
155
|
+
expected_body = extract_sorted_array_from_authorization_spec("OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"1oO2izFav1GP4kEH2EskwXkCRFg%3D\", oauth_version=\"1.0\"")
|
156
|
+
actual_body = extract_sorted_array_from_authorization_spec(request['authorization'])
|
157
|
+
assert_equal(Set.new(expected_body), Set.new(actual_body))
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_that_using_auth_headers_on_post_on_create_signed_requests_works
|
161
|
+
request=@consumer.create_signed_request(:post,@request_uri.path,@token,{:nonce => @nonce, :timestamp => @timestamp},@request_parameters,{})
|
162
|
+
assert_equal 'POST', request.method
|
163
|
+
assert_equal '/test', request.path
|
164
|
+
assert_equal 'key=value', request.body
|
165
|
+
expected_body = extract_sorted_array_from_authorization_spec("OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"26g7wHTtNO6ZWJaLltcueppHYiI%3D\", oauth_version=\"1.0\"")
|
166
|
+
actual_body = extract_sorted_array_from_authorization_spec(request['authorization'])
|
167
|
+
assert_equal(Set.new(expected_body), Set.new(actual_body))
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_that_signing_post_params_works_2
|
171
|
+
request=@consumer.create_signed_request(:post,@request_uri.path,@token,{:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp},@request_parameters,{})
|
172
|
+
|
173
|
+
assert_equal 'POST', request.method
|
174
|
+
assert_equal '/test', request.path
|
175
|
+
assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=26g7wHTtNO6ZWJaLltcueppHYiI%3d&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", request.body.split("&").sort.join("&")
|
176
|
+
assert_equal nil, request['authorization']
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_step_by_step_token_request
|
180
|
+
@consumer=OAuth::Consumer.new(
|
181
|
+
"key",
|
182
|
+
"secret",
|
183
|
+
{
|
184
|
+
:site=>"http://term.ie",
|
185
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
186
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
187
|
+
:authorize_path=>"/oauth/example/authorize.php",
|
188
|
+
:scheme=>:header
|
189
|
+
})
|
190
|
+
options={:nonce=>'nonce',:timestamp=>Time.now.to_i.to_s}
|
191
|
+
|
192
|
+
request = Net::HTTP::Get.new("/oauth/example/request_token.php")
|
193
|
+
signature_base_string=@consumer.signature_base_string(request,nil,options)
|
194
|
+
assert_equal "GET&http%3A%2F%2Fterm.ie%2Foauth%2Fexample%2Frequest_token.php&oauth_consumer_key%3Dkey%26oauth_nonce%3D#{options[:nonce]}%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D#{options[:timestamp]}%26oauth_version%3D1.0",signature_base_string
|
195
|
+
@consumer.sign!(request, nil,options)
|
196
|
+
|
197
|
+
assert_equal 'GET', request.method
|
198
|
+
assert_equal nil, request.body
|
199
|
+
response=@consumer.http.request(request)
|
200
|
+
assert_equal "200",response.code
|
201
|
+
assert_equal "oauth_token=requestkey&oauth_token_secret=requestsecret",response.body
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_get_token_sequence
|
205
|
+
@consumer=OAuth::Consumer.new(
|
206
|
+
"key",
|
207
|
+
"secret",
|
208
|
+
{
|
209
|
+
:site=>"http://term.ie",
|
210
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
211
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
212
|
+
:authorize_path=>"/oauth/example/authorize.php"
|
213
|
+
})
|
214
|
+
assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
|
215
|
+
assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
|
216
|
+
|
217
|
+
assert !@consumer.request_token_url?, "Should not use fully qualified request token url"
|
218
|
+
assert !@consumer.access_token_url?, "Should not use fully qualified access token url"
|
219
|
+
assert !@consumer.authorize_url?, "Should not use fully qualified url"
|
220
|
+
|
221
|
+
@request_token=@consumer.get_request_token
|
222
|
+
assert_not_nil @request_token
|
223
|
+
assert_equal "requestkey",@request_token.token
|
224
|
+
assert_equal "requestsecret",@request_token.secret
|
225
|
+
assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
|
226
|
+
|
227
|
+
@access_token=@request_token.get_access_token
|
228
|
+
assert_not_nil @access_token
|
229
|
+
assert_equal "accesskey",@access_token.token
|
230
|
+
assert_equal "accesssecret",@access_token.secret
|
231
|
+
|
232
|
+
@response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
|
233
|
+
assert_not_nil @response
|
234
|
+
assert_equal "200",@response.code
|
235
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
236
|
+
|
237
|
+
@response=@access_token.post("/oauth/example/echo_api.php",{'ok'=>'hello','test'=>'this'})
|
238
|
+
assert_not_nil @response
|
239
|
+
assert_equal "200",@response.code
|
240
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_get_token_sequence_using_fqdn
|
244
|
+
@consumer=OAuth::Consumer.new(
|
245
|
+
"key",
|
246
|
+
"secret",
|
247
|
+
{
|
248
|
+
:site=>"http://term.ie",
|
249
|
+
:request_token_url=>"http://term.ie/oauth/example/request_token.php",
|
250
|
+
:access_token_url=>"http://term.ie/oauth/example/access_token.php",
|
251
|
+
:authorize_url=>"http://term.ie/oauth/example/authorize.php"
|
252
|
+
})
|
253
|
+
assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
|
254
|
+
assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
|
255
|
+
|
256
|
+
assert @consumer.request_token_url?, "Should use fully qualified request token url"
|
257
|
+
assert @consumer.access_token_url?, "Should use fully qualified access token url"
|
258
|
+
assert @consumer.authorize_url?, "Should use fully qualified url"
|
259
|
+
|
260
|
+
@request_token=@consumer.get_request_token
|
261
|
+
assert_not_nil @request_token
|
262
|
+
assert_equal "requestkey",@request_token.token
|
263
|
+
assert_equal "requestsecret",@request_token.secret
|
264
|
+
assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
|
265
|
+
|
266
|
+
@access_token=@request_token.get_access_token
|
267
|
+
assert_not_nil @access_token
|
268
|
+
assert_equal "accesskey",@access_token.token
|
269
|
+
assert_equal "accesssecret",@access_token.secret
|
270
|
+
|
271
|
+
@response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
|
272
|
+
assert_not_nil @response
|
273
|
+
assert_equal "200",@response.code
|
274
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
275
|
+
|
276
|
+
@response=@access_token.post("/oauth/example/echo_api.php",{'ok'=>'hello','test'=>'this'})
|
277
|
+
assert_not_nil @response
|
278
|
+
assert_equal "200",@response.code
|
279
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
280
|
+
end
|
281
|
+
|
282
|
+
|
283
|
+
# This test does an actual https request (the result doesn't matter)
|
284
|
+
# to initialize the same way as get_request_token does. Can be any
|
285
|
+
# site that supports https.
|
286
|
+
#
|
287
|
+
# It also generates "warning: using default DH parameters." which I
|
288
|
+
# don't know how to get rid of
|
289
|
+
# def test_serialization_with_https
|
290
|
+
# consumer = OAuth::Consumer.new('token', 'secret', :site => 'https://plazes.net')
|
291
|
+
# consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
292
|
+
# consumer.http.get('/')
|
293
|
+
#
|
294
|
+
# assert_nothing_raised do
|
295
|
+
# # Specifically this should not raise TypeError: no marshal_dump
|
296
|
+
# # is defined for class OpenSSL::SSL::SSLContext
|
297
|
+
# Marshal.dump(consumer)
|
298
|
+
# end
|
299
|
+
# end
|
300
|
+
#
|
301
|
+
def test_get_request_token_with_custom_arguments
|
302
|
+
@consumer=OAuth::Consumer.new(
|
303
|
+
"key",
|
304
|
+
"secret",
|
305
|
+
{
|
306
|
+
:site=>"http://term.ie",
|
307
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
308
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
309
|
+
:authorize_path=>"/oauth/example/authorize.php"
|
310
|
+
})
|
311
|
+
|
312
|
+
|
313
|
+
debug = ""
|
314
|
+
@consumer.http.set_debug_output(debug)
|
315
|
+
|
316
|
+
# get_request_token should receive our custom request_options and *arguments parameters from get_request_token.
|
317
|
+
@consumer.get_request_token({}, {:scope => "http://www.google.com/calendar/feeds http://picasaweb.google.com/data"})
|
318
|
+
|
319
|
+
# Because this is a POST request, create_http_request should take the first element of *arguments
|
320
|
+
# and turn it into URL-encoded data in the body of the POST.
|
321
|
+
assert_match( /^<- "scope=http%3a%2f%2fwww.google.com%2fcalendar%2ffeeds%20http%3a%2f%2fpicasaweb.google.com%2fdata"/,
|
322
|
+
debug)
|
323
|
+
end
|
324
|
+
|
325
|
+
protected
|
326
|
+
|
327
|
+
def request_parameters_to_s
|
328
|
+
@request_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
|
329
|
+
end
|
330
|
+
|
331
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/../lib/oauth'
|
5
|
+
|
6
|
+
begin
|
7
|
+
# load redgreen unless running from within TextMate (in which case ANSI
|
8
|
+
# color codes mess with the output)
|
9
|
+
require 'redgreen' unless ENV['TM_CURRENT_LINE']
|
10
|
+
rescue LoadError
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
class Test::Unit::TestCase
|
16
|
+
def extract_sorted_array_from_authorization_spec(spec)
|
17
|
+
return spec.gsub(/^OAuth\s/,"").split(', ').sort
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'oauth/signature/hmac/sha1'
|
3
|
+
|
4
|
+
class TestSignatureHmacSha1 < Test::Unit::TestCase
|
5
|
+
def test_that_hmac_sha1_implements_hmac_sha1
|
6
|
+
assert OAuth::Signature.available_methods.include?('hmac-sha1')
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_that_get_request_from_oauth_test_cases_produces_matching_signature
|
10
|
+
request = Net::HTTP::Get.new('/photos?file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1')
|
11
|
+
|
12
|
+
consumer = OAuth::Consumer.new('dpf43f3p2l4k3l03', 'kd94hf93k423kf44')
|
13
|
+
token = OAuth::Token.new('nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00')
|
14
|
+
|
15
|
+
signature = OAuth::Signature.sign(request, { :consumer => consumer,
|
16
|
+
:token => token,
|
17
|
+
:uri => 'http://photos.example.net/photos' } )
|
18
|
+
|
19
|
+
assert_equal 'tR3+Ty81lMeYAr/Fid0kMTYa/WM=', signature
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'oauth/client/net_http'
|
3
|
+
|
4
|
+
class NetHTTPClientTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@consumer = OAuth::Consumer.new('consumer_key_86cad9', '5888bf0345e5d237')
|
8
|
+
@token = OAuth::Token.new('token_411a7f', '3196ffd991c8ebdb')
|
9
|
+
@request_uri = URI.parse('http://example.com/test?key=value')
|
10
|
+
@request_parameters = { 'key' => 'value' }
|
11
|
+
@nonce = 225579211881198842005988698334675835446
|
12
|
+
@timestamp = "1199645624"
|
13
|
+
@http = Net::HTTP.new(@request_uri.host, @request_uri.port)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_that_using_auth_headers_on_get_requests_works
|
17
|
+
request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
|
18
|
+
request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
|
19
|
+
|
20
|
+
assert_equal 'GET', request.method
|
21
|
+
assert_equal '/test?key=value', request.path
|
22
|
+
expected_headers = extract_sorted_array_from_authorization_spec("OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"1oO2izFav1GP4kEH2EskwXkCRFg%3D\", oauth_version=\"1.0\"")
|
23
|
+
actual_headers = extract_sorted_array_from_authorization_spec(request['authorization'])
|
24
|
+
assert_equal(Set.new(expected_headers), Set.new(actual_headers))
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_that_using_auth_headers_on_post_requests_works
|
28
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
29
|
+
request.set_form_data( @request_parameters )
|
30
|
+
request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
|
31
|
+
|
32
|
+
assert_equal 'POST', request.method
|
33
|
+
assert_equal '/test', request.path
|
34
|
+
assert_equal 'key=value', request.body
|
35
|
+
actual_headers = extract_sorted_array_from_authorization_spec(request['authorization'])
|
36
|
+
expected_headers = extract_sorted_array_from_authorization_spec("oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"26g7wHTtNO6ZWJaLltcueppHYiI%3D\", oauth_version=\"1.0\"")
|
37
|
+
assert_equal(Set.new(expected_headers), Set.new(actual_headers))
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def test_that_using_post_params_works
|
42
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
43
|
+
request.set_form_data( @request_parameters )
|
44
|
+
request.oauth!(@http, @consumer, @token, {:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp})
|
45
|
+
|
46
|
+
assert_equal 'POST', request.method
|
47
|
+
assert_equal '/test', request.path
|
48
|
+
assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=26g7wHTtNO6ZWJaLltcueppHYiI%3d&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", request.body.split("&").sort.join("&")
|
49
|
+
assert_equal nil, request['authorization']
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_that_using_get_params_works
|
53
|
+
request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
|
54
|
+
request.oauth!(@http, @consumer, @token, {:scheme => 'query_string', :nonce => @nonce, :timestamp => @timestamp})
|
55
|
+
|
56
|
+
assert_equal 'GET', request.method
|
57
|
+
uri = URI.parse(request.path)
|
58
|
+
assert_equal '/test', uri.path
|
59
|
+
assert_equal nil, uri.fragment
|
60
|
+
assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=1oO2izFav1GP4kEH2EskwXkCRFg%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", uri.query.split("&").sort.join("&")
|
61
|
+
assert_equal nil, request['authorization']
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_that_using_get_params_works_with_post_requests
|
65
|
+
request = Net::HTTP::Post.new(@request_uri.path + "?" + request_parameters_to_s)
|
66
|
+
request.oauth!(@http, @consumer, @token, {:scheme => 'query_string', :nonce => @nonce, :timestamp => @timestamp})
|
67
|
+
|
68
|
+
assert_equal 'POST', request.method
|
69
|
+
uri = URI.parse(request.path)
|
70
|
+
assert_equal '/test', uri.path
|
71
|
+
assert_equal nil, uri.fragment
|
72
|
+
assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=26g7wHTtNO6ZWJaLltcueppHYiI%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", uri.query.split("&").sort.join('&')
|
73
|
+
assert_equal nil, request.body
|
74
|
+
assert_equal nil, request['authorization']
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_that_using_get_params_works_with_post_requests_that_have_post_bodies
|
78
|
+
request = Net::HTTP::Post.new(@request_uri.path + "?" + request_parameters_to_s)
|
79
|
+
request.set_form_data( { 'key2' => 'value2' } )
|
80
|
+
request.oauth!(@http, @consumer, @token, {:scheme => :query_string, :nonce => @nonce, :timestamp => @timestamp})
|
81
|
+
|
82
|
+
assert_equal 'POST', request.method
|
83
|
+
uri = URI.parse(request.path)
|
84
|
+
assert_equal '/test', uri.path
|
85
|
+
assert_equal nil, uri.fragment
|
86
|
+
assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=4kSU8Zd1blWo3W6qJH7eaRTMkg0%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", uri.query.split("&").sort.join('&')
|
87
|
+
assert_equal "key2=value2", request.body
|
88
|
+
assert_equal nil, request['authorization']
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
def test_example_from_specs
|
93
|
+
consumer=OAuth::Consumer.new("dpf43f3p2l4k3l03","kd94hf93k423kf44")
|
94
|
+
token = OAuth::Token.new('nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00')
|
95
|
+
request_uri = URI.parse('http://photos.example.net/photos?file=vacation.jpg&size=original')
|
96
|
+
nonce = 'kllo9940pd9333jh'
|
97
|
+
timestamp = "1191242096"
|
98
|
+
http = Net::HTTP.new(request_uri.host, request_uri.port)
|
99
|
+
|
100
|
+
request = Net::HTTP::Get.new(request_uri.path + "?" + request_uri.query)
|
101
|
+
signature_base_string=request.signature_base_string(http, consumer, token, {:nonce => nonce, :timestamp => timestamp})
|
102
|
+
assert_equal 'GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal',signature_base_string
|
103
|
+
|
104
|
+
# request = Net::HTTP::Get.new(request_uri.path + "?" + request_uri.query)
|
105
|
+
request.oauth!(http, consumer, token, {:nonce => nonce, :timestamp => timestamp,:realm=>"http://photos.example.net/"})
|
106
|
+
|
107
|
+
assert_equal 'GET', request.method
|
108
|
+
assert_equal 'OAuth realm="http://photos.example.net/", oauth_nonce="kllo9940pd9333jh", oauth_signature_method="HMAC-SHA1", oauth_token="nnch734d00sl2jdk", oauth_timestamp="1191242096", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D", oauth_version="1.0"'.split(', ').sort, request['authorization'].split(', ').sort
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_step_by_step_token_request
|
113
|
+
consumer=OAuth::Consumer.new(
|
114
|
+
"key",
|
115
|
+
"secret")
|
116
|
+
request_uri = URI.parse('http://term.ie/oauth/example/request_token.php')
|
117
|
+
nonce = rand(2**128).to_s
|
118
|
+
timestamp = Time.now.to_i.to_s
|
119
|
+
http = Net::HTTP.new(request_uri.host, request_uri.port)
|
120
|
+
|
121
|
+
request = Net::HTTP::Get.new(request_uri.path)
|
122
|
+
signature_base_string=request.signature_base_string(http, consumer, nil, {:scheme=>:query_string,:nonce => nonce, :timestamp => timestamp})
|
123
|
+
assert_equal "GET&http%3A%2F%2Fterm.ie%2Foauth%2Fexample%2Frequest_token.php&oauth_consumer_key%3Dkey%26oauth_nonce%3D#{nonce}%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D#{timestamp}%26oauth_version%3D1.0",signature_base_string
|
124
|
+
|
125
|
+
# request = Net::HTTP::Get.new(request_uri.path)
|
126
|
+
request.oauth!(http, consumer, nil, {:scheme=>:query_string,:nonce => nonce, :timestamp => timestamp})
|
127
|
+
assert_equal 'GET', request.method
|
128
|
+
assert_nil request.body
|
129
|
+
assert_nil request['authorization']
|
130
|
+
# assert_equal 'OAuth oauth_nonce="kllo9940pd9333jh", oauth_signature_method="HMAC-SHA1", oauth_token="", oauth_timestamp="'+timestamp+'", oauth_consumer_key="key", oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D", oauth_version="1.0"', request['authorization']
|
131
|
+
|
132
|
+
response=http.request(request)
|
133
|
+
assert_equal "200",response.code
|
134
|
+
# assert_equal request['authorization'],response.body
|
135
|
+
assert_equal "oauth_token=requestkey&oauth_token_secret=requestsecret",response.body
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_that_put_bodies_not_signed
|
139
|
+
request = Net::HTTP::Put.new(@request_uri.path)
|
140
|
+
request.body = "<?xml version=\"1.0\"?><foo><bar>baz</bar></foo>"
|
141
|
+
request["Content-Type"] = "application/xml"
|
142
|
+
signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
|
143
|
+
assert_equal "PUT&http%3A%2F%2Fexample.com%2Ftest&oauth_consumer_key%3Dconsumer_key_86cad9%26oauth_nonce%3D225579211881198842005988698334675835446%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1199645624%26oauth_version%3D1.0", signature_base_string
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_that_put_bodies_not_signed_even_if_form_urlencoded
|
147
|
+
request = Net::HTTP::Put.new(@request_uri.path)
|
148
|
+
request.set_form_data( { 'key2' => 'value2' } )
|
149
|
+
signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
|
150
|
+
assert_equal "PUT&http%3A%2F%2Fexample.com%2Ftest&oauth_consumer_key%3Dconsumer_key_86cad9%26oauth_nonce%3D225579211881198842005988698334675835446%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1199645624%26oauth_version%3D1.0", signature_base_string
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_that_post_bodies_signed_if_form_urlencoded
|
154
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
155
|
+
request.set_form_data( { 'key2' => 'value2' } )
|
156
|
+
signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
|
157
|
+
assert_equal "POST&http%3A%2F%2Fexample.com%2Ftest&key2%3Dvalue2%26oauth_consumer_key%3Dconsumer_key_86cad9%26oauth_nonce%3D225579211881198842005988698334675835446%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1199645624%26oauth_version%3D1.0", signature_base_string
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_that_post_bodies_not_signed_if_other_content_type
|
161
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
162
|
+
request.body = "<?xml version=\"1.0\"?><foo><bar>baz</bar></foo>"
|
163
|
+
request["Content-Type"] = "application/xml"
|
164
|
+
signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
|
165
|
+
assert_equal "POST&http%3A%2F%2Fexample.com%2Ftest&oauth_consumer_key%3Dconsumer_key_86cad9%26oauth_nonce%3D225579211881198842005988698334675835446%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1199645624%26oauth_version%3D1.0", signature_base_string
|
166
|
+
end
|
167
|
+
|
168
|
+
protected
|
169
|
+
|
170
|
+
def request_parameters_to_s
|
171
|
+
@request_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|