qoobaa-oauth 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/History.txt +114 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +71 -0
  6. data/Rakefile +58 -0
  7. data/TODO +31 -0
  8. data/VERSION +1 -0
  9. data/lib/oauth.rb +4 -0
  10. data/lib/oauth/cli.rb +378 -0
  11. data/lib/oauth/client.rb +4 -0
  12. data/lib/oauth/client/action_controller_request.rb +54 -0
  13. data/lib/oauth/client/helper.rb +85 -0
  14. data/lib/oauth/client/net_http.rb +106 -0
  15. data/lib/oauth/consumer.rb +370 -0
  16. data/lib/oauth/errors.rb +3 -0
  17. data/lib/oauth/errors/error.rb +4 -0
  18. data/lib/oauth/errors/problem.rb +14 -0
  19. data/lib/oauth/errors/unauthorized.rb +12 -0
  20. data/lib/oauth/helper.rb +78 -0
  21. data/lib/oauth/oauth.rb +11 -0
  22. data/lib/oauth/oauth_test_helper.rb +25 -0
  23. data/lib/oauth/request_proxy.rb +24 -0
  24. data/lib/oauth/request_proxy/action_controller_request.rb +73 -0
  25. data/lib/oauth/request_proxy/base.rb +166 -0
  26. data/lib/oauth/request_proxy/jabber_request.rb +41 -0
  27. data/lib/oauth/request_proxy/mock_request.rb +44 -0
  28. data/lib/oauth/request_proxy/net_http.rb +65 -0
  29. data/lib/oauth/request_proxy/rack_request.rb +40 -0
  30. data/lib/oauth/request_proxy/typhoeus_request.rb +53 -0
  31. data/lib/oauth/server.rb +66 -0
  32. data/lib/oauth/signature.rb +40 -0
  33. data/lib/oauth/signature/base.rb +87 -0
  34. data/lib/oauth/signature/hmac/md5.rb +21 -0
  35. data/lib/oauth/signature/hmac/rmd160.rb +21 -0
  36. data/lib/oauth/signature/hmac/sha1.rb +22 -0
  37. data/lib/oauth/signature/hmac/sha2.rb +21 -0
  38. data/lib/oauth/signature/md5.rb +13 -0
  39. data/lib/oauth/signature/plaintext.rb +23 -0
  40. data/lib/oauth/signature/rsa/sha1.rb +45 -0
  41. data/lib/oauth/signature/sha1.rb +13 -0
  42. data/lib/oauth/token.rb +7 -0
  43. data/lib/oauth/tokens/access_token.rb +68 -0
  44. data/lib/oauth/tokens/consumer_token.rb +33 -0
  45. data/lib/oauth/tokens/request_token.rb +32 -0
  46. data/lib/oauth/tokens/server_token.rb +9 -0
  47. data/lib/oauth/tokens/token.rb +17 -0
  48. data/lib/oauth/version.rb +3 -0
  49. data/qoobaa-oauth.gemspec +149 -0
  50. data/test/cases/oauth_case.rb +19 -0
  51. data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
  52. data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
  53. data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
  54. data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
  55. data/test/integration/consumer_test.rb +304 -0
  56. data/test/keys/rsa.cert +11 -0
  57. data/test/keys/rsa.pem +16 -0
  58. data/test/test_access_token.rb +26 -0
  59. data/test/test_action_controller_request_proxy.rb +133 -0
  60. data/test/test_consumer.rb +159 -0
  61. data/test/test_helper.rb +14 -0
  62. data/test/test_hmac_sha1.rb +20 -0
  63. data/test/test_net_http_client.rb +224 -0
  64. data/test/test_net_http_request_proxy.rb +72 -0
  65. data/test/test_oauth_helper.rb +49 -0
  66. data/test/test_rack_request_proxy.rb +40 -0
  67. data/test/test_request_token.rb +51 -0
  68. data/test/test_rsa_sha1.rb +59 -0
  69. data/test/test_server.rb +40 -0
  70. data/test/test_signature.rb +21 -0
  71. data/test/test_signature_base.rb +32 -0
  72. data/test/test_signature_plain_text.rb +26 -0
  73. data/test/test_token.rb +14 -0
  74. data/test/test_typhoeus_request_proxy.rb +72 -0
  75. metadata +209 -0
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'oauth/signature'
3
+ require 'oauth/request_proxy/mock_request'
4
+
5
+
6
+ class OAuthCase < Test::Unit::TestCase
7
+ # avoid whining about a lack of tests
8
+ def run(*args)
9
+ return if @method_name.to_s == "default_test"
10
+ super
11
+ end
12
+
13
+ protected
14
+
15
+ # Creates a fake request
16
+ def request(params={},method='GET',uri="http://photos.example.net/photos")
17
+ OAuth::RequestProxy.proxy({'parameters'=>params,'method'=>method,'uri'=>uri})
18
+ end
19
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../oauth_case')
2
+
3
+ # See http://oauth.net/core/1.0/#anchor14
4
+ #
5
+ #9.1.2. Construct Request URL
6
+ #
7
+ #The Signature Base String includes the request absolute URL, tying the signature to a specific endpoint. The URL used in the Signature Base String MUST include the scheme, authority, and path, and MUST exclude the query and fragment as defined by [RFC3986] section 3.
8
+ #
9
+ #If the absolute request URL is not available to the Service Provider (it is always available to the Consumer), it can be constructed by combining the scheme being used, the HTTP Host header, and the relative HTTP request URL. If the Host header is not available, the Service Provider SHOULD use the host name communicated to the Consumer in the documentation or other means.
10
+ #
11
+ #The Service Provider SHOULD document the form of URL used in the Signature Base String to avoid ambiguity due to URL normalization. Unless specified, URL scheme and authority MUST be lowercase and include the port number; http default port 80 and https default port 443 MUST be excluded.
12
+ #
13
+ #For example, the request:
14
+ #
15
+ # HTTP://Example.com:80/resource?id=123
16
+ #Is included in the Signature Base String as:
17
+ #
18
+ # http://example.com/resource
19
+
20
+
21
+ class ConstructRequestUrlTest < OAuthCase
22
+
23
+ def test_from_spec
24
+ assert_request_url("http://example.com/resource","HTTP://Example.com:80/resource?id=123")
25
+ end
26
+
27
+ def test_simple_url_with_ending_slash
28
+ assert_request_url("http://example.com/","http://example.com/")
29
+ end
30
+
31
+ def test_simple_url_without_ending_slash
32
+ assert_request_url("http://example.com/","http://example.com")
33
+ end
34
+
35
+ def test_of_normalized_http
36
+ assert_request_url("http://example.com/resource","http://example.com/resource")
37
+ end
38
+
39
+ def test_of_https
40
+ assert_request_url("https://example.com/resource","HTTPS://Example.com:443/resource?id=123")
41
+ end
42
+
43
+ def test_of_normalized_https
44
+ assert_request_url("https://example.com/resource","https://example.com/resource")
45
+ end
46
+
47
+ def test_of_http_with_non_standard_port
48
+ assert_request_url("http://example.com:8080/resource","http://example.com:8080/resource")
49
+ end
50
+
51
+ def test_of_https_with_non_standard_port
52
+ assert_request_url("https://example.com:8080/resource","https://example.com:8080/resource")
53
+ end
54
+
55
+ protected
56
+
57
+
58
+ def assert_request_url(expected,given,message=nil)
59
+ assert_equal expected, request({},'GET',given).normalized_uri, message
60
+ end
61
+
62
+ end
@@ -0,0 +1,88 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../oauth_case')
2
+
3
+ # See http://oauth.net/core/1.0/#anchor14
4
+ #
5
+ # 9.1.1. Normalize Request Parameters
6
+ #
7
+ # The request parameters are collected, sorted and concatenated into a normalized string:
8
+ #
9
+ # Parameters in the OAuth HTTP Authorization header excluding the realm parameter.
10
+ # Parameters in the HTTP POST request body (with a content-type of application/x-www-form-urlencoded).
11
+ # HTTP GET parameters added to the URLs in the query part (as defined by [RFC3986] section 3).
12
+ # The oauth_signature parameter MUST be excluded.
13
+ #
14
+ # The parameters are normalized into a single string as follows:
15
+ #
16
+ # Parameters are sorted by name, using lexicographical byte value ordering.
17
+ # If two or more parameters share the same name, they are sorted by their value. For example:
18
+ #
19
+ # a=1, c=hi%20there, f=25, f=50, f=a, z=p, z=t
20
+ # Parameters are concatenated in their sorted order into a single string. For each parameter,
21
+ # the name is separated from the corresponding value by an ‘=’ character (ASCII code 61), even
22
+ # if the value is empty. Each name-value pair is separated by an ‘&’ character (ASCII code 38). For example:
23
+ # a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t
24
+ #
25
+
26
+
27
+ class NormalizeRequestParametersTest < OAuthCase
28
+
29
+ def test_parameters_for_signature
30
+ params={'a'=>1, 'c'=>'hi there', 'f'=>'25', 'f'=>'50', 'f'=>'a', 'z'=>'p', 'z'=>'t'}
31
+ assert_equal params,request(params).parameters_for_signature
32
+ end
33
+
34
+
35
+ def test_parameters_for_signature_removes_oauth_signature
36
+ params={'a'=>1, 'c'=>'hi there', 'f'=>'25', 'f'=>'50', 'f'=>'a', 'z'=>'p', 'z'=>'t'}
37
+ assert_equal params,request(params.merge({'oauth_signature'=>'blalbla'})).parameters_for_signature
38
+ end
39
+
40
+ def test_spec_example
41
+ assert_normalized 'a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t', { 'a' => 1, 'c' => 'hi there', 'f' => ['25', '50', 'a'], 'z' => ['p', 't'] }
42
+ end
43
+
44
+ def test_sorts_parameters_correctly
45
+ # values for 'f' are scrambled
46
+ assert_normalized 'a=1&c=hi%20there&f=5&f=70&f=a&z=p&z=t', { 'a' => 1, 'c' => 'hi there', 'f' => ['a', '70', '5'], 'z' => ['p', 't'] }
47
+ end
48
+
49
+ def test_empty
50
+ assert_normalized "",{}
51
+ end
52
+
53
+
54
+ # These are from the wiki http://wiki.oauth.net/TestCases
55
+ # in the section Normalize Request Parameters
56
+ # Parameters have already been x-www-form-urlencoded (i.e. + = <space>)
57
+ def test_wiki1
58
+ assert_normalized "name=",{"name"=>nil}
59
+ end
60
+
61
+ def test_wiki2
62
+ assert_normalized "a=b",{'a'=>'b'}
63
+ end
64
+
65
+ def test_wiki3
66
+ assert_normalized "a=b&c=d",{'a'=>'b','c'=>'d'}
67
+ end
68
+
69
+ def test_wiki4
70
+ assert_normalized "a=x%20y&a=x%21y",{'a'=>["x!y","x y"]}
71
+
72
+ end
73
+
74
+ def test_wiki5
75
+ assert_normalized "x=a&x%21y=a",{"x!y"=>'a','x'=>'a'}
76
+ end
77
+
78
+ protected
79
+
80
+
81
+ def assert_normalized(expected,params,message=nil)
82
+ assert_equal expected, normalize_request_parameters(params), message
83
+ end
84
+
85
+ def normalize_request_parameters(params={})
86
+ request(params).normalized_parameters
87
+ end
88
+ end
@@ -0,0 +1,86 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../oauth_case')
2
+
3
+ # See http://oauth.net/core/1.0/#encoding_parameters
4
+ #
5
+ # 5.1. Parameter Encoding
6
+ #
7
+ # All parameter names and values are escaped using the [RFC3986] percent-encoding (%xx) mechanism.
8
+ # Characters not in the unreserved character set ([RFC3986] section 2.3) MUST be encoded. Characters
9
+ # in the unreserved character set MUST NOT be encoded. Hexadecimal characters in encodings MUST be
10
+ # upper case. Text names and values MUST be encoded as UTF-8 octets before percent-encoding them per [RFC3629].
11
+ #
12
+ # unreserved = ALPHA, DIGIT, '-', '.', '_', '~'
13
+ #
14
+
15
+ class ParameterEncodingTest < OAuthCase
16
+ def test_encodings_alpha_num
17
+ assert_encoding 'abcABC123', 'abcABC123'
18
+ end
19
+
20
+ def test_encodings_non_escaped
21
+ assert_encoding '-._~', '-._~'
22
+ end
23
+
24
+ def test_encodings_percent
25
+ assert_encoding '%25', '%'
26
+ end
27
+
28
+ def test_encodings_plus
29
+ assert_encoding '%2B', '+'
30
+ end
31
+
32
+ def test_encodings_space
33
+ assert_encoding '%20', ' '
34
+ end
35
+
36
+ def test_encodings_query_param_symbols
37
+ assert_encoding '%26%3D%2A', '&=*'
38
+ end
39
+
40
+ def test_encodings_unicode_lf
41
+ assert_encoding '%0A', unicode_to_utf8('U+000A')
42
+ end
43
+
44
+ def test_encodings_unicode_space
45
+ assert_encoding '%20', unicode_to_utf8('U+0020')
46
+ end
47
+
48
+ def test_encodings_unicode_007f
49
+ assert_encoding '%7F', unicode_to_utf8('U+007F')
50
+ end
51
+
52
+ def test_encodings_unicode_0080
53
+ assert_encoding '%C2%80', unicode_to_utf8('U+0080')
54
+ end
55
+
56
+ def test_encoding_unicode_2708
57
+ assert_encoding '%E2%9C%88', unicode_to_utf8('U+2708')
58
+ end
59
+
60
+ def test_encodings_unicode_3001
61
+ assert_encoding '%E3%80%81', unicode_to_utf8('U+3001')
62
+ end
63
+
64
+ protected
65
+
66
+ def unicode_to_utf8(unicode)
67
+ return unicode if unicode =~ /\A[[:space:]]*\z/m
68
+
69
+ str = ''
70
+
71
+ unicode.scan(/(U\+(?:[[:digit:][:xdigit:]]{4,5}|10[[:digit:][:xdigit:]]{4})|.)/mu) do
72
+ c = $1
73
+ if c =~ /^U\+/
74
+ str << [c[2..-1].hex].pack('U*')
75
+ else
76
+ str << c
77
+ end
78
+ end
79
+
80
+ str
81
+ end
82
+
83
+ def assert_encoding(expected, given, message = nil)
84
+ assert_equal expected, OAuth::Helper.escape(given), message
85
+ end
86
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../oauth_case')
2
+
3
+ # See http://oauth.net/core/1.0/#anchor14
4
+ #
5
+ # 9.1. Signature Base String
6
+ #
7
+ # The Signature Base String is a consistent reproducible concatenation of the request elements
8
+ # into a single string. The string is used as an input in hashing or signing algorithms. The
9
+ # HMAC-SHA1 signature method provides both a standard and an example of using the Signature
10
+ # Base String with a signing algorithm to generate signatures. All the request parameters MUST
11
+ # be encoded as described in Parameter Encoding prior to constructing the Signature Base String.
12
+ #
13
+
14
+ class SignatureBaseStringTest < OAuthCase
15
+
16
+ def test_A_5_1
17
+ parameters={
18
+ 'oauth_consumer_key'=>'dpf43f3p2l4k3l03',
19
+ 'oauth_token'=>'nnch734d00sl2jdk',
20
+ 'oauth_signature_method'=>'HMAC-SHA1',
21
+ 'oauth_timestamp'=>'1191242096',
22
+ 'oauth_nonce'=>'kllo9940pd9333jh',
23
+ 'oauth_version'=>'1.0',
24
+ 'file'=>'vacation.jpg',
25
+ 'size'=>'original'
26
+ }
27
+ sbs='GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal'
28
+
29
+ assert_signature_base_string sbs,parameters,'GET',"http://photos.example.net/photos"
30
+ end
31
+
32
+ # These are from the wiki http://wiki.oauth.net/TestCases
33
+ # in the section Concatenate Test Elements
34
+
35
+ def test_wiki_1_simple_with_ending_slash
36
+ parameters={
37
+ 'n'=>'v'
38
+ }
39
+ sbs='GET&http%3A%2F%2Fexample.com%2F&n%3Dv'
40
+
41
+ assert_signature_base_string sbs,parameters,'GET',"http://example.com/"
42
+ end
43
+
44
+
45
+ def test_wiki_2_simple_without_ending_slash
46
+ parameters={
47
+ 'n'=>'v'
48
+ }
49
+ sbs='GET&http%3A%2F%2Fexample.com%2F&n%3Dv'
50
+
51
+ assert_signature_base_string sbs,parameters,'GET',"http://example.com"
52
+ end
53
+
54
+ def test_wiki_2_request_token
55
+ parameters={
56
+ 'oauth_version'=>'1.0',
57
+ 'oauth_consumer_key'=>'dpf43f3p2l4k3l03',
58
+ 'oauth_timestamp'=>'1191242090',
59
+ 'oauth_nonce'=>'hsu94j3884jdopsl',
60
+ 'oauth_signature_method'=>'PLAINTEXT',
61
+ 'oauth_signature'=>'ignored' }
62
+ sbs='POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884jdopsl%26oauth_signature_method%3DPLAINTEXT%26oauth_timestamp%3D1191242090%26oauth_version%3D1.0'
63
+
64
+ assert_signature_base_string sbs,parameters,'POST',"https://photos.example.net/request_token"
65
+ end
66
+
67
+ protected
68
+
69
+
70
+ def assert_signature_base_string(expected,params={},method='GET',uri="http://photos.example.net/photos",message="Signature Base String does not match")
71
+ assert_equal expected, signature_base_string(params,method,uri), message
72
+ end
73
+
74
+ def signature_base_string(params={},method='GET',uri="http://photos.example.net/photos")
75
+ request(params,method,uri).signature_base_string
76
+ end
77
+ end
@@ -0,0 +1,304 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ module Integration
4
+ class ConsumerTest < Test::Unit::TestCase
5
+ def setup
6
+ @consumer=OAuth::Consumer.new(
7
+ 'consumer_key_86cad9', '5888bf0345e5d237',
8
+ {
9
+ :site=>"http://blabla.bla",
10
+ :proxy=>"http://user:password@proxy.bla:8080",
11
+ :request_token_path=>"/oauth/example/request_token.php",
12
+ :access_token_path=>"/oauth/example/access_token.php",
13
+ :authorize_path=>"/oauth/example/authorize.php",
14
+ :scheme=>:header,
15
+ :http_method=>:get
16
+ })
17
+ @token = OAuth::ConsumerToken.new(@consumer,'token_411a7f', '3196ffd991c8ebdb')
18
+ @request_uri = URI.parse('http://example.com/test?key=value')
19
+ @request_parameters = { 'key' => 'value' }
20
+ @nonce = 225579211881198842005988698334675835446
21
+ @timestamp = "1199645624"
22
+ @consumer.http=Net::HTTP.new(@request_uri.host, @request_uri.port)
23
+ end
24
+
25
+ def test_that_signing_auth_headers_on_get_requests_works
26
+ request = Net::HTTP::Get.new(@request_uri.path + "?" + request_parameters_to_s)
27
+ @token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
28
+
29
+ assert_equal 'GET', request.method
30
+ assert_equal '/test?key=value', request.path
31
+ assert_equal "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"1oO2izFav1GP4kEH2EskwXkCRFg%3D\", oauth_version=\"1.0\"".split(', ').sort, request['authorization'].split(', ').sort
32
+ end
33
+
34
+ def test_that_setting_signature_method_on_consumer_effects_signing
35
+ require 'oauth/signature/plaintext'
36
+ request = Net::HTTP::Get.new(@request_uri.path)
37
+ consumer = @consumer.dup
38
+ consumer.options[:signature_method] = 'PLAINTEXT'
39
+ token = OAuth::ConsumerToken.new(consumer, 'token_411a7f', '3196ffd991c8ebdb')
40
+ token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
41
+
42
+ assert_no_match( /oauth_signature_method="HMAC-SHA1"/, request['authorization'])
43
+ assert_match( /oauth_signature_method="PLAINTEXT"/, request['authorization'])
44
+ end
45
+
46
+ def test_that_setting_signature_method_on_consumer_effects_signature_base_string
47
+ require 'oauth/signature/plaintext'
48
+ request = Net::HTTP::Get.new(@request_uri.path)
49
+ consumer = @consumer.dup
50
+ consumer.options[:signature_method] = 'PLAINTEXT'
51
+
52
+ request = Net::HTTP::Get.new('/')
53
+ signature_base_string = consumer.signature_base_string(request)
54
+
55
+ assert_no_match( /HMAC-SHA1/, signature_base_string)
56
+ assert_equal( "#{consumer.secret}&", signature_base_string)
57
+ end
58
+
59
+ def test_that_plaintext_signature_works
60
+ # Invalid test because server expects double-escaped signature
61
+ require 'oauth/signature/plaintext'
62
+ # consumer = OAuth::Consumer.new("key", "secret",
63
+ # :site => "http://term.ie", :signature_method => 'PLAINTEXT')
64
+ # access_token = OAuth::AccessToken.new(consumer, 'accesskey', 'accesssecret')
65
+ # response = access_token.get("/oauth/example/echo_api.php?echo=hello")
66
+
67
+ # assert_equal 'echo=hello', response.body
68
+ end
69
+
70
+ def test_that_signing_auth_headers_on_post_requests_works
71
+ request = Net::HTTP::Post.new(@request_uri.path)
72
+ request.set_form_data( @request_parameters )
73
+ @token.sign!(request, {:nonce => @nonce, :timestamp => @timestamp})
74
+ # assert_equal "",request.oauth_helper.signature_base_string
75
+
76
+ assert_equal 'POST', request.method
77
+ assert_equal '/test', request.path
78
+ assert_equal 'key=value', request.body
79
+ assert_equal "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"26g7wHTtNO6ZWJaLltcueppHYiI%3D\", oauth_version=\"1.0\"".split(', ').sort, request['authorization'].split(', ').sort
80
+ end
81
+
82
+ def test_that_signing_post_params_works
83
+ request = Net::HTTP::Post.new(@request_uri.path)
84
+ request.set_form_data( @request_parameters )
85
+ @token.sign!(request, {:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp})
86
+
87
+ assert_equal 'POST', request.method
88
+ assert_equal '/test', request.path
89
+ assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=26g7wHTtNO6ZWJaLltcueppHYiI%3d&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", request.body.split("&").sort.join("&")
90
+ assert_equal nil, request['authorization']
91
+ end
92
+
93
+ def test_that_using_auth_headers_on_get_on_create_signed_requests_works
94
+ request=@consumer.create_signed_request(:get,@request_uri.path+ "?" + request_parameters_to_s,@token,{:nonce => @nonce, :timestamp => @timestamp},@request_parameters)
95
+
96
+ assert_equal 'GET', request.method
97
+ assert_equal '/test?key=value', request.path
98
+ assert_equal "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"1oO2izFav1GP4kEH2EskwXkCRFg%3D\", oauth_version=\"1.0\"".split(', ').sort, request['authorization'].split(', ').sort
99
+ end
100
+
101
+ def test_that_using_auth_headers_on_post_on_create_signed_requests_works
102
+ request=@consumer.create_signed_request(:post,@request_uri.path,@token,{:nonce => @nonce, :timestamp => @timestamp},@request_parameters,{})
103
+ assert_equal 'POST', request.method
104
+ assert_equal '/test', request.path
105
+ assert_equal 'key=value', request.body
106
+ assert_equal "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"26g7wHTtNO6ZWJaLltcueppHYiI%3D\", oauth_version=\"1.0\"".split(', ').sort, request['authorization'].split(', ').sort
107
+ end
108
+
109
+ def test_that_signing_post_params_works_2
110
+ request=@consumer.create_signed_request(:post,@request_uri.path,@token,{:scheme => 'body', :nonce => @nonce, :timestamp => @timestamp},@request_parameters,{})
111
+
112
+ assert_equal 'POST', request.method
113
+ assert_equal '/test', request.path
114
+ assert_equal "key=value&oauth_consumer_key=consumer_key_86cad9&oauth_nonce=225579211881198842005988698334675835446&oauth_signature=26g7wHTtNO6ZWJaLltcueppHYiI%3d&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1199645624&oauth_token=token_411a7f&oauth_version=1.0", request.body.split("&").sort.join("&")
115
+ assert_equal nil, request['authorization']
116
+ end
117
+
118
+ def test_step_by_step_token_request
119
+ @consumer=OAuth::Consumer.new(
120
+ "key",
121
+ "secret",
122
+ {
123
+ :site=>"http://term.ie",
124
+ :request_token_path=>"/oauth/example/request_token.php",
125
+ :access_token_path=>"/oauth/example/access_token.php",
126
+ :authorize_path=>"/oauth/example/authorize.php",
127
+ :scheme=>:header
128
+ })
129
+ options={:nonce=>'nonce',:timestamp=>Time.now.to_i.to_s}
130
+
131
+ request = Net::HTTP::Get.new("/oauth/example/request_token.php")
132
+ signature_base_string=@consumer.signature_base_string(request,nil,options)
133
+ assert_equal "GET&http%3A%2F%2Fterm.ie%2Foauth%2Fexample%2Frequest_token.php&oauth_consumer_key%3Dkey%26oauth_nonce%3D#{options[:nonce]}%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D#{options[:timestamp]}%26oauth_version%3D1.0",signature_base_string
134
+ @consumer.sign!(request, nil,options)
135
+
136
+ assert_equal 'GET', request.method
137
+ assert_equal nil, request.body
138
+ response=@consumer.http.request(request)
139
+ assert_equal "200",response.code
140
+ assert_equal "oauth_token=requestkey&oauth_token_secret=requestsecret",response.body
141
+ end
142
+
143
+ def test_get_token_sequence
144
+ @consumer=OAuth::Consumer.new(
145
+ "key",
146
+ "secret",
147
+ {
148
+ :site=>"http://term.ie",
149
+ :request_token_path=>"/oauth/example/request_token.php",
150
+ :access_token_path=>"/oauth/example/access_token.php",
151
+ :authorize_path=>"/oauth/example/authorize.php"
152
+ })
153
+ assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
154
+ assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
155
+
156
+ assert !@consumer.request_token_url?, "Should not use fully qualified request token url"
157
+ assert !@consumer.access_token_url?, "Should not use fully qualified access token url"
158
+ assert !@consumer.authorize_url?, "Should not use fully qualified url"
159
+
160
+ @request_token=@consumer.get_request_token
161
+ assert_not_nil @request_token
162
+ assert_equal "requestkey",@request_token.token
163
+ assert_equal "requestsecret",@request_token.secret
164
+ assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
165
+
166
+ @access_token=@request_token.get_access_token
167
+ assert_not_nil @access_token
168
+ assert_equal "accesskey",@access_token.token
169
+ assert_equal "accesssecret",@access_token.secret
170
+
171
+ @response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
172
+ assert_not_nil @response
173
+ assert_equal "200",@response.code
174
+ assert_equal( "ok=hello&test=this",@response.body)
175
+
176
+ @response=@access_token.post("/oauth/example/echo_api.php",{'ok'=>'hello','test'=>'this'})
177
+ assert_not_nil @response
178
+ assert_equal "200",@response.code
179
+ assert_equal( "ok=hello&test=this",@response.body)
180
+ end
181
+
182
+ def test_get_token_sequence_using_fqdn
183
+ @consumer=OAuth::Consumer.new(
184
+ "key",
185
+ "secret",
186
+ {
187
+ :site=>"http://term.ie",
188
+ :request_token_url=>"http://term.ie/oauth/example/request_token.php",
189
+ :access_token_url=>"http://term.ie/oauth/example/access_token.php",
190
+ :authorize_url=>"http://term.ie/oauth/example/authorize.php"
191
+ })
192
+ assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
193
+ assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
194
+
195
+ assert @consumer.request_token_url?, "Should use fully qualified request token url"
196
+ assert @consumer.access_token_url?, "Should use fully qualified access token url"
197
+ assert @consumer.authorize_url?, "Should use fully qualified url"
198
+
199
+ @request_token=@consumer.get_request_token
200
+ assert_not_nil @request_token
201
+ assert_equal "requestkey",@request_token.token
202
+ assert_equal "requestsecret",@request_token.secret
203
+ assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
204
+
205
+ @access_token=@request_token.get_access_token
206
+ assert_not_nil @access_token
207
+ assert_equal "accesskey",@access_token.token
208
+ assert_equal "accesssecret",@access_token.secret
209
+
210
+ @response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
211
+ assert_not_nil @response
212
+ assert_equal "200",@response.code
213
+ assert_equal( "ok=hello&test=this",@response.body)
214
+
215
+ @response=@access_token.post("/oauth/example/echo_api.php",{'ok'=>'hello','test'=>'this'})
216
+ assert_not_nil @response
217
+ assert_equal "200",@response.code
218
+ assert_equal( "ok=hello&test=this",@response.body)
219
+ end
220
+
221
+
222
+ # This test does an actual https request (the result doesn't matter)
223
+ # to initialize the same way as get_request_token does. Can be any
224
+ # site that supports https.
225
+ #
226
+ # It also generates "warning: using default DH parameters." which I
227
+ # don't know how to get rid of
228
+ # def test_serialization_with_https
229
+ # consumer = OAuth::Consumer.new('token', 'secret', :site => 'https://plazes.net')
230
+ # consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
231
+ # consumer.http.get('/')
232
+ #
233
+ # assert_nothing_raised do
234
+ # # Specifically this should not raise TypeError: no marshal_dump
235
+ # # is defined for class OpenSSL::SSL::SSLContext
236
+ # Marshal.dump(consumer)
237
+ # end
238
+ # end
239
+ #
240
+ def test_get_request_token_with_custom_arguments
241
+ @consumer=OAuth::Consumer.new(
242
+ "key",
243
+ "secret",
244
+ {
245
+ :site=>"http://term.ie",
246
+ :request_token_path=>"/oauth/example/request_token.php",
247
+ :access_token_path=>"/oauth/example/access_token.php",
248
+ :authorize_path=>"/oauth/example/authorize.php"
249
+ })
250
+
251
+
252
+ debug = ""
253
+ @consumer.http.set_debug_output(debug)
254
+
255
+ # get_request_token should receive our custom request_options and *arguments parameters from get_request_token.
256
+ @consumer.get_request_token({}, {:scope => "http://www.google.com/calendar/feeds http://picasaweb.google.com/data"})
257
+
258
+ # Because this is a POST request, create_http_request should take the first element of *arguments
259
+ # and turn it into URL-encoded data in the body of the POST.
260
+ assert_match( /^<- "scope=http%3a%2f%2fwww.google.com%2fcalendar%2ffeeds%20http%3a%2f%2fpicasaweb.google.com%2fdata"/,
261
+ debug)
262
+ end
263
+
264
+ def test_post_with_body_stream
265
+ @consumer=OAuth::Consumer.new(
266
+ "key",
267
+ "secret",
268
+ {
269
+ :site=>"http://term.ie",
270
+ :request_token_path=>"/oauth/example/request_token.php",
271
+ :access_token_path=>"/oauth/example/access_token.php",
272
+ :authorize_path=>"/oauth/example/authorize.php"
273
+ })
274
+
275
+
276
+ @request_token=@consumer.get_request_token
277
+ @access_token=@request_token.get_access_token
278
+
279
+ request_body_string = "Hello, hello, hello"
280
+ request_body_stream = StringIO.new( request_body_string )
281
+
282
+ @response=@access_token.post("/oauth/example/echo_api.php",request_body_stream)
283
+ assert_not_nil @response
284
+ assert_equal "200",@response.code
285
+
286
+ request_body_file = File.open(__FILE__)
287
+
288
+ @response=@access_token.post("/oauth/example/echo_api.php",request_body_file)
289
+ assert_not_nil @response
290
+ assert_equal "200",@response.code
291
+
292
+ # unfortunately I don't know of a way to test that the body data was received correctly since the test server at http://term.ie
293
+ # echos back any non-oauth parameters but not the body. However, this does test that the request is still correctly signed
294
+ # (including the Content-Length header) and that the server received Content-Length bytes of body since it won't process the
295
+ # request & respond until the full body length is received.
296
+ end
297
+
298
+ private
299
+
300
+ def request_parameters_to_s
301
+ @request_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
302
+ end
303
+ end
304
+ end