oauthenticator 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,676 +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
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 'HMAC-SHA256' do
235
- it 'signs with a HMAC-SHA256 digest of the signature base' do
236
- request = example_request(
237
- :token => 'a token',
238
- :token_secret => 'a token secret',
239
- :signature_method => 'HMAC-SHA256',
240
- :nonce => 'a nonce',
241
- :timestamp => 1397726597,
242
- :hash_body? => false
243
- )
244
- assert_equal('Cb4UAr3l25eqC7p2PSm0l6j7lgXvh5SPnMOhPAJ1jWU=', request.signed_protocol_params['oauth_signature'])
245
- end
246
- end
247
-
248
- describe 'RSA-SHA1' do
249
- it 'signs with a RSA private key SHA1 signature' do
250
- request = example_request(
251
- :consumer_secret => rsa_private_key,
252
- :token => 'a token',
253
- :token_secret => 'a token secret',
254
- :signature_method => 'RSA-SHA1',
255
- :nonce => 'a nonce',
256
- :timestamp => 1397726597,
257
- :hash_body? => false
258
- )
259
- assert_equal(
260
- "s3/TkrCJw54tOpsKUHkoQ9PeH1r4wB2fNb70XC2G1ef7Wb/dwwNUOhtjtpGMSDhmYQHzEPt0dAJ+PgeNs1O5NZJQB5JqdsmrhLS3ZdHx2iucxYvZSuDNi0GxaEepz5VS9rg+y5Gmep60BpAKhX0KGnkMY9HIhomTPSrYidAfDOE=",
261
- request.signed_protocol_params['oauth_signature']
262
- )
263
- end
264
-
265
- it 'ignores the token secret' do
266
- request_attrs = {
267
- :consumer_secret => rsa_private_key,
268
- :token => 'a token',
269
- :signature_method => 'RSA-SHA1',
270
- :nonce => 'a nonce',
271
- :timestamp => 1397726597,
272
- }
273
- request1 = example_request(request_attrs.merge(:token_secret => 'a token secret'))
274
- request2 = example_request(request_attrs.merge(:token_secret => 'an entirely different token secret'))
275
- assert_equal(request1.signature, request2.signature)
276
- assert_equal(request1.authorization, request2.authorization)
277
- end
278
-
279
- describe 'with an invalid key' do
280
- it 'errors' do
281
- assert_raises(OpenSSL::PKey::RSAError) { example_request(:signature_method => 'RSA-SHA1').signature }
282
- end
283
- end
284
- end
285
- end
286
-
287
- describe 'protocol_params' do
288
- it 'includes given protocol params with an oauth_ prefix' do
289
- OAuthenticator::SignableRequest::PROTOCOL_PARAM_KEYS.each do |param_key|
290
- assert_equal(example_request(param_key => 'a value').protocol_params["oauth_#{param_key}"], 'a value')
291
- end
292
- end
293
- it 'does not include a calculated signature' do
294
- assert !example_request.protocol_params.key?('oauth_signature')
295
- end
296
- it 'does include the signature of a given authorization' do
297
- assert_equal('a signature', example_signed_request('oauth_signature' => 'a signature').protocol_params['oauth_signature'])
298
- end
299
- it 'does include unknown parameters of a given authorization' do
300
- assert_equal('bar', example_signed_request('foo' => 'bar').protocol_params['foo'])
301
- end
302
- end
303
-
304
- describe 'signed_protocol_params' do
305
- it 'includes a signature' do
306
- assert_equal 'a%20consumer%20secret&', example_request.signed_protocol_params['oauth_signature']
307
- end
308
-
309
- it 'has a different signature than the given authorization if the given authorization is wrong' do
310
- request = example_signed_request({
311
- 'oauth_consumer_key' => 'a consumer key',
312
- 'oauth_signature' => 'wrong%20secret&',
313
- 'oauth_signature_method' => 'PLAINTEXT',
314
- },
315
- {:consumer_secret => 'a consumer secret'}
316
- )
317
- refute_equal(
318
- request.protocol_params['oauth_signature'],
319
- request.signed_protocol_params['oauth_signature']
320
- )
321
- end
322
- end
323
-
324
- describe 'uri, per section 3.4.1.2' do
325
- it 'lowercases scheme and host' do
326
- [
327
- 'http://example.com/FooBar',
328
- 'Http://Example.com/FooBar',
329
- 'HTTP://EXAMPLE.cOM/FooBar',
330
- ].each do |uri|
331
- assert_equal('http://example.com/FooBar', example_request(:uri => uri).send(:base_string_uri))
332
- end
333
- end
334
-
335
- it 'normalizes port' do
336
- assert_equal('http://example.com/F', example_request(:uri => 'http://example.com/F').send(:base_string_uri))
337
- assert_equal('http://example.com/F', example_request(:uri => 'http://example.com:80/F').send(:base_string_uri))
338
- assert_equal('http://example.com:81/F', example_request(:uri => 'http://example.com:81/F').send(:base_string_uri))
339
- assert_equal('https://example.com/F', example_request(:uri => 'https://example.com/F').send(:base_string_uri))
340
- assert_equal('https://example.com/F', example_request(:uri => 'https://example.com:443/F').send(:base_string_uri))
341
- assert_equal('https://example.com:444/F', example_request(:uri => 'https://example.com:444/F').send(:base_string_uri))
342
- end
343
-
344
- it 'excludes query and fragment' do
345
- assert_equal('http://example.com/FooBar', example_request(:uri => 'http://example.com/FooBar?foo=bar#foobar').send(:base_string_uri))
346
- assert_equal('http://example.com/FooBar', example_request(:uri => 'http://example.com/FooBar#foobar').send(:base_string_uri))
347
- end
348
- end
349
-
350
- it 'accepts string or symbol request methods' do
351
- {'GET' => [:get, :Get, :GET, 'GeT', 'get'], 'OPTIONS' => [:options, 'Options']}.each do |norm, variants|
352
- variants.each do |request_method|
353
- assert_equal(norm, example_request(:request_method => request_method).send(:normalized_request_method))
354
- end
355
- end
356
- end
357
-
358
- describe 'body' do
359
- it 'takes a string' do
360
- assert_equal('abody', example_request(:body => 'abody').send(:read_body))
361
- end
362
- it 'takes an IO' do
363
- assert_equal('abody', example_request(:body => StringIO.new('abody')).send(:read_body))
364
- end
365
- it 'takes nil' do
366
- assert_equal('', example_request(:body => nil).send(:read_body))
367
- end
368
- it 'rejects something else' do
369
- assert_raises(TypeError) { example_request(:body => Object.new).send(:read_body) }
370
- end
371
- it 'calculates their authorization the same' do
372
- request_io_body = example_request(:body => StringIO.new('abody'))
373
- request_str_body = example_request(:body => 'abody')
374
- assert_equal(request_io_body.authorization, request_str_body.authorization)
375
- end
376
- end
377
-
378
- describe 'signature_base' do
379
- it 'includes unrecognized authorization params when calculating signature base' do
380
- authorization = %q(OAuth realm="Example",
381
- oauth_foo="bar",
382
- oauth_consumer_key="9djdj82h48djs9d2",
383
- oauth_signature_method="HMAC-SHA1",
384
- oauth_timestamp="137131201",
385
- oauth_nonce="7d8f3e4a"
386
- )
387
- assert(example_signed_request(OAuthenticator.parse_authorization(authorization)).send(:signature_base).include?("oauth_foo%3Dbar"))
388
- end
389
-
390
- it 'does include body in a formencoded request' do
391
- assert(example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar').send(:signature_base).include?('foo'))
392
- end
393
-
394
- it 'does include body in a formencoded request with alternate capitalization' do
395
- assert(example_request(:media_type => 'APPLICATION/X-WWW-FORM-URLENCODED', :body => 'foo=bar').send(:signature_base).include?('foo'))
396
- end
397
-
398
- it 'does not include body in a non-formencoded request' do
399
- assert(!example_request(:media_type => 'text/plain', :body => 'foo=bar').send(:signature_base).include?('foo'))
400
- end
401
- end
402
-
403
- describe 'normalized request params' do
404
- describe 'normalized request params string' do
405
- # this is effectively tested by #authorization so we won't here
406
- end
407
- describe 'protocol params' do
408
- it 'does not include realm with a new request' do
409
- request = example_request(:realm => 'everywhere')
410
- assert(!request.send(:normalized_request_params).any? { |k,v| k.downcase == 'realm' })
411
- end
412
- it 'does not include realm with a previously-signed request' do
413
- request = example_signed_request('realm' => 'somewhere')
414
- assert(!request.send(:normalized_request_params).any? { |k,v| k.downcase == 'realm' })
415
- end
416
- it 'does not include signature' do
417
- request = example_signed_request('oauth_signature' => 'totallylegit', 'foo' => 'bar')
418
- assert(!request.send(:normalized_request_params).any? { |k,v| k.downcase == 'oauth_signature' })
419
- end
420
- it 'does include all other given params' do
421
- request = example_signed_request(
422
- 'realm' => 'somewhere',
423
- 'foo' => 'bar',
424
- 'oauth_signature' => 'totallylegit',
425
- 'oauth_timestamp' => '137131201'
426
- )
427
- [['foo', 'bar'], ['oauth_timestamp', '137131201']].each do |pair|
428
- assert(request.send(:normalized_request_params).include?(pair))
429
- end
430
- end
431
- end
432
- describe 'query params' do
433
- it 'goes into normalized request params' do
434
- request = example_request(:uri => 'http://example.com/?a=b&c=d&e=&f')
435
- [['a', 'b'], ['c', 'd'], ['e', ''], ['f', nil]].each do |pair|
436
- assert(request.send(:normalized_request_params).include?(pair))
437
- end
438
- end
439
- it 'is empty with no query' do
440
- request = example_request(:uri => 'http://example.com/')
441
- assert_equal([], request.send(:query_params))
442
- end
443
- it 'decodes a + sign' do
444
- request = example_request(:uri => 'http://example.com/?a+key=a+value')
445
- assert_equal([['a key', 'a value']], request.send(:query_params))
446
- end
447
- it 'decodes %-encoded' do
448
- request = example_request(:uri => 'http://example.com/?a%20key=a%20value')
449
- assert_equal([['a key', 'a value']], request.send(:query_params))
450
- end
451
- it 'includes form encoded keys with an = sign and no value' do
452
- request = example_request(:uri => 'http://example.com/?a=')
453
- assert_equal([['a', '']], request.send(:query_params))
454
- end
455
- it 'includes form encoded keys with no = sign and no value' do
456
- request = example_request(:uri => 'http://example.com/?a')
457
- assert_equal([['a', nil]], request.send(:query_params))
458
- end
459
- end
460
- describe 'entity params' do
461
- it 'goes into normalized request params' do
462
- request = example_request(:body => 'a=b&c=d&e=&f', :media_type => 'application/x-www-form-urlencoded')
463
- [['a', 'b'], ['c', 'd'], ['e', ''], ['f', nil]].each do |pair|
464
- assert(request.send(:normalized_request_params).include?(pair))
465
- end
466
- end
467
- it 'includes all form encoded params' do
468
- request = example_request(:body => 'a=b&c=d', :media_type => 'application/x-www-form-urlencoded')
469
- assert_equal([['a', 'b'], ['c', 'd']], request.send(:entity_params))
470
- end
471
- it 'includes no non-form encoded params' do
472
- request = example_request(:body => 'a=b&c=d', :media_type => 'text/plain')
473
- assert_equal([], request.send(:entity_params))
474
- end
475
- it 'does not parse nested params' do
476
- request = example_request(:body => 'a[b]=c', :media_type => 'application/x-www-form-urlencoded')
477
- assert_equal([['a[b]', 'c']], request.send(:entity_params))
478
- end
479
- it 'decodes a + sign' do
480
- request = example_request(:body => 'a+key=a+value', :media_type => 'application/x-www-form-urlencoded')
481
- assert_equal([['a key', 'a value']], request.send(:entity_params))
482
- end
483
- it 'decodes %-encoded keys and values' do
484
- request = example_request(:body => 'a%20key=a%20value', :media_type => 'application/x-www-form-urlencoded')
485
- assert_equal([['a key', 'a value']], request.send(:entity_params))
486
- end
487
- it 'includes form encoded keys with an = sign and no value' do
488
- request = example_request(:body => 'a=', :media_type => 'application/x-www-form-urlencoded')
489
- assert_equal([['a', '']], request.send(:entity_params))
490
- end
491
- it 'includes form encoded keys with no = sign and no value' do
492
- request = example_request(:body => 'a', :media_type => 'application/x-www-form-urlencoded')
493
- assert_equal([['a', nil]], request.send(:entity_params))
494
- end
495
- end
496
- end
497
-
498
- describe 'body hash' do
499
- describe 'default inclusion' do
500
- it 'includes by default with non-form-encoded and HMAC-SHA1' do
501
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'HMAC-SHA1')
502
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', request.protocol_params['oauth_body_hash'])
503
- end
504
- it 'includes by default with non-form-encoded and HMAC-SHA256' do
505
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'HMAC-SHA256')
506
- assert_equal('O6iQfnolIydIjfOQ7VF8Rblt6tAzYAIZvcpxB9HT+Io=', request.protocol_params['oauth_body_hash'])
507
- end
508
- it 'includes by default with non-form-encoded and RSA-SHA1' do
509
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'RSA-SHA1', :consumer_secret => rsa_private_key)
510
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', request.protocol_params['oauth_body_hash'])
511
- end
512
- it 'does not include by default with non-form-encoded and PLAINTEXT' do
513
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'PLAINTEXT')
514
- assert(!request.protocol_params.key?('oauth_body_hash'))
515
- end
516
- it 'does not include by default with form-encoded and HMAC-SHA1' do
517
- request = example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar', :signature_method => 'HMAC-SHA1')
518
- assert(!request.protocol_params.key?('oauth_body_hash'))
519
- end
520
- it 'does not include by default with form-encoded and HMAC-SHA256' do
521
- request = example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar', :signature_method => 'HMAC-SHA256')
522
- assert(!request.protocol_params.key?('oauth_body_hash'))
523
- end
524
- it 'does not include by default with form-encoded and RSA-SHA1' do
525
- request = example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar', :signature_method => 'RSA-SHA1', :consumer_secret => rsa_private_key)
526
- assert(!request.protocol_params.key?('oauth_body_hash'))
527
- end
528
- it 'does not include by default with form-encoded and PLAINTEXT' do
529
- request = example_request(:media_type => 'application/x-www-form-urlencoded', :body => 'foo=bar', :signature_method => 'PLAINTEXT')
530
- assert(!request.protocol_params.key?('oauth_body_hash'))
531
- end
532
- end
533
- it 'respects the :hash_body? option' do
534
- attributes = {:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'HMAC-SHA1'}
535
- # ensure these would generate the hash by default, without :hash_body?
536
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', example_request(attributes).protocol_params['oauth_body_hash'])
537
- assert(!example_request(attributes.merge(:hash_body? => false)).protocol_params.key?('oauth_body_hash'))
538
- assert_equal('L7j0ARXdHmlcviPU+Xzlsftpfu4=', example_request(attributes.merge(:hash_body? => true)).protocol_params['oauth_body_hash'])
539
- end
540
- it 'does not generate a body hash when given a authorization' do
541
- assert(!example_signed_request({}).protocol_params.key?('oauth_body_hash'))
542
- end
543
-
544
- describe '#body_hash' do
545
- it 'is the same as goes in protocol params when generated' do
546
- request = example_request(:media_type => 'text/plain', :body => 'foo=bar', :signature_method => 'HMAC-SHA1')
547
- assert_equal(request.protocol_params['oauth_body_hash'], request.body_hash)
548
- end
549
- it 'matches the given protocol params for a valid request' do
550
- request = example_signed_request(
551
- {'oauth_body_hash' => 'Lve95gjOVATpfV8EL5X4nxwjKHE=', 'oauth_signature_method' => 'HMAC-SHA1'},
552
- :body => 'Hello World!', :media_type => 'text/plain'
553
- )
554
- assert_equal(request.protocol_params['oauth_body_hash'], request.body_hash)
555
- end
556
- it 'is different than the given protocol params for an invalid request' do
557
- request = example_signed_request(
558
- {'oauth_body_hash' => 'helloooooo?=', 'oauth_signature_method' => 'HMAC-SHA1'},
559
- :body => 'Hello World!', :media_type => 'text/plain'
560
- )
561
- refute_equal(request.protocol_params['oauth_body_hash'], request.body_hash)
562
- end
563
- it 'returns nil for an unsupported signature method' do
564
- assert_equal(nil, example_request(:signature_method => 'PLAINTEXT').body_hash)
565
- end
566
- end
567
-
568
- describe 'example appendix A1' do
569
- let :request do
570
- OAuthenticator::SignableRequest.new({
571
- :request_method => 'PUT',
572
- :uri => 'http://www.example.com/resource',
573
- :media_type => 'text/plain',
574
- :body => 'Hello World!',
575
- :signature_method => 'HMAC-SHA1',
576
- :token => "token",
577
- :consumer_key => "consumer",
578
- :timestamp => "1236874236",
579
- :nonce => "10369470270925",
580
- })
581
- end
582
- it 'has the same oauth body hash' do
583
- assert_equal('Lve95gjOVATpfV8EL5X4nxwjKHE=', request.signed_protocol_params['oauth_body_hash'])
584
- end
585
- it 'has the same signature base' do
586
- assert_equal(
587
- %q(PUT&http%3A%2F%2Fwww.example.com%2Fresource&oauth_body_hash%3D) +
588
- %q(Lve95gjOVATpfV8EL5X4nxwjKHE%253D%26oauth_consumer_key%3Dconsum) +
589
- %q(er%26oauth_nonce%3D10369470270925%26oauth_signature_method%3DH) +
590
- %q(MAC-SHA1%26oauth_timestamp%3D1236874236%26oauth_token%3Dtoken%) +
591
- %q(26oauth_version%3D1.0),
592
- request.send(:signature_base)
593
- )
594
- end
595
- end
596
- describe 'example appendix A2' do
597
- let :request do
598
- OAuthenticator::SignableRequest.new({
599
- :request_method => 'GET',
600
- :uri => 'http://www.example.com/resource',
601
- :media_type => nil,
602
- :body => nil,
603
- :signature_method => 'HMAC-SHA1',
604
- :token => "token",
605
- :consumer_key => "consumer",
606
- :timestamp => "1238395022",
607
- :nonce => "8628868109991",
608
- })
609
- end
610
- it 'has the same oauth body hash' do
611
- assert_equal('2jmj7l5rSw0yVb/vlWAYkK/YBwk=', request.signed_protocol_params['oauth_body_hash'])
612
- end
613
- it 'has the same signature base' do
614
- assert_equal(
615
- %q(GET&http%3A%2F%2Fwww.example.com%2Fresource&oauth_body_hash%3D2jmj7) +
616
- %q(l5rSw0yVb%252FvlWAYkK%252FYBwk%253D%26oauth_consumer_key%3Dconsumer) +
617
- %q(%26oauth_nonce%3D8628868109991%26oauth_signature_method%3DHMAC-SHA1) +
618
- %q(%26oauth_timestamp%3D1238395022%26oauth_token%3Dtoken%26oauth_versi) +
619
- %q(on%3D1.0),
620
- request.send(:signature_base)
621
- )
622
- end
623
- end
624
- end
625
-
626
- it 'reproduces a successful OAuth example GET (lifted from simple oauth)' do
627
- request = OAuthenticator::SignableRequest.new(
628
- :request_method => :get,
629
- :uri => 'http://photos.example.net/photos',
630
- :media_type => 'application/x-www-form-urlencoded',
631
- :body => 'file=vacaction.jpg&size=original',
632
- :consumer_key => 'dpf43f3p2l4k3l03',
633
- :consumer_secret => rsa_private_key,
634
- :nonce => '13917289812797014437',
635
- :signature_method => 'RSA-SHA1',
636
- :timestamp => '1196666512'
637
- )
638
- expected_protocol_params = {
639
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
640
- "oauth_nonce" => "13917289812797014437",
641
- "oauth_signature" => "jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=",
642
- "oauth_signature_method" => "RSA-SHA1",
643
- "oauth_timestamp" => "1196666512",
644
- "oauth_version" => "1.0",
645
- }
646
-
647
- assert_equal(expected_protocol_params, request.signed_protocol_params)
648
- end
649
-
650
- it 'reproduces a successful OAuth example GET (lifted from simple oauth)' do
651
- request = OAuthenticator::SignableRequest.new(
652
- :request_method => :get,
653
- :uri => 'http://host.net/resource?name=value',
654
- :media_type => 'application/x-www-form-urlencoded',
655
- :body => 'name=value',
656
- :consumer_key => 'abcd',
657
- :consumer_secret => 'efgh',
658
- :token => 'ijkl',
659
- :token_secret => 'mnop',
660
- :nonce => 'oLKtec51GQy',
661
- :signature_method => 'PLAINTEXT',
662
- :timestamp => '1286977095'
663
- )
664
- expected_protocol_params = {
665
- "oauth_consumer_key" => "abcd",
666
- "oauth_nonce" => "oLKtec51GQy",
667
- "oauth_signature" => "efgh&mnop",
668
- "oauth_signature_method" => "PLAINTEXT",
669
- "oauth_timestamp" => "1286977095",
670
- "oauth_token" => "ijkl",
671
- "oauth_version" => "1.0"
672
- }
673
-
674
- assert_equal(expected_protocol_params, request.signed_protocol_params)
675
- end
676
- end