oauth 0.1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oauth might be problematic. Click here for more details.

@@ -0,0 +1,34 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
23
+
24
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
25
+ task :install_gem_no_doc => [:clean, :package] do
26
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
27
+ end
28
+
29
+ namespace :manifest do
30
+ desc 'Recreate Manifest.txt to include ALL files'
31
+ task :refresh do
32
+ `rake check_manifest | patch -p0 > Manifest.txt`
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
@@ -0,0 +1,17 @@
1
+ desc 'Generate website files'
2
+ task :website_generate => :ruby_env do
3
+ (Dir['website/**/*.txt'] - Dir['website/version*.txt']).each do |txt|
4
+ sh %{ #{RUBY_APP} script/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
5
+ end
6
+ end
7
+
8
+ desc 'Upload website files to rubyforge'
9
+ task :website_upload do
10
+ host = "#{rubyforge_username}@rubyforge.org"
11
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
12
+ local_dir = 'website'
13
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
14
+ end
15
+
16
+ desc 'Generate and upload website files'
17
+ task :website => [:website_generate, :website_upload, :publish_docs]
@@ -0,0 +1,116 @@
1
+ require 'test/unit'
2
+ require 'oauth'
3
+
4
+ # This performs testing against Andy Smith's test server http://term.ie/oauth/example/
5
+ # Thanks Andy.
6
+ # This also means you have to be online to be able to run these.
7
+ class ConsumerTest < Test::Unit::TestCase
8
+ def setup
9
+ @consumer=OAuth::Consumer.new( {
10
+ :consumer_key=>"key",
11
+ :consumer_secret=>"secret",
12
+ :site=>"http://term.ie",
13
+ :request_token_path=>"/oauth/example/request_token.php",
14
+ :access_token_path=>"/oauth/example/access_token.php",
15
+ :authorize_path=>"/oauth/example/authorize.php",
16
+ :auth_method=>:query,
17
+ :http_method=>:get
18
+ })
19
+ end
20
+
21
+ def test_initializer
22
+ assert_equal "key",@consumer.key
23
+ assert_equal "secret",@consumer.secret
24
+ assert_equal "http://term.ie",@consumer.site
25
+ assert_equal "/oauth/example/request_token.php",@consumer.request_token_path
26
+ assert_equal "/oauth/example/access_token.php",@consumer.access_token_path
27
+ assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
28
+ assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
29
+ assert_equal :query,@consumer.auth_method
30
+ assert_equal :get,@consumer.http_method
31
+ end
32
+
33
+ def test_defaults
34
+ @consumer=OAuth::Consumer.new( {
35
+ :consumer_key=>"key",
36
+ :consumer_secret=>"secret",
37
+ :site=>"http://twitter.com"
38
+ })
39
+ assert_equal "key",@consumer.key
40
+ assert_equal "secret",@consumer.secret
41
+ assert_equal "http://twitter.com",@consumer.site
42
+ assert_equal "/oauth/request_token",@consumer.request_token_path
43
+ assert_equal "/oauth/access_token",@consumer.access_token_path
44
+ assert_equal "http://twitter.com/oauth/request_token",@consumer.request_token_url
45
+ assert_equal "http://twitter.com/oauth/access_token",@consumer.access_token_url
46
+ assert_equal :authorize,@consumer.auth_method
47
+ assert_equal :post,@consumer.http_method
48
+ end
49
+
50
+ def test_create_request
51
+ request=@consumer.create_request :get,'/oauth/example/request_token.php'
52
+ assert_equal 'http://term.ie/oauth/example/request_token.php',request.url
53
+ assert "key",request[:consumer_key]
54
+ assert !request.signed?
55
+ end
56
+
57
+ def test_create_post_request
58
+ request=@consumer.create_request(:post,'/oauth/example',{:oauth_token=>"token"},"BODY")
59
+ assert_equal "token",request[:oauth_token]
60
+ assert_equal "BODY",request.body
61
+ end
62
+
63
+ def test_create_put_request
64
+ request=@consumer.create_request(:put,'/oauth/example',{:oauth_token=>"token"},"BODY")
65
+ assert_equal "token",request[:oauth_token]
66
+ assert_equal "BODY",request.body
67
+ end
68
+
69
+ def test_signed_request
70
+ request=@consumer.signed_request :get,'/oauth/example/request_token.php'
71
+ assert_equal 'http://term.ie/oauth/example/request_token.php',request.url
72
+ assert "key",request[:consumer_key]
73
+ assert request.signed?
74
+ assert request.verify?(@consumer.secret)
75
+ end
76
+
77
+ def test_create_signed_post_request
78
+ request=@consumer.signed_request(:post,'/oauth/example',{:oauth_token=>"token"},'token secret',"BODY")
79
+ assert_equal "token",request[:oauth_token]
80
+ assert_equal "BODY",request.body
81
+ assert request.signed?
82
+ assert request.verify?(@consumer.secret,'token secret')
83
+ end
84
+
85
+ def test_create_signed_put_request
86
+ request=@consumer.signed_request(:put,'/oauth/example',{:oauth_token=>"token"},'token secret',"BODY")
87
+ assert_equal "token",request[:oauth_token]
88
+ assert_equal "BODY",request.body
89
+ assert request.signed?
90
+ assert request.verify?(@consumer.secret,'token secret')
91
+ end
92
+
93
+ def test_get_token_sequence
94
+ @request_token=@consumer.get_request_token
95
+ assert_not_nil @request_token
96
+ assert_equal "requestkey",@request_token.token
97
+ assert_equal "requestsecret",@request_token.secret
98
+ assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
99
+
100
+ @access_token=@request_token.get_access_token
101
+ assert_not_nil @access_token
102
+ assert_equal "accesskey",@access_token.token
103
+ assert_equal "accesssecret",@access_token.secret
104
+
105
+ @response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
106
+ assert_not_nil @response
107
+ assert_equal "200",@response.code
108
+ assert_equal( "ok=hello&test=this",@response.body)
109
+
110
+ @response=@access_token.post("/oauth/example/echo_api.php","ok=hello&test=this")
111
+ assert_not_nil @response
112
+ assert_equal "200",@response.code
113
+ assert_equal( "ok=hello&test=this",@response.body)
114
+
115
+ end
116
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/oauth'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestOauth < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,282 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'action_controller'
4
+ require 'action_controller/test_process'
5
+ require 'oauth'
6
+ class RequestTest < Test::Unit::TestCase
7
+ include OAuth::Key
8
+ include OAuth::OAuthTestHelper
9
+
10
+ def setup
11
+ @request=OAuth::Request.new( :get,"http://test.COM:80","/oauth?stuff=1&picture=test.png", {:realm=>'http://test.com/oauth/authorize',:oauth_field1=>"test",:oauth_field2=>"hello",'string_key'=>"should be set"})
12
+ end
13
+
14
+ def test_accessors
15
+ #as symbols
16
+ assert_equal @request[:oauth_field1],"test"
17
+ assert_equal @request[:oauth_field2],"hello"
18
+ assert_equal @request[:string_key],"should be set"
19
+ assert_equal @request[:oauth_signature_method],"HMAC-SHA1"
20
+ #as strings
21
+ assert_equal @request['oauth_field1'],"test"
22
+ assert_equal @request['oauth_field2'],"hello"
23
+ assert_equal @request['string_key'],"should be set"
24
+ end
25
+
26
+ def test_to_query
27
+ assert_equal "oauth_field1=test&oauth_field2=hello&oauth_nonce=#{URI.escape(@request.nonce)}&oauth_signature_method=HMAC-SHA1&oauth_timestamp=#{@request.timestamp}&oauth_version=1.0&picture=test.png&string_key=should%20be%20set&stuff=1",@request.to_query
28
+ end
29
+
30
+ def test_to_auth_string
31
+ assert_equal "OAuth realm=\"http://test.com/oauth/authorize\", oauth_field1=\"test\", oauth_field2=\"hello\", oauth_nonce=\"#{URI.escape(@request.nonce)}\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"#{@request.timestamp}\", oauth_version=\"1.0\", string_key=\"should%20be%20set\"",@request.to_auth_string
32
+ end
33
+
34
+ def test_has_http_method
35
+ assert_equal "GET",@request.http_method
36
+ end
37
+
38
+ def test_has_realm
39
+ assert_equal 'http://test.com/oauth/authorize',@request.realm
40
+ end
41
+
42
+ def test_removed_realm_from_params
43
+ assert_nil @request[:realm]
44
+ end
45
+
46
+ def test_url_normalization
47
+ #should remove port 80 from http
48
+ assert_equal "http://test.com:80/oauth?stuff=1&picture=test.png",@request.url
49
+ assert_equal "http://test.com/oauth",@request.normalized_url
50
+
51
+ # should not have port
52
+ @request.site="http://test.com"
53
+ assert_equal "http://test.com/oauth?stuff=1&picture=test.png",@request.url
54
+ assert_equal "http://test.com/oauth",@request.normalized_url
55
+
56
+ #should remove port 443 from https
57
+ @request.site="https://test.com:443"
58
+ assert_equal "https://test.com:443/oauth?stuff=1&picture=test.png",@request.url
59
+ assert_equal "https://test.com/oauth",@request.normalized_url
60
+
61
+ #should retain port number
62
+ @request.site="https://test.com:11822"
63
+ assert_equal "https://test.com:11822/oauth?stuff=1&picture=test.png",@request.url
64
+ assert_equal "https://test.com:11822/oauth",@request.normalized_url
65
+
66
+ # should retain port 80 on https
67
+ @request.site="https://test.com:80"
68
+ assert_equal "https://test.com:80/oauth?stuff=1&picture=test.png",@request.url
69
+ assert_equal "https://test.com:80/oauth",@request.normalized_url
70
+
71
+ # should retain port 443 on http
72
+ @request.site="http://test.com:443"
73
+ assert_equal "http://test.com:443/oauth?stuff=1&picture=test.png",@request.url
74
+ assert_equal "http://test.com:443/oauth",@request.normalized_url
75
+
76
+ end
77
+
78
+ def test_auth_methods_on_various_http_methods
79
+ # defaults
80
+ assert_equal :authorize,create_request(:get).auth_method
81
+ assert_equal :authorize,create_request(:head).auth_method
82
+ assert_equal :authorize,create_request(:delete).auth_method
83
+ assert_equal :authorize,create_request(:post).auth_method
84
+ assert_equal :authorize,create_request(:put).auth_method
85
+
86
+ # authorize
87
+ assert_equal :authorize,create_request(:get,{:auth_method=>:authorize}).auth_method
88
+ assert_equal :authorize,create_request(:head,{:auth_method=>:authorize}).auth_method
89
+ assert_equal :authorize,create_request(:delete,{:auth_method=>:authorize}).auth_method
90
+ assert_equal :authorize,create_request(:post,{:auth_method=>:authorize}).auth_method
91
+ assert_equal :authorize,create_request(:put,{:auth_method=>:authorize}).auth_method
92
+
93
+ # query
94
+ assert_equal :query,create_request(:get,{:auth_method=>:query}).auth_method
95
+ assert_equal :query,create_request(:head,{:auth_method=>:query}).auth_method
96
+ assert_equal :query,create_request(:delete,{:auth_method=>:query}).auth_method
97
+ assert_equal :authorize,create_request(:post,{:auth_method=>:query}).auth_method
98
+ assert_equal :authorize,create_request(:put,{:auth_method=>:query}).auth_method
99
+
100
+ # post
101
+ assert_equal :authorize,create_request(:get,{:auth_method=>:post}).auth_method
102
+ assert_equal :authorize,create_request(:head,{:auth_method=>:post}).auth_method
103
+ assert_equal :authorize,create_request(:delete,{:auth_method=>:post}).auth_method
104
+ assert_equal :post,create_request(:post,{:auth_method=>:post}).auth_method
105
+ assert_equal :post,create_request(:put,{:auth_method=>:post}).auth_method
106
+ end
107
+
108
+ def create_request(http_method,params={},*arguments)
109
+ OAuth::Request.new( http_method,'http://photos.example.net','/test',{
110
+ :oauth_consumer_key=>"dpf43f3p2l4k3l03"
111
+ }.merge(params),*arguments)
112
+ end
113
+
114
+ def test_has_body
115
+ request=create_request(:post,{},"BODY")
116
+ assert_equal "BODY",request.body
117
+
118
+ end
119
+ def test_has_nonce
120
+ assert_not_nil @request.nonce
121
+ end
122
+
123
+ def test_has_timestamp
124
+ assert_not_nil @request.timestamp
125
+ end
126
+
127
+ def test_not_signed
128
+ assert !@request.signed?
129
+ end
130
+
131
+ def test_has_signature_method
132
+ assert_equal @request.signature_method,"HMAC-SHA1"
133
+ end
134
+
135
+ def test_not_signed
136
+ assert !@request.signed?
137
+ end
138
+
139
+ def test_not_verified
140
+ assert !@request.verify?("secret")
141
+ end
142
+
143
+ def test_sign_request_token_with_query_string
144
+ @consumer_secret="kd94hf93k423kf44"
145
+ @test_params={
146
+ :oauth_consumer_key=>"dpf43f3p2l4k3l03"
147
+ }
148
+
149
+ @request=OAuth::Request.new( :get,'http://photos.example.net','/photos?file=vacation.jpg&size=original', @test_params)
150
+ assert !@request.signed?
151
+ assert !@request.verify?(@consumer_secret)
152
+ @request.sign(@consumer_secret)
153
+ assert @request.signed?
154
+ assert @request.verify?(@consumer_secret)
155
+ orig_sig=@request.signature
156
+
157
+ @incoming=mock_incoming_request_with_query(@request)
158
+ assert_equal "photos.example.net",@incoming.host_with_port
159
+ assert_equal "/photos",@incoming.path
160
+ assert_equal :get,@incoming.method
161
+ assert_equal( {"file"=>"vacation.jpg",
162
+ "size"=>"original",
163
+ "oauth_consumer_key"=>"dpf43f3p2l4k3l03",
164
+ 'oauth_timestamp'=>@request[:oauth_timestamp],
165
+ "oauth_nonce"=>@request[:oauth_nonce],
166
+ "oauth_signature_method"=>'HMAC-SHA1',
167
+ "oauth_version"=>"1.0",
168
+ "oauth_signature"=>orig_sig
169
+ },@incoming.parameters)
170
+
171
+
172
+ assert_equal "dpf43f3p2l4k3l03",OAuth::Request.extract_consumer_key(@incoming)
173
+
174
+ @request=OAuth::Request.incoming(@incoming)
175
+ assert @request.signed?
176
+ assert_equal( {
177
+ :file=>"vacation.jpg",
178
+ :size=>"original",
179
+ :oauth_consumer_key=>"dpf43f3p2l4k3l03",
180
+ :oauth_timestamp=>@request[:oauth_timestamp],
181
+ :oauth_nonce=>@request[:oauth_nonce],
182
+ :oauth_signature_method=>'HMAC-SHA1',
183
+ :oauth_version=>"1.0",
184
+ :oauth_signature=>orig_sig
185
+ },@request.to_hash)
186
+ assert @request.verify?(@consumer_secret)
187
+ assert_equal orig_sig,@request.signature
188
+ end
189
+
190
+ def test_sign_request_token_with_authorize_header
191
+ @consumer_secret="kd94hf93k423kf44"
192
+ @test_params={
193
+ :oauth_consumer_key=>"dpf43f3p2l4k3l03",
194
+ :realm=>"http://photos.example.net/oauth/authorize"
195
+ }
196
+
197
+ @request=OAuth::Request.new( :get,'http://photos.example.net','/photos?file=vacation.jpg&size=original', @test_params)
198
+ assert !@request.signed?
199
+ assert !@request.verify?(@consumer_secret)
200
+
201
+ @request.sign(@consumer_secret)
202
+ assert @request.signed?
203
+ assert @request.verify?(@consumer_secret)
204
+ assert_equal '/photos?file=vacation.jpg&size=original',@request.path
205
+ orig_sig=@request.signature
206
+ orig_base=OAuth::Signature.create(@request,@consumer_secret).base_string
207
+
208
+ orig_query_params=@request.http_parameters
209
+
210
+ @incoming=mock_incoming_request_with_authorize_header(@request)
211
+ assert_equal "photos.example.net",@incoming.host_with_port
212
+ assert_equal "/photos",@incoming.path
213
+ assert_equal :get,@incoming.method
214
+ assert_equal @request.to_auth_string, @incoming.env['HTTP_AUTHORIZATION']
215
+ assert_equal "dpf43f3p2l4k3l03",OAuth::Request.extract_consumer_key(@incoming)
216
+
217
+
218
+
219
+ @request=OAuth::Request.incoming(@incoming)
220
+ assert_equal '/photos?file=vacation.jpg&size=original',@request.path
221
+
222
+ assert_equal orig_query_params,@request.http_parameters
223
+
224
+ # test base string
225
+ new_base=OAuth::Signature.create(@request,@consumer_secret).base_string
226
+ assert_equal orig_base,new_base
227
+
228
+ assert_equal 'GET',@request.http_method
229
+ assert_equal "http://photos.example.net/photos?file=vacation.jpg&size=original",@request.url
230
+ assert @request.signed?
231
+
232
+ assert_equal( {
233
+ :file=>"vacation.jpg",
234
+ :size=>"original",
235
+ :oauth_consumer_key=>"dpf43f3p2l4k3l03",
236
+ :oauth_timestamp=>@request[:oauth_timestamp],
237
+ :oauth_nonce=>@request[:oauth_nonce],
238
+ :oauth_signature_method=>'HMAC-SHA1',
239
+ :oauth_version=>"1.0",
240
+ :oauth_signature=>orig_sig
241
+ },@request.to_hash)
242
+
243
+ assert @request.verify?(@consumer_secret)
244
+ assert_equal orig_sig,@request.signature
245
+ end
246
+
247
+ def test_sign_access_token
248
+ @consumer_secret="kd94hf93k423kf44"
249
+ @token_secret="pfkkdhi9sl3r4s00"
250
+ @test_params={
251
+ :oauth_consumer_key=>"dpf43f3p2l4k3l03",
252
+ :oauth_token=>"nnch734d00sl2jdk"
253
+ }
254
+
255
+ @request=OAuth::Request.new( :get,'http://photos.example.net','/photos?file=vacation.jpg&size=original', @test_params)
256
+ assert !@request.signed?
257
+ assert !@request.verify?(@consumer_secret,@token_secret)
258
+ @request.sign(@consumer_secret,@token_secret)
259
+ assert @request.signed?
260
+ assert @request.verify?(@consumer_secret,@token_secret)
261
+ end
262
+
263
+ def test_sign_post_request_url_form_encoded
264
+ @consumer_secret="kd94hf93k423kf44"
265
+ @token_secret="pfkkdhi9sl3r4s00"
266
+ @test_params={
267
+ :oauth_consumer_key=>"dpf43f3p2l4k3l03",
268
+ :oauth_token=>"nnch734d00sl2jdk"
269
+ }
270
+
271
+ @request=OAuth::Request.new( :post,'http://photos.example.net','/photos', @test_params,"file=vacation.jpg&size=original")
272
+ assert_equal "application/x-www-form-urlencoded",@request.content_type
273
+ assert !@request.signed?
274
+ assert !@request.verify?(@consumer_secret,@token_secret)
275
+ @request.sign(@consumer_secret,@token_secret)
276
+ assert @request.signed?
277
+ assert @request.verify?(@consumer_secret,@token_secret)
278
+ assert_equal "file=vacation.jpg&size=original",@request.body
279
+ end
280
+
281
+
282
+ end