signet 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.1.4
2
+
3
+ * Added support for a two-legged authorization flow
4
+
1
5
  == 0.1.3
2
6
 
3
7
  * fixed issue with headers passed in as a Hash
@@ -284,9 +284,8 @@ module Signet #:nodoc:
284
284
  # be a temporary credential secret when obtaining a token credential
285
285
  # for the first time
286
286
  base_string = self.generate_base_string(method, uri, parameters)
287
- signature_method = (
288
- parameters.inject({}) { |h,(k,v)| h[k]=v; h }
289
- )['oauth_signature_method']
287
+ parameters = parameters.inject({}) { |h,(k,v)| h[k.to_s]=v; h }
288
+ signature_method = parameters['oauth_signature_method']
290
289
  case signature_method
291
290
  when 'HMAC-SHA1'
292
291
  require 'signet/oauth_1/signature_methods/hmac_sha1'
@@ -305,13 +304,13 @@ module Signet #:nodoc:
305
304
  #
306
305
  # @param [Hash] options
307
306
  # The configuration parameters for the request.
308
- # - <code>:client_credential_key</code> —
307
+ # - <code>:client_credential_key</code> —
309
308
  # The client credential key.
310
- # - <code>:callback</code> —
309
+ # - <code>:callback</code> —
311
310
  # The OAuth callback. Defaults to {Signet::OAuth1::OUT_OF_BAND}.
312
- # - <code>:signature_method</code> —
311
+ # - <code>:signature_method</code> —
313
312
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
314
- # - <code>:additional_parameters</code> —
313
+ # - <code>:additional_parameters</code> —
315
314
  # Non-standard additional parameters.
316
315
  #
317
316
  # @return [Array]
@@ -380,13 +379,13 @@ module Signet #:nodoc:
380
379
  #
381
380
  # @param [Hash] options
382
381
  # The configuration parameters for the request.
383
- # - <code>:client_credential_key</code> —
382
+ # - <code>:client_credential_key</code> —
384
383
  # The client credential key.
385
- # - <code>:temporary_credential_key</code> —
384
+ # - <code>:temporary_credential_key</code> —
386
385
  # The temporary credential key.
387
- # - <code>:verifier</code> —
386
+ # - <code>:verifier</code> —
388
387
  # The OAuth verifier.
389
- # - <code>:signature_method</code> —
388
+ # - <code>:signature_method</code> —
390
389
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
391
390
  #
392
391
  # @return [Array]
@@ -428,37 +427,44 @@ module Signet #:nodoc:
428
427
  #
429
428
  # @param [Hash] options
430
429
  # The configuration parameters for the request.
431
- # - <code>:client_credential_key</code> —
430
+ # - <code>:client_credential_key</code> —
432
431
  # The client credential key.
433
- # - <code>:token_credential_key</code> —
432
+ # - <code>:token_credential_key</code> —
434
433
  # The token credential key.
435
- # - <code>:signature_method</code> —
434
+ # - <code>:signature_method</code> —
436
435
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
436
+ # - <code>:two_legged</code> —
437
+ # A switch for two-legged OAuth. Defaults to <code>false</code>.
437
438
  #
438
439
  # @return [Array]
439
440
  # The parameter list as an <code>Array</code> of key/value pairs.
440
441
  def self.unsigned_resource_parameters(options={})
441
442
  options = {
442
- :signature_method => 'HMAC-SHA1'
443
+ :signature_method => 'HMAC-SHA1',
444
+ :two_legged => false
443
445
  }.merge(options)
444
446
  client_credential_key =
445
447
  self.extract_credential_key_option(:client, options)
446
- token_credential_key =
447
- self.extract_credential_key_option(:token, options)
448
448
  if client_credential_key == nil
449
449
  raise ArgumentError, "Missing :client_credential_key parameter."
450
450
  end
451
- if token_credential_key == nil
452
- raise ArgumentError, "Missing :token_credential_key parameter."
451
+ unless options[:two_legged]
452
+ token_credential_key =
453
+ self.extract_credential_key_option(:token, options)
454
+ if token_credential_key == nil
455
+ raise ArgumentError, "Missing :token_credential_key parameter."
456
+ end
453
457
  end
454
458
  parameters = [
455
459
  ["oauth_consumer_key", client_credential_key],
456
- ["oauth_token", token_credential_key],
457
460
  ["oauth_signature_method", options[:signature_method]],
458
461
  ["oauth_timestamp", self.generate_timestamp()],
459
462
  ["oauth_nonce", self.generate_nonce()],
460
463
  ["oauth_version", "1.0"]
461
464
  ]
465
+ unless options[:two_legged]
466
+ parameters << ["oauth_token", token_credential_key]
467
+ end
462
468
  # No additional parameters allowed here
463
469
  return parameters
464
470
  end
@@ -26,14 +26,14 @@ module Signet #:nodoc:
26
26
  #
27
27
  # @param [Hash] options
28
28
  # The configuration parameters for the client.
29
- # - <code>:temporary_credential_uri</code> —
29
+ # - <code>:temporary_credential_uri</code> —
30
30
  # The OAuth temporary credentials URI.
31
31
  # - <code>:authorization_uri</code> — The OAuth authorization URI.
32
- # - <code>:token_credential_uri</code> —
32
+ # - <code>:token_credential_uri</code> —
33
33
  # The OAuth token credentials URI.
34
- # - <code>:client_credential_key</code> —
34
+ # - <code>:client_credential_key</code> —
35
35
  # The OAuth client credential key.
36
- # - <code>:client_credential_secret</code> —
36
+ # - <code>:client_credential_secret</code> —
37
37
  # The OAuth client credential secret.
38
38
  # - <code>:callback</code> — The OAuth callback. Defaults to 'oob'.
39
39
  #
@@ -67,6 +67,7 @@ module Signet #:nodoc:
67
67
  self.token_credential_secret =
68
68
  Signet::OAuth1.extract_credential_secret_option(:token, options)
69
69
  self.callback = options[:callback]
70
+ self.two_legged = options[:two_legged] || false
70
71
  end
71
72
 
72
73
  ##
@@ -480,16 +481,39 @@ module Signet #:nodoc:
480
481
  end
481
482
  end
482
483
 
484
+ ##
485
+ # Returns whether the client is in two-legged mode.
486
+ #
487
+ # @return [TrueClass, FalseClass]
488
+ # <code>true</code> for two-legged mode, <code>false</code> otherwise.
489
+ def two_legged
490
+ return @two_legged ||= false
491
+ end
492
+
493
+ ##
494
+ # Sets the client for two-legged mode.
495
+ #
496
+ # @param [TrueClass, FalseClass] new_two_legged
497
+ # <code>true</code> for two-legged mode, <code>false</code> otherwise.
498
+ def two_legged=(new_two_legged)
499
+ if new_two_legged != true && new_two_legged != false
500
+ raise TypeError,
501
+ "Expected true or false, got #{new_two_legged.class}."
502
+ else
503
+ @two_legged = new_two_legged
504
+ end
505
+ end
506
+
483
507
  ##
484
508
  # Generates a request for temporary credentials.
485
509
  #
486
510
  # @param [Hash] options
487
511
  # The configuration parameters for the request.
488
- # - <code>:signature_method</code> —
512
+ # - <code>:signature_method</code> —
489
513
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
490
- # - <code>:additional_parameters</code> —
514
+ # - <code>:additional_parameters</code> —
491
515
  # Non-standard additional parameters.
492
- # - <code>:realm</code> —
516
+ # - <code>:realm</code> —
493
517
  # The Authorization realm. See RFC 2617.
494
518
  #
495
519
  # @return [Array] The request object.
@@ -552,16 +576,16 @@ module Signet #:nodoc:
552
576
  #
553
577
  # @param [Hash] options
554
578
  # The configuration parameters for the request.
555
- # - <code>:signature_method</code> —
579
+ # - <code>:signature_method</code> —
556
580
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
557
- # - <code>:additional_parameters</code> —
581
+ # - <code>:additional_parameters</code> —
558
582
  # Non-standard additional parameters.
559
- # - <code>:realm</code> —
583
+ # - <code>:realm</code> —
560
584
  # The Authorization realm. See RFC 2617.
561
- # - <code>:adapter</code> —
585
+ # - <code>:adapter</code> —
562
586
  # The HTTP adapter.
563
587
  # Defaults to <code>HTTPAdapter::NetHTTPRequestAdapter</code>.
564
- # - <code>:connection</code> —
588
+ # - <code>:connection</code> —
565
589
  # An open, manually managed HTTP connection.
566
590
  # Must be of type <code>HTTPAdapter::Connection</code> and the
567
591
  # internal connection representation must match the HTTP adapter
@@ -620,16 +644,16 @@ module Signet #:nodoc:
620
644
  #
621
645
  # @param [Hash] options
622
646
  # The configuration parameters for the request.
623
- # - <code>:signature_method</code> —
647
+ # - <code>:signature_method</code> —
624
648
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
625
- # - <code>:additional_parameters</code> —
649
+ # - <code>:additional_parameters</code> —
626
650
  # Non-standard additional parameters.
627
- # - <code>:realm</code> —
651
+ # - <code>:realm</code> —
628
652
  # The Authorization realm. See RFC 2617.
629
- # - <code>:adapter</code> —
653
+ # - <code>:adapter</code> —
630
654
  # The HTTP adapter.
631
655
  # Defaults to <code>HTTPAdapter::NetHTTPRequestAdapter</code>.
632
- # - <code>:connection</code> —
656
+ # - <code>:connection</code> —
633
657
  # An open, manually managed HTTP connection.
634
658
  # Must be of type <code>HTTPAdapter::Connection</code> and the
635
659
  # internal connection representation must match the HTTP adapter
@@ -655,11 +679,11 @@ module Signet #:nodoc:
655
679
  #
656
680
  # @param [Hash] options
657
681
  # The configuration parameters for the request.
658
- # - <code>:verifier</code> —
682
+ # - <code>:verifier</code> —
659
683
  # The OAuth verifier provided by the server. Required.
660
- # - <code>:signature_method</code> —
684
+ # - <code>:signature_method</code> —
661
685
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
662
- # - <code>:realm</code> —
686
+ # - <code>:realm</code> —
663
687
  # The Authorization realm. See RFC 2617.
664
688
  #
665
689
  # @return [Array] The request object.
@@ -724,16 +748,16 @@ module Signet #:nodoc:
724
748
  #
725
749
  # @param [Hash] options
726
750
  # The configuration parameters for the request.
727
- # - <code>:verifier</code> —
751
+ # - <code>:verifier</code> —
728
752
  # The OAuth verifier provided by the server. Required.
729
- # - <code>:signature_method</code> —
753
+ # - <code>:signature_method</code> —
730
754
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
731
- # - <code>:realm</code> —
755
+ # - <code>:realm</code> —
732
756
  # The Authorization realm. See RFC 2617.
733
- # - <code>:adapter</code> —
757
+ # - <code>:adapter</code> —
734
758
  # The HTTP adapter.
735
759
  # Defaults to <code>HTTPAdapter::NetHTTPRequestAdapter</code>.
736
- # - <code>:connection</code> —
760
+ # - <code>:connection</code> —
737
761
  # An open, manually managed HTTP connection.
738
762
  # Must be of type <code>HTTPAdapter::Connection</code> and the
739
763
  # internal connection representation must match the HTTP adapter
@@ -790,16 +814,16 @@ module Signet #:nodoc:
790
814
  #
791
815
  # @param [Hash] options
792
816
  # The configuration parameters for the request.
793
- # - <code>:signature_method</code> —
817
+ # - <code>:signature_method</code> —
794
818
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
795
- # - <code>:additional_parameters</code> —
819
+ # - <code>:additional_parameters</code> —
796
820
  # Non-standard additional parameters.
797
- # - <code>:realm</code> —
821
+ # - <code>:realm</code> —
798
822
  # The Authorization realm. See RFC 2617.
799
- # - <code>:adapter</code> —
823
+ # - <code>:adapter</code> —
800
824
  # The HTTP adapter.
801
825
  # Defaults to <code>HTTPAdapter::NetHTTPRequestAdapter</code>.
802
- # - <code>:connection</code> —
826
+ # - <code>:connection</code> —
803
827
  # An open, manually managed HTTP connection.
804
828
  # Must be of type <code>HTTPAdapter::Connection</code> and the
805
829
  # internal connection representation must match the HTTP adapter
@@ -823,29 +847,33 @@ module Signet #:nodoc:
823
847
  #
824
848
  # @param [Hash] options
825
849
  # The configuration parameters for the request.
826
- # - <code>:request</code> —
850
+ # - <code>:request</code> —
827
851
  # A pre-constructed request to sign.
828
- # - <code>:method</code> —
852
+ # - <code>:method</code> —
829
853
  # The HTTP method for the request. Defaults to 'GET'.
830
- # - <code>:uri</code> —
854
+ # - <code>:uri</code> —
831
855
  # The URI for the request.
832
- # - <code>:headers</code> —
856
+ # - <code>:headers</code> —
833
857
  # The HTTP headers for the request.
834
- # - <code>:body</code> —
858
+ # - <code>:body</code> —
835
859
  # The HTTP body for the request.
836
- # - <code>:signature_method</code> —
860
+ # - <code>:signature_method</code> —
837
861
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
838
- # - <code>:realm</code> —
862
+ # - <code>:realm</code> —
839
863
  # The Authorization realm. See RFC 2617.
840
864
  #
841
865
  # @return [Array] The request object.
842
866
  def generate_authenticated_request(options={})
843
867
  verifications = {
844
868
  :client_credential_key => 'Client credential key',
845
- :client_credential_secret => 'Client credential secret',
846
- :token_credential_key => 'Token credential key',
847
- :token_credential_secret => 'Token credential secret'
869
+ :client_credential_secret => 'Client credential secret'
848
870
  }
871
+ unless self.two_legged
872
+ verifications.update(
873
+ :token_credential_key => 'Token credential key',
874
+ :token_credential_secret => 'Token credential secret'
875
+ )
876
+ end
849
877
  # Make sure all required state is set
850
878
  verifications.each do |(key, value)|
851
879
  unless self.send(key)
@@ -898,7 +926,8 @@ module Signet #:nodoc:
898
926
  parameters = ::Signet::OAuth1.unsigned_resource_parameters(
899
927
  :client_credential_key => self.client_credential_key,
900
928
  :token_credential_key => self.token_credential_key,
901
- :signature_method => options[:signature_method]
929
+ :signature_method => options[:signature_method],
930
+ :two_legged => self.two_legged
902
931
  )
903
932
  media_type = nil
904
933
  headers.each do |(header, value)|
@@ -938,24 +967,24 @@ module Signet #:nodoc:
938
967
  #
939
968
  # @param [Hash] options
940
969
  # The configuration parameters for the request.
941
- # - <code>:request</code> —
970
+ # - <code>:request</code> —
942
971
  # A pre-constructed request to sign.
943
- # - <code>:method</code> —
972
+ # - <code>:method</code> —
944
973
  # The HTTP method for the request. Defaults to 'GET'.
945
- # - <code>:uri</code> —
974
+ # - <code>:uri</code> —
946
975
  # The URI for the request.
947
- # - <code>:headers</code> —
976
+ # - <code>:headers</code> —
948
977
  # The HTTP headers for the request.
949
- # - <code>:body</code> —
978
+ # - <code>:body</code> —
950
979
  # The HTTP body for the request.
951
- # - <code>:signature_method</code> —
980
+ # - <code>:signature_method</code> —
952
981
  # The signature method. Defaults to <code>'HMAC-SHA1'</code>.
953
- # - <code>:realm</code> —
982
+ # - <code>:realm</code> —
954
983
  # The Authorization realm. See RFC 2617.
955
- # - <code>:adapter</code> —
984
+ # - <code>:adapter</code> —
956
985
  # The HTTP adapter.
957
986
  # Defaults to <code>HTTPAdapter::NetHTTPRequestAdapter</code>.
958
- # - <code>:connection</code> —
987
+ # - <code>:connection</code> —
959
988
  # An open, manually managed HTTP connection.
960
989
  # Must be of type <code>HTTPAdapter::Connection</code> and the
961
990
  # internal connection representation must match the HTTP adapter
@@ -18,7 +18,7 @@ unless defined? Signet::VERSION
18
18
  module VERSION #:nodoc:
19
19
  MAJOR = 0
20
20
  MINOR = 1
21
- TINY = 3
21
+ TINY = 4
22
22
 
23
23
  STRING = [MAJOR, MINOR, TINY].join('.')
24
24
  end
@@ -362,6 +362,13 @@ describe Signet::OAuth1::Client, 'unconfigured' do
362
362
  @client.token_credential_secret = 54321
363
363
  end).should raise_error(TypeError)
364
364
  end
365
+
366
+ it 'should not allow the two_legged flag ' +
367
+ 'to be set to a non-Boolean' do
368
+ (lambda do
369
+ @client.two_legged = 42
370
+ end).should raise_error(TypeError)
371
+ end
365
372
  end
366
373
 
367
374
  describe Signet::OAuth1::Client, 'configured' do
@@ -235,3 +235,22 @@ describe Signet::OAuth1::Client, 'configured for standard Google APIs' do
235
235
  merge_body(body).should == '{"data":"goes here"}'
236
236
  end
237
237
  end
238
+
239
+ describe Signet::OAuth1::Client, 'configured for two-legged OAuth' do
240
+ before do
241
+ @client = Signet::OAuth1::Client.new(
242
+ :client_credential_key => '12345',
243
+ :client_credential_secret => '12345',
244
+ :two_legged => true
245
+ )
246
+ end
247
+
248
+ it 'should raise an error if the client credentials are bogus' do
249
+ (lambda do
250
+ @client.fetch_protected_resource(
251
+ :uri =>
252
+ 'http://www-opensocial.googleusercontent.com/api/people/@me/@self'
253
+ )
254
+ end).should raise_error(Signet::AuthorizationError)
255
+ end
256
+ end
data/tasks/spec.rake CHANGED
@@ -18,7 +18,8 @@ namespace :spec do
18
18
  '--exclude', 'spec',
19
19
  '--exclude', 'compat',
20
20
  '--exclude', '1\\.8\\/gems',
21
- '--exclude', '1\\.9\\/gems'
21
+ '--exclude', '1\\.9\\/gems',
22
+ '--exclude', '\\.rvm'
22
23
  ]
23
24
  end
24
25
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bob Aman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-13 00:00:00 -07:00
18
+ date: 2010-10-21 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency