oauth 0.5.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +504 -0
  3. data/CODE_OF_CONDUCT.md +84 -0
  4. data/CONTRIBUTING.md +40 -0
  5. data/LICENSE +19 -17
  6. data/README.md +390 -0
  7. data/SECURITY.md +26 -0
  8. data/lib/oauth/client/action_controller_request.rb +23 -21
  9. data/lib/oauth/client/em_http.rb +99 -99
  10. data/lib/oauth/client/helper.rb +83 -82
  11. data/lib/oauth/client/net_http.rb +112 -105
  12. data/lib/oauth/client.rb +2 -0
  13. data/lib/oauth/consumer.rb +147 -133
  14. data/lib/oauth/errors/error.rb +2 -0
  15. data/lib/oauth/errors/problem.rb +3 -0
  16. data/lib/oauth/errors/unauthorized.rb +7 -1
  17. data/lib/oauth/errors.rb +5 -3
  18. data/lib/oauth/helper.rb +26 -18
  19. data/lib/oauth/oauth.rb +6 -4
  20. data/lib/oauth/oauth_test_helper.rb +6 -4
  21. data/lib/oauth/request_proxy/action_controller_request.rb +49 -71
  22. data/lib/oauth/request_proxy/action_dispatch_request.rb +8 -4
  23. data/lib/oauth/request_proxy/base.rb +136 -132
  24. data/lib/oauth/request_proxy/curb_request.rb +49 -43
  25. data/lib/oauth/request_proxy/em_http_request.rb +59 -49
  26. data/lib/oauth/request_proxy/jabber_request.rb +12 -9
  27. data/lib/oauth/request_proxy/mock_request.rb +5 -3
  28. data/lib/oauth/request_proxy/net_http.rb +61 -54
  29. data/lib/oauth/request_proxy/rack_request.rb +35 -31
  30. data/lib/oauth/request_proxy/rest_client_request.rb +54 -50
  31. data/lib/oauth/request_proxy/typhoeus_request.rb +51 -45
  32. data/lib/oauth/request_proxy.rb +7 -4
  33. data/lib/oauth/server.rb +14 -12
  34. data/lib/oauth/signature/base.rb +78 -71
  35. data/lib/oauth/signature/hmac/sha1.rb +16 -10
  36. data/lib/oauth/signature/hmac/sha256.rb +16 -10
  37. data/lib/oauth/signature/plaintext.rb +18 -20
  38. data/lib/oauth/signature/rsa/sha1.rb +46 -38
  39. data/lib/oauth/signature.rb +8 -5
  40. data/lib/oauth/token.rb +7 -5
  41. data/lib/oauth/tokens/access_token.rb +5 -3
  42. data/lib/oauth/tokens/consumer_token.rb +4 -2
  43. data/lib/oauth/tokens/request_token.rb +12 -10
  44. data/lib/oauth/tokens/server_token.rb +2 -1
  45. data/lib/oauth/tokens/token.rb +2 -0
  46. data/lib/oauth/version.rb +5 -1
  47. data/lib/oauth.rb +17 -9
  48. metadata +105 -98
  49. data/README.rdoc +0 -88
  50. data/bin/oauth +0 -11
  51. data/lib/oauth/cli/authorize_command.rb +0 -71
  52. data/lib/oauth/cli/base_command.rb +0 -208
  53. data/lib/oauth/cli/help_command.rb +0 -22
  54. data/lib/oauth/cli/query_command.rb +0 -25
  55. data/lib/oauth/cli/sign_command.rb +0 -81
  56. data/lib/oauth/cli/version_command.rb +0 -7
  57. data/lib/oauth/cli.rb +0 -56
@@ -1,208 +0,0 @@
1
- class OAuth::CLI
2
- class BaseCommand
3
- def initialize(stdout, stdin, stderr, arguments)
4
- @stdout, @stdin, @stderr = stdout, stdin, stderr
5
-
6
- @options = {}
7
- option_parser.parse!(arguments)
8
- end
9
-
10
- def run
11
- missing = required_options - options.keys
12
- if missing.empty?
13
- _run
14
- else
15
- show_missing(missing)
16
- puts option_parser.help
17
- end
18
- end
19
-
20
- def required_options
21
- []
22
- end
23
-
24
- protected
25
-
26
- attr_reader :options
27
-
28
- def show_missing(array)
29
- array = array.map { |s| "--#{s}" }.join(' ')
30
- OAuth::CLI.puts_red "Options missing to OAuth CLI: #{array}"
31
- end
32
-
33
- def xmpp?
34
- options[:xmpp]
35
- end
36
-
37
- def verbose?
38
- options[:verbose]
39
- end
40
-
41
- def puts(string=nil)
42
- @stdout.puts(string)
43
- end
44
-
45
- def alert(string=nil)
46
- @stderr.puts(string)
47
- end
48
-
49
- def parameters
50
- @parameters ||= begin
51
- escaped_pairs = options[:params].collect do |pair|
52
- if pair =~ /:/
53
- Hash[*pair.split(":", 2)].collect do |k,v|
54
- [CGI.escape(k.strip), CGI.escape(v.strip)] * "="
55
- end
56
- else
57
- pair
58
- end
59
- end
60
-
61
- querystring = escaped_pairs * "&"
62
- cli_params = CGI.parse(querystring)
63
-
64
- {
65
- "oauth_consumer_key" => options[:oauth_consumer_key],
66
- "oauth_nonce" => options[:oauth_nonce],
67
- "oauth_timestamp" => options[:oauth_timestamp],
68
- "oauth_token" => options[:oauth_token],
69
- "oauth_signature_method" => options[:oauth_signature_method],
70
- "oauth_version" => options[:oauth_version]
71
- }.reject { |_k,v| v.nil? || v == "" }.merge(cli_params)
72
- end
73
- end
74
-
75
- def option_parser
76
- @option_parser ||= OptionParser.new do |opts|
77
- opts.banner = "Usage: oauth <command> [ARGS]"
78
-
79
- _option_parser_defaults
80
- _option_parser_common(opts)
81
- _option_parser_sign_and_query(opts)
82
- _option_parser_authorization(opts)
83
- end
84
- end
85
-
86
- def _option_parser_defaults
87
- options[:oauth_nonce] = OAuth::Helper.generate_key
88
- options[:oauth_signature_method] = "HMAC-SHA1"
89
- options[:oauth_timestamp] = OAuth::Helper.generate_timestamp
90
- options[:oauth_version] = "1.0"
91
- options[:method] = :post
92
- options[:params] = []
93
- options[:scheme] = :header
94
- options[:version] = "1.0"
95
- end
96
-
97
- def _option_parser_common(opts)
98
- ## Common Options
99
-
100
- opts.on("-B", "--body", "Use the request body for OAuth parameters.") do
101
- options[:scheme] = :body
102
- end
103
-
104
- opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
105
- options[:oauth_consumer_key] = v
106
- end
107
-
108
- opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
109
- options[:oauth_consumer_secret] = v
110
- end
111
-
112
- opts.on("-H", "--header", "Use the 'Authorization' header for OAuth parameters (default).") do
113
- options[:scheme] = :header
114
- end
115
-
116
- opts.on("-Q", "--query-string", "Use the query string for OAuth parameters.") do
117
- options[:scheme] = :query_string
118
- end
119
-
120
- opts.on("-O", "--options FILE", "Read options from a file") do |v|
121
- arguments = open(v).readlines.map { |l| l.chomp.split(" ") }.flatten
122
- options2 = parse_options(arguments)
123
- options.merge!(options2)
124
- end
125
- end
126
-
127
- def _option_parser_sign_and_query(opts)
128
- opts.separator("\n options for signing and querying")
129
-
130
- opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
131
- options[:method] = v
132
- end
133
-
134
- opts.on("--nonce NONCE", "Specifies the nonce to use.") do |v|
135
- options[:oauth_nonce] = v
136
- end
137
-
138
- opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
139
- options[:params] << v
140
- end
141
-
142
- opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
143
- options[:oauth_signature_method] = v
144
- end
145
-
146
- opts.on("--token TOKEN", "Specifies the token to use.") do |v|
147
- options[:oauth_token] = v
148
- end
149
-
150
- opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
151
- options[:oauth_token_secret] = v
152
- end
153
-
154
- opts.on("--timestamp TIMESTAMP", "Specifies the timestamp to use.") do |v|
155
- options[:oauth_timestamp] = v
156
- end
157
-
158
- opts.on("--realm REALM", "Specifies the realm to use.") do |v|
159
- options[:realm] = v
160
- end
161
-
162
- opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
163
- options[:uri] = v
164
- end
165
-
166
- opts.on("--version [VERSION]", "Specifies the OAuth version to use.") do |v|
167
- options[:oauth_version] = v
168
- end
169
-
170
- opts.on("--no-version", "Omit oauth_version.") do
171
- options[:oauth_version] = nil
172
- end
173
-
174
- opts.on("--xmpp", "Generate XMPP stanzas.") do
175
- options[:xmpp] = true
176
- options[:method] ||= "iq"
177
- end
178
-
179
- opts.on("-v", "--verbose", "Be verbose.") do
180
- options[:verbose] = true
181
- end
182
- end
183
-
184
- def _option_parser_authorization(opts)
185
- opts.separator("\n options for authorization")
186
-
187
- opts.on("--access-token-url URL", "Specifies the access token URL.") do |v|
188
- options[:access_token_url] = v
189
- end
190
-
191
- opts.on("--authorize-url URL", "Specifies the authorization URL.") do |v|
192
- options[:authorize_url] = v
193
- end
194
-
195
- opts.on("--callback-url URL", "Specifies a callback URL.") do |v|
196
- options[:oauth_callback] = v
197
- end
198
-
199
- opts.on("--request-token-url URL", "Specifies the request token URL.") do |v|
200
- options[:request_token_url] = v
201
- end
202
-
203
- opts.on("--scope SCOPE", "Specifies the scope (Google-specific).") do |v|
204
- options[:scope] = v
205
- end
206
- end
207
- end
208
- end
@@ -1,22 +0,0 @@
1
- class OAuth::CLI
2
- class HelpCommand < BaseCommand
3
- def run
4
- puts <<-EOT
5
- Usage: oauth COMMAND [ARGS]
6
-
7
- Available oauth commands are:
8
- a, authorize Obtain an access token and secret for a user
9
- q, query Query a protected resource
10
- s, sign Generate an OAuth signature
11
-
12
- In addition to those, there are:
13
- v, version Displays the current version of the library (or --version, -v)
14
- h, help Displays this help (or --help, -h)
15
-
16
- Tip: All commands can be run without args for specific help.
17
-
18
-
19
- EOT
20
- end
21
- end
22
- end
@@ -1,25 +0,0 @@
1
- class OAuth::CLI
2
- class QueryCommand < BaseCommand
3
- extend OAuth::Helper
4
-
5
- def required_options
6
- [:oauth_consumer_key, :oauth_consumer_secret, :oauth_token, :oauth_token_secret]
7
- end
8
-
9
- def _run
10
- consumer = OAuth::Consumer.new(options[:oauth_consumer_key], options[:oauth_consumer_secret], scheme: options[:scheme])
11
-
12
- access_token = OAuth::AccessToken.new(consumer, options[:oauth_token], options[:oauth_token_secret])
13
-
14
- # append params to the URL
15
- uri = URI.parse(options[:uri])
16
- params = parameters.map { |k,v| Array(v).map { |v2| "#{OAuth::Helper.escape(k)}=#{OAuth::Helper.escape(v2)}" } * "&" }
17
- uri.query = [uri.query, *params].reject { |x| x.nil? } * "&"
18
- puts uri.to_s
19
-
20
- response = access_token.request(options[:method].to_s.downcase.to_sym, uri.to_s)
21
- puts "#{response.code} #{response.message}"
22
- puts response.body
23
- end
24
- end
25
- end
@@ -1,81 +0,0 @@
1
- class OAuth::CLI
2
- class SignCommand < BaseCommand
3
-
4
- def required_options
5
- [:oauth_consumer_key, :oauth_consumer_secret, :oauth_token, :oauth_token_secret]
6
- end
7
-
8
- def _run
9
- request = OAuth::RequestProxy.proxy \
10
- "method" => options[:method],
11
- "uri" => options[:uri],
12
- "parameters" => parameters
13
-
14
- if verbose?
15
- puts_verbose_parameters(request)
16
- end
17
-
18
- request.sign! \
19
- :consumer_secret => options[:oauth_consumer_secret],
20
- :token_secret => options[:oauth_token_secret]
21
-
22
- if verbose?
23
- puts_verbose_request(request)
24
- else
25
- puts request.oauth_signature
26
- end
27
- end
28
-
29
- def puts_verbose_parameters(request)
30
- puts "OAuth parameters:"
31
- request.oauth_parameters.each do |k,v|
32
- puts " " + [k, v] * ": "
33
- end
34
- puts
35
-
36
- if request.non_oauth_parameters.any?
37
- puts "Parameters:"
38
- request.non_oauth_parameters.each do |k,v|
39
- puts " " + [k, v] * ": "
40
- end
41
- puts
42
- end
43
- end
44
-
45
- def puts_verbose_request(request)
46
- puts "Method: #{request.method}"
47
- puts "URI: #{request.uri}"
48
- puts "Normalized params: #{request.normalized_parameters}" unless options[:xmpp]
49
- puts "Signature base string: #{request.signature_base_string}"
50
-
51
- if xmpp?
52
- puts
53
- puts "XMPP Stanza:"
54
- puts xmpp_output(request)
55
- puts
56
- puts "Note: You may want to use bare JIDs in your URI."
57
- puts
58
- else
59
- puts "OAuth Request URI: #{request.signed_uri}"
60
- puts "Request URI: #{request.signed_uri(false)}"
61
- puts "Authorization header: #{request.oauth_header(:realm => options[:realm])}"
62
- end
63
- puts "Signature: #{request.oauth_signature}"
64
- puts "Escaped signature: #{OAuth::Helper.escape(request.oauth_signature)}"
65
- end
66
-
67
- def xmpp_output(request)
68
- <<-EOS
69
- <oauth xmlns='urn:xmpp:oauth:0'>
70
- <oauth_consumer_key>#{request.oauth_consumer_key}</oauth_consumer_key>
71
- <oauth_token>#{request.oauth_token}</oauth_token>
72
- <oauth_signature_method>#{request.oauth_signature_method}</oauth_signature_method>
73
- <oauth_signature>#{request.oauth_signature}</oauth_signature>
74
- <oauth_timestamp>#{request.oauth_timestamp}</oauth_timestamp>
75
- <oauth_nonce>#{request.oauth_nonce}</oauth_nonce>
76
- <oauth_version>#{request.oauth_version}</oauth_version>
77
- </oauth>
78
- EOS
79
- end
80
- end
81
- end
@@ -1,7 +0,0 @@
1
- class OAuth::CLI
2
- class VersionCommand < BaseCommand
3
- def run
4
- puts "OAuth Gem #{OAuth::VERSION}"
5
- end
6
- end
7
- end
data/lib/oauth/cli.rb DELETED
@@ -1,56 +0,0 @@
1
- require 'optparse'
2
- require 'oauth/cli/base_command'
3
- require 'oauth/cli/help_command'
4
- require 'oauth/cli/query_command'
5
- require 'oauth/cli/authorize_command'
6
- require 'oauth/cli/sign_command'
7
- require 'oauth/cli/version_command'
8
- require 'active_support/core_ext/string/inflections'
9
-
10
- module OAuth
11
- class CLI
12
- def self.puts_red(string)
13
- puts "\033[0;91m#{string}\033[0m"
14
- end
15
-
16
- ALIASES = {
17
- 'h' => 'help',
18
- 'v' => 'version',
19
- 'q' => 'query',
20
- 'a' => 'authorize',
21
- 's' => 'sign',
22
- }
23
-
24
- def initialize(stdout, stdin, stderr, command, arguments)
25
- klass = get_command_class(parse_command(command))
26
- @command = klass.new(stdout, stdin, stderr, arguments)
27
- @help_command = HelpCommand.new(stdout, stdin, stderr, [])
28
- end
29
-
30
- def run
31
- @command.run
32
- end
33
-
34
- private
35
-
36
- def get_command_class(command)
37
- Object.const_get("OAuth::CLI::#{command.camelize}Command")
38
- end
39
-
40
- def parse_command(command)
41
- case command = command.to_s.downcase
42
- when '--version', '-v'
43
- 'version'
44
- when '--help', '-h', nil, ''
45
- 'help'
46
- when *ALIASES.keys
47
- ALIASES[command]
48
- when *ALIASES.values
49
- command
50
- else
51
- OAuth::CLI.puts_red "Command '#{command}' not found"
52
- 'help'
53
- end
54
- end
55
- end
56
- end