mzsanford-oauth 0.3.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +91 -0
- data/License.txt +20 -0
- data/Manifest.txt +84 -0
- data/README.rdoc +71 -0
- data/Rakefile +36 -0
- data/TODO +31 -0
- data/bin/oauth +5 -0
- data/examples/yql.rb +44 -0
- data/lib/oauth.rb +4 -0
- data/lib/oauth/cli.rb +342 -0
- data/lib/oauth/client.rb +4 -0
- data/lib/oauth/client/action_controller_request.rb +54 -0
- data/lib/oauth/client/helper.rb +83 -0
- data/lib/oauth/client/net_http.rb +106 -0
- data/lib/oauth/consumer.rb +337 -0
- data/lib/oauth/errors.rb +3 -0
- data/lib/oauth/errors/error.rb +4 -0
- data/lib/oauth/errors/problem.rb +14 -0
- data/lib/oauth/errors/unauthorized.rb +12 -0
- data/lib/oauth/helper.rb +78 -0
- data/lib/oauth/oauth.rb +11 -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 +61 -0
- data/lib/oauth/request_proxy/base.rb +165 -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 +37 -0
- data/lib/oauth/signature/base.rb +91 -0
- data/lib/oauth/signature/hmac/base.rb +12 -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 +9 -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 +33 -0
- data/lib/oauth/tokens/request_token.rb +32 -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 +127 -0
- data/test/test_consumer.rb +361 -0
- data/test/test_helper.rb +10 -0
- data/test/test_hmac_sha1.rb +21 -0
- data/test/test_net_http_client.rb +204 -0
- data/test/test_net_http_request_proxy.rb +73 -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 +19 -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 +212 -0
@@ -0,0 +1,127 @@
|
|
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(request_method = :get, uri_params = {}, body_params = {})
|
9
|
+
request = ActionController::TestRequest.new
|
10
|
+
|
11
|
+
case request_method
|
12
|
+
when :post
|
13
|
+
request.env['REQUEST_METHOD'] = 'POST'
|
14
|
+
when :put
|
15
|
+
request.env['REQUEST_METHOD'] = 'PUT'
|
16
|
+
end
|
17
|
+
|
18
|
+
request.env['RAW_POST_DATA'] = body_params.to_query
|
19
|
+
request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
|
20
|
+
|
21
|
+
yield request if block_given?
|
22
|
+
OAuth::RequestProxy.proxy(request, :parameters=>uri_params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_that_proxy_simple_get_request_works_with_query_params
|
26
|
+
request_proxy = request_proxy(:get, {'key'=>'value'})
|
27
|
+
|
28
|
+
expected_parameters = [["key", "value"]]
|
29
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
30
|
+
assert_equal 'GET', request_proxy.method
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_that_proxy_simple_post_request_works_with_query_params
|
34
|
+
request_proxy = request_proxy(:post, {'key'=>'value'})
|
35
|
+
|
36
|
+
expected_parameters = [["key", "value"]]
|
37
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
38
|
+
assert_equal 'POST', request_proxy.method
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_that_proxy_simple_put_request_works_with_query_params
|
42
|
+
request_proxy = request_proxy(:put, {'key'=>'value'})
|
43
|
+
|
44
|
+
expected_parameters = [["key", "value"]]
|
45
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
46
|
+
assert_equal 'PUT', request_proxy.method
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_that_proxy_simple_put_request_works_with_post_params
|
50
|
+
request_proxy = request_proxy(:get, {}, {'key'=>'value'})
|
51
|
+
|
52
|
+
expected_parameters = []
|
53
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
54
|
+
assert_equal 'GET', request_proxy.method
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_that_proxy_simple_post_request_works_with_post_params
|
58
|
+
request_proxy = request_proxy(:post, {}, {'key'=>'value'})
|
59
|
+
|
60
|
+
expected_parameters = [["key", "value"]]
|
61
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
62
|
+
assert_equal 'POST', request_proxy.method
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_that_proxy_simple_put_request_works_with_post_params
|
66
|
+
request_proxy = request_proxy(:put, {}, {'key'=>'value'})
|
67
|
+
|
68
|
+
expected_parameters = []
|
69
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
70
|
+
assert_equal 'PUT', request_proxy.method
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_that_proxy_simple_put_request_works_with_mixed_params
|
74
|
+
request_proxy = request_proxy(:get, {'key'=>'value'}, {'key2'=>'value2'})
|
75
|
+
|
76
|
+
expected_parameters = [["key", "value"]]
|
77
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
78
|
+
assert_equal 'GET', request_proxy.method
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_that_proxy_simple_post_request_works_with_mixed_params
|
82
|
+
request_proxy = request_proxy(:post, {'key'=>'value'}, {'key2'=>'value2'})
|
83
|
+
|
84
|
+
expected_parameters = [["key", "value"],["key2", "value2"]]
|
85
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
86
|
+
assert_equal 'POST', request_proxy.method
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_that_proxy_simple_put_request_works_with_mixed_params
|
90
|
+
request_proxy = request_proxy(:put, {'key'=>'value'}, {'key2'=>'value2'})
|
91
|
+
|
92
|
+
expected_parameters = [["key", "value"]]
|
93
|
+
assert_equal expected_parameters, request_proxy.parameters_for_signature
|
94
|
+
assert_equal 'PUT', request_proxy.method
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_parameter_keys_should_preserve_brackets_from_hash
|
98
|
+
assert_equal(
|
99
|
+
[["message[body]", "This is a test"]],
|
100
|
+
request_proxy(:post, { :message => { :body => 'This is a test' }}).parameters_for_signature
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_parameter_values_with_amps_should_not_break_parameter_parsing
|
105
|
+
assert_equal(
|
106
|
+
[['message[body]', 'http://foo.com/?a=b&c=d']],
|
107
|
+
request_proxy(:post, { :message => { :body => 'http://foo.com/?a=b&c=d'}}).parameters_for_signature
|
108
|
+
)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_parameter_keys_should_preserve_brackets_from_array
|
112
|
+
assert_equal(
|
113
|
+
[["foo[]", "123"], ["foo[]", "456"]],
|
114
|
+
request_proxy(:post, { :foo => [123, 456] }).parameters_for_signature.sort
|
115
|
+
)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_query_string_parameter_values_should_be_cgi_unescaped
|
119
|
+
request = request_proxy do |r|
|
120
|
+
r.env['QUERY_STRING'] = 'url=http%3A%2F%2Ffoo.com%2F%3Fa%3Db%26c%3Dd'
|
121
|
+
end
|
122
|
+
assert_equal(
|
123
|
+
[['url', 'http://foo.com/?a=b&c=d']],
|
124
|
+
request.parameters_for_signature.sort
|
125
|
+
)
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,361 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require 'oauth/consumer'
|
3
|
+
require 'oauth/signature/rsa/sha1'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
|
7
|
+
# This performs testing against Andy Smith's test server http://term.ie/oauth/example/
|
8
|
+
# Thanks Andy.
|
9
|
+
# This also means you have to be online to be able to run these.
|
10
|
+
class ConsumerTest < Test::Unit::TestCase
|
11
|
+
def setup
|
12
|
+
@consumer=OAuth::Consumer.new(
|
13
|
+
'consumer_key_86cad9', '5888bf0345e5d237',
|
14
|
+
{
|
15
|
+
:site=>"http://blabla.bla",
|
16
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
17
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
18
|
+
:authorize_path=>"/oauth/example/authorize.php",
|
19
|
+
:scheme=>:header,
|
20
|
+
:http_method=>:get
|
21
|
+
})
|
22
|
+
@token = OAuth::ConsumerToken.new(@consumer,'token_411a7f', '3196ffd991c8ebdb')
|
23
|
+
@request_uri = URI.parse('http://example.com/test?key=value')
|
24
|
+
@request_parameters = { 'key' => 'value' }
|
25
|
+
@nonce = 225579211881198842005988698334675835446
|
26
|
+
@timestamp = "1199645624"
|
27
|
+
@consumer.http=Net::HTTP.new(@request_uri.host, @request_uri.port)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_initializer
|
31
|
+
assert_equal "consumer_key_86cad9",@consumer.key
|
32
|
+
assert_equal "5888bf0345e5d237",@consumer.secret
|
33
|
+
assert_equal "http://blabla.bla",@consumer.site
|
34
|
+
assert_equal "/oauth/example/request_token.php",@consumer.request_token_path
|
35
|
+
assert_equal "/oauth/example/access_token.php",@consumer.access_token_path
|
36
|
+
assert_equal "http://blabla.bla/oauth/example/request_token.php",@consumer.request_token_url
|
37
|
+
assert_equal "http://blabla.bla/oauth/example/access_token.php",@consumer.access_token_url
|
38
|
+
assert_equal "http://blabla.bla/oauth/example/authorize.php",@consumer.authorize_url
|
39
|
+
assert_equal :header,@consumer.scheme
|
40
|
+
assert_equal :get,@consumer.http_method
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_defaults
|
44
|
+
@consumer=OAuth::Consumer.new(
|
45
|
+
"key",
|
46
|
+
"secret",
|
47
|
+
{
|
48
|
+
:site=>"http://twitter.com"
|
49
|
+
})
|
50
|
+
assert_equal "key",@consumer.key
|
51
|
+
assert_equal "secret",@consumer.secret
|
52
|
+
assert_equal "http://twitter.com",@consumer.site
|
53
|
+
assert_equal "/oauth/request_token",@consumer.request_token_path
|
54
|
+
assert_equal "/oauth/access_token",@consumer.access_token_path
|
55
|
+
assert_equal "http://twitter.com/oauth/request_token",@consumer.request_token_url
|
56
|
+
assert_equal "http://twitter.com/oauth/access_token",@consumer.access_token_url
|
57
|
+
assert_equal "http://twitter.com/oauth/authorize",@consumer.authorize_url
|
58
|
+
assert_equal :header,@consumer.scheme
|
59
|
+
assert_equal :post,@consumer.http_method
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_override_paths
|
63
|
+
@consumer=OAuth::Consumer.new(
|
64
|
+
"key",
|
65
|
+
"secret",
|
66
|
+
{
|
67
|
+
:site=>"http://twitter.com",
|
68
|
+
:request_token_url=>"http://oauth.twitter.com/request_token",
|
69
|
+
:access_token_url=>"http://oauth.twitter.com/access_token",
|
70
|
+
:authorize_url=>"http://site.twitter.com/authorize"
|
71
|
+
})
|
72
|
+
assert_equal "key",@consumer.key
|
73
|
+
assert_equal "secret",@consumer.secret
|
74
|
+
assert_equal "http://twitter.com",@consumer.site
|
75
|
+
assert_equal "/oauth/request_token",@consumer.request_token_path
|
76
|
+
assert_equal "/oauth/access_token",@consumer.access_token_path
|
77
|
+
assert_equal "http://oauth.twitter.com/request_token",@consumer.request_token_url
|
78
|
+
assert_equal "http://oauth.twitter.com/access_token",@consumer.access_token_url
|
79
|
+
assert_equal "http://site.twitter.com/authorize",@consumer.authorize_url
|
80
|
+
assert_equal :header,@consumer.scheme
|
81
|
+
assert_equal :post,@consumer.http_method
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_that_signing_auth_headers_on_get_requests_works
|
85
|
+
request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
|
86
|
+
@token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
|
87
|
+
|
88
|
+
assert_equal 'GET', request.method
|
89
|
+
assert_equal '/test?key=value', request.path
|
90
|
+
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
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_that_setting_signature_method_on_consumer_effects_signing
|
94
|
+
require 'oauth/signature/plaintext'
|
95
|
+
request = Net::HTTP::Get.new(@request_uri.path)
|
96
|
+
consumer = @consumer.dup
|
97
|
+
consumer.options[:signature_method] = 'PLAINTEXT'
|
98
|
+
token = OAuth::ConsumerToken.new(consumer, 'token_411a7f', '3196ffd991c8ebdb')
|
99
|
+
token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
|
100
|
+
|
101
|
+
assert_no_match( /oauth_signature_method="HMAC-SHA1"/, request['authorization'])
|
102
|
+
assert_match( /oauth_signature_method="PLAINTEXT"/, request['authorization'])
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_that_setting_signature_method_on_consumer_effects_signature_base_string
|
106
|
+
require 'oauth/signature/plaintext'
|
107
|
+
request = Net::HTTP::Get.new(@request_uri.path)
|
108
|
+
consumer = @consumer.dup
|
109
|
+
consumer.options[:signature_method] = 'PLAINTEXT'
|
110
|
+
|
111
|
+
request = Net::HTTP::Get.new('/')
|
112
|
+
signature_base_string = consumer.signature_base_string(request)
|
113
|
+
|
114
|
+
assert_no_match( /HMAC-SHA1/, signature_base_string)
|
115
|
+
assert_equal( "#{consumer.secret}%26", signature_base_string)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_that_plaintext_signature_works
|
119
|
+
require 'oauth/signature/plaintext'
|
120
|
+
consumer = OAuth::Consumer.new("key", "secret",
|
121
|
+
:site => "http://term.ie", :signature_method => 'PLAINTEXT')
|
122
|
+
access_token = OAuth::AccessToken.new(consumer, 'accesskey', 'accesssecret')
|
123
|
+
response = access_token.get("/oauth/example/echo_api.php?echo=hello")
|
124
|
+
|
125
|
+
assert_equal 'echo=hello', response.body
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_that_signing_auth_headers_on_post_requests_works
|
129
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
130
|
+
request.set_form_data( @request_parameters )
|
131
|
+
@token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
|
132
|
+
# assert_equal "",request.oauth_helper.signature_base_string
|
133
|
+
|
134
|
+
assert_equal 'POST', request.method
|
135
|
+
assert_equal '/test', request.path
|
136
|
+
assert_equal 'key=value', request.body
|
137
|
+
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
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_that_signing_post_params_works
|
141
|
+
request = Net::HTTP::Post.new(@request_uri.path)
|
142
|
+
request.set_form_data( @request_parameters )
|
143
|
+
@token.sign!(request, {:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp})
|
144
|
+
|
145
|
+
assert_equal 'POST', request.method
|
146
|
+
assert_equal '/test', request.path
|
147
|
+
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("&")
|
148
|
+
assert_equal nil, request['authorization']
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_that_using_auth_headers_on_get_on_create_signed_requests_works
|
152
|
+
request=@consumer.create_signed_request(:get,@request_uri.path+ "?" + request_parameters_to_s,@token,{:nonce => @nonce, :timestamp => @timestamp},@request_parameters)
|
153
|
+
|
154
|
+
assert_equal 'GET', request.method
|
155
|
+
assert_equal '/test?key=value', request.path
|
156
|
+
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
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_that_using_auth_headers_on_post_on_create_signed_requests_works
|
160
|
+
request=@consumer.create_signed_request(:post,@request_uri.path,@token,{:nonce => @nonce, :timestamp => @timestamp},@request_parameters,{})
|
161
|
+
assert_equal 'POST', request.method
|
162
|
+
assert_equal '/test', request.path
|
163
|
+
assert_equal 'key=value', request.body
|
164
|
+
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
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_that_signing_post_params_works_2
|
168
|
+
request=@consumer.create_signed_request(:post,@request_uri.path,@token,{:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp},@request_parameters,{})
|
169
|
+
|
170
|
+
assert_equal 'POST', request.method
|
171
|
+
assert_equal '/test', request.path
|
172
|
+
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("&")
|
173
|
+
assert_equal nil, request['authorization']
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_step_by_step_token_request
|
177
|
+
@consumer=OAuth::Consumer.new(
|
178
|
+
"key",
|
179
|
+
"secret",
|
180
|
+
{
|
181
|
+
:site=>"http://term.ie",
|
182
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
183
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
184
|
+
:authorize_path=>"/oauth/example/authorize.php",
|
185
|
+
:scheme=>:header
|
186
|
+
})
|
187
|
+
options={:nonce=>'nonce',:timestamp=>Time.now.to_i.to_s}
|
188
|
+
|
189
|
+
request = Net::HTTP::Get.new("/oauth/example/request_token.php")
|
190
|
+
signature_base_string=@consumer.signature_base_string(request,nil,options)
|
191
|
+
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
|
192
|
+
@consumer.sign!(request, nil,options)
|
193
|
+
|
194
|
+
assert_equal 'GET', request.method
|
195
|
+
assert_equal nil, request.body
|
196
|
+
response=@consumer.http.request(request)
|
197
|
+
assert_equal "200",response.code
|
198
|
+
assert_equal "oauth_token=requestkey&oauth_token_secret=requestsecret",response.body
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_get_token_sequence
|
202
|
+
@consumer=OAuth::Consumer.new(
|
203
|
+
"key",
|
204
|
+
"secret",
|
205
|
+
{
|
206
|
+
:site=>"http://term.ie",
|
207
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
208
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
209
|
+
:authorize_path=>"/oauth/example/authorize.php"
|
210
|
+
})
|
211
|
+
assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
|
212
|
+
assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
|
213
|
+
|
214
|
+
assert !@consumer.request_token_url?, "Should not use fully qualified request token url"
|
215
|
+
assert !@consumer.access_token_url?, "Should not use fully qualified access token url"
|
216
|
+
assert !@consumer.authorize_url?, "Should not use fully qualified url"
|
217
|
+
|
218
|
+
@request_token=@consumer.get_request_token
|
219
|
+
assert_not_nil @request_token
|
220
|
+
assert_equal "requestkey",@request_token.token
|
221
|
+
assert_equal "requestsecret",@request_token.secret
|
222
|
+
assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
|
223
|
+
|
224
|
+
@access_token=@request_token.get_access_token
|
225
|
+
assert_not_nil @access_token
|
226
|
+
assert_equal "accesskey",@access_token.token
|
227
|
+
assert_equal "accesssecret",@access_token.secret
|
228
|
+
|
229
|
+
@response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
|
230
|
+
assert_not_nil @response
|
231
|
+
assert_equal "200",@response.code
|
232
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
233
|
+
|
234
|
+
@response=@access_token.post("/oauth/example/echo_api.php",{'ok'=>'hello','test'=>'this'})
|
235
|
+
assert_not_nil @response
|
236
|
+
assert_equal "200",@response.code
|
237
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_get_token_sequence_using_fqdn
|
241
|
+
@consumer=OAuth::Consumer.new(
|
242
|
+
"key",
|
243
|
+
"secret",
|
244
|
+
{
|
245
|
+
:site=>"http://term.ie",
|
246
|
+
:request_token_url=>"http://term.ie/oauth/example/request_token.php",
|
247
|
+
:access_token_url=>"http://term.ie/oauth/example/access_token.php",
|
248
|
+
:authorize_url=>"http://term.ie/oauth/example/authorize.php"
|
249
|
+
})
|
250
|
+
assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
|
251
|
+
assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
|
252
|
+
|
253
|
+
assert @consumer.request_token_url?, "Should use fully qualified request token url"
|
254
|
+
assert @consumer.access_token_url?, "Should use fully qualified access token url"
|
255
|
+
assert @consumer.authorize_url?, "Should use fully qualified url"
|
256
|
+
|
257
|
+
@request_token=@consumer.get_request_token
|
258
|
+
assert_not_nil @request_token
|
259
|
+
assert_equal "requestkey",@request_token.token
|
260
|
+
assert_equal "requestsecret",@request_token.secret
|
261
|
+
assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
|
262
|
+
|
263
|
+
@access_token=@request_token.get_access_token
|
264
|
+
assert_not_nil @access_token
|
265
|
+
assert_equal "accesskey",@access_token.token
|
266
|
+
assert_equal "accesssecret",@access_token.secret
|
267
|
+
|
268
|
+
@response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
|
269
|
+
assert_not_nil @response
|
270
|
+
assert_equal "200",@response.code
|
271
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
272
|
+
|
273
|
+
@response=@access_token.post("/oauth/example/echo_api.php",{'ok'=>'hello','test'=>'this'})
|
274
|
+
assert_not_nil @response
|
275
|
+
assert_equal "200",@response.code
|
276
|
+
assert_equal( "ok=hello&test=this",@response.body)
|
277
|
+
end
|
278
|
+
|
279
|
+
|
280
|
+
# This test does an actual https request (the result doesn't matter)
|
281
|
+
# to initialize the same way as get_request_token does. Can be any
|
282
|
+
# site that supports https.
|
283
|
+
#
|
284
|
+
# It also generates "warning: using default DH parameters." which I
|
285
|
+
# don't know how to get rid of
|
286
|
+
# def test_serialization_with_https
|
287
|
+
# consumer = OAuth::Consumer.new('token', 'secret', :site => 'https://plazes.net')
|
288
|
+
# consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
289
|
+
# consumer.http.get('/')
|
290
|
+
#
|
291
|
+
# assert_nothing_raised do
|
292
|
+
# # Specifically this should not raise TypeError: no marshal_dump
|
293
|
+
# # is defined for class OpenSSL::SSL::SSLContext
|
294
|
+
# Marshal.dump(consumer)
|
295
|
+
# end
|
296
|
+
# end
|
297
|
+
#
|
298
|
+
def test_get_request_token_with_custom_arguments
|
299
|
+
@consumer=OAuth::Consumer.new(
|
300
|
+
"key",
|
301
|
+
"secret",
|
302
|
+
{
|
303
|
+
:site=>"http://term.ie",
|
304
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
305
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
306
|
+
:authorize_path=>"/oauth/example/authorize.php"
|
307
|
+
})
|
308
|
+
|
309
|
+
|
310
|
+
debug = ""
|
311
|
+
@consumer.http.set_debug_output(debug)
|
312
|
+
|
313
|
+
# get_request_token should receive our custom request_options and *arguments parameters from get_request_token.
|
314
|
+
@consumer.get_request_token({}, {:scope => "http://www.google.com/calendar/feeds http://picasaweb.google.com/data"})
|
315
|
+
|
316
|
+
# Because this is a POST request, create_http_request should take the first element of *arguments
|
317
|
+
# and turn it into URL-encoded data in the body of the POST.
|
318
|
+
assert_match( /^<- "scope=http%3a%2f%2fwww.google.com%2fcalendar%2ffeeds%20http%3a%2f%2fpicasaweb.google.com%2fdata"/,
|
319
|
+
debug)
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_post_with_body_stream
|
323
|
+
@consumer=OAuth::Consumer.new(
|
324
|
+
"key",
|
325
|
+
"secret",
|
326
|
+
{
|
327
|
+
:site=>"http://term.ie",
|
328
|
+
:request_token_path=>"/oauth/example/request_token.php",
|
329
|
+
:access_token_path=>"/oauth/example/access_token.php",
|
330
|
+
:authorize_path=>"/oauth/example/authorize.php"
|
331
|
+
})
|
332
|
+
|
333
|
+
|
334
|
+
@request_token=@consumer.get_request_token
|
335
|
+
@access_token=@request_token.get_access_token
|
336
|
+
|
337
|
+
request_body_string = "Hello, hello, hello"
|
338
|
+
request_body_stream = StringIO.new( request_body_string )
|
339
|
+
|
340
|
+
@response=@access_token.post("/oauth/example/echo_api.php",request_body_stream)
|
341
|
+
assert_not_nil @response
|
342
|
+
assert_equal "200",@response.code
|
343
|
+
|
344
|
+
request_body_file = File.open(__FILE__)
|
345
|
+
|
346
|
+
@response=@access_token.post("/oauth/example/echo_api.php",request_body_file)
|
347
|
+
assert_not_nil @response
|
348
|
+
assert_equal "200",@response.code
|
349
|
+
|
350
|
+
# unfortunately I don't know of a way to test that the body data was received correctly since the test server at http://term.ie
|
351
|
+
# echos back any non-oauth parameters but not the body. However, this does test that the request is still correctly signed
|
352
|
+
# (including the Content-Length header) and that the server received Content-Length bytes of body since it won't process the
|
353
|
+
# request & respond until the full body length is received.
|
354
|
+
end
|
355
|
+
|
356
|
+
protected
|
357
|
+
|
358
|
+
def request_parameters_to_s
|
359
|
+
@request_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
|
360
|
+
end
|
361
|
+
end
|