proletarian-oauth 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/History.txt +48 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +71 -0
  4. data/README.rdoc +73 -0
  5. data/Rakefile +34 -0
  6. data/TODO +14 -0
  7. data/bin/oauth +5 -0
  8. data/lib/oauth/cli.rb +130 -0
  9. data/lib/oauth/client/action_controller_request.rb +52 -0
  10. data/lib/oauth/client/helper.rb +75 -0
  11. data/lib/oauth/client/net_http.rb +75 -0
  12. data/lib/oauth/client.rb +4 -0
  13. data/lib/oauth/consumer.rb +246 -0
  14. data/lib/oauth/helper.rb +17 -0
  15. data/lib/oauth/oauth_test_helper.rb +26 -0
  16. data/lib/oauth/request_proxy/action_controller_request.rb +62 -0
  17. data/lib/oauth/request_proxy/base.rb +107 -0
  18. data/lib/oauth/request_proxy/jabber_request.rb +42 -0
  19. data/lib/oauth/request_proxy/mock_request.rb +36 -0
  20. data/lib/oauth/request_proxy/net_http.rb +65 -0
  21. data/lib/oauth/request_proxy/rack_request.rb +40 -0
  22. data/lib/oauth/request_proxy.rb +24 -0
  23. data/lib/oauth/server.rb +68 -0
  24. data/lib/oauth/signature/base.rb +89 -0
  25. data/lib/oauth/signature/hmac/base.rb +12 -0
  26. data/lib/oauth/signature/hmac/md5.rb +9 -0
  27. data/lib/oauth/signature/hmac/rmd160.rb +9 -0
  28. data/lib/oauth/signature/hmac/sha1.rb +10 -0
  29. data/lib/oauth/signature/hmac/sha2.rb +9 -0
  30. data/lib/oauth/signature/md5.rb +13 -0
  31. data/lib/oauth/signature/plaintext.rb +23 -0
  32. data/lib/oauth/signature/rsa/sha1.rb +44 -0
  33. data/lib/oauth/signature/sha1.rb +13 -0
  34. data/lib/oauth/signature.rb +28 -0
  35. data/lib/oauth/token.rb +137 -0
  36. data/lib/oauth/version.rb +3 -0
  37. data/lib/oauth.rb +3 -0
  38. data/oauth.gemspec +43 -0
  39. data/script/destroy +14 -0
  40. data/script/generate +14 -0
  41. data/script/txt2html +74 -0
  42. data/setup.rb +1585 -0
  43. data/tasks/deployment.rake +34 -0
  44. data/tasks/environment.rake +7 -0
  45. data/tasks/website.rake +17 -0
  46. data/test/cases/oauth_case.rb +19 -0
  47. data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
  48. data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
  49. data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
  50. data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
  51. data/test/keys/rsa.cert +11 -0
  52. data/test/keys/rsa.pem +16 -0
  53. data/test/test_action_controller_request_proxy.rb +28 -0
  54. data/test/test_consumer.rb +328 -0
  55. data/test/test_helper.rb +15 -0
  56. data/test/test_hmac_sha1.rb +21 -0
  57. data/test/test_net_http_client.rb +169 -0
  58. data/test/test_net_http_request_proxy.rb +38 -0
  59. data/test/test_rack_request_proxy.rb +40 -0
  60. data/test/test_rsa_sha1.rb +59 -0
  61. data/test/test_server.rb +40 -0
  62. data/test/test_signature.rb +11 -0
  63. data/test/test_signature_base.rb +32 -0
  64. data/test/test_signature_plain_text.rb +31 -0
  65. data/test/test_token.rb +14 -0
  66. data/website/index.html +87 -0
  67. data/website/index.txt +73 -0
  68. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  69. data/website/stylesheets/screen.css +138 -0
  70. data/website/template.rhtml +48 -0
  71. metadata +177 -0
data/History.txt ADDED
@@ -0,0 +1,48 @@
1
+ == 0.3.1
2
+
3
+ * Michael Wood identified a problem with relative and absolute token request paths. This should now be fixed and is tested for both cases.
4
+
5
+ == 0.3.0
6
+
7
+ * Support ActionController::Request from Edge Rails (László Bácsi)
8
+ * Correctly handle multi-valued parameters (Seth)
9
+ * Added #normalized_parameters to OAuth::RequestProxy::Base (Pelle)
10
+ * OAuth::Signature.sign and friends now yield the RequestProxy instead of the
11
+ token when the passed block's arity is 1. (Seth)
12
+ * Token requests are made to the configured URL rather than generating a
13
+ potentially incorrect one. (Kellan Elliott-McCrea)
14
+ * Command-line app for generating signatures. (Seth)
15
+ * Improved test-cases and compatibility for encoding issues. (Pelle)
16
+
17
+ == 0.2.7 2008-9-10 The lets fix the last release release
18
+ Fix in plain text signatures to bug found by Andrew Arrow. Who contributed new new unit tests for plain text sigs.
19
+
20
+ There was an error in the RSA requests using oauth tokens. Thanks to Philip Lipu Tsai for noticing this.
21
+
22
+ == 0.2.6 2008-9-9 The lets RSA release
23
+
24
+ - Bill Kocik's fix for Ruby 1.8.7
25
+ - Fixed rsa verification, so you can actually create an OAuth server yourself now using Ruby and RSA
26
+ - Added better testing for RSA
27
+ - Fixed issue where token was being included for rsa signatures
28
+ - Chris Mear added support for a private_key_file option for rsa signatures
29
+ - Scott Hill fixed several edge cases where parameters were incorrectly being signed
30
+ - Patch from choonkeat fixing a problem with rsa signing.
31
+
32
+ == 0.2.2 2008-2-22 Lets actually support SSL release
33
+
34
+ It didn't actually use https when required.
35
+
36
+ == 0.2 2008-1-19 All together now release
37
+
38
+ This is a big release, where we have merged the efforts of various parties into one common library. This means there are definitely some API changes you should be aware of. They should be minimal but please have a look at the unit tests.
39
+
40
+ == 0.1.2 2007-12-1
41
+
42
+ * 1 Fixed a problem where incoming request didn't check whether oauth parameters where missing. While not giving unauthorized access it did cause extra processing where not necessary.
43
+ * 2 Includes Pat's fix for getting the realm out.
44
+
45
+ == 0.1.1 2007-11-26
46
+
47
+ * 1 First release as a GEM
48
+ * Moved all non rails functions into this GEM from the Rails plugin http://code.google.com/p/oauth-plugin/
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Blaine Cook, Larry Halff, Pelle Braendgaard
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,71 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ TODO
7
+ bin/oauth
8
+ lib/oauth.rb
9
+ lib/oauth/cli.rb
10
+ lib/oauth/client.rb
11
+ lib/oauth/client/action_controller_request.rb
12
+ lib/oauth/client/helper.rb
13
+ lib/oauth/client/net_http.rb
14
+ lib/oauth/consumer.rb
15
+ lib/oauth/helper.rb
16
+ lib/oauth/oauth_test_helper.rb
17
+ lib/oauth/request_proxy.rb
18
+ lib/oauth/request_proxy/action_controller_request.rb
19
+ lib/oauth/request_proxy/base.rb
20
+ lib/oauth/request_proxy/jabber_request.rb
21
+ lib/oauth/request_proxy/mock_request.rb
22
+ lib/oauth/request_proxy/net_http.rb
23
+ lib/oauth/request_proxy/rack_request.rb
24
+ lib/oauth/server.rb
25
+ lib/oauth/signature.rb
26
+ lib/oauth/signature/base.rb
27
+ lib/oauth/signature/hmac/base.rb
28
+ lib/oauth/signature/hmac/md5.rb
29
+ lib/oauth/signature/hmac/rmd160.rb
30
+ lib/oauth/signature/hmac/sha1.rb
31
+ lib/oauth/signature/hmac/sha2.rb
32
+ lib/oauth/signature/md5.rb
33
+ lib/oauth/signature/plaintext.rb
34
+ lib/oauth/signature/rsa/sha1.rb
35
+ lib/oauth/signature/sha1.rb
36
+ lib/oauth/token.rb
37
+ lib/oauth/version.rb
38
+ oauth.gemspec
39
+ script/destroy
40
+ script/generate
41
+ script/txt2html
42
+ setup.rb
43
+ specs.txt
44
+ tasks/deployment.rake
45
+ tasks/environment.rake
46
+ tasks/website.rake
47
+ test/cases/oauth_case.rb
48
+ test/cases/spec/1_0-final/test_construct_request_url.rb
49
+ test/cases/spec/1_0-final/test_normalize_request_parameters.rb
50
+ test/cases/spec/1_0-final/test_parameter_encodings.rb
51
+ test/cases/spec/1_0-final/test_signature_base_strings.rb
52
+ test/keys/rsa.cert
53
+ test/keys/rsa.pem
54
+ test/test_action_controller_request_proxy.rb
55
+ test/test_consumer.rb
56
+ test/test_helper.rb
57
+ test/test_hmac_sha1.rb
58
+ test/test_net_http_client.rb
59
+ test/test_net_http_request_proxy.rb
60
+ test/test_rack_request_proxy.rb
61
+ test/test_rsa_sha1.rb
62
+ test/test_server.rb
63
+ test/test_signature.rb
64
+ test/test_signature_base.rb
65
+ test/test_signature_plain_text.rb
66
+ test/test_token.rb
67
+ website/index.html
68
+ website/index.txt
69
+ website/javascripts/rounded_corners_lite.inc.js
70
+ website/stylesheets/screen.css
71
+ website/template.rhtml
data/README.rdoc ADDED
@@ -0,0 +1,73 @@
1
+ = Ruby OAuth GEM
2
+
3
+ == What
4
+
5
+ This is a RubyGem for implementing both OAuth clients and servers in Ruby applications.
6
+
7
+ See the OAuth specs http://oauth.net/core/1.0/
8
+
9
+ == Installing
10
+
11
+ sudo gem install oauth
12
+
13
+ You can also install it from the oauth rubyforge project http://rubyforge.org/projects/oauth/.
14
+
15
+ The source code is now hosted on the OAuth GitHub Project http://github.com/pelle/oauth/tree/master
16
+
17
+ == The basics
18
+
19
+ This is a ruby library which is intended to be used in creating Ruby Consumer and Service Provider applications. It is NOT a Rails plugin, but could easily be used for the foundation for such a Rails plugin.
20
+
21
+ As a matter of fact it has been pulled out from an OAuth Rails Plugin http://code.google.com/p/oauth-plugin/ which now requires this GEM.
22
+
23
+ == Demonstration of usage
24
+
25
+ Create a new consumer instance by passing it a configuration hash:
26
+
27
+ @consumer=OAuth::Consumer.new( "key","secret", {
28
+ :site=>"https://agree2"
29
+ })
30
+
31
+ Start the process by requesting a token
32
+
33
+ @request_token=@consumer.get_request_token
34
+ session[:request_token]=@request_token
35
+ redirect_to @request_token.authorize_url
36
+
37
+ When user returns create an access_token
38
+
39
+ @access_token=@request_token.get_access_token
40
+ @photos=@access_token.get('/photos.xml')
41
+
42
+ For more detailed instructions I have written this OAuth Client Tutorial http://stakeventures.com/articles/2008/02/23/developing-oauth-clients-in-ruby and "How to turn your rails site into an OAuth Provider ":http://stakeventures.com/articles/2007/11/26/how-to-turn-your-rails-site-into-an-oauth-provider .
43
+
44
+ Finally be sure to check out the OAuth RDoc Manual http://oauth.rubyforge.org/rdoc/ .
45
+
46
+ == Documentation Wiki
47
+
48
+ There is some documentation on the Google Code project for the "OAuth Rails Plugin":http://code.google.com/p/oauth-plugin/ :
49
+
50
+ * RequestToken http://code.google.com/p/oauth-plugin/wiki/RequestToken
51
+ * AccessToken http://code.google.com/p/oauth-plugin/wiki/AccessToken
52
+
53
+ == Forum
54
+
55
+ http://groups.google.com/group/oauth-ruby
56
+
57
+
58
+ == How to submit patches
59
+
60
+ Read the "8 steps for fixing other people's code" http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/.
61
+
62
+ The source code is now hosted on the OAuth GitHub Project http://github.com/pelle/oauth/tree/master
63
+
64
+ To submit a patch, please fork the oauth project and create a patch with tests. Once you're happy with it send a pull request and post a message to the google group.
65
+
66
+ == License
67
+
68
+ This code is free to use under the terms of the MIT license.
69
+
70
+ == Contact
71
+
72
+ Comments are welcome. Send an email to "Pelle Braendgaard" pelleb@gmail.com email via the OAuth Ruby mailing list http://groups.google.com/group/oauth-ruby
73
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ $LOAD_PATH << File.dirname(__FILE__) + '/lib'
3
+ require 'oauth'
4
+ require 'oauth/version'
5
+
6
+ # Generate all the Rake tasks
7
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
8
+ $hoe = Hoe.new('oauth', OAuth::VERSION) do |p|
9
+ p.author = ['Pelle Braendgaard','Blaine Cook','Larry Halff','Jesse Clark','Jon Crosby', 'Seth Fitzsimmons']
10
+ p.email = "pelleb@gmail.com"
11
+ p.description = "OAuth Core Ruby implementation"
12
+ p.summary = p.description
13
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
14
+ p.rubyforge_name = p.name # TODO this is default value
15
+ p.url = "http://oauth.rubyforge.org"
16
+
17
+ p.extra_deps = [
18
+ ['ruby-hmac','>= 0.3.1']
19
+ ]
20
+ p.extra_dev_deps = [
21
+ ['newgem', ">= #{::Newgem::VERSION}"]
22
+ ]
23
+
24
+ p.clean_globs |= %w[**/.DS_Store tmp *.log **/.*.sw? *.gem .config **/.DS_Store]
25
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
26
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
27
+ p.rsync_args = '-av --delete --ignore-errors'
28
+ end
29
+
30
+ require 'newgem/tasks' # load /tasks/*.rake
31
+ Dir['tasks/**/*.rake'].each { |t| load t }
32
+
33
+ # TODO - want other tests/tasks run by default? Add them to the list
34
+ # task :default => [:spec, :features]
data/TODO ADDED
@@ -0,0 +1,14 @@
1
+ Common use-cases should be streamlined:
2
+
3
+ * I have a URL that I want to sign (given consumer key/secret, optional
4
+ token/secret, optional nonce/timestamp).
5
+ * I have a URL that I want to sign AND I want to see what the components
6
+ (e.g. signature base string, etc.) are while it's being signed (i.e. verbose
7
+ signing).
8
+ * I have a URL that I want to sign and I only want the signature.
9
+ * I have a URL that I want to sign and I want something suitable to put in
10
+ {the header, the querystring, XMPP}.
11
+ * I want to make a query to an OAuth-enabled web service (with sensible
12
+ errors, if available).
13
+ * I want to host an OAuth-enabled web service.
14
+ * I want to test my OAuth-enabled web service (i.e. test helpers)
data/bin/oauth ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "oauth/cli"
4
+
5
+ OAuth::CLI.execute(STDOUT, ARGV)
data/lib/oauth/cli.rb ADDED
@@ -0,0 +1,130 @@
1
+ require 'optparse'
2
+ require 'oauth'
3
+
4
+ module OAuth
5
+ class CLI
6
+ SUPPORTED_COMMANDS = %w(sign)
7
+
8
+ attr_reader :command
9
+ attr_reader :options
10
+ attr_reader :stdout
11
+
12
+ def self.execute(stdout, arguments = [])
13
+ self.new.execute(stdout, arguments)
14
+ end
15
+
16
+ def execute(stdout, arguments = [])
17
+ @stdout = stdout
18
+ extract_command_and_parse_options(arguments)
19
+
20
+ if sufficient_options? && valid_command?
21
+ case command
22
+ when "sign"
23
+ request = OAuth::RequestProxy.proxy \
24
+ "method" => options[:method],
25
+ "uri" => options[:uri],
26
+ "parameters" => prepare_parameters
27
+
28
+ # can't pass options unless they respond to :secret, so use this alternative
29
+ signature = OAuth::Signature.sign \
30
+ request,
31
+ :consumer_secret => options[:oauth_consumer_secret],
32
+ :token_secret => options[:oauth_token_secret] do |request|
33
+
34
+ # while we have access to the request being signed, display some internals
35
+ if verbose?
36
+ stdout.puts "Method: #{request.method}"
37
+ stdout.puts "URI: #{request.uri}"
38
+ stdout.puts "Normalized params: #{request.normalized_parameters}"
39
+ stdout.puts "Signature base string: #{request.signature_base_string}"
40
+ end
41
+ end
42
+
43
+ if verbose?
44
+ stdout.puts "Signature: #{signature}"
45
+ stdout.puts "Escaped signature: #{OAuth::Helper.escape(signature)}"
46
+ else
47
+ stdout.puts signature
48
+ end
49
+ end
50
+ else
51
+ usage
52
+ end
53
+ end
54
+
55
+ def extract_command_and_parse_options(arguments)
56
+ @command = arguments[-1]
57
+ parse_options(arguments[0..-1])
58
+ end
59
+
60
+ def parse_options(arguments)
61
+ @options = {}
62
+ OptionParser.new do |opts|
63
+ opts.banner = "Usage: #{$0} [options] <command>"
64
+
65
+ # defaults
66
+ options[:oauth_signature_method] = "HMAC-SHA1"
67
+
68
+ opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
69
+ options[:oauth_consumer_key] = v
70
+ end
71
+
72
+ opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
73
+ options[:oauth_consumer_secret] = v
74
+ end
75
+
76
+ opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
77
+ options[:method] = v
78
+ end
79
+
80
+ opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
81
+ options[:params] = v
82
+ end
83
+
84
+ opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
85
+ options[:oauth_signature_method] = v
86
+ end
87
+
88
+ opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
89
+ options[:oauth_token_secret] = v
90
+ end
91
+
92
+ opts.on("--token TOKEN", "Specifies the token to use.") do |v|
93
+ options[:oauth_token] = v
94
+ end
95
+
96
+ opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
97
+ options[:uri] = v
98
+ end
99
+
100
+ opts.on("-v", "--verbose", "Be verbose.") do
101
+ options[:verbose] = true
102
+ end
103
+ end.parse!(arguments)
104
+ end
105
+
106
+ def prepare_parameters
107
+ {
108
+ "oauth_consumer_key" => options[:oauth_consumer_key],
109
+ "oauth_token" => options[:oauth_token],
110
+ "oauth_signature_method" => options[:oauth_signature_method]
111
+ }.merge(CGI.parse(options[:params]))
112
+ end
113
+
114
+ def sufficient_options?
115
+ options[:oauth_consumer_key] && options[:oauth_consumer_secret] && options[:method] && options[:uri]
116
+ end
117
+
118
+ def usage
119
+ stdout.puts "Should be generated by OptionParser"
120
+ end
121
+
122
+ def valid_command?
123
+ SUPPORTED_COMMANDS.include?(command)
124
+ end
125
+
126
+ def verbose?
127
+ options[:verbose]
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,52 @@
1
+ require 'oauth/client/helper'
2
+ require 'oauth/request_proxy/action_controller_request'
3
+ require 'action_controller/test_process'
4
+
5
+ module ActionController
6
+ class Base
7
+ def process_with_oauth(request,response=nil)
8
+ request.apply_oauth!
9
+ process_without_oauth(request,response)
10
+ end
11
+
12
+ alias_method_chain :process, :oauth
13
+ end
14
+
15
+ class TestRequest
16
+ def self.use_oauth=(bool)
17
+ @use_oauth = bool
18
+ end
19
+
20
+ def self.use_oauth?
21
+ @use_oauth
22
+ end
23
+
24
+ def configure_oauth(consumer = nil, token = nil, options = {})
25
+ @oauth_options = { :consumer => consumer,
26
+ :token => token,
27
+ :scheme => 'header',
28
+ :signature_method => nil,
29
+ :nonce => nil,
30
+ :timestamp => nil }.merge(options)
31
+ end
32
+
33
+ def apply_oauth!
34
+ return unless ActionController::TestRequest.use_oauth? && @oauth_options
35
+ @oauth_helper = OAuth::Client::Helper.new(self, @oauth_options.merge( { :request_uri => request_uri } ))
36
+
37
+ self.send("set_oauth_#{@oauth_options[:scheme]}")
38
+ end
39
+
40
+ def set_oauth_header
41
+ env['Authorization'] = @oauth_helper.header
42
+ end
43
+
44
+ def set_oauth_parameters
45
+ @query_parameters = @oauth_helper.parameters_with_oauth
46
+ @query_parameters.merge!( { :oauth_signature => @oauth_helper.signature } )
47
+ end
48
+
49
+ def set_oauth_query_string
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,75 @@
1
+ require 'oauth/client'
2
+ require 'oauth/consumer'
3
+ require 'oauth/helper'
4
+ require 'oauth/token'
5
+ require 'oauth/signature/hmac/sha1'
6
+
7
+ module OAuth::Client
8
+ class Helper
9
+ include OAuth::Helper
10
+
11
+ def initialize(request, options = {})
12
+ @request = request
13
+ @options = options
14
+ @options[:signature_method] ||= 'HMAC-SHA1'
15
+ end
16
+
17
+ def options
18
+ @options
19
+ end
20
+
21
+ def nonce
22
+ options[:nonce] ||= generate_key
23
+ end
24
+
25
+ def timestamp
26
+ options[:timestamp] ||= generate_timestamp
27
+ end
28
+
29
+ def generate_timestamp
30
+ Time.now.to_i.to_s
31
+ end
32
+
33
+ def oauth_parameters
34
+ {
35
+ 'oauth_consumer_key' => options[:consumer].key,
36
+ 'oauth_token' => options[:token] ? options[:token].token : '',
37
+ 'oauth_signature_method' => options[:signature_method],
38
+ 'oauth_timestamp' => timestamp,
39
+ 'oauth_nonce' => nonce,
40
+ 'oauth_version' => '1.0'
41
+ }.reject { |k,v| v == "" }
42
+ end
43
+
44
+ def signature(extra_options = {})
45
+ OAuth::Signature.sign(@request, { :uri => options[:request_uri],
46
+ :consumer => options[:consumer],
47
+ :token => options[:token] }.merge(extra_options) )
48
+ end
49
+
50
+ def signature_base_string(extra_options = {})
51
+ OAuth::Signature.signature_base_string(@request, { :uri => options[:request_uri],
52
+ :consumer => options[:consumer],
53
+ :token => options[:token],
54
+ :parameters => oauth_parameters}.merge(extra_options) )
55
+ end
56
+
57
+ def header
58
+ parameters = oauth_parameters
59
+ parameters.merge!( { 'oauth_signature' => signature( options.merge({ :parameters => parameters }) ) } )
60
+
61
+ header_params_str = parameters.map { |k,v| "#{k}=\"#{escape(v)}\"" }.join(', ')
62
+
63
+ realm = "realm=\"#{options[:realm]}\", " if options[:realm]
64
+ "OAuth #{realm}#{header_params_str}"
65
+ end
66
+
67
+ def parameters
68
+ OAuth::RequestProxy.proxy(@request).parameters
69
+ end
70
+
71
+ def parameters_with_oauth
72
+ oauth_parameters.merge( parameters )
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,75 @@
1
+ require 'oauth/helper'
2
+ require 'oauth/client/helper'
3
+ require 'oauth/request_proxy/net_http'
4
+
5
+ class Net::HTTPRequest
6
+ include OAuth::Helper
7
+
8
+ def oauth!(http, consumer = nil, token = nil, options = {})
9
+ options = { :request_uri => oauth_full_request_uri(http),
10
+ :consumer => consumer,
11
+ :token => token,
12
+ :scheme => 'header',
13
+ :signature_method => nil,
14
+ :nonce => nil,
15
+ :timestamp => nil }.merge(options)
16
+
17
+ @oauth_helper = OAuth::Client::Helper.new(self, options)
18
+ self.send("set_oauth_#{options[:scheme]}")
19
+ end
20
+
21
+ def signature_base_string(http, consumer = nil, token = nil, options = {})
22
+ options = { :request_uri => oauth_full_request_uri(http),
23
+ :consumer => consumer,
24
+ :token => token,
25
+ :scheme => 'header',
26
+ :signature_method => nil,
27
+ :nonce => nil,
28
+ :timestamp => nil }.merge(options)
29
+
30
+ OAuth::Client::Helper.new(self, options).signature_base_string
31
+ end
32
+
33
+ def oauth_helper
34
+ @oauth_helper
35
+ end
36
+ private
37
+
38
+ def oauth_full_request_uri(http)
39
+ uri = URI.parse(self.path)
40
+ uri.host = http.address
41
+ uri.port = http.port
42
+ if http.respond_to?(:use_ssl?)
43
+ uri.scheme = http.use_ssl? ? 'https' : 'http'
44
+ end
45
+ uri.to_s
46
+ end
47
+
48
+ def set_oauth_header
49
+ self['Authorization'] = @oauth_helper.header
50
+ end
51
+
52
+ # FIXME: if you're using a POST body and query string parameters, using this
53
+ # method will convert those parameters on the query string into parameters in
54
+ # the body. this is broken, and should be fixed.
55
+ def set_oauth_body
56
+ self.set_form_data(@oauth_helper.parameters_with_oauth)
57
+ params_with_sig = @oauth_helper.parameters.merge(:oauth_signature => @oauth_helper.signature)
58
+ self.set_form_data(params_with_sig)
59
+ end
60
+
61
+ def set_oauth_query_string
62
+ oauth_params_str = @oauth_helper.oauth_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
63
+
64
+ uri = URI.parse(path)
65
+ if !uri.query || uri.query == ''
66
+ uri.query = oauth_params_str
67
+ else
68
+ uri.query = uri.query + "&" + oauth_params_str
69
+ end
70
+
71
+ @path = uri.to_s
72
+
73
+ @path << "&oauth_signature=#{escape(@oauth_helper.signature)}"
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+ module OAuth
2
+ module Client
3
+ end
4
+ end