pelle-oauth 0.3.1 → 0.3.5

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.
Files changed (54) hide show
  1. data/History.txt +66 -17
  2. data/Manifest.txt +14 -1
  3. data/README.rdoc +7 -9
  4. data/Rakefile +7 -5
  5. data/TODO +17 -0
  6. data/bin/oauth +2 -2
  7. data/examples/yql.rb +44 -0
  8. data/lib/oauth.rb +1 -0
  9. data/lib/oauth/cli.rb +279 -31
  10. data/lib/oauth/client/action_controller_request.rb +14 -12
  11. data/lib/oauth/client/helper.rb +22 -14
  12. data/lib/oauth/client/net_http.rb +53 -22
  13. data/lib/oauth/consumer.rb +217 -111
  14. data/lib/oauth/errors.rb +3 -0
  15. data/lib/oauth/errors/error.rb +4 -0
  16. data/lib/oauth/errors/problem.rb +14 -0
  17. data/lib/oauth/errors/unauthorized.rb +12 -0
  18. data/lib/oauth/helper.rb +67 -6
  19. data/lib/oauth/oauth.rb +11 -0
  20. data/lib/oauth/oauth_test_helper.rb +12 -13
  21. data/lib/oauth/request_proxy/action_controller_request.rb +8 -8
  22. data/lib/oauth/request_proxy/base.rb +102 -44
  23. data/lib/oauth/request_proxy/jabber_request.rb +1 -2
  24. data/lib/oauth/request_proxy/mock_request.rb +8 -0
  25. data/lib/oauth/request_proxy/net_http.rb +2 -2
  26. data/lib/oauth/request_proxy/rack_request.rb +7 -7
  27. data/lib/oauth/server.rb +31 -33
  28. data/lib/oauth/signature.rb +9 -0
  29. data/lib/oauth/signature/base.rb +23 -21
  30. data/lib/oauth/signature/hmac/base.rb +1 -1
  31. data/lib/oauth/signature/hmac/sha1.rb +0 -1
  32. data/lib/oauth/signature/plaintext.rb +2 -2
  33. data/lib/oauth/signature/rsa/sha1.rb +5 -4
  34. data/lib/oauth/token.rb +6 -136
  35. data/lib/oauth/tokens/access_token.rb +68 -0
  36. data/lib/oauth/tokens/consumer_token.rb +33 -0
  37. data/lib/oauth/tokens/request_token.rb +32 -0
  38. data/lib/oauth/tokens/server_token.rb +9 -0
  39. data/lib/oauth/tokens/token.rb +17 -0
  40. data/lib/oauth/version.rb +1 -1
  41. data/oauth.gemspec +13 -7
  42. data/test/cases/spec/1_0-final/test_construct_request_url.rb +1 -1
  43. data/test/test_access_token.rb +28 -0
  44. data/test/test_action_controller_request_proxy.rb +105 -6
  45. data/test/test_consumer.rb +41 -5
  46. data/test/test_helper.rb +0 -5
  47. data/test/test_net_http_client.rb +38 -20
  48. data/test/test_net_http_request_proxy.rb +43 -8
  49. data/test/test_oauth_helper.rb +50 -0
  50. data/test/test_request_token.rb +53 -0
  51. data/test/test_server.rb +1 -1
  52. data/test/test_signature.rb +19 -11
  53. data/website/index.html +2 -2
  54. metadata +46 -6
@@ -5,11 +5,11 @@ require 'rack'
5
5
  module OAuth::RequestProxy
6
6
  class RackRequest < OAuth::RequestProxy::Base
7
7
  proxies Rack::Request
8
-
8
+
9
9
  def method
10
- request.request_method
10
+ request.env["rack.methodoverride.original_method"] || request.request_method
11
11
  end
12
-
12
+
13
13
  def uri
14
14
  request.url
15
15
  end
@@ -22,12 +22,12 @@ module OAuth::RequestProxy
22
22
  params.merge(options[:parameters] || {})
23
23
  end
24
24
  end
25
-
25
+
26
26
  def signature
27
27
  parameters['oauth_signature']
28
28
  end
29
-
30
- protected
29
+
30
+ protected
31
31
 
32
32
  def query_params
33
33
  request.GET
@@ -37,4 +37,4 @@ module OAuth::RequestProxy
37
37
  request.params
38
38
  end
39
39
  end
40
- end
40
+ end
data/lib/oauth/server.rb CHANGED
@@ -1,68 +1,66 @@
1
1
  require 'oauth/helper'
2
2
  require 'oauth/consumer'
3
+
3
4
  module OAuth
4
5
  # This is mainly used to create consumer credentials and can pretty much be ignored if you want to create your own
5
6
  class Server
6
7
  include OAuth::Helper
7
8
  attr_accessor :base_url
8
-
9
- @@server_paths={
10
- :request_token_path=>"/oauth/request_token",
11
- :authorize_path=>"/oauth/authorize",
12
- :access_token_path=>"/oauth/access_token"
9
+
10
+ @@server_paths = {
11
+ :request_token_path => "/oauth/request_token",
12
+ :authorize_path => "/oauth/authorize",
13
+ :access_token_path => "/oauth/access_token"
13
14
  }
15
+
14
16
  # Create a new server instance
15
- def initialize(base_url,paths={})
16
- @base_url=base_url
17
- @paths=@@server_paths.merge(paths)
17
+ def initialize(base_url, paths = {})
18
+ @base_url = base_url
19
+ @paths = @@server_paths.merge(paths)
18
20
  end
19
-
20
- def generate_credentials()
21
- [generate_key(16),generate_key]
21
+
22
+ def generate_credentials
23
+ [generate_key(16), generate_key]
22
24
  end
23
-
24
- def generate_consumer_credentials(params={})
25
- Consumer.new( *generate_credentials)
25
+
26
+ def generate_consumer_credentials(params = {})
27
+ Consumer.new(*generate_credentials)
26
28
  end
27
29
 
28
30
  # mainly for testing purposes
29
31
  def create_consumer
30
- credentials=generate_credentials
31
- Consumer.new(
32
- credentials[0],
33
- credentials[1],
32
+ creds = generate_credentials
33
+ Consumer.new(creds[0], creds[1],
34
34
  {
35
- :site=>base_url,
36
- :request_token_path=>request_token_path,
37
- :authorize_path=>authorize_path,
38
- :access_token_path=>access_token_path
39
- })
35
+ :site => base_url,
36
+ :request_token_path => request_token_path,
37
+ :authorize_path => authorize_path,
38
+ :access_token_path => access_token_path
39
+ })
40
40
  end
41
-
41
+
42
42
  def request_token_path
43
43
  @paths[:request_token_path]
44
44
  end
45
-
45
+
46
46
  def request_token_url
47
- base_url+request_token_path
47
+ base_url + request_token_path
48
48
  end
49
-
49
+
50
50
  def authorize_path
51
51
  @paths[:authorize_path]
52
52
  end
53
-
53
+
54
54
  def authorize_url
55
- base_url+authorize_path
55
+ base_url + authorize_path
56
56
  end
57
-
57
+
58
58
  def access_token_path
59
59
  @paths[:access_token_path]
60
60
  end
61
61
 
62
62
  def access_token_url
63
- base_url+access_token_path
63
+ base_url + access_token_path
64
64
  end
65
-
66
-
67
65
  end
68
66
  end
@@ -1,9 +1,13 @@
1
1
  module OAuth
2
2
  module Signature
3
+ # Returns a list of available signature methods
3
4
  def self.available_methods
4
5
  @available_methods ||= {}
5
6
  end
6
7
 
8
+ # Build a signature from a +request+.
9
+ #
10
+ # Raises UnknownSignatureMethod exception if the signature method is unknown.
7
11
  def self.build(request, options = {}, &block)
8
12
  request = OAuth::RequestProxy.proxy(request, options)
9
13
  klass = available_methods[(request.signature_method || "").downcase]
@@ -11,14 +15,19 @@ module OAuth
11
15
  klass.new(request, options, &block)
12
16
  end
13
17
 
18
+ # Sign a +request+
14
19
  def self.sign(request, options = {}, &block)
15
20
  self.build(request, options, &block).signature
16
21
  end
17
22
 
23
+ # Verify the signature of +request+
18
24
  def self.verify(request, options = {}, &block)
19
25
  self.build(request, options, &block).verify
20
26
  end
21
27
 
28
+ # Create the signature base string for +request+. This string is the normalized parameter information.
29
+ #
30
+ # See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1]
22
31
  def self.signature_base_string(request, options = {}, &block)
23
32
  self.build(request, options, &block).signature_base_string
24
33
  end
@@ -6,9 +6,10 @@ require 'base64'
6
6
  module OAuth::Signature
7
7
  class Base
8
8
  include OAuth::Helper
9
-
9
+
10
10
  attr_accessor :options
11
-
11
+ attr_reader :token_secret, :consumer_secret, :request
12
+
12
13
  def self.implements(signature_method)
13
14
  OAuth::Signature.available_methods[signature_method] = self
14
15
  end
@@ -18,32 +19,34 @@ module OAuth::Signature
18
19
  @digest_class = digest_class
19
20
  end
20
21
 
21
- attr_reader :token_secret, :consumer_secret, :request
22
-
23
22
  def initialize(request, options = {}, &block)
24
23
  raise TypeError unless request.kind_of?(OAuth::RequestProxy::Base)
25
24
  @request = request
26
25
  @options = options
27
26
 
28
- if block_given?
27
+ ## consumer secret was determined beforehand
29
28
 
30
- # consumer secret and token secret need to be looked up based on pieces of the request
31
- @token_secret, @consumer_secret = yield block.arity == 1 ? request : [token, consumer_key,nonce,request.timestamp]
29
+ @consumer_secret = options[:consumer].secret if options[:consumer]
32
30
 
33
- else
34
- ## consumer secret was determined beforehand
31
+ # presence of :consumer_secret option will override any Consumer that's provided
32
+ @consumer_secret = options[:consumer_secret] if options[:consumer_secret]
35
33
 
36
- @consumer_secret = options[:consumer].secret if options[:consumer]
34
+ ## token secret was determined beforehand
37
35
 
38
- # presence of :consumer_secret option will override any Consumer that's provided
39
- @consumer_secret = options[:consumer_secret] if options[:consumer_secret]
36
+ @token_secret = options[:token].secret if options[:token]
40
37
 
41
- ## token secret was determined beforehand
38
+ # presence of :token_secret option will override any Token that's provided
39
+ @token_secret = options[:token_secret] if options[:token_secret]
42
40
 
43
- @token_secret = options[:token].secret if options[:token]
44
41
 
45
- # presence of :token_secret option will override any Token that's provided
46
- @token_secret = options[:token_secret] if options[:token_secret]
42
+ # override secrets based on the values returned from the block (if any)
43
+ if block_given?
44
+ # consumer secret and token secret need to be looked up based on pieces of the request
45
+ secrets = yield block.arity == 1 ? request : [token, consumer_key, nonce, request.timestamp]
46
+ if secrets.is_a?(Array) && secrets.size == 2
47
+ @token_secret = secrets[0]
48
+ @consumer_secret = secrets[1]
49
+ end
47
50
  end
48
51
  end
49
52
 
@@ -62,17 +65,17 @@ module OAuth::Signature
62
65
  def signature_base_string
63
66
  request.signature_base_string
64
67
  end
65
-
66
- private
68
+
69
+ private
67
70
 
68
71
  def token
69
72
  request.token
70
73
  end
71
-
74
+
72
75
  def consumer_key
73
76
  request.consumer_key
74
77
  end
75
-
78
+
76
79
  def nonce
77
80
  request.nonce
78
81
  end
@@ -84,6 +87,5 @@ module OAuth::Signature
84
87
  def digest
85
88
  self.class.digest_class.digest(signature_base_string)
86
89
  end
87
-
88
90
  end
89
91
  end
@@ -3,7 +3,7 @@ require 'oauth/signature/base'
3
3
  module OAuth::Signature::HMAC
4
4
  class Base < OAuth::Signature::Base
5
5
 
6
- private
6
+ private
7
7
 
8
8
  def digest
9
9
  self.class.digest_class.digest(secret, signature_base_string)
@@ -1,5 +1,4 @@
1
1
  require 'oauth/signature/hmac/base'
2
- require 'rubygems'
3
2
  require 'hmac-sha1'
4
3
 
5
4
  module OAuth::Signature::HMAC
@@ -15,9 +15,9 @@ module OAuth::Signature
15
15
  def signature_base_string
16
16
  secret
17
17
  end
18
-
18
+
19
19
  def secret
20
- escape super
20
+ escape(super)
21
21
  end
22
22
  end
23
23
  end
@@ -18,9 +18,9 @@ module OAuth::Signature::RSA
18
18
  consumer_secret
19
19
  end
20
20
  end
21
-
22
- private
23
-
21
+
22
+ private
23
+
24
24
  def decode_public_key
25
25
  case consumer_secret
26
26
  when /-----BEGIN CERTIFICATE-----/
@@ -29,7 +29,7 @@ module OAuth::Signature::RSA
29
29
  OpenSSL::PKey::RSA.new( consumer_secret)
30
30
  end
31
31
  end
32
-
32
+
33
33
  def digest
34
34
  private_key = OpenSSL::PKey::RSA.new(
35
35
  if options[:private_key_file]
@@ -38,6 +38,7 @@ module OAuth::Signature::RSA
38
38
  consumer_secret
39
39
  end
40
40
  )
41
+
41
42
  private_key.sign(OpenSSL::Digest::SHA1.new, signature_base_string)
42
43
  end
43
44
  end
data/lib/oauth/token.rb CHANGED
@@ -1,137 +1,7 @@
1
- require 'oauth/helper'
2
- module OAuth
3
-
4
- # Superclass for the various tokens used by OAuth
5
-
6
- class Token
7
- include OAuth::Helper
8
-
9
- attr_accessor :token, :secret
1
+ # this exists for backwards-compatibility
10
2
 
11
- def initialize(token, secret)
12
- @token = token
13
- @secret = secret
14
- end
15
-
16
- def to_query
17
- "oauth_token=#{escape(token)}&oauth_secret=#{escape(secret)}"
18
- end
19
-
20
- end
21
-
22
- # Used on the server for generating tokens
23
- class ServerToken<Token
24
-
25
- def initialize
26
- super generate_key(16),generate_key
27
- end
28
- end
29
- # Superclass for tokens used by OAuth Clients
30
- class ConsumerToken<Token
31
- attr_accessor :consumer
32
-
33
- def initialize(consumer,token="",secret="")
34
- super token,secret
35
- @consumer=consumer
36
- end
37
-
38
- # Make a signed request using given http_method to the path
39
- #
40
- # @token.request(:get,'/people')
41
- # @token.request(:post,'/people',@person.to_xml,{ 'Content-Type' => 'application/xml' })
42
- #
43
- def request(http_method,path,*arguments)
44
- response=consumer.request(http_method,path,self,{},*arguments)
45
- end
46
-
47
- # Sign a request generated elsewhere using Net:HTTP::Post.new or friends
48
- def sign!(request,options = {})
49
- consumer.sign!(request,self,options)
50
- end
51
-
52
- end
53
-
54
- # The RequestToken is used for the initial Request.
55
- # This is normally created by the Consumer object.
56
- class RequestToken<ConsumerToken
57
-
58
- # Returns the authorization url that you need to use for redirecting the user
59
- def authorize_url
60
- consumer.authorize_url+"?oauth_token="+CGI.escape(token)
61
- end
62
-
63
- # exchange for AccessToken on server
64
- def get_access_token(options={})
65
- response=consumer.token_request(consumer.http_method,(consumer.access_token_url? ? consumer.access_token_url : consumer.access_token_path),self,options)
66
- OAuth::AccessToken.new(consumer,response[:oauth_token],response[:oauth_token_secret])
67
- end
68
- end
69
-
70
- # The Access Token is used for the actual "real" web service calls thatyou perform against the server
71
- class AccessToken<ConsumerToken
72
-
73
- # The less intrusive way. Otherwise, if we are to do it correctly inside consumer,
74
- # we need to restructure and touch more methods: request(), sign!(), etc.
75
- def request(http_method, path, *arguments)
76
- request_uri = URI.parse(path)
77
- site_uri = consumer.uri
78
- is_service_uri_different = (request_uri.absolute? && request_uri != site_uri)
79
- consumer.uri(request_uri) if is_service_uri_different
80
- resp = super(http_method, path, *arguments)
81
- # NOTE: reset for wholesomeness? meaning that we admit only AccessToken service calls may use different URIs?
82
- # so reset in case consumer is still used for other token-management tasks subsequently?
83
- consumer.uri(site_uri) if is_service_uri_different
84
- resp
85
- end
86
-
87
- # Make a regular get request using AccessToken
88
- #
89
- # @response=@token.get('/people')
90
- # @response=@token.get('/people',{'Accept'=>'application/xml'})
91
- #
92
- def get(path,headers={})
93
- request(:get,path,headers)
94
- end
95
-
96
- # Make a regular head request using AccessToken
97
- #
98
- # @response=@token.head('/people')
99
- #
100
- def head(path,headers={})
101
- request(:head,path,headers)
102
- end
103
-
104
- # Make a regular post request using AccessToken
105
- #
106
- # @response=@token.post('/people')
107
- # @response=@token.post('/people',{:name=>'Bob',:email=>'bob@mailinator.com'})
108
- # @response=@token.post('/people',{:name=>'Bob',:email=>'bob@mailinator.com'},{'Accept'=>'application/xml'})
109
- # @response=@token.post('/people',nil,{'Accept'=>'application/xml'})
110
- # @response=@token.post('/people',@person.to_xml,{'Accept'=>'application/xml','Content-Type' => 'application/xml'})
111
- #
112
- def post(path, body = '',headers={})
113
- request(:post,path,body,headers)
114
- end
115
-
116
- # Make a regular put request using AccessToken
117
- #
118
- # @response=@token.put('/people/123')
119
- # @response=@token.put('/people/123',{:name=>'Bob',:email=>'bob@mailinator.com'})
120
- # @response=@token.put('/people/123',{:name=>'Bob',:email=>'bob@mailinator.com'},{'Accept'=>'application/xml'})
121
- # @response=@token.put('/people/123',nil,{'Accept'=>'application/xml'})
122
- # @response=@token.put('/people/123',@person.to_xml,{'Accept'=>'application/xml','Content-Type' => 'application/xml'})
123
- #
124
- def put(path, body = '', headers={})
125
- request(:put,path,body,headers)
126
- end
127
-
128
- # Make a regular delete request using AccessToken
129
- #
130
- # @response=@token.delete('/people/123')
131
- # @response=@token.delete('/people/123',{'Accept'=>'application/xml'})
132
- #
133
- def delete(path,headers={})
134
- request(:delete,path,headers)
135
- end
136
- end
137
- end
3
+ require 'oauth/tokens/token'
4
+ require 'oauth/tokens/server_token'
5
+ require 'oauth/tokens/consumer_token'
6
+ require 'oauth/tokens/request_token'
7
+ require 'oauth/tokens/access_token'