mojodna-oauth 0.3.1.1 → 0.3.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -6,6 +6,7 @@ Rakefile
6
6
  TODO
7
7
  bin/oauth
8
8
  lib/oauth.rb
9
+ lib/oauth/oauth.rb
9
10
  lib/oauth/cli.rb
10
11
  lib/oauth/client.rb
11
12
  lib/oauth/client/action_controller_request.rb
@@ -40,7 +41,6 @@ script/destroy
40
41
  script/generate
41
42
  script/txt2html
42
43
  setup.rb
43
- specs.txt
44
44
  tasks/deployment.rake
45
45
  tasks/environment.rake
46
46
  tasks/website.rake
data/lib/oauth/cli.rb CHANGED
@@ -24,29 +24,43 @@ module OAuth
24
24
  if sufficient_options? && valid_command?
25
25
  case command
26
26
  when "sign"
27
+ parameters = prepare_parameters
28
+
27
29
  request = OAuth::RequestProxy.proxy \
28
30
  "method" => options[:method],
29
31
  "uri" => options[:uri],
30
- "parameters" => prepare_parameters
32
+ "parameters" => parameters
31
33
 
32
- # can't pass options unless they respond to :secret, so use this alternative
33
- signature = OAuth::Signature.sign \
34
- request,
35
- :consumer_secret => options[:oauth_consumer_secret],
36
- :token_secret => options[:oauth_token_secret] do |request|
37
-
38
- # while we have access to the request being signed, display some internals
39
- if verbose?
40
- stdout.puts "Method: #{request.method}"
41
- stdout.puts "URI: #{request.uri}"
42
- stdout.puts "Normalized params: #{request.normalized_parameters}"
43
- stdout.puts "Signature base string: #{request.signature_base_string}"
34
+ if verbose?
35
+ stdout.puts "OAuth parameters:"
36
+ request.oauth_parameters.each do |k,v|
37
+ stdout.puts " " + [k, v] * ": "
38
+ end
39
+ stdout.puts
40
+
41
+ if request.non_oauth_parameters.any?
42
+ stdout.puts "Parameters:"
43
+ request.non_oauth_parameters.each do |k,v|
44
+ stdout.puts " " + [k, v] * ": "
45
+ end
46
+ stdout.puts
44
47
  end
45
48
  end
46
49
 
50
+ request.sign! \
51
+ :consumer_secret => options[:oauth_consumer_secret],
52
+ :token_secret => options[:oauth_token_secret]
53
+
47
54
  if verbose?
48
- stdout.puts "Signature: #{signature}"
49
- stdout.puts "Escaped signature: #{OAuth::Helper.escape(signature)}"
55
+ stdout.puts "Method: #{request.method}"
56
+ stdout.puts "Base URI: #{request.uri}"
57
+ stdout.puts "Normalized params: #{request.normalized_parameters}"
58
+ stdout.puts "Signature base string: #{request.signature_base_string}"
59
+ stdout.puts "OAuth Request URI: #{request.signed_uri}"
60
+ stdout.puts "URI: #{request.signed_uri(false)}"
61
+ stdout.puts "Authorization header: #{request.oauth_header(:realm => options[:realm])}"
62
+ stdout.puts "Signature: #{request.signature}"
63
+ stdout.puts "Escaped signature: #{OAuth::Helper.escape(request.signature)}"
50
64
  else
51
65
  stdout.puts signature
52
66
  end
@@ -68,7 +82,11 @@ module OAuth
68
82
  opts.banner = "Usage: #{$0} [options] <command>"
69
83
 
70
84
  # defaults
85
+ options[:oauth_nonce] = OAuth::Helper.generate_key
71
86
  options[:oauth_signature_method] = "HMAC-SHA1"
87
+ options[:oauth_timestamp] = OAuth::Helper.generate_timestamp
88
+ options[:oauth_version] = "1.0"
89
+ options[:params] = ""
72
90
 
73
91
  opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
74
92
  options[:oauth_consumer_key] = v
@@ -82,6 +100,10 @@ module OAuth
82
100
  options[:method] = v
83
101
  end
84
102
 
103
+ opts.on("--nonce NONCE", "Specifies the none to use.") do |v|
104
+ options[:oauth_nonce] = v
105
+ end
106
+
85
107
  opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
86
108
  options[:params] = v
87
109
  end
@@ -94,14 +116,30 @@ module OAuth
94
116
  options[:oauth_token_secret] = v
95
117
  end
96
118
 
119
+ opts.on("--timestamp TIMESTAMP", "Specifies the timestamp to use.") do |v|
120
+ options[:oauth_timestamp] = v
121
+ end
122
+
97
123
  opts.on("--token TOKEN", "Specifies the token to use.") do |v|
98
124
  options[:oauth_token] = v
99
125
  end
100
126
 
127
+ opts.on("--realm REALM", "Specifies the realm to use.") do |v|
128
+ options[:realm] = v
129
+ end
130
+
101
131
  opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
102
132
  options[:uri] = v
103
133
  end
104
134
 
135
+ opts.on("--version VERSION", "Specifies the OAuth version to use.") do |v|
136
+ options[:oauth_version] = v
137
+ end
138
+
139
+ opts.on("--no-version", "Omit oauth_version.") do
140
+ options[:oauth_version] = nil
141
+ end
142
+
105
143
  opts.on("-v", "--verbose", "Be verbose.") do
106
144
  options[:verbose] = true
107
145
  end
@@ -113,11 +151,25 @@ module OAuth
113
151
  end
114
152
 
115
153
  def prepare_parameters
154
+ escaped_pairs = options[:params].split("&").collect do |pair|
155
+ Hash[*pair.split("=")].collect do |k,v|
156
+ puts "k: #{k}"
157
+ puts "v: #{v}"
158
+ [CGI.escape(k), CGI.escape(v)] * "="
159
+ end
160
+ end
161
+
162
+ querystring = escaped_pairs * "&"
163
+ cli_params = CGI.parse(querystring)
164
+
116
165
  {
117
166
  "oauth_consumer_key" => options[:oauth_consumer_key],
167
+ "oauth_nonce" => options[:oauth_nonce],
168
+ "oauth_timestamp" => options[:oauth_timestamp],
118
169
  "oauth_token" => options[:oauth_token],
119
- "oauth_signature_method" => options[:oauth_signature_method]
120
- }.merge(CGI.parse(options[:params]))
170
+ "oauth_signature_method" => options[:oauth_signature_method],
171
+ "oauth_version" => options[:oauth_version]
172
+ }.reject { |k,v| v.nil? || v == "" }.merge(cli_params)
121
173
  end
122
174
 
123
175
  def sufficient_options?
@@ -26,10 +26,6 @@ module OAuth::Client
26
26
  options[:timestamp] ||= generate_timestamp
27
27
  end
28
28
 
29
- def generate_timestamp
30
- Time.now.to_i.to_s
31
- end
32
-
33
29
  def oauth_parameters
34
30
  {
35
31
  'oauth_consumer_key' => options[:consumer].key,
@@ -102,7 +102,7 @@ module OAuth
102
102
  # @consumer.request(:post, '/people', @token, {}, @person.to_xml, { 'Content-Type' => 'application/xml' })
103
103
  #
104
104
  def request(http_method, path, token = nil, request_options = {}, *arguments)
105
- if path = ~/^\//
105
+ if path =~ /^\//
106
106
  _http = http
107
107
  else
108
108
  _http = create_http(path)
data/lib/oauth/helper.rb CHANGED
@@ -13,5 +13,27 @@ module OAuth
13
13
  def generate_key(size=32)
14
14
  Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
15
15
  end
16
+
17
+ def generate_timestamp
18
+ Time.now.to_i.to_s
19
+ end
20
+
21
+ def normalize(params)
22
+ params.sort.map do |k, values|
23
+
24
+ if values.is_a?(Array)
25
+ # multiple values were provided for a single key
26
+ values.sort.collect do |v|
27
+ [escape(k),escape(v)] * "="
28
+ end
29
+ else
30
+ [escape(k),escape(values)] * "="
31
+ end
32
+ end * "&"
33
+ end
34
+
35
+ def unescape(value)
36
+ URI.unescape(value.gsub('+', '%2B'))
37
+ end
16
38
  end
17
39
  end
@@ -24,10 +24,20 @@ module OAuth::RequestProxy
24
24
  parameters['oauth_consumer_key']
25
25
  end
26
26
 
27
+ def parameters
28
+ raise NotImplementedError, "Must be implemented by subclasses"
29
+ end
30
+
27
31
  def parameters_for_signature
28
- p = parameters.dup
29
- p.delete("oauth_signature")
30
- p
32
+ parameters.reject { |k,v| k == "oauth_signature" }
33
+ end
34
+
35
+ def oauth_parameters
36
+ parameters.select { |k,v| OAuth::PARAMETERS.include?(k) }.reject { |k,v| v == "" }
37
+ end
38
+
39
+ def non_oauth_parameters
40
+ parameters.reject { |k,v| OAuth::PARAMETERS.include?(k) }
31
41
  end
32
42
 
33
43
  def nonce
@@ -59,17 +69,17 @@ module OAuth::RequestProxy
59
69
 
60
70
  # See 9.1.1. in specs Normalize Request Parameters
61
71
  def normalized_parameters
62
- parameters_for_signature.sort.map do |k, values|
72
+ normalize(parameters_for_signature)
73
+ end
63
74
 
64
- if values.is_a?(Array)
65
- # multiple values were provided for a single key
66
- values.sort.collect do |v|
67
- [escape(k),escape(v)] * "="
68
- end
69
- else
70
- [escape(k),escape(values)] * "="
71
- end
72
- end * "&"
75
+ def sign(options = {})
76
+ OAuth::Signature.sign(self, options)
77
+ end
78
+
79
+ def sign!(options = {})
80
+ parameters["oauth_signature"] = sign(options)
81
+ @signed = true
82
+ signature
73
83
  end
74
84
 
75
85
  # See 9.1 in specs
@@ -78,6 +88,34 @@ module OAuth::RequestProxy
78
88
  base.map { |v| escape(v) }.join("&")
79
89
  end
80
90
 
91
+ # Has this request been signed yet?
92
+ def signed?
93
+ @signed
94
+ end
95
+
96
+ # URI, including OAuth parameters
97
+ def signed_uri(with_oauth = true)
98
+ if signed?
99
+ if with_oauth
100
+ params = parameters
101
+ else
102
+ params = non_oauth_parameters
103
+ end
104
+
105
+ [uri, normalize(params)] * "?"
106
+ else
107
+ STDERR.puts "This request has not yet been signed!"
108
+ end
109
+ end
110
+
111
+ # Authorization header for OAuth
112
+ def oauth_header(options = {})
113
+ header_params_str = oauth_parameters.map { |k,v| "#{k}=\"#{escape(v)}\"" }.join(', ')
114
+
115
+ realm = "realm=\"#{options[:realm]}\", " if options[:realm]
116
+ "OAuth #{realm}#{header_params_str}"
117
+ end
118
+
81
119
  protected
82
120
 
83
121
  def header_params
@@ -98,9 +136,5 @@ module OAuth::RequestProxy
98
136
 
99
137
  return {}
100
138
  end
101
-
102
- def unescape(value)
103
- URI.unescape(value.gsub('+', '%2B'))
104
- end
105
139
  end
106
140
  end
data/lib/oauth/server.rb CHANGED
@@ -29,13 +29,14 @@ module OAuth
29
29
 
30
30
  # mainly for testing purposes
31
31
  def create_consumer
32
- Consumer.new(*generate_credentials,
32
+ creds = generate_credentials
33
+ Consumer.new(creds[0], creds[1],
33
34
  {
34
35
  :site => base_url,
35
36
  :request_token_path => request_token_path,
36
37
  :authorize_path => authorize_path,
37
38
  :access_token_path => access_token_path
38
- })
39
+ })
39
40
  end
40
41
 
41
42
  def request_token_path
data/lib/oauth/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OAuth #:nodoc:
2
- VERSION = '0.3.1.1'
2
+ VERSION = '0.3.1.2'
3
3
  end
data/lib/oauth.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'oauth/oauth'
1
2
  require 'oauth/client/helper'
2
3
  require 'oauth/signature/hmac/sha1'
3
4
  require 'oauth/request_proxy/mock_request'
data/oauth.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{oauth}
5
- s.version = "0.3.1.1"
5
+ s.version = "0.3.1.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Pelle Braendgaard", "Blaine Cook", "Larry Halff", "Jesse Clark", "Jon Crosby", "Seth Fitzsimmons"]
@@ -11,8 +11,8 @@ Gem::Specification.new do |s|
11
11
  s.description = %q{OAuth Core Ruby implementation}
12
12
  s.email = %q{pelleb@gmail.com}
13
13
  s.executables = ["oauth"]
14
- s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "README.rdoc", "specs.txt", "website/index.txt"]
15
- s.files = ["History.txt", "License.txt", "Manifest.txt", "README.rdoc", "Rakefile", "TODO", "bin/oauth", "lib/oauth.rb", "lib/oauth/cli.rb", "lib/oauth/client.rb", "lib/oauth/client/action_controller_request.rb", "lib/oauth/client/helper.rb", "lib/oauth/client/net_http.rb", "lib/oauth/consumer.rb", "lib/oauth/helper.rb", "lib/oauth/oauth_test_helper.rb", "lib/oauth/request_proxy.rb", "lib/oauth/request_proxy/action_controller_request.rb", "lib/oauth/request_proxy/base.rb", "lib/oauth/request_proxy/jabber_request.rb", "lib/oauth/request_proxy/mock_request.rb", "lib/oauth/request_proxy/net_http.rb", "lib/oauth/request_proxy/rack_request.rb", "lib/oauth/server.rb", "lib/oauth/signature.rb", "lib/oauth/signature/base.rb", "lib/oauth/signature/hmac/base.rb", "lib/oauth/signature/hmac/md5.rb", "lib/oauth/signature/hmac/rmd160.rb", "lib/oauth/signature/hmac/sha1.rb", "lib/oauth/signature/hmac/sha2.rb", "lib/oauth/signature/md5.rb", "lib/oauth/signature/plaintext.rb", "lib/oauth/signature/rsa/sha1.rb", "lib/oauth/signature/sha1.rb", "lib/oauth/token.rb", "lib/oauth/version.rb", "oauth.gemspec", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "specs.txt", "tasks/deployment.rake", "tasks/environment.rake", "tasks/website.rake", "test/cases/oauth_case.rb", "test/cases/spec/1_0-final/test_construct_request_url.rb", "test/cases/spec/1_0-final/test_normalize_request_parameters.rb", "test/cases/spec/1_0-final/test_parameter_encodings.rb", "test/cases/spec/1_0-final/test_signature_base_strings.rb", "test/keys/rsa.cert", "test/keys/rsa.pem", "test/test_action_controller_request_proxy.rb", "test/test_consumer.rb", "test/test_helper.rb", "test/test_hmac_sha1.rb", "test/test_net_http_client.rb", "test/test_net_http_request_proxy.rb", "test/test_rack_request_proxy.rb", "test/test_rsa_sha1.rb", "test/test_server.rb", "test/test_signature.rb", "test/test_signature_base.rb", "test/test_signature_plain_text.rb", "test/test_token.rb", "website/index.html", "website/index.txt", "website/javascripts/rounded_corners_lite.inc.js", "website/stylesheets/screen.css", "website/template.rhtml"]
14
+ s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "README.rdoc", "website/index.txt"]
15
+ s.files = ["History.txt", "License.txt", "Manifest.txt", "README.rdoc", "Rakefile", "TODO", "bin/oauth", "lib/oauth.rb", "lib/oauth/cli.rb", "lib/oauth/client.rb", "lib/oauth/client/action_controller_request.rb", "lib/oauth/client/helper.rb", "lib/oauth/client/net_http.rb", "lib/oauth/consumer.rb", "lib/oauth/helper.rb", "lib/oauth/oauth_test_helper.rb", "lib/oauth/request_proxy.rb", "lib/oauth/request_proxy/action_controller_request.rb", "lib/oauth/request_proxy/base.rb", "lib/oauth/request_proxy/jabber_request.rb", "lib/oauth/request_proxy/mock_request.rb", "lib/oauth/request_proxy/net_http.rb", "lib/oauth/request_proxy/rack_request.rb", "lib/oauth/server.rb", "lib/oauth/signature.rb", "lib/oauth/signature/base.rb", "lib/oauth/signature/hmac/base.rb", "lib/oauth/signature/hmac/md5.rb", "lib/oauth/signature/hmac/rmd160.rb", "lib/oauth/signature/hmac/sha1.rb", "lib/oauth/signature/hmac/sha2.rb", "lib/oauth/signature/md5.rb", "lib/oauth/signature/plaintext.rb", "lib/oauth/signature/rsa/sha1.rb", "lib/oauth/signature/sha1.rb", "lib/oauth/token.rb", "lib/oauth/version.rb", "oauth.gemspec", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "tasks/deployment.rake", "tasks/environment.rake", "tasks/website.rake", "test/cases/oauth_case.rb", "test/cases/spec/1_0-final/test_construct_request_url.rb", "test/cases/spec/1_0-final/test_normalize_request_parameters.rb", "test/cases/spec/1_0-final/test_parameter_encodings.rb", "test/cases/spec/1_0-final/test_signature_base_strings.rb", "test/keys/rsa.cert", "test/keys/rsa.pem", "test/test_action_controller_request_proxy.rb", "test/test_consumer.rb", "test/test_helper.rb", "test/test_hmac_sha1.rb", "test/test_net_http_client.rb", "test/test_net_http_request_proxy.rb", "test/test_rack_request_proxy.rb", "test/test_rsa_sha1.rb", "test/test_server.rb", "test/test_signature.rb", "test/test_signature_base.rb", "test/test_signature_plain_text.rb", "test/test_token.rb", "website/index.html", "website/index.txt", "website/javascripts/rounded_corners_lite.inc.js", "website/stylesheets/screen.css", "website/template.rhtml"]
16
16
  s.has_rdoc = true
17
17
  s.homepage = %q{http://oauth.rubyforge.org}
18
18
  s.rdoc_options = ["--main", "README.rdoc"]
@@ -1,5 +1,4 @@
1
- require 'rubygems'
2
- require 'test/unit'
1
+ require File.dirname(__FILE__) + '/test_helper'
3
2
  require 'oauth/consumer'
4
3
  require 'oauth/signature/rsa/sha1'
5
4
 
data/test/test_server.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'test/unit'
1
+ require File.dirname(__FILE__) + '/test_helper'
2
2
  require 'oauth/server'
3
3
  class ServerTest < Test::Unit::TestCase
4
4
  def setup
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mojodna-oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1.1
4
+ version: 0.3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pelle Braendgaard
@@ -55,7 +55,6 @@ extra_rdoc_files:
55
55
  - License.txt
56
56
  - Manifest.txt
57
57
  - README.rdoc
58
- - specs.txt
59
58
  - website/index.txt
60
59
  files:
61
60
  - History.txt
@@ -100,7 +99,6 @@ files:
100
99
  - script/generate
101
100
  - script/txt2html
102
101
  - setup.rb
103
- - specs.txt
104
102
  - tasks/deployment.rake
105
103
  - tasks/environment.rake
106
104
  - tasks/website.rake