zmalltalker-oauth 0.3.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/History.txt +61 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +79 -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/examples/yql.rb +44 -0
  9. data/lib/oauth.rb +5 -0
  10. data/lib/oauth/cli.rb +231 -0
  11. data/lib/oauth/client.rb +4 -0
  12. data/lib/oauth/client/action_controller_request.rb +53 -0
  13. data/lib/oauth/client/helper.rb +71 -0
  14. data/lib/oauth/client/net_http.rb +80 -0
  15. data/lib/oauth/consumer.rb +253 -0
  16. data/lib/oauth/helper.rb +55 -0
  17. data/lib/oauth/oauth.rb +7 -0
  18. data/lib/oauth/oauth_test_helper.rb +25 -0
  19. data/lib/oauth/request_proxy.rb +24 -0
  20. data/lib/oauth/request_proxy/action_controller_request.rb +63 -0
  21. data/lib/oauth/request_proxy/base.rb +157 -0
  22. data/lib/oauth/request_proxy/jabber_request.rb +41 -0
  23. data/lib/oauth/request_proxy/mock_request.rb +44 -0
  24. data/lib/oauth/request_proxy/net_http.rb +65 -0
  25. data/lib/oauth/request_proxy/rack_request.rb +40 -0
  26. data/lib/oauth/server.rb +66 -0
  27. data/lib/oauth/signature.rb +28 -0
  28. data/lib/oauth/signature/base.rb +91 -0
  29. data/lib/oauth/signature/hmac/base.rb +16 -0
  30. data/lib/oauth/signature/hmac/md5.rb +9 -0
  31. data/lib/oauth/signature/hmac/rmd160.rb +9 -0
  32. data/lib/oauth/signature/hmac/sha1.rb +10 -0
  33. data/lib/oauth/signature/hmac/sha2.rb +9 -0
  34. data/lib/oauth/signature/md5.rb +13 -0
  35. data/lib/oauth/signature/plaintext.rb +23 -0
  36. data/lib/oauth/signature/rsa/sha1.rb +45 -0
  37. data/lib/oauth/signature/sha1.rb +13 -0
  38. data/lib/oauth/token.rb +7 -0
  39. data/lib/oauth/tokens/access_token.rb +68 -0
  40. data/lib/oauth/tokens/consumer_token.rb +32 -0
  41. data/lib/oauth/tokens/request_token.rb +28 -0
  42. data/lib/oauth/tokens/server_token.rb +9 -0
  43. data/lib/oauth/tokens/token.rb +17 -0
  44. data/lib/oauth/version.rb +3 -0
  45. data/oauth.gemspec +49 -0
  46. data/script/destroy +14 -0
  47. data/script/generate +14 -0
  48. data/script/txt2html +74 -0
  49. data/setup.rb +1585 -0
  50. data/tasks/deployment.rake +34 -0
  51. data/tasks/environment.rake +7 -0
  52. data/tasks/website.rake +17 -0
  53. data/test/cases/oauth_case.rb +19 -0
  54. data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
  55. data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
  56. data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
  57. data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
  58. data/test/keys/rsa.cert +11 -0
  59. data/test/keys/rsa.pem +16 -0
  60. data/test/test_access_token.rb +28 -0
  61. data/test/test_action_controller_request_proxy.rb +45 -0
  62. data/test/test_consumer.rb +331 -0
  63. data/test/test_helper.rb +19 -0
  64. data/test/test_hmac_sha1.rb +21 -0
  65. data/test/test_net_http_client.rb +174 -0
  66. data/test/test_net_http_request_proxy.rb +38 -0
  67. data/test/test_rack_request_proxy.rb +40 -0
  68. data/test/test_request_token.rb +53 -0
  69. data/test/test_rsa_sha1.rb +59 -0
  70. data/test/test_server.rb +40 -0
  71. data/test/test_signature.rb +11 -0
  72. data/test/test_signature_base.rb +32 -0
  73. data/test/test_signature_plain_text.rb +31 -0
  74. data/test/test_token.rb +14 -0
  75. data/website/index.html +87 -0
  76. data/website/index.txt +73 -0
  77. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  78. data/website/stylesheets/screen.css +138 -0
  79. data/website/template.rhtml +48 -0
  80. metadata +209 -0
data/History.txt ADDED
@@ -0,0 +1,61 @@
1
+ == 0.3.2
2
+
3
+ * Fixed ActionController parameter escaping behavior (Thiago Arrais, László
4
+ Bácsi, Brett Gibson, et al)
5
+ * Fixed signature calculation when both options and a block were provided to
6
+ OAuth::Signature::Base#initialize. (Seth)
7
+ * Added help to the 'oauth' CLI. (Seth)
8
+ * Fixed a problem when attempting to normalize MockRequest URIs. (Seth)
9
+
10
+ == 0.3.1 2009-1-26
11
+
12
+ * Fixed a problem with relative and absolute token request paths. (Michael
13
+ Wood)
14
+
15
+ == 0.3.0 2009-1-25
16
+
17
+ * Support ActionController::Request from Edge Rails (László Bácsi)
18
+ * Correctly handle multi-valued parameters (Seth)
19
+ * Added #normalized_parameters to OAuth::RequestProxy::Base (Pelle)
20
+ * OAuth::Signature.sign and friends now yield the RequestProxy instead of the
21
+ token when the passed block's arity is 1. (Seth)
22
+ * Token requests are made to the configured URL rather than generating a
23
+ potentially incorrect one. (Kellan Elliott-McCrea)
24
+ * Command-line app for generating signatures. (Seth)
25
+ * Improved test-cases and compatibility for encoding issues. (Pelle)
26
+
27
+ == 0.2.7 2008-9-10 The lets fix the last release release
28
+
29
+ * Fixed plain text signatures (Andrew Arrow)
30
+ * Fixed RSA requests using OAuthTokens. (Philip Lipu Tsai)
31
+
32
+ == 0.2.6 2008-9-9 The lets RSA release
33
+
34
+ * Improved support for Ruby 1.8.7 (Bill Kocik)
35
+ * Fixed RSA verification to support RSA providers
36
+ now using Ruby and RSA
37
+ * Improved RSA testing
38
+ * Omit token when signing with RSA
39
+ * Added support for 'private_key_file' option for RSA signatures (Chris Mear)
40
+ * Fixed several edge cases where params were being incorrectly signed (Scott
41
+ Hill)
42
+ * Fixed RSA signing (choonkeat)
43
+
44
+ == 0.2.2 2008-2-22 Lets actually support SSL release
45
+
46
+ * Use HTTPS when required.
47
+
48
+ == 0.2 2008-1-19 All together now release
49
+
50
+ 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.
51
+
52
+ == 0.1.2 2007-12-1
53
+
54
+ * Fixed checks for missing OAuth params to improve performance
55
+ * Includes Pat's fix for getting the realm out.
56
+
57
+ == 0.1.1 2007-11-26
58
+
59
+ * First release as a GEM
60
+ * Moved all non-Rails functionality from the Rails plugin:
61
+ 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,79 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ TODO
7
+ bin/oauth
8
+ examples/yql.rb
9
+ lib/oauth.rb
10
+ lib/oauth/oauth.rb
11
+ lib/oauth/cli.rb
12
+ lib/oauth/client.rb
13
+ lib/oauth/client/action_controller_request.rb
14
+ lib/oauth/client/helper.rb
15
+ lib/oauth/client/net_http.rb
16
+ lib/oauth/consumer.rb
17
+ lib/oauth/helper.rb
18
+ lib/oauth/oauth_test_helper.rb
19
+ lib/oauth/request_proxy.rb
20
+ lib/oauth/request_proxy/action_controller_request.rb
21
+ lib/oauth/request_proxy/base.rb
22
+ lib/oauth/request_proxy/jabber_request.rb
23
+ lib/oauth/request_proxy/mock_request.rb
24
+ lib/oauth/request_proxy/net_http.rb
25
+ lib/oauth/request_proxy/rack_request.rb
26
+ lib/oauth/server.rb
27
+ lib/oauth/signature.rb
28
+ lib/oauth/signature/base.rb
29
+ lib/oauth/signature/hmac/base.rb
30
+ lib/oauth/signature/hmac/md5.rb
31
+ lib/oauth/signature/hmac/rmd160.rb
32
+ lib/oauth/signature/hmac/sha1.rb
33
+ lib/oauth/signature/hmac/sha2.rb
34
+ lib/oauth/signature/md5.rb
35
+ lib/oauth/signature/plaintext.rb
36
+ lib/oauth/signature/rsa/sha1.rb
37
+ lib/oauth/signature/sha1.rb
38
+ lib/oauth/token.rb
39
+ lib/oauth/tokens/access_token.rb
40
+ lib/oauth/tokens/consumer_token.rb
41
+ lib/oauth/tokens/request_token.rb
42
+ lib/oauth/tokens/server_token.rb
43
+ lib/oauth/tokens/token.rb
44
+ lib/oauth/version.rb
45
+ oauth.gemspec
46
+ script/destroy
47
+ script/generate
48
+ script/txt2html
49
+ setup.rb
50
+ tasks/deployment.rake
51
+ tasks/environment.rake
52
+ tasks/website.rake
53
+ test/cases/oauth_case.rb
54
+ test/cases/spec/1_0-final/test_construct_request_url.rb
55
+ test/cases/spec/1_0-final/test_normalize_request_parameters.rb
56
+ test/cases/spec/1_0-final/test_parameter_encodings.rb
57
+ test/cases/spec/1_0-final/test_signature_base_strings.rb
58
+ test/keys/rsa.cert
59
+ test/keys/rsa.pem
60
+ test/test_access_token.rb
61
+ test/test_action_controller_request_proxy.rb
62
+ test/test_consumer.rb
63
+ test/test_helper.rb
64
+ test/test_hmac_sha1.rb
65
+ test/test_net_http_client.rb
66
+ test/test_net_http_request_proxy.rb
67
+ test/test_rack_request_proxy.rb
68
+ test/test_request_token.rb
69
+ test/test_rsa_sha1.rb
70
+ test/test_server.rb
71
+ test/test_signature.rb
72
+ test/test_signature_base.rb
73
+ test/test_signature_plain_text.rb
74
+ test/test_token.rb
75
+ website/index.html
76
+ website/index.txt
77
+ website/javascripts/rounded_corners_lite.inc.js
78
+ website/stylesheets/screen.css
79
+ 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/examples/yql.rb ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+
3
+ # Sample queries:
4
+ # ./yql.rb --consumer-key <key> --consumer-secret <secret> "show tables"
5
+ # ./yql.rb --consumer-key <key> --consumer-secret <secret> "select * from flickr.photos.search where text='Cat' limit 10"
6
+
7
+ require 'oauth'
8
+ require 'optparse'
9
+ require 'json'
10
+ require 'pp'
11
+
12
+ options = {}
13
+
14
+ option_parser = OptionParser.new do |opts|
15
+ opts.banner = "Usage: #{$0} [options] <query>"
16
+
17
+ opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
18
+ options[:consumer_key] = v
19
+ end
20
+
21
+ opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
22
+ options[:consumer_secret] = v
23
+ end
24
+ end
25
+
26
+ option_parser.parse!
27
+ query = ARGV.pop
28
+ query = STDIN.read if query == "-"
29
+
30
+ if options[:consumer_key].nil? || options[:consumer_secret].nil? || query.nil?
31
+ puts option_parser.help
32
+ exit 1
33
+ end
34
+
35
+ consumer = OAuth::Consumer.new \
36
+ options[:consumer_key],
37
+ options[:consumer_secret],
38
+ :site => "http://query.yahooapis.com"
39
+
40
+ access_token = OAuth::AccessToken.new(consumer)
41
+
42
+ response = access_token.request(:get, url = "/v1/yql?q=#{OAuth::Helper.escape(query)}&format=json")
43
+ rsp = JSON.parse(response.body)
44
+ pp rsp
data/lib/oauth.rb ADDED
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__)))
2
+ require 'oauth/oauth'
3
+ require 'oauth/client/helper'
4
+ require 'oauth/signature/hmac/sha1'
5
+ require 'oauth/request_proxy/mock_request'
data/lib/oauth/cli.rb ADDED
@@ -0,0 +1,231 @@
1
+ require 'optparse'
2
+ require 'oauth'
3
+
4
+ module OAuth
5
+ class CLI
6
+ SUPPORTED_COMMANDS = {
7
+ "debug" => "Verbosely generate an OAuth signature",
8
+ "sign" => "Generate an OAuth signature"
9
+ }
10
+
11
+ attr_reader :command
12
+ attr_reader :options
13
+ attr_reader :stdout
14
+
15
+ def self.execute(stdout, arguments = [])
16
+ self.new.execute(stdout, arguments)
17
+ end
18
+
19
+ def initialize
20
+ @options = {}
21
+ end
22
+
23
+ def execute(stdout, arguments = [])
24
+ @stdout = stdout
25
+ extract_command_and_parse_options(arguments)
26
+
27
+ if sufficient_options? && valid_command?
28
+ if command == "debug"
29
+ @command = "sign"
30
+ @options[:verbose] = true
31
+ end
32
+
33
+ case command
34
+ when "sign"
35
+ parameters = prepare_parameters
36
+
37
+ request = OAuth::RequestProxy.proxy \
38
+ "method" => options[:method],
39
+ "uri" => options[:uri],
40
+ "parameters" => parameters
41
+
42
+ if verbose?
43
+ stdout.puts "OAuth parameters:"
44
+ request.oauth_parameters.each do |k,v|
45
+ stdout.puts " " + [k, v] * ": "
46
+ end
47
+ stdout.puts
48
+
49
+ if request.non_oauth_parameters.any?
50
+ stdout.puts "Parameters:"
51
+ request.non_oauth_parameters.each do |k,v|
52
+ stdout.puts " " + [k, v] * ": "
53
+ end
54
+ stdout.puts
55
+ end
56
+ end
57
+
58
+ request.sign! \
59
+ :consumer_secret => options[:oauth_consumer_secret],
60
+ :token_secret => options[:oauth_token_secret]
61
+
62
+ if verbose?
63
+ stdout.puts "Method: #{request.method}"
64
+ stdout.puts "URI: #{request.uri}"
65
+ stdout.puts "Normalized params: #{request.normalized_parameters}" unless options[:xmpp]
66
+ stdout.puts "Signature base string: #{request.signature_base_string}"
67
+
68
+ if options[:xmpp]
69
+ stdout.puts
70
+ stdout.puts "XMPP Stanza:"
71
+ stdout.puts <<-EOS
72
+ <oauth xmlns='urn:xmpp:tmp:oauth'>
73
+ <oauth_consumer_key>#{request.oauth_consumer_key}</oauth_consumer_key>
74
+ <oauth_token>#{request.oauth_token}</oauth_token>
75
+ <oauth_signature_method>#{request.oauth_signature_method}</oauth_signature_method>
76
+ <oauth_signature>#{request.oauth_signature}</oauth_signature>
77
+ <oauth_timestamp>#{request.oauth_timestamp}</oauth_timestamp>
78
+ <oauth_nonce>#{request.oauth_nonce}</oauth_nonce>
79
+ <oauth_version>#{request.oauth_version}</oauth_version>
80
+ </oauth>
81
+ EOS
82
+ stdout.puts
83
+ stdout.puts "Note: You may want to use bare JIDs in your URI."
84
+ stdout.puts
85
+ else
86
+ stdout.puts "OAuth Request URI: #{request.signed_uri}"
87
+ stdout.puts "Request URI: #{request.signed_uri(false)}"
88
+ stdout.puts "Authorization header: #{request.oauth_header(:realm => options[:realm])}"
89
+ end
90
+ stdout.puts "Signature: #{request.oauth_signature}"
91
+ stdout.puts "Escaped signature: #{OAuth::Helper.escape(request.oauth_signature)}"
92
+ else
93
+ stdout.puts request.oauth_signature
94
+ end
95
+ end
96
+ else
97
+ usage
98
+ end
99
+ end
100
+
101
+ protected
102
+
103
+ def extract_command_and_parse_options(arguments)
104
+ @command = arguments[-1]
105
+ parse_options(arguments[0..-1])
106
+ end
107
+
108
+ def option_parser
109
+ option_parser = OptionParser.new do |opts|
110
+ opts.banner = "Usage: #{$0} [options] <command>"
111
+
112
+ # defaults
113
+ options[:oauth_nonce] = OAuth::Helper.generate_key
114
+ options[:oauth_signature_method] = "HMAC-SHA1"
115
+ options[:oauth_timestamp] = OAuth::Helper.generate_timestamp
116
+ options[:oauth_version] = "1.0"
117
+ options[:params] = []
118
+
119
+ opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
120
+ options[:oauth_consumer_key] = v
121
+ end
122
+
123
+ opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
124
+ options[:oauth_consumer_secret] = v
125
+ end
126
+
127
+ opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
128
+ options[:method] = v
129
+ end
130
+
131
+ opts.on("--nonce NONCE", "Specifies the none to use.") do |v|
132
+ options[:oauth_nonce] = v
133
+ end
134
+
135
+ opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
136
+ options[:params] << v
137
+ end
138
+
139
+ opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
140
+ options[:oauth_signature_method] = v
141
+ end
142
+
143
+ opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
144
+ options[:oauth_token_secret] = v
145
+ end
146
+
147
+ opts.on("--timestamp TIMESTAMP", "Specifies the timestamp to use.") do |v|
148
+ options[:oauth_timestamp] = v
149
+ end
150
+
151
+ opts.on("--token TOKEN", "Specifies the token to use.") do |v|
152
+ options[:oauth_token] = v
153
+ end
154
+
155
+ opts.on("--realm REALM", "Specifies the realm to use.") do |v|
156
+ options[:realm] = v
157
+ end
158
+
159
+ opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
160
+ options[:uri] = v
161
+ end
162
+
163
+ opts.on("--version VERSION", "Specifies the OAuth version to use.") do |v|
164
+ options[:oauth_version] = v
165
+ end
166
+
167
+ opts.on("--no-version", "Omit oauth_version.") do
168
+ options[:oauth_version] = nil
169
+ end
170
+
171
+ opts.on("--xmpp", "Generate XMPP stanzas.") do
172
+ options[:xmpp] = true
173
+ options[:method] ||= "iq"
174
+ end
175
+
176
+ opts.on("-v", "--verbose", "Be verbose.") do
177
+ options[:verbose] = true
178
+ end
179
+ end
180
+ end
181
+
182
+ def parse_options(arguments)
183
+ option_parser.parse!(arguments)
184
+ end
185
+
186
+ def prepare_parameters
187
+ escaped_pairs = options[:params].collect do |pair|
188
+ if pair =~ /:/
189
+ Hash[*pair.split(":", 2)].collect do |k,v|
190
+ [CGI.escape(k.strip), CGI.escape(v.strip)] * "="
191
+ end
192
+ else
193
+ pair
194
+ end
195
+ end
196
+
197
+ querystring = escaped_pairs * "&"
198
+ cli_params = CGI.parse(querystring)
199
+
200
+ {
201
+ "oauth_consumer_key" => options[:oauth_consumer_key],
202
+ "oauth_nonce" => options[:oauth_nonce],
203
+ "oauth_timestamp" => options[:oauth_timestamp],
204
+ "oauth_token" => options[:oauth_token],
205
+ "oauth_signature_method" => options[:oauth_signature_method],
206
+ "oauth_version" => options[:oauth_version]
207
+ }.reject { |k,v| v.nil? || v == "" }.merge(cli_params)
208
+ end
209
+
210
+ def sufficient_options?
211
+ options[:oauth_consumer_key] && options[:oauth_consumer_secret] && options[:method] && options[:uri]
212
+ end
213
+
214
+ def usage
215
+ stdout.puts option_parser.help
216
+ stdout.puts
217
+ stdout.puts "Available commands:"
218
+ SUPPORTED_COMMANDS.each do |command, desc|
219
+ puts " #{command.ljust(15)}#{desc}"
220
+ end
221
+ end
222
+
223
+ def valid_command?
224
+ SUPPORTED_COMMANDS.keys.include?(command)
225
+ end
226
+
227
+ def verbose?
228
+ options[:verbose]
229
+ end
230
+ end
231
+ end