mojodna-oauth 0.3.1.1

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 +59 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +71 -0
  4. data/README.rdoc +71 -0
  5. data/Rakefile +36 -0
  6. data/TODO +30 -0
  7. data/bin/oauth +5 -0
  8. data/lib/oauth/cli.rb +144 -0
  9. data/lib/oauth/client/action_controller_request.rb +53 -0
  10. data/lib/oauth/client/helper.rb +75 -0
  11. data/lib/oauth/client/net_http.rb +80 -0
  12. data/lib/oauth/client.rb +4 -0
  13. data/lib/oauth/consumer.rb +249 -0
  14. data/lib/oauth/helper.rb +17 -0
  15. data/lib/oauth/oauth_test_helper.rb +25 -0
  16. data/lib/oauth/request_proxy/action_controller_request.rb +62 -0
  17. data/lib/oauth/request_proxy/base.rb +106 -0
  18. data/lib/oauth/request_proxy/jabber_request.rb +41 -0
  19. data/lib/oauth/request_proxy/mock_request.rb +44 -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 +65 -0
  24. data/lib/oauth/signature/base.rb +91 -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 +45 -0
  33. data/lib/oauth/signature/sha1.rb +13 -0
  34. data/lib/oauth/signature.rb +28 -0
  35. data/lib/oauth/token.rb +135 -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 +10 -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 +176 -0
data/History.txt ADDED
@@ -0,0 +1,59 @@
1
+ == 0.3.2
2
+
3
+ * Fixed signature calculation when both options and a block were provided to
4
+ OAuth::Signature::Base#initialize. (Seth)
5
+ * Added help to the 'oauth' CLI. (Seth)
6
+ * Fixed a problem when attempting to normalize MockRequest URIs. (Seth)
7
+
8
+ == 0.3.1 2009-1-26
9
+
10
+ * Fixed a problem with relative and absolute token request paths. (Michael
11
+ Wood)
12
+
13
+ == 0.3.0 2009-1-25
14
+
15
+ * Support ActionController::Request from Edge Rails (László Bácsi)
16
+ * Correctly handle multi-valued parameters (Seth)
17
+ * Added #normalized_parameters to OAuth::RequestProxy::Base (Pelle)
18
+ * OAuth::Signature.sign and friends now yield the RequestProxy instead of the
19
+ token when the passed block's arity is 1. (Seth)
20
+ * Token requests are made to the configured URL rather than generating a
21
+ potentially incorrect one. (Kellan Elliott-McCrea)
22
+ * Command-line app for generating signatures. (Seth)
23
+ * Improved test-cases and compatibility for encoding issues. (Pelle)
24
+
25
+ == 0.2.7 2008-9-10 The lets fix the last release release
26
+
27
+ * Fixed plain text signatures (Andrew Arrow)
28
+ * Fixed RSA requests using OAuthTokens. (Philip Lipu Tsai)
29
+
30
+ == 0.2.6 2008-9-9 The lets RSA release
31
+
32
+ * Improved support for Ruby 1.8.7 (Bill Kocik)
33
+ * Fixed RSA verification to support RSA providers
34
+ now using Ruby and RSA
35
+ * Improved RSA testing
36
+ * Omit token when signing with RSA
37
+ * Added support for 'private_key_file' option for RSA signatures (Chris Mear)
38
+ * Fixed several edge cases where params were being incorrectly signed (Scott
39
+ Hill)
40
+ * Fixed RSA signing (choonkeat)
41
+
42
+ == 0.2.2 2008-2-22 Lets actually support SSL release
43
+
44
+ * Use HTTPS when required.
45
+
46
+ == 0.2 2008-1-19 All together now release
47
+
48
+ 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.
49
+
50
+ == 0.1.2 2007-12-1
51
+
52
+ * Fixed checks for missing OAuth params to improve performance
53
+ * Includes Pat's fix for getting the realm out.
54
+
55
+ == 0.1.1 2007-11-26
56
+
57
+ * First release as a GEM
58
+ * Moved all non-Rails functionality from the Rails plugin:
59
+ 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,71 @@
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", :site => "https://agree2")
28
+
29
+ Start the process by requesting a token
30
+
31
+ @request_token = @consumer.get_request_token
32
+ session[:request_token] = @request_token
33
+ redirect_to @request_token.authorize_url
34
+
35
+ When user returns create an access_token
36
+
37
+ @access_token = @request_token.get_access_token
38
+ @photos = @access_token.get('/photos.xml')
39
+
40
+ 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 .
41
+
42
+ Finally be sure to check out the OAuth RDoc Manual http://oauth.rubyforge.org/rdoc/ .
43
+
44
+ == Documentation Wiki
45
+
46
+ There is some documentation on the Google Code project for the "OAuth Rails Plugin":http://code.google.com/p/oauth-plugin/ :
47
+
48
+ * RequestToken http://code.google.com/p/oauth-plugin/wiki/RequestToken
49
+ * AccessToken http://code.google.com/p/oauth-plugin/wiki/AccessToken
50
+
51
+ == Forum
52
+
53
+ http://groups.google.com/group/oauth-ruby
54
+
55
+
56
+ == How to submit patches
57
+
58
+ Read the "8 steps for fixing other people's code" http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/.
59
+
60
+ The source code is now hosted on the OAuth GitHub Project http://github.com/pelle/oauth/tree/master
61
+
62
+ 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.
63
+
64
+ == License
65
+
66
+ This code is free to use under the terms of the MIT license.
67
+
68
+ == Contact
69
+
70
+ 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
71
+
data/Rakefile ADDED
@@ -0,0 +1,36 @@
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
+ ['actionpack'],
23
+ ['rack']
24
+ ]
25
+
26
+ p.clean_globs |= %w[**/.DS_Store tmp *.log **/.*.sw? *.gem .config **/.DS_Store]
27
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
28
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
29
+ p.rsync_args = '-av --delete --ignore-errors'
30
+ end
31
+
32
+ require 'newgem/tasks' # load /tasks/*.rake
33
+ Dir['tasks/**/*.rake'].each { |t| load t }
34
+
35
+ # TODO - want other tests/tasks run by default? Add them to the list
36
+ # task :default => [:spec, :features]
data/TODO ADDED
@@ -0,0 +1,30 @@
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)
15
+
16
+ Example applications for:
17
+ * Ning
18
+ * Fire Eagle
19
+ * Google (blogger, contacts)
20
+ * Twitter
21
+ * YOS / YQL
22
+ * Netflix
23
+
24
+ In addition to providing best practices of use, these can also be part of
25
+ the pre-release checks to make sure that there have been no regressions.
26
+
27
+ Random TODOs:
28
+ * finish CLI
29
+ * sensible Exception hierarchy
30
+
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,144 @@
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 initialize
17
+ @options = {}
18
+ end
19
+
20
+ def execute(stdout, arguments = [])
21
+ @stdout = stdout
22
+ extract_command_and_parse_options(arguments)
23
+
24
+ if sufficient_options? && valid_command?
25
+ case command
26
+ when "sign"
27
+ request = OAuth::RequestProxy.proxy \
28
+ "method" => options[:method],
29
+ "uri" => options[:uri],
30
+ "parameters" => prepare_parameters
31
+
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}"
44
+ end
45
+ end
46
+
47
+ if verbose?
48
+ stdout.puts "Signature: #{signature}"
49
+ stdout.puts "Escaped signature: #{OAuth::Helper.escape(signature)}"
50
+ else
51
+ stdout.puts signature
52
+ end
53
+ end
54
+ else
55
+ usage
56
+ end
57
+ end
58
+
59
+ protected
60
+
61
+ def extract_command_and_parse_options(arguments)
62
+ @command = arguments[-1]
63
+ parse_options(arguments[0..-1])
64
+ end
65
+
66
+ def option_parser
67
+ option_parser = OptionParser.new do |opts|
68
+ opts.banner = "Usage: #{$0} [options] <command>"
69
+
70
+ # defaults
71
+ options[:oauth_signature_method] = "HMAC-SHA1"
72
+
73
+ opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
74
+ options[:oauth_consumer_key] = v
75
+ end
76
+
77
+ opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
78
+ options[:oauth_consumer_secret] = v
79
+ end
80
+
81
+ opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
82
+ options[:method] = v
83
+ end
84
+
85
+ opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
86
+ options[:params] = v
87
+ end
88
+
89
+ opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
90
+ options[:oauth_signature_method] = v
91
+ end
92
+
93
+ opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
94
+ options[:oauth_token_secret] = v
95
+ end
96
+
97
+ opts.on("--token TOKEN", "Specifies the token to use.") do |v|
98
+ options[:oauth_token] = v
99
+ end
100
+
101
+ opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
102
+ options[:uri] = v
103
+ end
104
+
105
+ opts.on("-v", "--verbose", "Be verbose.") do
106
+ options[:verbose] = true
107
+ end
108
+ end
109
+ end
110
+
111
+ def parse_options(arguments)
112
+ option_parser.parse!(arguments)
113
+ end
114
+
115
+ def prepare_parameters
116
+ {
117
+ "oauth_consumer_key" => options[:oauth_consumer_key],
118
+ "oauth_token" => options[:oauth_token],
119
+ "oauth_signature_method" => options[:oauth_signature_method]
120
+ }.merge(CGI.parse(options[:params]))
121
+ end
122
+
123
+ def sufficient_options?
124
+ options[:oauth_consumer_key] && options[:oauth_consumer_secret] && options[:method] && options[:uri]
125
+ end
126
+
127
+ def usage
128
+ stdout.puts option_parser.help
129
+ stdout.puts
130
+ stdout.puts "Available commands:"
131
+ SUPPORTED_COMMANDS.each do |command|
132
+ puts " #{command.ljust(15)}"
133
+ end
134
+ end
135
+
136
+ def valid_command?
137
+ SUPPORTED_COMMANDS.include?(command)
138
+ end
139
+
140
+ def verbose?
141
+ options[:verbose]
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,53 @@
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
+
36
+ @oauth_helper = OAuth::Client::Helper.new(self, @oauth_options.merge(:request_uri => request_uri))
37
+
38
+ self.send("set_oauth_#{@oauth_options[:scheme]}")
39
+ end
40
+
41
+ def set_oauth_header
42
+ env['Authorization'] = @oauth_helper.header
43
+ end
44
+
45
+ def set_oauth_parameters
46
+ @query_parameters = @oauth_helper.parameters_with_oauth
47
+ @query_parameters.merge!(:oauth_signature => @oauth_helper.signature)
48
+ end
49
+
50
+ def set_oauth_query_string
51
+ end
52
+ end
53
+ 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,80 @@
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
+
37
+ private
38
+
39
+ def oauth_full_request_uri(http)
40
+ uri = URI.parse(self.path)
41
+ uri.host = http.address
42
+ uri.port = http.port
43
+
44
+ if http.respond_to?(:use_ssl?) && http.use_ssl?
45
+ uri.scheme = "https"
46
+ else
47
+ uri.scheme = "http"
48
+ end
49
+
50
+ uri.to_s
51
+ end
52
+
53
+ def set_oauth_header
54
+ self['Authorization'] = @oauth_helper.header
55
+ end
56
+
57
+ # FIXME: if you're using a POST body and query string parameters, using this
58
+ # method will convert those parameters on the query string into parameters in
59
+ # the body. this is broken, and should be fixed.
60
+ def set_oauth_body
61
+ self.set_form_data(@oauth_helper.parameters_with_oauth)
62
+ params_with_sig = @oauth_helper.parameters.merge(:oauth_signature => @oauth_helper.signature)
63
+ self.set_form_data(params_with_sig)
64
+ end
65
+
66
+ def set_oauth_query_string
67
+ oauth_params_str = @oauth_helper.oauth_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
68
+
69
+ uri = URI.parse(path)
70
+ if !uri.query || uri.query == ''
71
+ uri.query = oauth_params_str
72
+ else
73
+ uri.query = uri.query + "&" + oauth_params_str
74
+ end
75
+
76
+ @path = uri.to_s
77
+
78
+ @path << "&oauth_signature=#{escape(@oauth_helper.signature)}"
79
+ end
80
+ end
@@ -0,0 +1,4 @@
1
+ module OAuth
2
+ module Client
3
+ end
4
+ end