oauthenticator 1.3.5 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,654 +0,0 @@
1
- # encoding: utf-8
2
- proc { |p| $:.unshift(p) unless $:.any? { |lp| File.expand_path(lp) == p } }.call(File.expand_path('.', File.dirname(__FILE__)))
3
- require 'helper'
4
-
5
- describe OAuthenticator::SignableRequest do
6
- let :base_example_initialize_attrs do
7
- {
8
- :request_method => 'get',
9
- :uri => 'http://example.com',
10
- :media_type => 'text/plain',
11
- :body => 'hi there',
12
- }
13
- end
14
- let :example_initialize_attrs do
15
- base_example_initialize_attrs.merge({
16
- :consumer_key => 'a consumer key',
17
- :consumer_secret => 'a consumer secret',
18
- :signature_method => 'PLAINTEXT'
19
- })
20
- end
21
-
22
- def example_request(attributes={})
23
- OAuthenticator::SignableRequest.new(example_initialize_attrs.reject do |k,_|
24
- attributes.keys.any? { |ak| ak.to_s == k.to_s }
25
- end.merge(attributes))
26
- end
27
-
28
- def example_signed_request(authorization, attributes={})
29
- attributes = attributes.merge(:authorization => authorization)
30
- OAuthenticator::SignableRequest.new(base_example_initialize_attrs.reject do |k,_|
31
- attributes.keys.any? { |ak| ak.to_s == k.to_s }
32
- end.merge(attributes))
33
- end
34
-
35
- let :rsa_private_key do
36
- %q(
37
- -----BEGIN PRIVATE KEY-----
38
- MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALRiMLAh9iimur8V
39
- A7qVvdqxevEuUkW4K+2KdMXmnQbG9Aa7k7eBjK1S+0LYmVjPKlJGNXHDGuy5Fw/d
40
- 7rjVJ0BLB+ubPK8iA/Tw3hLQgXMRRGRXXCn8ikfuQfjUS1uZSatdLB81mydBETlJ
41
- hI6GH4twrbDJCR2Bwy/XWXgqgGRzAgMBAAECgYBYWVtleUzavkbrPjy0T5FMou8H
42
- X9u2AC2ry8vD/l7cqedtwMPp9k7TubgNFo+NGvKsl2ynyprOZR1xjQ7WgrgVB+mm
43
- uScOM/5HVceFuGRDhYTCObE+y1kxRloNYXnx3ei1zbeYLPCHdhxRYW7T0qcynNmw
44
- rn05/KO2RLjgQNalsQJBANeA3Q4Nugqy4QBUCEC09SqylT2K9FrrItqL2QKc9v0Z
45
- zO2uwllCbg0dwpVuYPYXYvikNHHg+aCWF+VXsb9rpPsCQQDWR9TT4ORdzoj+Nccn
46
- qkMsDmzt0EfNaAOwHOmVJ2RVBspPcxt5iN4HI7HNeG6U5YsFBb+/GZbgfBT3kpNG
47
- WPTpAkBI+gFhjfJvRw38n3g/+UeAkwMI2TJQS4n8+hid0uus3/zOjDySH3XHCUno
48
- cn1xOJAyZODBo47E+67R4jV1/gzbAkEAklJaspRPXP877NssM5nAZMU0/O/NGCZ+
49
- 3jPgDUno6WbJn5cqm8MqWhW1xGkImgRk+fkDBquiq4gPiT898jusgQJAd5Zrr6Q8
50
- AO/0isr/3aa6O6NLQxISLKcPDk2NOccAfS/xOtfOz4sJYM3+Bs4Io9+dZGSDCA54
51
- Lw03eHTNQghS0A==
52
- -----END PRIVATE KEY-----
53
- ).strip.split("\n").map(&:strip).join("\n")
54
- end
55
-
56
- describe 'initialize' do
57
- describe 'default attributes' do
58
- describe 'with any signature method' do
59
- OAuthenticator::SignableRequest::SIGNATURE_METHODS.keys.each do |signature_method|
60
- it("defaults to version 1.0 with #{signature_method}") do
61
- request = example_request(:signature_method => signature_method)
62
- assert_equal('1.0', request.protocol_params['oauth_version'])
63
- end
64
- it("lets you omit version if you really want to with #{signature_method}") do
65
- request = example_request(:version => nil, :signature_method => signature_method)
66
- assert(!request.protocol_params.key?('oauth_version'))
67
- end
68
- end
69
- end
70
- describe 'not plaintext' do
71
- it('generates nonces') do
72
- nonces = 2.times.map do
73
- example_request(:signature_method => 'HMAC-SHA1').protocol_params['oauth_nonce']
74
- end
75
- assert_equal(2, nonces.uniq.compact.size)
76
- end
77
- it 'generates timestamp' do
78
- Timecop.freeze Time.at 1391021695
79
- request = example_request(:signature_method => 'HMAC-SHA1')
80
- assert_equal 1391021695.to_s, request.protocol_params['oauth_timestamp']
81
- end
82
- end
83
- describe 'plaintext' do
84
- it('does not generate nonces') do
85
- request = example_request(:signature_method => 'PLAINTEXT')
86
- assert(!request.protocol_params.key?('oauth_nonce'))
87
- end
88
- it 'does not generate timestamp' do
89
- request = example_request(:signature_method => 'PLAINTEXT')
90
- assert(!request.protocol_params.key?('oauth_timestamp'))
91
- end
92
- end
93
- end
94
-
95
- it 'accepts string and symbol' do
96
- initialize_attr_variants = [
97
- # by string
98
- example_initialize_attrs.map { |k,v| {k.to_s => v} }.inject({}, &:update),
99
- # by symbol
100
- example_initialize_attrs.map { |k,v| {k.to_sym => v} }.inject({}, &:update),
101
- # random mix
102
- example_initialize_attrs.map { |k,v| {rand(2) == 0 ? k.to_s : k.to_sym => v} }.inject({}, &:update),
103
- ]
104
- authorizations = initialize_attr_variants.map do |attrs|
105
- OAuthenticator::SignableRequest.new(attrs).authorization
106
- end
107
- assert_equal(1, authorizations.uniq.size)
108
- end
109
-
110
- it 'checks type' do
111
- assert_raises(TypeError) { OAuthenticator::SignableRequest.new("hello!") }
112
- end
113
-
114
- it 'checks authorization type' do
115
- assert_raises(TypeError) { example_request(:authorization => "hello!") }
116
- end
117
-
118
- it 'does not allow protocol parameters to be specified when authorization is specified' do
119
- OAuthenticator::SignableRequest::PROTOCOL_PARAM_KEYS.map do |key|
120
- assert_raises(ArgumentError) do
121
- example_signed_request({}, key => 'val')
122
- end
123
- end
124
- end
125
-
126
- describe 'required attributes' do
127
- it 'complains about missing required params' do
128
- err = assert_raises(ArgumentError) { OAuthenticator::SignableRequest.new({}) }
129
- %w(request_method uri media_type body consumer_key signature_method).each do |required|
130
- assert_match /#{required}/, err.message
131
- end
132
- end
133
- end
134
- end
135
-
136
- describe 'the example in 3.1' do
137
- # a request with attributes from the oauth spec
138
- def spec_request(attributes={})
139
- example_request({
140
- :request_method => 'POST',
141
- :uri => 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b',
142
- :media_type => 'application/x-www-form-urlencoded',
143
- :body => 'c2&a3=2+q',
144
- :consumer_key => '9djdj82h48djs9d2',
145
- :token => 'kkk9d7dh3k39sjv7',
146
- :consumer_secret => 'j49sk3j29djd',
147
- :token_secret => 'dh893hdasih9',
148
- :signature_method => 'HMAC-SHA1',
149
- :timestamp => '137131201',
150
- :nonce => '7d8f3e4a',
151
- :version => nil,
152
- :realm => "Example",
153
- })
154
- end
155
-
156
- it 'has the same signature base string' do
157
- spec_signature_base = (
158
- "POST&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q" +
159
- "%26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_" +
160
- "key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_m" +
161
- "ethod%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk" +
162
- "9d7dh3k39sjv7"
163
- )
164
- assert_equal(spec_signature_base, spec_request.send(:signature_base))
165
- end
166
-
167
- it 'has the same normalized parameters' do
168
- spec_normalized_request_params_string = (
169
- "a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9dj" +
170
- "dj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1" +
171
- "&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7"
172
- )
173
- assert_equal(spec_normalized_request_params_string, spec_request.send(:normalized_request_params_string))
174
-
175
- end
176
-
177
- it 'calculates authorization the same' do
178
- # a keen observer may note that the signature is different than the one in the actual spec. the spec is
179
- # in error - see http://www.rfc-editor.org/errata_search.php?rfc=5849
180
- spec_authorization = OAuthenticator.parse_authorization(%q(OAuth realm="Example",
181
- oauth_consumer_key="9djdj82h48djs9d2",
182
- oauth_token="kkk9d7dh3k39sjv7",
183
- oauth_signature_method="HMAC-SHA1",
184
- oauth_timestamp="137131201",
185
- oauth_nonce="7d8f3e4a",
186
- oauth_signature="r6%2FTJjbCOr97%2F%2BUU0NsvSne7s5g%3D"
187
- ))
188
- assert_equal(spec_authorization, spec_request.signed_protocol_params)
189
- end
190
- end
191
-
192
- describe '#authorization' do
193
- it 'has the parameter name followed by an = and a quoted encoded value' do
194
- many_characters = %q( !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~Ā)
195
- authorization = example_request(:consumer_key => many_characters).authorization
196
- # only alphas, numerics, and -._~ remain unencoded per 3.6
197
- # hexes are uppercase
198
- assert authorization.include?(%q(consumer_key="%20%21%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%C4%80"))
199
- end
200
-
201
- it 'generally looks like: OAuth key="quoted-value", anotherkey="anothervalue"' do
202
- assert_equal(%q(OAuth ) +
203
- %q(oauth_consumer_key="a%20consumer%20key", ) +
204
- %q(oauth_signature="a%2520consumer%2520secret%26", ) +
205
- %q(oauth_signature_method="PLAINTEXT", ) +
206
- %q(oauth_version="1.0"),
207
- example_request.authorization
208
- )
209
- end
210
- end
211
-
212
- describe 'signature' do
213
- describe 'PLAINTEXT' do
214
- it 'signs with the consumer and token secrets, encoded and &-joined' do
215
- request = example_request(:token => 'a token', :token_secret => 'a token secret', :signature_method => 'PLAINTEXT')
216
- assert_equal('a%20consumer%20secret&a%20token%20secret', request.signed_protocol_params['oauth_signature'])
217
- end
218
- end
219
-
220
- describe 'HMAC-SHA1' do
221
- it 'signs with a HMAC-SHA1 digest of the signature base' do
222
- request = example_request(
223
- :token => 'a token',
224
- :token_secret => 'a token secret',
225
- :signature_method => 'HMAC-SHA1',
226
- :nonce => 'a nonce',
227
- :timestamp => 1397726597,
228
- :hash_body? => false
229
- )
230
- assert_equal('rVKcy4CgAih1kv4HAMGiNnjmUJk=', request.signed_protocol_params['oauth_signature'])
231
- end
232
- end
233
-
234
- describe 'RSA-SHA1' do
235
- it 'signs with a RSA private key SHA1 signature' do
236
- request = example_request(
237
- :consumer_secret => rsa_private_key,
238
- :token => 'a token',
239
- :token_secret => 'a token secret',
240
- :signature_method => 'RSA-SHA1',
241
- :nonce => 'a nonce',
242
- :timestamp => 1397726597,
243
- :hash_body? => false
244
- )
245
- assert_equal(
246
- "s3/TkrCJw54tOpsKUHkoQ9PeH1r4wB2fNb70XC2G1ef7Wb/dwwNUOhtjtpGMSDhmYQHzEPt0dAJ+PgeNs1O5NZJQB5JqdsmrhLS3ZdHx2iucxYvZSuDNi0GxaEepz5VS9rg+y5Gmep60BpAKhX0KGnkMY9HIhomTPSrYidAfDOE=",
247
- request.signed_protocol_params['oauth_signature']
248
- )
249
- end
250
-
251
- it 'ignores the token secret' do
252
- request_attrs = {
253
- :consumer_secret => rsa_private_key,
254
- :token => 'a token',
255
- :signature_method => 'RSA-SHA1',
256
- :nonce => 'a nonce',
257
- :timestamp => 1397726597,
258
- }
259
- request1 = example_request(request_attrs.merge(:token_secret => 'a token secret'))
260
- request2 = example_request(request_attrs.merge(:token_secret => 'an entirely different token secret'))
261
- assert_equal(request1.signature, request2.signature)
262
- assert_equal(request1.authorization, request2.authorization)
263
- end
264
-
265
- describe 'with an invalid key' do
266
- it 'errors' do
267
- assert_raises(OpenSSL::PKey::RSAError) { example_request(:signature_method => 'RSA-SHA1').signature }
268
- end
269
- end
270
- end
271
- end
272
-
273
- describe 'protocol_params' do
274
- it 'includes given protocol params with an oauth_ prefix' do
275
- OAuthenticator::SignableRequest::PROTOCOL_PARAM_KEYS.each do |param_key|
276
- assert_equal(example_request(param_key => 'a value').protocol_params["oauth_#{param_key}"], 'a value')
277
- end
278
- end
279
- it 'does not include a calculated signature' do
280
- assert !example_request.protocol_params.key?('oauth_signature')
281
- end
282
- it 'does include the signature of a given authorization' do
283
- assert_equal('a signature', example_signed_request('oauth_signature' => 'a signature').protocol_params['oauth_signature'])
284
- end
285
- it 'does include unknown parameters of a given authorization' do
286
- assert_equal('bar', example_signed_request('foo' => 'bar').protocol_params['foo'])
287
- end
288
- end
289
-
290
- describe 'signed_protocol_params' do
291
- it 'includes a signature' do
292
- assert_equal 'a%20consumer%20secret&', example_request.signed_protocol_params['oauth_signature']
293
- end
294
-
295
- it 'has a different signature than the given authorization if the given authorization is wrong' do
296
- request = example_signed_request({
297
- 'oauth_consumer_key' => 'a consumer key',
298
- 'oauth_signature' => 'wrong%20secret&',
299
- 'oauth_signature_method' => 'PLAINTEXT',
300
- },
301
- {:consumer_secret => 'a consumer secret'}
302
- )
303
- refute_equal(
304
- request.protocol_params['oauth_signature'],
305
- request.signed_protocol_params['oauth_signature']
306
- )
307
- end
308
- end
309
-
310
- describe 'uri, per section 3.4.1.2' do
311
- it 'lowercases scheme and host' do
312
- [
313
- 'http://example.com/FooBar',
314
- 'Http://Example.com/FooBar',
315
- 'HTTP://EXAMPLE.cOM/FooBar',
316
- ].each do |uri|
317
- assert_equal('http://example.com/FooBar', example_request(:uri => uri).send(:base_string_uri))
318
- end
319
- end
320
-
321
- it 'normalizes port' do
322
- assert_equal('http://example.com/F', example_request(:uri => 'http://example.com/F').send(:base_string_uri))
323
- assert_equal('http://example.com/F', example_request(:uri => 'http://example.com:80/F').send(:base_string_uri))
324
- assert_equal('http://example.com:81/F', example_request(:uri => 'http://example.com:81/F').send(:base_string_uri))
325
- assert_equal('https://example.com/F', example_request(:uri => 'https://example.com/F').send(:base_string_uri))
326
- assert_equal('https://example.com/F', example_request(:uri => 'https://example.com:443/F').send(:base_string_uri))
327
- assert_equal('https://example.com:444/F', example_request(:uri => 'https://example.com:444/F').send(:base_string_uri))
328
- end
329
-
330
- it 'excludes query and fragment' do
331
- assert_equal('http://example.com/FooBar', example_request(:uri => 'http://example.com/FooBar?foo=bar#foobar').send(:base_string_uri))
332
- assert_equal('http://example.com/FooBar', example_request(:uri => 'http://example.com/FooBar#foobar').send(:base_string_uri))
333
- end
334
- end
335
-
336
- it 'accepts string or symbol request methods' do
337
- {'GET' => [:get, :Get, :GET, 'GeT', 'get'], 'OPTIONS' => [:options, 'Options']}.each do |norm, variants|
338
- variants.each do |request_method|
339
- assert_equal(norm, example_request(:request_method => request_method).send(:normalized_request_method))
340
- end
341
- end
342
- end
343
-
344
- describe 'body' do
345
- it 'takes a string' do
346
- assert_equal('abody', example_request(:body => 'abody').send(:read_body))
347
- end
348
- it 'takes an IO' do
349
- assert_equal('abody', example_request(:body => StringIO.new('abody')).send(:read_body))
350
- end
351
- it 'takes nil' do
352
- assert_equal('', example_request(:body => nil).send(:read_body))
353
- end
354
- it 'rejects something else' do
355
- assert_raises(TypeError) { example_request(:body => Object.new).send(:read_body) }
356
- end
357
- it 'calculates their authorization the same' do
358
- request_io_body = example_request(:body => StringIO.new('abody'))
359
- request_str_body = example_request(:body => 'abody')
360
- assert_equal(request_io_body.authorization, request_str_body.authorization)
361
- end
362
- end
363
-
364
- describe 'signature_base' do
365
- it 'includes unrecognized authorization params when calculating signature base' do
366
- authorization = %q(OAuth realm="Example",
367
- oauth_foo="bar",
368
- oauth_consumer_key="9djdj82h48djs9d2",
369
- oauth_signature_method="HMAC-SHA1",
370
- oauth_timestamp="137131201",
371
- oauth_nonce="7d8f3e4a"
372
- )
373
- assert(example_signed_request(OAuthenticator.parse_authorization(authorization)).send(:signature_base).include?("oauth_foo%3Dbar"))
374
- end
375
-
376
- it 'does include body in a formencoded request' do
377
- assert(example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar').send(:signature_base).include?('foo'))
378
- end
379
-
380
- it 'does include body in a formencoded request with alternate capitalization' do
381
- assert(example_request(:media_type => 'APPLICATION/X-WWW-FORM-URLENCODED', :body => 'foo=bar').send(:signature_base).include?('foo'))
382
- end
383
-
384
- it 'does not include body in a non-formencoded request' do
385
- assert(!example_request(:media_type => 'text/plain', :body => 'foo=bar').send(:signature_base).include?('foo'))
386
- end
387
- end
388
-
389
- describe 'normalized request params' do
390
- describe 'normalized request params string' do
391
- # this is effectively tested by #authorization so we won't here
392
- end
393
- describe 'protocol params' do
394
- it 'does not include realm with a new request' do
395
- request = example_request(:realm => 'everywhere')
396
- assert(!request.send(:normalized_request_params).any? { |k,v| k.downcase == 'realm' })
397
- end
398
- it 'does not include realm with a previously-signed request' do
399
- request = example_signed_request('realm' => 'somewhere')
400
- assert(!request.send(:normalized_request_params).any? { |k,v| k.downcase == 'realm' })
401
- end
402
- it 'does not include signature' do
403
- request = example_signed_request('oauth_signature' => 'totallylegit', 'foo' => 'bar')
404
- assert(!request.send(:normalized_request_params).any? { |k,v| k.downcase == 'oauth_signature' })
405
- end
406
- it 'does include all other given params' do
407
- request = example_signed_request(
408
- 'realm' => 'somewhere',
409
- 'foo' => 'bar',
410
- 'oauth_signature' => 'totallylegit',
411
- 'oauth_timestamp' => '137131201'
412
- )
413
- [['foo', 'bar'], ['oauth_timestamp', '137131201']].each do |pair|
414
- assert(request.send(:normalized_request_params).include?(pair))
415
- end
416
- end
417
- end
418
- describe 'query params' do
419
- it 'goes into normalized request params' do
420
- request = example_request(:uri => 'http://example.com/?a=b&c=d&e=&f')
421
- [['a', 'b'], ['c', 'd'], ['e', ''], ['f', nil]].each do |pair|
422
- assert(request.send(:normalized_request_params).include?(pair))
423
- end
424
- end
425
- it 'is empty with no query' do
426
- request = example_request(:uri => 'http://example.com/')
427
- assert_equal([], request.send(:query_params))
428
- end
429
- it 'decodes a + sign' do
430
- request = example_request(:uri => 'http://example.com/?a+key=a+value')
431
- assert_equal([['a key', 'a value']], request.send(:query_params))
432
- end
433
- it 'decodes %-encoded' do
434
- request = example_request(:uri => 'http://example.com/?a%20key=a%20value')
435
- assert_equal([['a key', 'a value']], request.send(:query_params))
436
- end
437
- it 'includes form encoded keys with an = sign and no value' do
438
- request = example_request(:uri => 'http://example.com/?a=')
439
- assert_equal([['a', '']], request.send(:query_params))
440
- end
441
- it 'includes form encoded keys with no = sign and no value' do
442
- request = example_request(:uri => 'http://example.com/?a')
443
- assert_equal([['a', nil]], request.send(:query_params))
444
- end
445
- end
446
- describe 'entity params' do
447
- it 'goes into normalized request params' do
448
- request = example_request(:body => 'a=b&c=d&e=&f', :media_type => 'application/x-www-form-urlencoded')
449
- [['a', 'b'], ['c', 'd'], ['e', ''], ['f', nil]].each do |pair|
450
- assert(request.send(:normalized_request_params).include?(pair))
451
- end
452
- end
453
- it 'includes all form encoded params' do
454
- request = example_request(:body => 'a=b&c=d', :media_type => 'application/x-www-form-urlencoded')
455
- assert_equal([['a', 'b'], ['c', 'd']], request.send(:entity_params))
456
- end
457
- it 'includes no non-form encoded params' do
458
- request = example_request(:body => 'a=b&c=d', :media_type => 'text/plain')
459
- assert_equal([], request.send(:entity_params))
460
- end
461
- it 'does not parse nested params' do
462
- request = example_request(:body => 'a[b]=c', :media_type => 'application/x-www-form-urlencoded')
463
- assert_equal([['a[b]', 'c']], request.send(:entity_params))
464
- end
465
- it 'decodes a + sign' do
466
- request = example_request(:body => 'a+key=a+value', :media_type => 'application/x-www-form-urlencoded')
467
- assert_equal([['a key', 'a value']], request.send(:entity_params))
468
- end
469
- it 'decodes %-encoded keys and values' do
470
- request = example_request(:body => 'a%20key=a%20value', :media_type => 'application/x-www-form-urlencoded')
471
- assert_equal([['a key', 'a value']], request.send(:entity_params))
472
- end
473
- it 'includes form encoded keys with an = sign and no value' do
474
- request = example_request(:body => 'a=', :media_type => 'application/x-www-form-urlencoded')
475
- assert_equal([['a', '']], request.send(:entity_params))
476
- end
477
- it 'includes form encoded keys with no = sign and no value' do
478
- request = example_request(:body => 'a', :media_type => 'application/x-www-form-urlencoded')
479
- assert_equal([['a', nil]], request.send(:entity_params))
480
- end
481
- end
482
- end
483
-
484
- describe 'body hash' do
485
- describe 'default inclusion' do
486
- it 'includes by default with non-form-encoded and HMAC-SHA1' do
487
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'HMAC-SHA1')
488
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', request.protocol_params['oauth_body_hash'])
489
- end
490
- it 'includes by default with non-form-encoded and RSA-SHA1' do
491
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'RSA-SHA1', :consumer_secret => rsa_private_key)
492
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', request.protocol_params['oauth_body_hash'])
493
- end
494
- it 'does not include by default with non-form-encoded and PLAINTEXT' do
495
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'PLAINTEXT')
496
- assert(!request.protocol_params.key?('oauth_body_hash'))
497
- end
498
- it 'does not include by default with form-encoded and HMAC-SHA1' do
499
- request = example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar', :signature_method => 'HMAC-SHA1')
500
- assert(!request.protocol_params.key?('oauth_body_hash'))
501
- end
502
- it 'does not include by default with form-encoded and RSA-SHA1' do
503
- request = example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar', :signature_method => 'RSA-SHA1', :consumer_secret => rsa_private_key)
504
- assert(!request.protocol_params.key?('oauth_body_hash'))
505
- end
506
- it 'does not include by default with form-encoded and PLAINTEXT' do
507
- request = example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar', :signature_method => 'PLAINTEXT')
508
- assert(!request.protocol_params.key?('oauth_body_hash'))
509
- end
510
- end
511
- it 'respects the :hash_body? option' do
512
- attributes = {:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'HMAC-SHA1'}
513
- # ensure these would generate the hash by default, without :hash_body?
514
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', example_request(attributes).protocol_params['oauth_body_hash'])
515
- assert(!example_request(attributes.merge(:hash_body? => false)).protocol_params.key?('oauth_body_hash'))
516
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', example_request(attributes.merge(:hash_body? => true)).protocol_params['oauth_body_hash'])
517
- end
518
- it 'does not generate a body hash when given a authorization' do
519
- assert(!example_signed_request({}).protocol_params.key?('oauth_body_hash'))
520
- end
521
-
522
- describe '#body_hash' do
523
- it 'is the same as goes in protocol params when generated' do
524
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'HMAC-SHA1')
525
- assert_equal(request.protocol_params['oauth_body_hash'], request.body_hash)
526
- end
527
- it 'matches the given protocol params for a valid request' do
528
- request = example_signed_request(
529
- {'oauth_body_hash' => 'Lve95gjOVATpfV8EL5X4nxwjKHE=', 'oauth_signature_method' => 'HMAC-SHA1'},
530
- :body => 'Hello World!', :media_type => 'text/plain'
531
- )
532
- assert_equal(request.protocol_params['oauth_body_hash'], request.body_hash)
533
- end
534
- it 'is different than the given protocol params for an invalid request' do
535
- request = example_signed_request(
536
- {'oauth_body_hash' => 'helloooooo?=', 'oauth_signature_method' => 'HMAC-SHA1'},
537
- :body => 'Hello World!', :media_type => 'text/plain'
538
- )
539
- refute_equal(request.protocol_params['oauth_body_hash'], request.body_hash)
540
- end
541
- it 'returns nil for an unsupported signature method' do
542
- assert_equal(nil, example_request(:signature_method => 'PLAINTEXT').body_hash)
543
- end
544
- end
545
-
546
- describe 'example appendix A1' do
547
- let :request do
548
- OAuthenticator::SignableRequest.new({
549
- :request_method => 'PUT',
550
- :uri => 'http://www.example.com/resource',
551
- :media_type => 'text/plain',
552
- :body => 'Hello World!',
553
- :signature_method => 'HMAC-SHA1',
554
- :token => "token",
555
- :consumer_key => "consumer",
556
- :timestamp => "1236874236",
557
- :nonce => "10369470270925",
558
- })
559
- end
560
- it 'has the same oauth body hash' do
561
- assert_equal('Lve95gjOVATpfV8EL5X4nxwjKHE=', request.signed_protocol_params['oauth_body_hash'])
562
- end
563
- it 'has the same signature base' do
564
- assert_equal(
565
- %q(PUT&http%3A%2F%2Fwww.example.com%2Fresource&oauth_body_hash%3D) +
566
- %q(Lve95gjOVATpfV8EL5X4nxwjKHE%253D%26oauth_consumer_key%3Dconsum) +
567
- %q(er%26oauth_nonce%3D10369470270925%26oauth_signature_method%3DH) +
568
- %q(MAC-SHA1%26oauth_timestamp%3D1236874236%26oauth_token%3Dtoken%) +
569
- %q(26oauth_version%3D1.0),
570
- request.send(:signature_base)
571
- )
572
- end
573
- end
574
- describe 'example appendix A2' do
575
- let :request do
576
- OAuthenticator::SignableRequest.new({
577
- :request_method => 'GET',
578
- :uri => 'http://www.example.com/resource',
579
- :media_type => nil,
580
- :body => nil,
581
- :signature_method => 'HMAC-SHA1',
582
- :token => "token",
583
- :consumer_key => "consumer",
584
- :timestamp => "1238395022",
585
- :nonce => "8628868109991",
586
- })
587
- end
588
- it 'has the same oauth body hash' do
589
- assert_equal('2jmj7l5rSw0yVb/vlWAYkK/YBwk=', request.signed_protocol_params['oauth_body_hash'])
590
- end
591
- it 'has the same signature base' do
592
- assert_equal(
593
- %q(GET&http%3A%2F%2Fwww.example.com%2Fresource&oauth_body_hash%3D2jmj7) +
594
- %q(l5rSw0yVb%252FvlWAYkK%252FYBwk%253D%26oauth_consumer_key%3Dconsumer) +
595
- %q(%26oauth_nonce%3D8628868109991%26oauth_signature_method%3DHMAC-SHA1) +
596
- %q(%26oauth_timestamp%3D1238395022%26oauth_token%3Dtoken%26oauth_versi) +
597
- %q(on%3D1.0),
598
- request.send(:signature_base)
599
- )
600
- end
601
- end
602
- end
603
-
604
- it 'reproduces a successful OAuth example GET (lifted from simple oauth)' do
605
- request = OAuthenticator::SignableRequest.new(
606
- :request_method => :get,
607
- :uri => 'http://photos.example.net/photos',
608
- :media_type => 'application/x-www-form-urlencoded',
609
- :body => 'file=vacaction.jpg&size=original',
610
- :consumer_key => 'dpf43f3p2l4k3l03',
611
- :consumer_secret => rsa_private_key,
612
- :nonce => '13917289812797014437',
613
- :signature_method => 'RSA-SHA1',
614
- :timestamp => '1196666512'
615
- )
616
- expected_protocol_params = {
617
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
618
- "oauth_nonce" => "13917289812797014437",
619
- "oauth_signature" => "jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=",
620
- "oauth_signature_method" => "RSA-SHA1",
621
- "oauth_timestamp" => "1196666512",
622
- "oauth_version" => "1.0",
623
- }
624
-
625
- assert_equal(expected_protocol_params, request.signed_protocol_params)
626
- end
627
-
628
- it 'reproduces a successful OAuth example GET (lifted from simple oauth)' do
629
- request = OAuthenticator::SignableRequest.new(
630
- :request_method => :get,
631
- :uri => 'http://host.net/resource?name=value',
632
- :media_type => 'application/x-www-form-urlencoded',
633
- :body => 'name=value',
634
- :consumer_key => 'abcd',
635
- :consumer_secret => 'efgh',
636
- :token => 'ijkl',
637
- :token_secret => 'mnop',
638
- :nonce => 'oLKtec51GQy',
639
- :signature_method => 'PLAINTEXT',
640
- :timestamp => '1286977095'
641
- )
642
- expected_protocol_params = {
643
- "oauth_consumer_key" => "abcd",
644
- "oauth_nonce" => "oLKtec51GQy",
645
- "oauth_signature" => "efgh&mnop",
646
- "oauth_signature_method" => "PLAINTEXT",
647
- "oauth_timestamp" => "1286977095",
648
- "oauth_token" => "ijkl",
649
- "oauth_version" => "1.0"
650
- }
651
-
652
- assert_equal(expected_protocol_params, request.signed_protocol_params)
653
- end
654
- end
@@ -1,12 +0,0 @@
1
- # encoding: utf-8
2
- proc { |p| $:.unshift(p) unless $:.any? { |lp| File.expand_path(lp) == p } }.call(File.expand_path('.', File.dirname(__FILE__)))
3
- require 'helper'
4
-
5
- # most everything about this is tested via the middlware in oauthenticator_test, so not a lot here
6
- describe OAuthenticator::SignedRequest do
7
- describe '#initialize' do
8
- it 'checks for unrecognized attributes' do
9
- assert_raises(ArgumentError) { OAuthenticator::SignedRequest.new(:foo => 'bar') }
10
- end
11
- end
12
- end