# File lib/oauth/cli.rb, line 31
31: def execute(stdout, stdin, stderr, arguments = [])
32: @stdout = stdout
33: @stdin = stdin
34: @stderr = stderr
35: extract_command_and_parse_options(arguments)
36:
37: if sufficient_options? && valid_command?
38: if command == "debug"
39: @command = "sign"
40: @options[:verbose] = true
41: end
42:
43: case command
44: # TODO move command logic elsewhere
45: when "authorize"
46: begin
47: consumer = OAuth::Consumer.new options[:oauth_consumer_key],
48: options[:oauth_consumer_secret],
49: :access_token_url => options[:access_token_url],
50: :authorize_url => options[:authorize_url],
51: :request_token_url => options[:request_token_url],
52: :scheme => options[:scheme],
53: :http_method => options[:method].to_s.downcase.to_sym
54:
55: # parameters for OAuth 1.0a
56: oauth_verifier = nil
57:
58: # get a request token
59: request_token = consumer.get_request_token({ :oauth_callback => options[:oauth_callback] }, { "scope" => options[:scope] })
60:
61: if request_token.callback_confirmed?
62: stdout.puts "Server appears to support OAuth 1.0a; enabling support."
63: options[:version] = "1.0a"
64: end
65:
66: stdout.puts "Please visit this url to authorize:"
67: stdout.puts request_token.authorize_url
68:
69: if options[:version] == "1.0a"
70: stdout.puts "Please enter the verification code provided by the SP (oauth_verifier):"
71: oauth_verifier = stdin.gets.chomp
72: else
73: stdout.puts "Press return to continue..."
74: stdin.gets
75: end
76:
77: begin
78: # get an access token
79: access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)
80:
81: stdout.puts "Response:"
82: access_token.params.each do |k,v|
83: stdout.puts " #{k}: #{v}" unless k.is_a?(Symbol)
84: end
85: rescue OAuth::Unauthorized => e
86: stderr.puts "A problem occurred while attempting to obtain an access token:"
87: stderr.puts e
88: stderr.puts e.request.body
89: end
90: rescue OAuth::Unauthorized => e
91: stderr.puts "A problem occurred while attempting to authorize:"
92: stderr.puts e
93: stderr.puts e.request.body
94: end
95: when "query"
96: consumer = OAuth::Consumer.new options[:oauth_consumer_key],
97: options[:oauth_consumer_secret],
98: :scheme => options[:scheme]
99:
100: access_token = OAuth::AccessToken.new(consumer, options[:oauth_token], options[:oauth_token_secret])
101:
102: # append params to the URL
103: uri = URI.parse(options[:uri])
104: params = prepare_parameters.map { |k,v| v.map { |v2| "#{URI.encode(k)}=#{URI.encode(v2)}" } * "&" }
105: uri.query = [uri.query, *params].reject { |x| x.nil? } * "&"
106: p uri.to_s
107:
108: response = access_token.request(options[:method].downcase.to_sym, uri.to_s)
109: puts "#{response.code} #{response.message}"
110: puts response.body
111: when "sign"
112: parameters = prepare_parameters
113:
114: request = OAuth::RequestProxy.proxy "method" => options[:method],
115: "uri" => options[:uri],
116: "parameters" => parameters
117:
118: if verbose?
119: stdout.puts "OAuth parameters:"
120: request.oauth_parameters.each do |k,v|
121: stdout.puts " " + [k, v] * ": "
122: end
123: stdout.puts
124:
125: if request.non_oauth_parameters.any?
126: stdout.puts "Parameters:"
127: request.non_oauth_parameters.each do |k,v|
128: stdout.puts " " + [k, v] * ": "
129: end
130: stdout.puts
131: end
132: end
133:
134: request.sign! :consumer_secret => options[:oauth_consumer_secret],
135: :token_secret => options[:oauth_token_secret]
136:
137: if verbose?
138: stdout.puts "Method: #{request.method}"
139: stdout.puts "URI: #{request.uri}"
140: stdout.puts "Normalized params: #{request.normalized_parameters}" unless options[:xmpp]
141: stdout.puts "Signature base string: #{request.signature_base_string}"
142:
143: if options[:xmpp]
144: stdout.puts
145: stdout.puts "XMPP Stanza:"
146: stdout.puts <oauth xmlns='urn:xmpp:oauth:0'> <oauth_consumer_key>#{request.oauth_consumer_key}</oauth_consumer_key> <oauth_token>#{request.oauth_token}</oauth_token> <oauth_signature_method>#{request.oauth_signature_method}</oauth_signature_method> <oauth_signature>#{request.oauth_signature}</oauth_signature> <oauth_timestamp>#{request.oauth_timestamp}</oauth_timestamp> <oauth_nonce>#{request.oauth_nonce}</oauth_nonce> <oauth_version>#{request.oauth_version}</oauth_version> </oauth>
147: stdout.puts
148: stdout.puts "Note: You may want to use bare JIDs in your URI."
149: stdout.puts
150: else
151: stdout.puts "OAuth Request URI: #{request.signed_uri}"
152: stdout.puts "Request URI: #{request.signed_uri(false)}"
153: stdout.puts "Authorization header: #{request.oauth_header(:realm => options[:realm])}"
154: end
155: stdout.puts "Signature: #{request.oauth_signature}"
156: stdout.puts "Escaped signature: #{OAuth::Helper.escape(request.oauth_signature)}"
157: else
158: stdout.puts request.oauth_signature
159: end
160: when "version"
161: puts "OAuth for Ruby #{OAuth::VERSION}"
162: end
163: else
164: usage
165: end
166: end
# File lib/oauth/cli.rb, line 184
184: def extract_command_and_parse_options(arguments)
185: @command = arguments[1]
186: parse_options(arguments[0..1])
187: end
# File lib/oauth/cli.rb, line 189
189: def option_parser(arguments = "")
190: # TODO add realm parameter
191: # TODO add user-agent parameter
192: option_parser = OptionParser.new do |opts|
193: opts.banner = "Usage: #{$0} [options] <command>"
194:
195: # defaults
196: options[:oauth_nonce] = OAuth::Helper.generate_key
197: options[:oauth_signature_method] = "HMAC-SHA1"
198: options[:oauth_timestamp] = OAuth::Helper.generate_timestamp
199: options[:oauth_version] = "1.0"
200: options[:method] = :post
201: options[:params] = []
202: options[:scheme] = :header
203: options[:version] = "1.0"
204:
205: ## Common Options
206:
207: opts.on("-B", "--body", "Use the request body for OAuth parameters.") do
208: options[:scheme] = :body
209: end
210:
211: opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
212: options[:oauth_consumer_key] = v
213: end
214:
215: opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
216: options[:oauth_consumer_secret] = v
217: end
218:
219: opts.on("-H", "--header", "Use the 'Authorization' header for OAuth parameters (default).") do
220: options[:scheme] = :header
221: end
222:
223: opts.on("-Q", "--query-string", "Use the query string for OAuth parameters.") do
224: options[:scheme] = :query_string
225: end
226:
227: opts.on("-O", "--options FILE", "Read options from a file") do |v|
228: arguments.unshift(*open(v).readlines.map { |l| l.chomp.split(" ") }.flatten)
229: end
230:
231: ## Options for signing and making requests
232:
233: opts.separator("\n options for signing and querying")
234:
235: opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
236: options[:method] = v
237: end
238:
239: opts.on("--nonce NONCE", "Specifies the none to use.") do |v|
240: options[:oauth_nonce] = v
241: end
242:
243: opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
244: options[:params] << v
245: end
246:
247: opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
248: options[:oauth_signature_method] = v
249: end
250:
251: opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
252: options[:oauth_token_secret] = v
253: end
254:
255: opts.on("--timestamp TIMESTAMP", "Specifies the timestamp to use.") do |v|
256: options[:oauth_timestamp] = v
257: end
258:
259: opts.on("--token TOKEN", "Specifies the token to use.") do |v|
260: options[:oauth_token] = v
261: end
262:
263: opts.on("--realm REALM", "Specifies the realm to use.") do |v|
264: options[:realm] = v
265: end
266:
267: opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
268: options[:uri] = v
269: end
270:
271: opts.on(:OPTIONAL, "--version VERSION", "Specifies the OAuth version to use.") do |v|
272: if v
273: options[:oauth_version] = v
274: else
275: @command = "version"
276: end
277: end
278:
279: opts.on("--no-version", "Omit oauth_version.") do
280: options[:oauth_version] = nil
281: end
282:
283: opts.on("--xmpp", "Generate XMPP stanzas.") do
284: options[:xmpp] = true
285: options[:method] ||= "iq"
286: end
287:
288: opts.on("-v", "--verbose", "Be verbose.") do
289: options[:verbose] = true
290: end
291:
292: ## Options for authorization
293:
294: opts.separator("\n options for authorization")
295:
296: opts.on("--access-token-url URL", "Specifies the access token URL.") do |v|
297: options[:access_token_url] = v
298: end
299:
300: opts.on("--authorize-url URL", "Specifies the authorization URL.") do |v|
301: options[:authorize_url] = v
302: end
303:
304: opts.on("--callback-url URL", "Specifies a callback URL.") do |v|
305: options[:oauth_callback] = v
306: end
307:
308: opts.on("--request-token-url URL", "Specifies the request token URL.") do |v|
309: options[:request_token_url] = v
310: end
311:
312: opts.on("--scope SCOPE", "Specifies the scope (Google-specific).") do |v|
313: options[:scope] = v
314: end
315: end
316: end
# File lib/oauth/cli.rb, line 318
318: def parse_options(arguments)
319: option_parser(arguments).parse!(arguments)
320: end
# File lib/oauth/cli.rb, line 322
322: def prepare_parameters
323: escaped_pairs = options[:params].collect do |pair|
324: if pair =~ /:/
325: Hash[*pair.split(":", 2)].collect do |k,v|
326: [CGI.escape(k.strip), CGI.escape(v.strip)] * "="
327: end
328: else
329: pair
330: end
331: end
332:
333: querystring = escaped_pairs * "&"
334: cli_params = CGI.parse(querystring)
335:
336: {
337: "oauth_consumer_key" => options[:oauth_consumer_key],
338: "oauth_nonce" => options[:oauth_nonce],
339: "oauth_timestamp" => options[:oauth_timestamp],
340: "oauth_token" => options[:oauth_token],
341: "oauth_signature_method" => options[:oauth_signature_method],
342: "oauth_version" => options[:oauth_version]
343: }.reject { |k,v| v.nil? || v == "" }.merge(cli_params)
344: end
# File lib/oauth/cli.rb, line 346
346: def sufficient_options?
347: case command
348: # TODO move command logic elsewhere
349: when "authorize"
350: options[:oauth_consumer_key] && options[:oauth_consumer_secret] &&
351: options[:access_token_url] && options[:authorize_url] &&
352: options[:request_token_url]
353: when "version"
354: true
355: else
356: options[:oauth_consumer_key] && options[:oauth_consumer_secret] &&
357: options[:method] && options[:uri]
358: end
359: end
# File lib/oauth/cli.rb, line 361
361: def usage
362: stdout.puts option_parser.help
363: stdout.puts
364: stdout.puts "Available commands:"
365: SUPPORTED_COMMANDS.each do |command, desc|
366: puts " #{command.ljust(15)}#{desc}"
367: end
368: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.