qoobaa-oauth 0.3.8

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 (75) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/History.txt +114 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +71 -0
  6. data/Rakefile +58 -0
  7. data/TODO +31 -0
  8. data/VERSION +1 -0
  9. data/lib/oauth.rb +4 -0
  10. data/lib/oauth/cli.rb +378 -0
  11. data/lib/oauth/client.rb +4 -0
  12. data/lib/oauth/client/action_controller_request.rb +54 -0
  13. data/lib/oauth/client/helper.rb +85 -0
  14. data/lib/oauth/client/net_http.rb +106 -0
  15. data/lib/oauth/consumer.rb +370 -0
  16. data/lib/oauth/errors.rb +3 -0
  17. data/lib/oauth/errors/error.rb +4 -0
  18. data/lib/oauth/errors/problem.rb +14 -0
  19. data/lib/oauth/errors/unauthorized.rb +12 -0
  20. data/lib/oauth/helper.rb +78 -0
  21. data/lib/oauth/oauth.rb +11 -0
  22. data/lib/oauth/oauth_test_helper.rb +25 -0
  23. data/lib/oauth/request_proxy.rb +24 -0
  24. data/lib/oauth/request_proxy/action_controller_request.rb +73 -0
  25. data/lib/oauth/request_proxy/base.rb +166 -0
  26. data/lib/oauth/request_proxy/jabber_request.rb +41 -0
  27. data/lib/oauth/request_proxy/mock_request.rb +44 -0
  28. data/lib/oauth/request_proxy/net_http.rb +65 -0
  29. data/lib/oauth/request_proxy/rack_request.rb +40 -0
  30. data/lib/oauth/request_proxy/typhoeus_request.rb +53 -0
  31. data/lib/oauth/server.rb +66 -0
  32. data/lib/oauth/signature.rb +40 -0
  33. data/lib/oauth/signature/base.rb +87 -0
  34. data/lib/oauth/signature/hmac/md5.rb +21 -0
  35. data/lib/oauth/signature/hmac/rmd160.rb +21 -0
  36. data/lib/oauth/signature/hmac/sha1.rb +22 -0
  37. data/lib/oauth/signature/hmac/sha2.rb +21 -0
  38. data/lib/oauth/signature/md5.rb +13 -0
  39. data/lib/oauth/signature/plaintext.rb +23 -0
  40. data/lib/oauth/signature/rsa/sha1.rb +45 -0
  41. data/lib/oauth/signature/sha1.rb +13 -0
  42. data/lib/oauth/token.rb +7 -0
  43. data/lib/oauth/tokens/access_token.rb +68 -0
  44. data/lib/oauth/tokens/consumer_token.rb +33 -0
  45. data/lib/oauth/tokens/request_token.rb +32 -0
  46. data/lib/oauth/tokens/server_token.rb +9 -0
  47. data/lib/oauth/tokens/token.rb +17 -0
  48. data/lib/oauth/version.rb +3 -0
  49. data/qoobaa-oauth.gemspec +149 -0
  50. data/test/cases/oauth_case.rb +19 -0
  51. data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
  52. data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
  53. data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
  54. data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
  55. data/test/integration/consumer_test.rb +304 -0
  56. data/test/keys/rsa.cert +11 -0
  57. data/test/keys/rsa.pem +16 -0
  58. data/test/test_access_token.rb +26 -0
  59. data/test/test_action_controller_request_proxy.rb +133 -0
  60. data/test/test_consumer.rb +159 -0
  61. data/test/test_helper.rb +14 -0
  62. data/test/test_hmac_sha1.rb +20 -0
  63. data/test/test_net_http_client.rb +224 -0
  64. data/test/test_net_http_request_proxy.rb +72 -0
  65. data/test/test_oauth_helper.rb +49 -0
  66. data/test/test_rack_request_proxy.rb +40 -0
  67. data/test/test_request_token.rb +51 -0
  68. data/test/test_rsa_sha1.rb +59 -0
  69. data/test/test_server.rb +40 -0
  70. data/test/test_signature.rb +21 -0
  71. data/test/test_signature_base.rb +32 -0
  72. data/test/test_signature_plain_text.rb +26 -0
  73. data/test/test_token.rb +14 -0
  74. data/test/test_typhoeus_request_proxy.rb +72 -0
  75. metadata +209 -0
@@ -0,0 +1,11 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIBpjCCAQ+gAwIBAgIBATANBgkqhkiG9w0BAQUFADAZMRcwFQYDVQQDDA5UZXN0
3
+ IFByaW5jaXBhbDAeFw03MDAxMDEwODAwMDBaFw0zODEyMzEwODAwMDBaMBkxFzAV
4
+ BgNVBAMMDlRlc3QgUHJpbmNpcGFsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
5
+ gQC0YjCwIfYoprq/FQO6lb3asXrxLlJFuCvtinTF5p0GxvQGu5O3gYytUvtC2JlY
6
+ zypSRjVxwxrsuRcP3e641SdASwfrmzyvIgP08N4S0IFzEURkV1wp/IpH7kH41Etb
7
+ mUmrXSwfNZsnQRE5SYSOhh+LcK2wyQkdgcMv11l4KoBkcwIDAQABMA0GCSqGSIb3
8
+ DQEBBQUAA4GBAGZLPEuJ5SiJ2ryq+CmEGOXfvlTtEL2nuGtr9PewxkgnOjZpUy+d
9
+ 4TvuXJbNQc8f4AMWL/tO9w0Fk80rWKp9ea8/df4qMq5qlFWlx6yOLQxumNOmECKb
10
+ WpkUQDIDJEoFUzKMVuJf4KO/FJ345+BNLGgbJ6WujreoM1X/gYfdnJ/J
11
+ -----END CERTIFICATE-----
@@ -0,0 +1,16 @@
1
+ -----BEGIN PRIVATE KEY-----
2
+ MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALRiMLAh9iimur8V
3
+ A7qVvdqxevEuUkW4K+2KdMXmnQbG9Aa7k7eBjK1S+0LYmVjPKlJGNXHDGuy5Fw/d
4
+ 7rjVJ0BLB+ubPK8iA/Tw3hLQgXMRRGRXXCn8ikfuQfjUS1uZSatdLB81mydBETlJ
5
+ hI6GH4twrbDJCR2Bwy/XWXgqgGRzAgMBAAECgYBYWVtleUzavkbrPjy0T5FMou8H
6
+ X9u2AC2ry8vD/l7cqedtwMPp9k7TubgNFo+NGvKsl2ynyprOZR1xjQ7WgrgVB+mm
7
+ uScOM/5HVceFuGRDhYTCObE+y1kxRloNYXnx3ei1zbeYLPCHdhxRYW7T0qcynNmw
8
+ rn05/KO2RLjgQNalsQJBANeA3Q4Nugqy4QBUCEC09SqylT2K9FrrItqL2QKc9v0Z
9
+ zO2uwllCbg0dwpVuYPYXYvikNHHg+aCWF+VXsb9rpPsCQQDWR9TT4ORdzoj+Nccn
10
+ qkMsDmzt0EfNaAOwHOmVJ2RVBspPcxt5iN4HI7HNeG6U5YsFBb+/GZbgfBT3kpNG
11
+ WPTpAkBI+gFhjfJvRw38n3g/+UeAkwMI2TJQS4n8+hid0uus3/zOjDySH3XHCUno
12
+ cn1xOJAyZODBo47E+67R4jV1/gzbAkEAklJaspRPXP877NssM5nAZMU0/O/NGCZ+
13
+ 3jPgDUno6WbJn5cqm8MqWhW1xGkImgRk+fkDBquiq4gPiT898jusgQJAd5Zrr6Q8
14
+ AO/0isr/3aa6O6NLQxISLKcPDk2NOccAfS/xOtfOz4sJYM3+Bs4Io9+dZGSDCA54
15
+ Lw03eHTNQghS0A==
16
+ -----END PRIVATE KEY-----
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
2
+
3
+ class TestAccessToken < Test::Unit::TestCase
4
+ def setup
5
+ @fake_response = {
6
+ :user_id => 5734758743895,
7
+ :oauth_token => "key",
8
+ :oauth_token_secret => "secret"
9
+ }
10
+ # setup a fake req. token. mocking Consumer would be more appropriate...
11
+ @access_token = OAuth::AccessToken.from_hash(
12
+ OAuth::Consumer.new("key", "secret", {}),
13
+ @fake_response
14
+ )
15
+ end
16
+
17
+ def test_provides_response_parameters
18
+ assert @access_token
19
+ assert_respond_to @access_token, :params
20
+ end
21
+
22
+ def test_access_token_makes_non_oauth_response_params_available
23
+ assert_not_nil @access_token.params[:user_id]
24
+ assert_equal 5734758743895, @access_token.params[:user_id]
25
+ end
26
+ end
@@ -0,0 +1,133 @@
1
+ gem 'actionpack','>= 3.0.0'
2
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
3
+
4
+ require 'oauth/request_proxy/action_controller_request'
5
+ require 'action_dispatch/testing/test_process'
6
+ require 'action_controller/test_case'
7
+
8
+ class ActionControllerRequestProxyTest < Test::Unit::TestCase
9
+ def request_proxy(request_method = :get, uri_params = {}, body_params = {})
10
+ request = ActionController::TestRequest.new
11
+ request.request_uri = '/'
12
+
13
+ case request_method
14
+ when :post
15
+ request.env['REQUEST_METHOD'] = 'POST'
16
+ when :put
17
+ request.env['REQUEST_METHOD'] = 'PUT'
18
+ end
19
+
20
+ request.env['REQUEST_URI'] = '/'
21
+ request.env['RAW_POST_DATA'] = body_params.to_query
22
+ request.env['QUERY_STRING'] = body_params.to_query
23
+ request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
24
+
25
+ yield request if block_given?
26
+ OAuth::RequestProxy.proxy(request, :parameters => uri_params)
27
+ end
28
+
29
+ def test_that_proxy_simple_get_request_works_with_query_params
30
+ request_proxy = request_proxy(:get, {'key'=>'value'})
31
+
32
+ expected_parameters = [["key", "value"]]
33
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
34
+ assert_equal 'GET', request_proxy.method
35
+ end
36
+
37
+ def test_that_proxy_simple_post_request_works_with_query_params
38
+ request_proxy = request_proxy(:post, {'key'=>'value'})
39
+
40
+ expected_parameters = [["key", "value"]]
41
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
42
+ assert_equal 'POST', request_proxy.method
43
+ end
44
+
45
+ def test_that_proxy_simple_put_request_works_with_query_params
46
+ request_proxy = request_proxy(:put, {'key'=>'value'})
47
+
48
+ expected_parameters = [["key", "value"]]
49
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
50
+ assert_equal 'PUT', request_proxy.method
51
+ end
52
+
53
+ def test_that_proxy_simple_get_request_works_with_post_params
54
+ request_proxy = request_proxy(:get, {}, {'key'=>'value'})
55
+
56
+ expected_parameters = []
57
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
58
+ assert_equal 'GET', request_proxy.method
59
+ end
60
+
61
+ def test_that_proxy_simple_post_request_works_with_post_params
62
+ request_proxy = request_proxy(:post, {}, {'key'=>'value'})
63
+
64
+ expected_parameters = [["key", "value"]]
65
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
66
+ assert_equal 'POST', request_proxy.method
67
+ end
68
+
69
+ def test_that_proxy_simple_put_request_works_with_post_params
70
+ request_proxy = request_proxy(:put, {}, {'key'=>'value'})
71
+
72
+ expected_parameters = []
73
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
74
+ assert_equal 'PUT', request_proxy.method
75
+ end
76
+
77
+ def test_that_proxy_simple_get_request_works_with_mixed_params
78
+ request_proxy = request_proxy(:get, {'key'=>'value'}, {'key2'=>'value2'})
79
+
80
+ expected_parameters = [["key", "value"]]
81
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
82
+ assert_equal 'GET', request_proxy.method
83
+ end
84
+
85
+ def test_that_proxy_simple_post_request_works_with_mixed_params
86
+ request_proxy = request_proxy(:post, {'key'=>'value'}, {'key2'=>'value2'})
87
+
88
+ expected_parameters = [["key", "value"],["key2", "value2"]]
89
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
90
+ assert_equal 'POST', request_proxy.method
91
+ end
92
+
93
+ def test_that_proxy_simple_put_request_works_with_mixed_params
94
+ request_proxy = request_proxy(:put, {'key'=>'value'}, {'key2'=>'value2'})
95
+
96
+ expected_parameters = [["key", "value"]]
97
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
98
+ assert_equal 'PUT', request_proxy.method
99
+ end
100
+
101
+ def test_parameter_keys_should_preserve_brackets_from_hash
102
+ assert_equal(
103
+ [["message[body]", "This is a test"]],
104
+ request_proxy(:post, { :message => { :body => 'This is a test' }}).parameters_for_signature
105
+ )
106
+ end
107
+
108
+ def test_parameter_values_with_amps_should_not_break_parameter_parsing
109
+ assert_equal(
110
+ [['message[body]', 'http://foo.com/?a=b&c=d']],
111
+ request_proxy(:post, { :message => { :body => 'http://foo.com/?a=b&c=d'}}).parameters_for_signature
112
+ )
113
+ end
114
+
115
+ def test_parameter_keys_should_preserve_brackets_from_array
116
+ assert_equal(
117
+ [["foo[]", "123"], ["foo[]", "456"]],
118
+ request_proxy(:post, { :foo => [123, 456] }).parameters_for_signature.sort
119
+ )
120
+ end
121
+
122
+ # TODO disabled; ActionController::TestRequest does not appear to parse
123
+ # QUERY_STRING
124
+ def x_test_query_string_parameter_values_should_be_cgi_unescaped
125
+ request = request_proxy do |r|
126
+ r.env['QUERY_STRING'] = 'url=http%3A%2F%2Ffoo.com%2F%3Fa%3Db%26c%3Dd'
127
+ end
128
+ assert_equal(
129
+ [['url', 'http://foo.com/?a=b&c=d']],
130
+ request.parameters_for_signature.sort
131
+ )
132
+ end
133
+ end
@@ -0,0 +1,159 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+ require 'mocha'
3
+
4
+ require 'stringio'
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
+ :proxy=>"http://user:password@proxy.bla:8080",
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 "http://user:password@proxy.bla:8080",@consumer.proxy
35
+ assert_equal "/oauth/example/request_token.php",@consumer.request_token_path
36
+ assert_equal "/oauth/example/access_token.php",@consumer.access_token_path
37
+ assert_equal "http://blabla.bla/oauth/example/request_token.php",@consumer.request_token_url
38
+ assert_equal "http://blabla.bla/oauth/example/access_token.php",@consumer.access_token_url
39
+ assert_equal "http://blabla.bla/oauth/example/authorize.php",@consumer.authorize_url
40
+ assert_equal :header,@consumer.scheme
41
+ assert_equal :get,@consumer.http_method
42
+ end
43
+
44
+ def test_defaults
45
+ @consumer=OAuth::Consumer.new(
46
+ "key",
47
+ "secret",
48
+ {
49
+ :site=>"http://twitter.com"
50
+ })
51
+ assert_equal "key",@consumer.key
52
+ assert_equal "secret",@consumer.secret
53
+ assert_equal "http://twitter.com",@consumer.site
54
+ assert_nil @consumer.proxy
55
+ assert_equal "/oauth/request_token",@consumer.request_token_path
56
+ assert_equal "/oauth/access_token",@consumer.access_token_path
57
+ assert_equal "http://twitter.com/oauth/request_token",@consumer.request_token_url
58
+ assert_equal "http://twitter.com/oauth/access_token",@consumer.access_token_url
59
+ assert_equal "http://twitter.com/oauth/authorize",@consumer.authorize_url
60
+ assert_equal :header,@consumer.scheme
61
+ assert_equal :post,@consumer.http_method
62
+ end
63
+
64
+ def test_override_paths
65
+ @consumer=OAuth::Consumer.new(
66
+ "key",
67
+ "secret",
68
+ {
69
+ :site=>"http://twitter.com",
70
+ :request_token_url=>"http://oauth.twitter.com/request_token",
71
+ :access_token_url=>"http://oauth.twitter.com/access_token",
72
+ :authorize_url=>"http://site.twitter.com/authorize"
73
+ })
74
+ assert_equal "key",@consumer.key
75
+ assert_equal "secret",@consumer.secret
76
+ assert_equal "http://twitter.com",@consumer.site
77
+ assert_equal "/oauth/request_token",@consumer.request_token_path
78
+ assert_equal "/oauth/access_token",@consumer.access_token_path
79
+ assert_equal "http://oauth.twitter.com/request_token",@consumer.request_token_url
80
+ assert_equal "http://oauth.twitter.com/access_token",@consumer.access_token_url
81
+ assert_equal "http://site.twitter.com/authorize",@consumer.authorize_url
82
+ assert_equal :header,@consumer.scheme
83
+ assert_equal :post,@consumer.http_method
84
+ end
85
+
86
+ def test_that_token_response_should_be_uri_parameter_format_as_default
87
+ @consumer.expects(:request).returns(create_stub_http_response("oauth_token=token&oauth_token_secret=secret"))
88
+
89
+ hash = @consumer.token_request(:get, "")
90
+
91
+ assert_equal "token", hash[:oauth_token]
92
+ assert_equal "secret", hash[:oauth_token_secret]
93
+ end
94
+
95
+ def test_can_provided_a_block_to_interpret_token_response
96
+ @consumer.expects(:request).returns(create_stub_http_response)
97
+
98
+ hash = @consumer.token_request(:get, '') {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
99
+
100
+ assert_equal 'token', hash[:oauth_token]
101
+ assert_equal 'secret', hash[:oauth_token_secret]
102
+ end
103
+
104
+ def test_that_can_provide_a_block_to_interpret_a_request_token_response
105
+ @consumer.expects(:request).returns(create_stub_http_response)
106
+
107
+ token = @consumer.get_request_token {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
108
+
109
+ assert_equal 'token', token.token
110
+ assert_equal 'secret', token.secret
111
+ end
112
+
113
+ def test_that_block_is_not_mandatory_for_getting_an_access_token
114
+ stub_token = mock
115
+ @consumer.expects(:request).returns(create_stub_http_response("oauth_token=token&oauth_token_secret=secret"))
116
+
117
+ token = @consumer.get_access_token(stub_token)
118
+
119
+ assert_equal 'token', token.token
120
+ assert_equal 'secret', token.secret
121
+ end
122
+
123
+ def test_that_can_provide_a_block_to_interpret_an_access_token_response
124
+ stub_token = mock
125
+ @consumer.expects(:request).returns(create_stub_http_response)
126
+
127
+ token = @consumer.get_access_token(stub_token) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
128
+
129
+ assert_equal 'token', token.token
130
+ assert_equal 'secret', token.secret
131
+ end
132
+
133
+ def test_that_not_setting_ignore_callback_will_include_oauth_callback_in_request_options
134
+ request_options = {}
135
+ @consumer.stubs(:request).returns(create_stub_http_response)
136
+
137
+ @consumer.get_request_token(request_options) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
138
+
139
+ assert_equal 'oob', request_options[:oauth_callback]
140
+ end
141
+
142
+ def test_that_setting_ignore_callback_will_exclude_oauth_callback_in_request_options
143
+ request_options = { :exclude_callback=> true }
144
+ @consumer.stubs(:request).returns(create_stub_http_response)
145
+
146
+ @consumer.get_request_token(request_options) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
147
+
148
+ assert_nil request_options[:oauth_callback]
149
+ end
150
+
151
+ private
152
+
153
+ def create_stub_http_response expected_body=nil
154
+ stub_http_response = stub
155
+ stub_http_response.stubs(:code).returns(200)
156
+ stub_http_response.stubs(:body).tap {|expectation| expectation.returns(expected_body) unless expected_body.nil? }
157
+ return stub_http_response
158
+ end
159
+ end
@@ -0,0 +1,14 @@
1
+ require 'test/unit'
2
+
3
+ $LOAD_PATH << File.dirname(__FILE__) + '/../lib/'
4
+ require 'oauth'
5
+
6
+ # require File.dirname(__FILE__) + '/../lib/oauth'
7
+
8
+ begin
9
+ # load redgreen unless running from within TextMate (in which case ANSI
10
+ # color codes mess with the output)
11
+ require 'redgreen' unless ENV['TM_CURRENT_LINE']
12
+ rescue LoadError
13
+ nil
14
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
2
+
3
+ class TestSignatureHmacSha1 < Test::Unit::TestCase
4
+ def test_that_hmac_sha1_implements_hmac_sha1
5
+ assert OAuth::Signature.available_methods.include?('hmac-sha1')
6
+ end
7
+
8
+ def test_that_get_request_from_oauth_test_cases_produces_matching_signature
9
+ 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')
10
+
11
+ consumer = OAuth::Consumer.new('dpf43f3p2l4k3l03', 'kd94hf93k423kf44')
12
+ token = OAuth::Token.new('nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00')
13
+
14
+ signature = OAuth::Signature.sign(request, { :consumer => consumer,
15
+ :token => token,
16
+ :uri => 'http://photos.example.net/photos' } )
17
+
18
+ assert_equal 'tR3+Ty81lMeYAr/Fid0kMTYa/WM=', signature
19
+ end
20
+ end
@@ -0,0 +1,224 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
2
+
3
+ class NetHTTPClientTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @consumer = OAuth::Consumer.new('consumer_key_86cad9', '5888bf0345e5d237')
7
+ @token = OAuth::Token.new('token_411a7f', '3196ffd991c8ebdb')
8
+ @request_uri = URI.parse('http://example.com/test?key=value')
9
+ @request_parameters = { 'key' => 'value' }
10
+ @nonce = 225579211881198842005988698334675835446
11
+ @timestamp = "1199645624"
12
+ @http = Net::HTTP.new(@request_uri.host, @request_uri.port)
13
+ end
14
+
15
+ def test_that_using_auth_headers_on_get_requests_works
16
+ request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
17
+ request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
18
+
19
+ assert_equal 'GET', request.method
20
+ assert_equal '/test?key=value', request.path
21
+ assert_equal "OAuth", request['authorization'][0..4]
22
+ assert_equal "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'][6..-1].split(', ').sort
23
+ end
24
+
25
+ def test_that_using_auth_headers_on_get_requests_works_with_plaintext
26
+ require 'oauth/signature/plaintext'
27
+ c = OAuth::Consumer.new('consumer_key_86cad9', '5888bf0345e5d237',{
28
+ :signature_method => 'PLAINTEXT'
29
+ })
30
+ request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
31
+ request.oauth!(@http, c, @token, {:nonce => @nonce, :timestamp => @timestamp, :signature_method => 'PLAINTEXT'})
32
+
33
+ assert_equal 'GET', request.method
34
+ assert_equal '/test?key=value', request.path
35
+ assert_equal "OAuth", request['authorization'][0..4]
36
+ assert_equal "oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"PLAINTEXT\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"5888bf0345e5d237%263196ffd991c8ebdb\", oauth_version=\"1.0\"".split(', ').sort, request['authorization'][6..-1].split(', ').sort
37
+ end
38
+
39
+ def test_that_using_auth_headers_on_post_requests_works
40
+ request = Net::HTTP::Post.new(@request_uri.path)
41
+ request.set_form_data( @request_parameters )
42
+ request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
43
+
44
+ assert_equal 'POST', request.method
45
+ assert_equal '/test', request.path
46
+ assert_equal 'key=value', request.body
47
+ assert_equal "OAuth", request['authorization'][0..4]
48
+ assert_equal "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'][6..-1].split(', ').sort
49
+ end
50
+
51
+ def test_that_version_is_added_to_existing_user_agent
52
+ request = Net::HTTP::Post.new(@request_uri.path)
53
+ request['User-Agent'] = "MyApp"
54
+ request.set_form_data( @request_parameters )
55
+ request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
56
+
57
+ assert_equal "MyApp (OAuth gem v#{OAuth::VERSION})", request['User-Agent']
58
+ end
59
+
60
+ def test_that_version_is_set_when_no_user_agent
61
+ request = Net::HTTP::Post.new(@request_uri.path)
62
+ request.set_form_data( @request_parameters )
63
+ request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
64
+
65
+ assert_match /OAuth gem v#{OAuth::VERSION}/, request['User-Agent']
66
+ end
67
+
68
+ def test_that_using_get_params_works
69
+ request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
70
+ request.oauth!(@http, @consumer, @token, {:scheme => 'query_string', :nonce => @nonce, :timestamp => @timestamp})
71
+
72
+ assert_equal 'GET', request.method
73
+ uri = URI.parse(request.path)
74
+ assert_equal '/test', uri.path
75
+ assert_equal nil, uri.fragment
76
+ 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("&")
77
+ assert_equal nil, request['authorization']
78
+ end
79
+
80
+ def test_that_using_get_params_works_with_plaintext
81
+ request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
82
+ request.oauth!(@http, @consumer, @token, {:scheme => 'query_string', :nonce => @nonce, :timestamp => @timestamp, :signature_method => 'PLAINTEXT'})
83
+
84
+ assert_equal 'GET', request.method
85
+ uri = URI.parse(request.path)
86
+ assert_equal '/test', uri.path
87
+ assert_equal nil, uri.fragment
88
+ assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=5888bf0345e5d237%263196ffd991c8ebdb&oauth_signature_method=PLAINTEXT&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", uri.query.split("&").sort.join("&")
89
+ assert_equal nil, request['authorization']
90
+ end
91
+
92
+ def test_that_using_post_params_works
93
+ request = Net::HTTP::Post.new(@request_uri.path)
94
+ request.set_form_data( @request_parameters )
95
+ request.oauth!(@http, @consumer, @token, {:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp})
96
+
97
+ assert_equal 'POST', request.method
98
+ assert_equal '/test', request.path
99
+ 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("&")
100
+ assert_equal nil, request['authorization']
101
+ end
102
+
103
+ def test_that_using_post_params_works_with_plaintext
104
+ request = Net::HTTP::Post.new(@request_uri.path)
105
+ request.set_form_data( @request_parameters )
106
+ request.oauth!(@http, @consumer, @token, {:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp, :signature_method => 'PLAINTEXT'})
107
+
108
+ assert_equal 'POST', request.method
109
+ assert_equal '/test', request.path
110
+ assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=5888bf0345e5d237%263196ffd991c8ebdb&oauth_signature_method=PLAINTEXT&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", request.body.split("&").sort.join("&")
111
+ assert_equal nil, request['authorization']
112
+ end
113
+
114
+ def test_that_using_post_with_uri_params_works
115
+ request = Net::HTTP::Post.new(@request_uri.path + "?" + request_parameters_to_s)
116
+ request.oauth!(@http, @consumer, @token, {:scheme => 'query_string', :nonce => @nonce, :timestamp => @timestamp})
117
+
118
+ assert_equal 'POST', request.method
119
+ uri = URI.parse(request.path)
120
+ assert_equal '/test', uri.path
121
+ assert_equal nil, uri.fragment
122
+ 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('&')
123
+ assert_equal nil, request.body
124
+ assert_equal nil, request['authorization']
125
+ end
126
+
127
+ def test_that_using_post_with_uri_and_form_params_works
128
+ request = Net::HTTP::Post.new(@request_uri.path + "?" + request_parameters_to_s)
129
+ request.set_form_data( { 'key2' => 'value2' } )
130
+ request.oauth!(@http, @consumer, @token, {:scheme => :query_string, :nonce => @nonce, :timestamp => @timestamp})
131
+
132
+ assert_equal 'POST', request.method
133
+ uri = URI.parse(request.path)
134
+ assert_equal '/test', uri.path
135
+ assert_equal nil, uri.fragment
136
+ 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('&')
137
+ assert_equal "key2=value2", request.body
138
+ assert_equal nil, request['authorization']
139
+ end
140
+
141
+
142
+ def test_example_from_specs
143
+ consumer=OAuth::Consumer.new("dpf43f3p2l4k3l03","kd94hf93k423kf44")
144
+ token = OAuth::Token.new('nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00')
145
+ request_uri = URI.parse('http://photos.example.net/photos?file=vacation.jpg&size=original')
146
+ nonce = 'kllo9940pd9333jh'
147
+ timestamp = "1191242096"
148
+ http = Net::HTTP.new(request_uri.host, request_uri.port)
149
+
150
+ request = Net::HTTP::Get.new(request_uri.path + "?" + request_uri.query)
151
+ signature_base_string=request.signature_base_string(http, consumer, token, {:nonce => nonce, :timestamp => timestamp})
152
+ 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
153
+
154
+ # request = Net::HTTP::Get.new(request_uri.path + "?" + request_uri.query)
155
+ request.oauth!(http, consumer, token, {:nonce => nonce, :timestamp => timestamp,:realm=>"http://photos.example.net/"})
156
+
157
+ assert_equal 'GET', request.method
158
+ 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
159
+
160
+ end
161
+
162
+ def test_step_by_step_token_request
163
+ consumer=OAuth::Consumer.new(
164
+ "key",
165
+ "secret")
166
+ request_uri = URI.parse('http://term.ie/oauth/example/request_token.php')
167
+ nonce = rand(2**128).to_s
168
+ timestamp = Time.now.to_i.to_s
169
+ http = Net::HTTP.new(request_uri.host, request_uri.port)
170
+
171
+ request = Net::HTTP::Get.new(request_uri.path)
172
+ signature_base_string=request.signature_base_string(http, consumer, nil, {:scheme=>:query_string,:nonce => nonce, :timestamp => timestamp})
173
+ 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
174
+
175
+ # request = Net::HTTP::Get.new(request_uri.path)
176
+ request.oauth!(http, consumer, nil, {:scheme=>:query_string,:nonce => nonce, :timestamp => timestamp})
177
+ assert_equal 'GET', request.method
178
+ assert_nil request.body
179
+ assert_nil request['authorization']
180
+ # 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']
181
+
182
+ response=http.request(request)
183
+ assert_equal "200",response.code
184
+ # assert_equal request['authorization'],response.body
185
+ assert_equal "oauth_token=requestkey&oauth_token_secret=requestsecret",response.body
186
+ end
187
+
188
+ def test_that_put_bodies_not_signed
189
+ request = Net::HTTP::Put.new(@request_uri.path)
190
+ request.body = "<?xml version=\"1.0\"?><foo><bar>baz</bar></foo>"
191
+ request["Content-Type"] = "application/xml"
192
+ signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
193
+ 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
194
+ end
195
+
196
+ def test_that_put_bodies_not_signed_even_if_form_urlencoded
197
+ request = Net::HTTP::Put.new(@request_uri.path)
198
+ request.set_form_data( { 'key2' => 'value2' } )
199
+ signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
200
+ 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
201
+ end
202
+
203
+ def test_that_post_bodies_signed_if_form_urlencoded
204
+ request = Net::HTTP::Post.new(@request_uri.path)
205
+ request.set_form_data( { 'key2' => 'value2' } )
206
+ signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
207
+ 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
208
+ end
209
+
210
+ def test_that_post_bodies_not_signed_if_other_content_type
211
+ request = Net::HTTP::Post.new(@request_uri.path)
212
+ request.body = "<?xml version=\"1.0\"?><foo><bar>baz</bar></foo>"
213
+ request["Content-Type"] = "application/xml"
214
+ signature_base_string=request.signature_base_string(@http, @consumer, nil, { :nonce => @nonce, :timestamp => @timestamp })
215
+ 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
216
+ end
217
+
218
+ protected
219
+
220
+ def request_parameters_to_s
221
+ @request_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
222
+ end
223
+
224
+ end