class OAuth::TTY::Command

Base class for oauth-tty commands.

Includes {OAuth::TTY::AUTH_SANITIZER::FilteredAttributes} so inspect output redacts the accumulated command options hash, which may contain consumer or token secrets read from CLI flags or option files.

Attributes

options[R]

Public Class Methods

new(stdout, stdin, stderr, arguments) click to toggle source
   # File lib/oauth/tty/command.rb
17 def initialize(stdout, stdin, stderr, arguments)
18   @stdout = stdout
19   @stdin = stdin
20   @stderr = stderr
21 
22   @options = {}
23   option_parser.parse!(arguments)
24 end

Public Instance Methods

inspect() click to toggle source
   # File lib/oauth/tty/command.rb
26 def inspect
27   format(
28     "#<%<klass>s:0x%<object_id>x @stdout=%<stdout>s, @stdin=%<stdin>s, @stderr=%<stderr>s, @options=[FILTERED], @option_parser=[FILTERED]>",
29     klass: self.class,
30     object_id: object_id,
31     stdout: @stdout.inspect,
32     stdin: @stdin.inspect,
33     stderr: @stderr.inspect
34   )
35 end
required_options() click to toggle source
   # File lib/oauth/tty/command.rb
47 def required_options
48   []
49 end
run() click to toggle source
   # File lib/oauth/tty/command.rb
37 def run
38   missing = required_options - options.keys
39   if missing.empty?
40     _run
41   else
42     show_missing(missing)
43     puts option_parser.help
44   end
45 end

Protected Instance Methods

_option_parser_authorization(opts) click to toggle source
    # File lib/oauth/tty/command.rb
233 def _option_parser_authorization(opts)
234   opts.separator("\n  options for authorization")
235 
236   opts.on("--access-token-url URL", "Specifies the access token URL.") do |v|
237     options[:access_token_url] = v
238   end
239 
240   opts.on("--authorize-url URL", "Specifies the authorization URL.") do |v|
241     options[:authorize_url] = v
242   end
243 
244   opts.on("--callback-url URL", "Specifies a callback URL.") do |v|
245     options[:oauth_callback] = v
246   end
247 
248   opts.on("--request-token-url URL", "Specifies the request token URL.") do |v|
249     options[:request_token_url] = v
250   end
251 
252   opts.on("--scope SCOPE", "Specifies the scope (Google-specific).") do |v|
253     options[:scope] = v
254   end
255 end
_option_parser_common(opts) click to toggle source
    # File lib/oauth/tty/command.rb
145 def _option_parser_common(opts)
146   ## Common Options
147 
148   opts.on("-B", "--body", "Use the request body for OAuth parameters.") do
149     options[:scheme] = :body
150   end
151 
152   opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
153     options[:oauth_consumer_key] = v
154   end
155 
156   opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
157     options[:oauth_consumer_secret] = v
158   end
159 
160   opts.on("-H", "--header", "Use the 'Authorization' header for OAuth parameters (default).") do
161     options[:scheme] = :header
162   end
163 
164   opts.on("-Q", "--query-string", "Use the query string for OAuth parameters.") do
165     options[:scheme] = :query_string
166   end
167 
168   opts.on("-O", "--options FILE", "Read options from a file") do |v|
169     require "shellwords"
170     arguments = File.open(v).readlines.flat_map { |l| Shellwords.shellsplit(l.chomp) }
171     options2 = parse_options(arguments)
172     options.merge!(options2)
173   end
174 end
_option_parser_defaults() click to toggle source
    # File lib/oauth/tty/command.rb
134 def _option_parser_defaults
135   options[:oauth_nonce] = OAuth::Helper.generate_key
136   options[:oauth_signature_method] = "HMAC-SHA1"
137   options[:oauth_timestamp] = OAuth::Helper.generate_timestamp
138   options[:oauth_version] = "1.0"
139   options[:method] = :post
140   options[:params] = []
141   options[:scheme] = :header
142   options[:version] = "1.0"
143 end
_option_parser_sign_and_query(opts) click to toggle source
    # File lib/oauth/tty/command.rb
176 def _option_parser_sign_and_query(opts)
177   opts.separator("\n  options for signing and querying")
178 
179   opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
180     options[:method] = v
181   end
182 
183   opts.on("--nonce NONCE", "Specifies the nonce to use.") do |v|
184     options[:oauth_nonce] = v
185   end
186 
187   opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
188     options[:params] << v
189   end
190 
191   opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
192     options[:oauth_signature_method] = v
193   end
194 
195   opts.on("--token TOKEN", "Specifies the token to use.") do |v|
196     options[:oauth_token] = v
197   end
198 
199   opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
200     options[:oauth_token_secret] = v
201   end
202 
203   opts.on("--timestamp TIMESTAMP", "Specifies the timestamp to use.") do |v|
204     options[:oauth_timestamp] = v
205   end
206 
207   opts.on("--realm REALM", "Specifies the realm to use.") do |v|
208     options[:realm] = v
209   end
210 
211   opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
212     options[:uri] = v
213   end
214 
215   opts.on("--version [VERSION]", "Specifies the OAuth version to use.") do |v|
216     options[:oauth_version] = v
217   end
218 
219   opts.on("--no-version", "Omit oauth_version.") do
220     options[:oauth_version] = nil
221   end
222 
223   opts.on("--xmpp", "Generate XMPP stanzas.") do
224     options[:xmpp] = true
225     options[:method] ||= "iq"
226   end
227 
228   opts.on("-v", "--verbose", "Be verbose.") do
229     options[:verbose] = true
230   end
231 end
alert(string = nil) click to toggle source
   # File lib/oauth/tty/command.rb
74 def alert(string = nil)
75   @stderr.puts(string)
76 end
option_parser() click to toggle source
    # File lib/oauth/tty/command.rb
104 def option_parser
105   @option_parser ||= OptionParser.new do |opts|
106     opts.banner = "Usage: oauth <command> [ARGS]"
107 
108     _option_parser_defaults
109     _option_parser_common(opts)
110     _option_parser_sign_and_query(opts)
111     _option_parser_authorization(opts)
112   end
113 end
parameters() click to toggle source
    # File lib/oauth/tty/command.rb
 78 def parameters
 79   @parameters ||= begin
 80     escaped_pairs = options[:params].collect do |pair|
 81       if pair.to_s.include?(":")
 82         Hash[*pair.split(":", 2)].collect do |k, v|
 83           [CGI.escape(k.strip), CGI.escape(v.strip)].join("=")
 84         end
 85       else
 86         pair
 87       end
 88     end
 89 
 90     querystring = escaped_pairs * "&"
 91     cli_params = CGI.parse(querystring)
 92 
 93     {
 94       "oauth_consumer_key" => options[:oauth_consumer_key],
 95       "oauth_nonce" => options[:oauth_nonce],
 96       "oauth_timestamp" => options[:oauth_timestamp],
 97       "oauth_token" => options[:oauth_token],
 98       "oauth_signature_method" => options[:oauth_signature_method],
 99       "oauth_version" => options[:oauth_version]
100     }.reject { |_k, v| v.nil? || v == "" }.merge(cli_params)
101   end
102 end
parse_options(arguments) click to toggle source

Parse an array of CLI-like arguments into an options hash without mutating current state This is used by the -O/–options FILE feature to load args from a file and merge them

    # File lib/oauth/tty/command.rb
117 def parse_options(arguments)
118   original_options = @options
119   begin
120     temp_options = {}
121     @options = temp_options
122     _option_parser_defaults
123     OptionParser.new do |opts|
124       _option_parser_common(opts)
125       _option_parser_sign_and_query(opts)
126       _option_parser_authorization(opts)
127     end.parse!(arguments)
128     temp_options
129   ensure
130     @options = original_options
131   end
132 end
puts(string = nil) click to toggle source
   # File lib/oauth/tty/command.rb
70 def puts(string = nil)
71   @stdout.puts(string)
72 end
show_missing(array) click to toggle source
   # File lib/oauth/tty/command.rb
57 def show_missing(array)
58   array = array.map { |s| "--#{s}" }.join(" ")
59   OAuth::TTY::CLI.puts_red("Options missing to OAuth CLI: #{array}")
60 end
verbose?() click to toggle source
   # File lib/oauth/tty/command.rb
66 def verbose?
67   options[:verbose]
68 end
xmpp?() click to toggle source
   # File lib/oauth/tty/command.rb
62 def xmpp?
63   options[:xmpp]
64 end