signet 0.14.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,1010 +0,0 @@
1
- # Copyright (C) 2010 Google Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- require "spec_helper"
15
- require "signet/oauth_1"
16
- require "signet/oauth_1/client"
17
- require "signet/oauth_1/credential"
18
-
19
- describe Signet::OAuth1 do
20
- it "should correctly normalize parameters" do
21
- parameters = [
22
- %w[a 1],
23
- ["c", "hi there"],
24
- %w[f 25],
25
- %w[f 50],
26
- %w[f a],
27
- %w[z p],
28
- %w[z t]
29
- ]
30
- expect(Signet::OAuth1.normalize_parameters(parameters)).to eq(
31
- "a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t"
32
- )
33
- end
34
-
35
- it "should correctly normalize parameters" do
36
- parameters = [
37
- ["b5", "=%3D"],
38
- %w[a3 a],
39
- ["c@", ""],
40
- ["a2", "r b"],
41
- %w[oauth_consumer_key 9djdj82h48djs9d2],
42
- %w[oauth_token kkk9d7dh3k39sjv7],
43
- ["oauth_signature_method", "HMAC-SHA1"],
44
- %w[oauth_timestamp 137131201],
45
- %w[oauth_nonce 7d8f3e4a],
46
- ["c2", ""],
47
- ["a3", "2 q"]
48
- ]
49
- expect(Signet::OAuth1.normalize_parameters(parameters)).to eq(
50
- "a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9dj" \
51
- "dj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1" \
52
- "&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7"
53
- )
54
- end
55
-
56
- it 'should exclude the "oauth_signature" parameter when normalizing' do
57
- parameters = [
58
- %w[a 1],
59
- %w[b 2],
60
- %w[c 3],
61
- %w[oauth_signature dpf43f3p2l4k3l03]
62
- ]
63
- expect(Signet::OAuth1.normalize_parameters(parameters)).to eq(
64
- "a=1&b=2&c=3"
65
- )
66
- end
67
-
68
- it "should raise an error if normalizing parameters with bogus values" do
69
- expect(lambda do
70
- Signet::OAuth1.normalize_parameters 42
71
- end).to raise_error(TypeError)
72
- end
73
-
74
- it "should raise an error if generating a base string with bogus values" do
75
- expect(lambda do
76
- Signet::OAuth1.generate_base_string(
77
- "GET", "http://photos.example.net/photos", 42
78
- )
79
- end).to raise_error(TypeError)
80
- end
81
-
82
- it "should correctly generate a base string" do
83
- method = "GET"
84
- uri = "http://photos.example.net/photos"
85
- parameters = {
86
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
87
- "oauth_token" => "nnch734d00sl2jdk",
88
- "oauth_signature_method" => "HMAC-SHA1",
89
- "oauth_timestamp" => "1191242096",
90
- "oauth_nonce" => "kllo9940pd9333jh",
91
- "oauth_version" => "1.0",
92
- "file" => "vacation.jpg",
93
- "size" => "original"
94
- }
95
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
96
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
97
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
98
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
99
- "oauth_signature_method%3DHMAC-SHA1%26" \
100
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
101
- "oauth_version%3D1.0%26size%3Doriginal"
102
- )
103
- end
104
-
105
- it "should correctly generate a base string with an already encoded URI" do
106
- method = "GET"
107
- uri = "http://photos.example.net/https%3A%2F%2Fwww.example.com"
108
- parameters = {
109
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
110
- "oauth_token" => "nnch734d00sl2jdk",
111
- "oauth_signature_method" => "HMAC-SHA1",
112
- "oauth_timestamp" => "1191242096",
113
- "oauth_nonce" => "kllo9940pd9333jh",
114
- "oauth_version" => "1.0",
115
- "file" => "vacation.jpg",
116
- "size" => "original"
117
- }
118
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
119
- "GET&http%3A%2F%2Fphotos.example.net%2F" \
120
- "https%253A%252F%252Fwww.example.com&file%3Dvacation.jpg%26" \
121
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
122
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
123
- "oauth_signature_method%3DHMAC-SHA1%26" \
124
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
125
- "oauth_version%3D1.0%26size%3Doriginal"
126
- )
127
- end
128
-
129
- it "should correctly generate a base string with an already encoded URI" do
130
- method = "GET"
131
- uri = "http://example.com/r%20v/X?id=123"
132
- parameters = {
133
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
134
- "oauth_token" => "nnch734d00sl2jdk",
135
- "oauth_signature_method" => "HMAC-SHA1",
136
- "oauth_timestamp" => "1191242096",
137
- "oauth_nonce" => "kllo9940pd9333jh",
138
- "oauth_version" => "1.0"
139
- }
140
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
141
- "GET&http%3A%2F%2Fexample.com%2Fr%2520v%2FX&" \
142
- "id%3D123%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
143
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
144
- "oauth_signature_method%3DHMAC-SHA1%26" \
145
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
146
- "oauth_version%3D1.0"
147
- )
148
- end
149
-
150
- it "should correctly generate a base string when port 8080 is specified" do
151
- method = "GET"
152
- uri = "http://www.example.net:8080/?q=1"
153
- parameters = {
154
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
155
- "oauth_token" => "nnch734d00sl2jdk",
156
- "oauth_signature_method" => "HMAC-SHA1",
157
- "oauth_timestamp" => "1191242096",
158
- "oauth_nonce" => "kllo9940pd9333jh",
159
- "oauth_version" => "1.0"
160
- }
161
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
162
- "GET&http%3A%2F%2Fwww.example.net%3A8080%2F&" \
163
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
164
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
165
- "oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26" \
166
- "oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26q%3D1"
167
- )
168
- end
169
-
170
- it "should correctly generate a base string when port 80 is specified" do
171
- method = "GET"
172
- uri = "http://photos.example.net:80/photos"
173
- parameters = {
174
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
175
- "oauth_token" => "nnch734d00sl2jdk",
176
- "oauth_signature_method" => "HMAC-SHA1",
177
- "oauth_timestamp" => "1191242096",
178
- "oauth_nonce" => "kllo9940pd9333jh",
179
- "oauth_version" => "1.0",
180
- "file" => "vacation.jpg",
181
- "size" => "original"
182
- }
183
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
184
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
185
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
186
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
187
- "oauth_signature_method%3DHMAC-SHA1%26" \
188
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
189
- "oauth_version%3D1.0%26size%3Doriginal"
190
- )
191
- end
192
-
193
- it "should correctly generate a base string when port 443 is specified" do
194
- method = "GET"
195
- uri = "https://photos.example.net:443/photos"
196
- parameters = {
197
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
198
- "oauth_token" => "nnch734d00sl2jdk",
199
- "oauth_signature_method" => "HMAC-SHA1",
200
- "oauth_timestamp" => "1191242096",
201
- "oauth_nonce" => "kllo9940pd9333jh",
202
- "oauth_version" => "1.0",
203
- "file" => "vacation.jpg",
204
- "size" => "original"
205
- }
206
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
207
- "GET&https%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
208
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
209
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
210
- "oauth_signature_method%3DHMAC-SHA1%26" \
211
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
212
- "oauth_version%3D1.0%26size%3Doriginal"
213
- )
214
- end
215
-
216
- it "should correctly generate a base signature with uppercase scheme" do
217
- method = "GET"
218
- uri =
219
- "HTTP://photos.example.net/photos?file=vacation.jpg"
220
- parameters = {
221
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
222
- "oauth_token" => "nnch734d00sl2jdk",
223
- "oauth_signature_method" => "HMAC-SHA1",
224
- "oauth_timestamp" => "1191242096",
225
- "oauth_nonce" => "kllo9940pd9333jh",
226
- "oauth_version" => "1.0",
227
- "size" => "original"
228
- }
229
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
230
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
231
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
232
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
233
- "oauth_signature_method%3DHMAC-SHA1%26" \
234
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
235
- "oauth_version%3D1.0%26size%3Doriginal"
236
- )
237
- end
238
-
239
- it "should correctly generate a base signature with mixedcase authority" do
240
- method = "GET"
241
- uri =
242
- "http://photos.eXaMpLe.NET/photos?file=vacation.jpg"
243
- parameters = {
244
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
245
- "oauth_token" => "nnch734d00sl2jdk",
246
- "oauth_signature_method" => "HMAC-SHA1",
247
- "oauth_timestamp" => "1191242096",
248
- "oauth_nonce" => "kllo9940pd9333jh",
249
- "oauth_version" => "1.0",
250
- "size" => "original"
251
- }
252
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
253
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
254
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
255
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
256
- "oauth_signature_method%3DHMAC-SHA1%26" \
257
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
258
- "oauth_version%3D1.0%26size%3Doriginal"
259
- )
260
- end
261
-
262
- it "should correctly generate a base signature with a method symbol" do
263
- method = :get
264
- uri =
265
- "http://photos.example.net/photos?file=vacation.jpg"
266
- parameters = {
267
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
268
- "oauth_token" => "nnch734d00sl2jdk",
269
- "oauth_signature_method" => "HMAC-SHA1",
270
- "oauth_timestamp" => "1191242096",
271
- "oauth_nonce" => "kllo9940pd9333jh",
272
- "oauth_version" => "1.0",
273
- "size" => "original"
274
- }
275
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
276
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
277
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
278
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
279
- "oauth_signature_method%3DHMAC-SHA1%26" \
280
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
281
- "oauth_version%3D1.0%26size%3Doriginal"
282
- )
283
- end
284
-
285
- it "should correctly generate an authorization header" do
286
- parameters = [
287
- %w[oauth_consumer_key 0685bd9184jfhq22],
288
- %w[oauth_token ad180jjd733klru7],
289
- ["oauth_signature_method", "HMAC-SHA1"],
290
- ["oauth_signature", "wOJIO9A2W5mFwDgiDvZbTSMK/PY="],
291
- %w[oauth_timestamp 137131200],
292
- %w[oauth_nonce 4572616e48616d6d65724c61686176],
293
- ["oauth_version", "1.0"]
294
- ]
295
- expect(Signet::OAuth1.generate_authorization_header(
296
- parameters, "http://sp.example.com/"
297
- )).to eq(
298
- 'OAuth realm="http://sp.example.com/", ' \
299
- 'oauth_consumer_key="0685bd9184jfhq22", ' \
300
- 'oauth_token="ad180jjd733klru7", ' \
301
- 'oauth_signature_method="HMAC-SHA1", ' \
302
- 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' \
303
- 'oauth_timestamp="137131200", ' \
304
- 'oauth_nonce="4572616e48616d6d65724c61686176", ' \
305
- 'oauth_version="1.0"'
306
- )
307
- end
308
-
309
- it "should raise an error if generating an authorization header " \
310
- "with bogus values" do
311
- expect(lambda do
312
- Signet::OAuth1.generate_authorization_header 42
313
- end).to raise_error(TypeError)
314
- end
315
-
316
- it "should raise an error if generating an authorization header " \
317
- 'with the "realm" parameter specified the wrong way' do
318
- parameters = [
319
- ["realm", "http://sp.example.com/"],
320
- %w[oauth_consumer_key 0685bd9184jfhq22],
321
- %w[oauth_token ad180jjd733klru7],
322
- ["oauth_signature_method", "HMAC-SHA1"],
323
- ["oauth_signature", "wOJIO9A2W5mFwDgiDvZbTSMK/PY="],
324
- %w[oauth_timestamp 137131200],
325
- %w[oauth_nonce 4572616e48616d6d65724c61686176],
326
- ["oauth_version", "1.0"]
327
- ]
328
- expect(lambda do
329
- Signet::OAuth1.generate_authorization_header parameters
330
- end).to raise_error(ArgumentError)
331
- end
332
-
333
- it "should correctly parse an authorization header" do
334
- parameters = Signet::OAuth1.parse_authorization_header(
335
- 'OAuth realm="http://sp.example.com/", ' \
336
- 'oauth_consumer_key="0685bd9184jfhq22", ' \
337
- 'oauth_token="ad180jjd733klru7", ' \
338
- 'oauth_signature_method="HMAC-SHA1", ' \
339
- 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' \
340
- 'oauth_timestamp="137131200", ' \
341
- 'oauth_nonce="4572616e48616d6d65724c61686176", ' \
342
- 'oauth_version="1.0"'
343
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
344
- expect(parameters["realm"]).to eq "http://sp.example.com/"
345
- expect(parameters["oauth_consumer_key"]).to eq "0685bd9184jfhq22"
346
- expect(parameters["oauth_token"]).to eq "ad180jjd733klru7"
347
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
348
- expect(parameters["oauth_signature"]).to eq "wOJIO9A2W5mFwDgiDvZbTSMK/PY="
349
- expect(parameters["oauth_timestamp"]).to eq "137131200"
350
- expect(parameters["oauth_nonce"]).to eq "4572616e48616d6d65724c61686176"
351
- expect(parameters["oauth_version"]).to eq "1.0"
352
- end
353
-
354
- it "should not unescape a realm in an authorization header" do
355
- parameters = Signet::OAuth1.parse_authorization_header(
356
- 'OAuth realm="http%3A%2F%2Fsp.example.com%2F", ' \
357
- 'domain="http%3A%2F%2Fsp.example.com%2F", ' \
358
- 'oauth_consumer_key="0685bd9184jfhq22", ' \
359
- 'oauth_token="ad180jjd733klru7", ' \
360
- 'oauth_signature_method="HMAC-SHA1", ' \
361
- 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' \
362
- 'oauth_timestamp="137131200", ' \
363
- 'oauth_nonce="4572616e48616d6d65724c61686176", ' \
364
- 'oauth_version="1.0"'
365
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
366
- expect(parameters["realm"]).to eq "http%3A%2F%2Fsp.example.com%2F"
367
- expect(parameters["domain"]).to eq "http://sp.example.com/"
368
- expect(parameters["oauth_consumer_key"]).to eq "0685bd9184jfhq22"
369
- expect(parameters["oauth_token"]).to eq "ad180jjd733klru7"
370
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
371
- expect(parameters["oauth_signature"]).to eq "wOJIO9A2W5mFwDgiDvZbTSMK/PY="
372
- expect(parameters["oauth_timestamp"]).to eq "137131200"
373
- expect(parameters["oauth_nonce"]).to eq "4572616e48616d6d65724c61686176"
374
- expect(parameters["oauth_version"]).to eq "1.0"
375
- end
376
-
377
- it "should raise an error if parsing an authorization header " \
378
- "with bogus values" do
379
- expect(lambda do
380
- Signet::OAuth1.parse_authorization_header 42
381
- end).to raise_error(TypeError)
382
- end
383
-
384
- it "should raise an error if parsing a non-OAuth authorization header" do
385
- expect(lambda do
386
- Signet::OAuth1.parse_authorization_header(
387
- "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
388
- )
389
- end).to raise_error(Signet::ParseError)
390
- end
391
-
392
- it "should correctly parse a form encoded credential" do
393
- credential = Signet::OAuth1.parse_form_encoded_credentials(
394
- "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03"
395
- )
396
- expect(credential.key).to eq "hh5s93j4hdidpola"
397
- expect(credential.secret).to eq "hdhd0244k9j7ao03"
398
- end
399
-
400
- it "should correctly parse a form encoded credential" do
401
- credential = Signet::OAuth1.parse_form_encoded_credentials(
402
- "oauth_token=hdk48Djdsa&oauth_token_secret=xyz4992k83j47x0b&" \
403
- "oauth_callback_confirmed=true"
404
- )
405
- expect(credential.key).to eq "hdk48Djdsa"
406
- expect(credential.secret).to eq "xyz4992k83j47x0b"
407
- end
408
-
409
- it "should raise an error if parsing a form encoded credential " \
410
- "with bogus values" do
411
- expect(lambda do
412
- Signet::OAuth1.parse_form_encoded_credentials 42
413
- end).to raise_error(TypeError)
414
- end
415
-
416
- it "should correctly generate a signature for a set of parameters" do
417
- method = :get
418
- uri = "http://photos.example.net/photos"
419
- client_credential_secret = "kd94hf93k423kf44"
420
- token_credential_secret = "pfkkdhi9sl3r4s00"
421
- parameters = {
422
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
423
- "oauth_token" => "nnch734d00sl2jdk",
424
- "oauth_signature_method" => "HMAC-SHA1",
425
- "oauth_timestamp" => "1191242096",
426
- "oauth_nonce" => "kllo9940pd9333jh",
427
- "oauth_version" => "1.0",
428
- "file" => "vacation.jpg",
429
- "size" => "original"
430
- }
431
- expect(Signet::OAuth1.sign_parameters(
432
- method,
433
- uri,
434
- parameters,
435
- client_credential_secret,
436
- token_credential_secret
437
- )).to eq "tR3+Ty81lMeYAr/Fid0kMTYa/WM="
438
- end
439
-
440
- it "should raise an error when trying to sign with with unknown method" do
441
- method = :get
442
- uri = "http://photos.example.net/photos"
443
- client_credential_secret = "kd94hf93k423kf44"
444
- token_credential_secret = "pfkkdhi9sl3r4s00"
445
- parameters = {
446
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
447
- "oauth_token" => "nnch734d00sl2jdk",
448
- "oauth_signature_method" => "HMAC-BOGUS", # Unknown signature method
449
- "oauth_timestamp" => "1191242096",
450
- "oauth_nonce" => "kllo9940pd9333jh",
451
- "oauth_version" => "1.0",
452
- "file" => "vacation.jpg",
453
- "size" => "original"
454
- }
455
- expect(lambda do
456
- Signet::OAuth1.sign_parameters(
457
- method,
458
- uri,
459
- parameters,
460
- client_credential_secret,
461
- token_credential_secret
462
- )
463
- end).to raise_error(NotImplementedError)
464
- end
465
-
466
- it "should correctly generate authorization URIs" do
467
- authorization_uri = "http://photos.example.net/authorize"
468
- temporary_credential_key = "hh5s93j4hdidpola"
469
- callback = "http://printer.example.com/request_token_ready"
470
- parsed_uri = Addressable::URI.parse(
471
- Signet::OAuth1.generate_authorization_uri(
472
- authorization_uri,
473
- temporary_credential_key: temporary_credential_key,
474
- callback: callback
475
- )
476
- )
477
- expect(parsed_uri.query_values).to have_key("oauth_token")
478
- expect(parsed_uri.query_values["oauth_token"]).to eq temporary_credential_key
479
- expect(parsed_uri.query_values).to have_key("oauth_callback")
480
- expect(parsed_uri.query_values["oauth_callback"]).to eq callback
481
- end
482
- end
483
-
484
- describe Signet::OAuth1, "when generating temporary credentials parameters" do
485
- before do
486
- @client_credential_key = "dpf43f3p2l4k3l03"
487
- @callback = "http://printer.example.com/request_token_ready"
488
- @signature_method = "HMAC-SHA1"
489
- @scope = "http://photos.example.com/full_access"
490
- @additional_parameters = [["scope", @scope]]
491
- @unsigned_parameters =
492
- Signet::OAuth1.unsigned_temporary_credential_parameters(
493
- client_credential_key: @client_credential_key,
494
- callback: @callback,
495
- signature_method: @signature_method,
496
- additional_parameters: @additional_parameters
497
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
498
- end
499
-
500
- it "should raise an error if the client credential key is missing" do
501
- expect(lambda do
502
- Signet::OAuth1.unsigned_temporary_credential_parameters(
503
- client_credential_key: nil,
504
- callback: @callback,
505
- signature_method: @signature_method,
506
- additional_parameters: @additional_parameters
507
- )
508
- end).to raise_error(ArgumentError)
509
- end
510
-
511
- it "should have the correct client credential key" do
512
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
513
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential_key
514
- end
515
-
516
- it "should have the correct signature method" do
517
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
518
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
519
- end
520
-
521
- it "should have a valid timestamp" do
522
- # Verify that we have a timestamp, it's in the correct format and within
523
- # a reasonable range of the current time.
524
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
525
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
526
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
527
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
528
- end
529
-
530
- it "should have a valid nonce" do
531
- # Verify that we have a nonce and that it has sufficient length for
532
- # uniqueness.
533
- expect(@unsigned_parameters).to have_key("oauth_nonce")
534
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
535
- end
536
-
537
- it "should have the correct callback" do
538
- expect(@unsigned_parameters).to have_key("oauth_callback")
539
- expect(@unsigned_parameters["oauth_callback"]).to eq @callback
540
- end
541
-
542
- it "should have the correct scope parameter" do
543
- expect(@unsigned_parameters).to have_key("scope")
544
- expect(@unsigned_parameters["scope"]).to eq @scope
545
- end
546
-
547
- it "should have the correct OAuth version" do
548
- expect(@unsigned_parameters).to have_key("oauth_version")
549
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
550
- end
551
- end
552
-
553
- describe Signet::OAuth1, "when generating token credential parameters" do
554
- before do
555
- @client_credential_key = "dpf43f3p2l4k3l03"
556
- @temporary_credential_key = "hh5s93j4hdidpola"
557
- @verifier = "473f82d3"
558
- @signature_method = "HMAC-SHA1"
559
- @unsigned_parameters =
560
- Signet::OAuth1.unsigned_token_credential_parameters(
561
- client_credential_key: @client_credential_key,
562
- temporary_credential_key: @temporary_credential_key,
563
- signature_method: @signature_method,
564
- verifier: @verifier
565
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
566
- end
567
-
568
- it "should raise an error if the client credential key is missing" do
569
- expect(lambda do
570
- Signet::OAuth1.unsigned_token_credential_parameters(
571
- client_credential_key: nil,
572
- temporary_credential_key: @temporary_credential_key,
573
- signature_method: @signature_method,
574
- verifier: @verifier
575
- )
576
- end).to raise_error(ArgumentError)
577
- end
578
-
579
- it "should raise an error if the temporary credential key is missing" do
580
- expect(lambda do
581
- Signet::OAuth1.unsigned_token_credential_parameters(
582
- client_credential_key: @client_credential_key,
583
- temporary_credential_key: nil,
584
- signature_method: @signature_method,
585
- verifier: @verifier
586
- )
587
- end).to raise_error(ArgumentError)
588
- end
589
-
590
- it "should raise an error if the verifier is missing" do
591
- expect(lambda do
592
- Signet::OAuth1.unsigned_token_credential_parameters(
593
- client_credential_key: @client_credential_key,
594
- temporary_credential_key: @temporary_credential_key,
595
- signature_method: @signature_method,
596
- verifier: nil
597
- )
598
- end).to raise_error(ArgumentError)
599
- end
600
-
601
- it "should have the correct client credential key" do
602
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
603
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential_key
604
- end
605
-
606
- it "should have the correct temporary credentials key" do
607
- expect(@unsigned_parameters).to have_key("oauth_token")
608
- expect(@unsigned_parameters["oauth_token"]).to eq @temporary_credential_key
609
- end
610
-
611
- it "should have the correct signature method" do
612
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
613
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
614
- end
615
-
616
- it "should have a valid timestamp" do
617
- # Verify that we have a timestamp, it's in the correct format and within
618
- # a reasonable range of the current time.
619
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
620
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
621
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
622
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
623
- end
624
-
625
- it "should have a valid nonce" do
626
- # Verify that we have a nonce and that it has sufficient length for
627
- # uniqueness.
628
- expect(@unsigned_parameters).to have_key("oauth_nonce")
629
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
630
- end
631
-
632
- it "should have the verifier" do
633
- expect(@unsigned_parameters).to have_key("oauth_verifier")
634
- expect(@unsigned_parameters["oauth_verifier"]).to eq @verifier
635
- end
636
-
637
- it "should have the correct OAuth version" do
638
- expect(@unsigned_parameters).to have_key("oauth_version")
639
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
640
- end
641
- end
642
-
643
- describe Signet::OAuth1, "when generating protected resource parameters" do
644
- before do
645
- @client_credential_key = "dpf43f3p2l4k3l03"
646
- @token_credential_key = "nnch734d00sl2jdk"
647
- @signature_method = "HMAC-SHA1"
648
- @unsigned_parameters =
649
- Signet::OAuth1.unsigned_resource_parameters(
650
- client_credential_key: @client_credential_key,
651
- token_credential_key: @token_credential_key,
652
- signature_method: @signature_method
653
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
654
- end
655
-
656
- it "should raise an error if the client credential key is missing" do
657
- expect(lambda do
658
- Signet::OAuth1.unsigned_resource_parameters(
659
- client_credential_key: nil,
660
- token_credential_key: @token_credential_key,
661
- signature_method: @signature_method
662
- )
663
- end).to raise_error(ArgumentError)
664
- end
665
-
666
- it "should raise an error if the token credential key is missing" do
667
- expect(lambda do
668
- Signet::OAuth1.unsigned_resource_parameters(
669
- client_credential_key: @client_credential_key,
670
- token_credential_key: nil,
671
- signature_method: @signature_method
672
- )
673
- end).to raise_error(ArgumentError)
674
- end
675
-
676
- it "should have the correct client credential key" do
677
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
678
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential_key
679
- end
680
-
681
- it "should have the correct token credentials key" do
682
- expect(@unsigned_parameters).to have_key("oauth_token")
683
- expect(@unsigned_parameters["oauth_token"]).to eq @token_credential_key
684
- end
685
-
686
- it "should have the correct signature method" do
687
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
688
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
689
- end
690
-
691
- it "should have a valid timestamp" do
692
- # Verify that we have a timestamp, it's in the correct format and within
693
- # a reasonable range of the current time.
694
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
695
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
696
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
697
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
698
- end
699
-
700
- it "should have a valid nonce" do
701
- # Verify that we have a nonce and that it has sufficient length for
702
- # uniqueness.
703
- expect(@unsigned_parameters).to have_key("oauth_nonce")
704
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
705
- end
706
-
707
- it "should have the correct OAuth version" do
708
- expect(@unsigned_parameters).to have_key("oauth_version")
709
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
710
- end
711
- end
712
-
713
- describe Signet::OAuth1, "when generating token credential parameters " \
714
- "with Signet::OAuth1::Credential objects" do
715
- before do
716
- @client_credential = Signet::OAuth1::Credential.new(
717
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
718
- )
719
- @temporary_credential = Signet::OAuth1::Credential.new(
720
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
721
- )
722
- @verifier = "473f82d3"
723
- @signature_method = "HMAC-SHA1"
724
- @unsigned_parameters =
725
- Signet::OAuth1.unsigned_token_credential_parameters(
726
- client_credential: @client_credential,
727
- temporary_credential: @temporary_credential,
728
- signature_method: @signature_method,
729
- verifier: @verifier
730
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
731
- end
732
-
733
- it "should have the correct client credential key" do
734
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
735
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential.key
736
- end
737
-
738
- it "should have the correct temporary credentials key" do
739
- expect(@unsigned_parameters).to have_key("oauth_token")
740
- expect(@unsigned_parameters["oauth_token"]).to eq @temporary_credential.key
741
- end
742
-
743
- it "should have the correct signature method" do
744
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
745
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
746
- end
747
-
748
- it "should have a valid timestamp" do
749
- # Verify that we have a timestamp, it's in the correct format and within
750
- # a reasonable range of the current time.
751
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
752
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
753
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
754
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
755
- end
756
-
757
- it "should have a valid nonce" do
758
- # Verify that we have a nonce and that it has sufficient length for
759
- # uniqueness.
760
- expect(@unsigned_parameters).to have_key("oauth_nonce")
761
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
762
- end
763
-
764
- it "should have the correct OAuth version" do
765
- expect(@unsigned_parameters).to have_key("oauth_version")
766
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
767
- end
768
- end
769
-
770
- describe Signet::OAuth1, "when generating token credential parameters " \
771
- "with a Signet::OAuth1::Client object" do
772
- before do
773
- @client = Signet::OAuth1::Client.new
774
- @client.client_credential = Signet::OAuth1::Credential.new(
775
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
776
- )
777
- @client.temporary_credential = Signet::OAuth1::Credential.new(
778
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
779
- )
780
- @verifier = "473f82d3"
781
- @signature_method = "HMAC-SHA1"
782
- @unsigned_parameters =
783
- Signet::OAuth1.unsigned_token_credential_parameters(
784
- client: @client,
785
- signature_method: @signature_method,
786
- verifier: @verifier
787
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
788
- end
789
-
790
- it "should have the correct client credential key" do
791
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
792
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client.client_credential_key
793
- end
794
-
795
- it "should have the correct temporary credentials key" do
796
- expect(@unsigned_parameters).to have_key("oauth_token")
797
- expect(@unsigned_parameters["oauth_token"]).to eq @client.temporary_credential_key
798
- end
799
-
800
- it "should have the correct signature method" do
801
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
802
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
803
- end
804
-
805
- it "should have a valid timestamp" do
806
- # Verify that we have a timestamp, it's in the correct format and within
807
- # a reasonable range of the current time.
808
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
809
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
810
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
811
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
812
- end
813
-
814
- it "should have a valid nonce" do
815
- # Verify that we have a nonce and that it has sufficient length for
816
- # uniqueness.
817
- expect(@unsigned_parameters).to have_key("oauth_nonce")
818
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
819
- end
820
-
821
- it "should have the correct OAuth version" do
822
- expect(@unsigned_parameters).to have_key("oauth_version")
823
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
824
- end
825
- end
826
-
827
- describe Signet::OAuth1, "when generating token credential parameters " \
828
- "with Signet::OAuth1::Credential objects" do
829
- before do
830
- @client_credential = Signet::OAuth1::Credential.new(
831
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
832
- )
833
- @temporary_credential = Signet::OAuth1::Credential.new(
834
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
835
- )
836
- @verifier = "473f82d3"
837
- @signature_method = "HMAC-SHA1"
838
- @unsigned_parameters =
839
- Signet::OAuth1.unsigned_token_credential_parameters(
840
- client_credential: @client_credential,
841
- temporary_credential: @temporary_credential,
842
- signature_method: @signature_method,
843
- verifier: @verifier
844
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
845
- end
846
-
847
- it "should have the correct client credential key" do
848
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
849
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential.key
850
- end
851
-
852
- it "should have the correct temporary credentials key" do
853
- expect(@unsigned_parameters).to have_key("oauth_token")
854
- expect(@unsigned_parameters["oauth_token"]).to eq @temporary_credential.key
855
- end
856
-
857
- it "should have the correct signature method" do
858
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
859
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
860
- end
861
-
862
- it "should have a valid timestamp" do
863
- # Verify that we have a timestamp, it's in the correct format and within
864
- # a reasonable range of the current time.
865
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
866
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
867
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
868
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
869
- end
870
-
871
- it "should have a valid nonce" do
872
- # Verify that we have a nonce and that it has sufficient length for
873
- # uniqueness.
874
- expect(@unsigned_parameters).to have_key("oauth_nonce")
875
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
876
- end
877
-
878
- it "should have the correct OAuth version" do
879
- expect(@unsigned_parameters).to have_key("oauth_version")
880
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
881
- end
882
- end
883
-
884
- describe Signet::OAuth1, "extracting credential keys from options" do
885
- it "should raise an error for bogus credentials" do
886
- expect(lambda do
887
- Signet::OAuth1.extract_credential_key_option(
888
- :client, client_credential_key: true
889
- )
890
- end).to raise_error(TypeError)
891
- end
892
-
893
- it "should raise an error for bogus credentials" do
894
- expect(lambda do
895
- Signet::OAuth1.extract_credential_key_option(
896
- :client, client_credential: 42
897
- )
898
- end).to raise_error(TypeError)
899
- end
900
-
901
- it "should raise an error for bogus credentials" do
902
- expect(lambda do
903
- Signet::OAuth1.extract_credential_key_option(
904
- :client, client: 42
905
- )
906
- end).to raise_error(TypeError)
907
- end
908
-
909
- it "should return nil for missing credential key" do
910
- expect(Signet::OAuth1.extract_credential_key_option(:client, {})).to eq nil
911
- end
912
-
913
- it "should find the correct credential key" do
914
- expect(Signet::OAuth1.extract_credential_key_option(
915
- :client, client_credential_key: "dpf43f3p2l4k3l03"
916
- )).to eq "dpf43f3p2l4k3l03"
917
- end
918
-
919
- it "should find the correct credential key" do
920
- expect(Signet::OAuth1.extract_credential_key_option(
921
- :client, client_credential: Signet::OAuth1::Credential.new(
922
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
923
- )
924
- )).to eq "dpf43f3p2l4k3l03"
925
- end
926
-
927
- it "should find the correct credential key" do
928
- client = Signet::OAuth1::Client.new
929
- client.client_credential = Signet::OAuth1::Credential.new(
930
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
931
- )
932
- expect(Signet::OAuth1.extract_credential_key_option(
933
- :client, client: client
934
- )).to eq "dpf43f3p2l4k3l03"
935
- end
936
-
937
- it "should find the correct credential key" do
938
- client = Signet::OAuth1::Client.new
939
- client.temporary_credential = Signet::OAuth1::Credential.new(
940
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
941
- )
942
- expect(Signet::OAuth1.extract_credential_key_option(
943
- :temporary, client: client
944
- )).to eq "hh5s93j4hdidpola"
945
- end
946
- end
947
-
948
- describe Signet::OAuth1, "extracting credential secrets from options" do
949
- it "should raise an error for bogus credentials" do
950
- expect(lambda do
951
- Signet::OAuth1.extract_credential_secret_option(
952
- :client, client_credential_secret: true
953
- )
954
- end).to raise_error(TypeError)
955
- end
956
-
957
- it "should raise an error for bogus credentials" do
958
- expect(lambda do
959
- Signet::OAuth1.extract_credential_secret_option(
960
- :client, client_credential: 42
961
- )
962
- end).to raise_error(TypeError)
963
- end
964
-
965
- it "should raise an error for bogus credentials" do
966
- expect(lambda do
967
- Signet::OAuth1.extract_credential_secret_option(
968
- :client, client: 42
969
- )
970
- end).to raise_error(TypeError)
971
- end
972
-
973
- it "should raise an error for missing credential secret" do
974
- expect(Signet::OAuth1.extract_credential_secret_option(:client, {})).to eq nil
975
- end
976
-
977
- it "should find the correct credential secret" do
978
- expect(Signet::OAuth1.extract_credential_secret_option(
979
- :client, client_credential_secret: "kd94hf93k423kf44"
980
- )).to eq "kd94hf93k423kf44"
981
- end
982
-
983
- it "should find the correct credential secret" do
984
- expect(Signet::OAuth1.extract_credential_secret_option(
985
- :client, client_credential: Signet::OAuth1::Credential.new(
986
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
987
- )
988
- )).to eq "kd94hf93k423kf44"
989
- end
990
-
991
- it "should find the correct credential secret" do
992
- client = Signet::OAuth1::Client.new
993
- client.client_credential = Signet::OAuth1::Credential.new(
994
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
995
- )
996
- expect(Signet::OAuth1.extract_credential_secret_option(
997
- :client, client: client
998
- )).to eq "kd94hf93k423kf44"
999
- end
1000
-
1001
- it "should find the correct credential secret" do
1002
- client = Signet::OAuth1::Client.new
1003
- client.temporary_credential = Signet::OAuth1::Credential.new(
1004
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
1005
- )
1006
- expect(Signet::OAuth1.extract_credential_secret_option(
1007
- :temporary, client: client
1008
- )).to eq "hdhd0244k9j7ao03"
1009
- end
1010
- end