1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
class OAuth::CLI
class BaseCommand
def initialize(stdout, stdin, stderr, arguments)
@stdout, @stdin, @stderr = stdout, stdin, stderr
@options = {}
option_parser.parse!(arguments)
end
def run
missing = required_options - options.keys
if missing.empty?
_run
else
show_missing(missing)
puts option_parser.help
end
end
def required_options
[]
end
protected
attr_reader :options
def show_missing(array)
array = array.map { |s| "--#{s}" }.join(' ')
OAuth::CLI.puts_red "Options missing to OAuth CLI: #{array}"
end
def xmpp?
options[:xmpp]
end
def verbose?
options[:verbose]
end
def puts(string=nil)
@stdout.puts(string)
end
def alert(string=nil)
@stderr.puts(string)
end
def parameters
@parameters ||= begin
escaped_pairs = options[:params].collect do |pair|
if pair =~ /:/
Hash[*pair.split(":", 2)].collect do |k,v|
[CGI.escape(k.strip), CGI.escape(v.strip)] * "="
end
else
pair
end
end
querystring = escaped_pairs * "&"
cli_params = CGI.parse(querystring)
{
"oauth_consumer_key" => options[:oauth_consumer_key],
"oauth_nonce" => options[:oauth_nonce],
"oauth_timestamp" => options[:oauth_timestamp],
"oauth_token" => options[:oauth_token],
"oauth_signature_method" => options[:oauth_signature_method],
"oauth_version" => options[:oauth_version]
}.reject { |_k,v| v.nil? || v == "" }.merge(cli_params)
end
end
def option_parser
@option_parser ||= OptionParser.new do |opts|
opts.banner = "Usage: oauth <command> [ARGS]"
_option_parser_defaults
_option_parser_common(opts)
_option_parser_sign_and_query(opts)
_option_parser_authorization(opts)
end
end
def _option_parser_defaults
options[:oauth_nonce] = OAuth::Helper.generate_key
options[:oauth_signature_method] = "HMAC-SHA1"
options[:oauth_timestamp] = OAuth::Helper.generate_timestamp
options[:oauth_version] = "1.0"
options[:method] = :post
options[:params] = []
options[:scheme] = :header
options[:version] = "1.0"
end
def _option_parser_common(opts)
## Common Options
opts.on("-B", "--body", "Use the request body for OAuth parameters.") do
options[:scheme] = :body
end
opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
options[:oauth_consumer_key] = v
end
opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
options[:oauth_consumer_secret] = v
end
opts.on("-H", "--header", "Use the 'Authorization' header for OAuth parameters (default).") do
options[:scheme] = :header
end
opts.on("-Q", "--query-string", "Use the query string for OAuth parameters.") do
options[:scheme] = :query_string
end
opts.on("-O", "--options FILE", "Read options from a file") do |v|
arguments = open(v).readlines.map { |l| l.chomp.split(" ") }.flatten
options2 = parse_options(arguments)
options.merge!(options2)
end
end
def _option_parser_sign_and_query(opts)
opts.separator("\n options for signing and querying")
opts.on("--method METHOD", "Specifies the method (e.g. GET) to use when signing.") do |v|
options[:method] = v
end
opts.on("--nonce NONCE", "Specifies the nonce to use.") do |v|
options[:oauth_nonce] = v
end
opts.on("--parameters PARAMS", "Specifies the parameters to use when signing.") do |v|
options[:params] << v
end
opts.on("--signature-method METHOD", "Specifies the signature method to use; defaults to HMAC-SHA1.") do |v|
options[:oauth_signature_method] = v
end
opts.on("--token TOKEN", "Specifies the token to use.") do |v|
options[:oauth_token] = v
end
opts.on("--secret SECRET", "Specifies the token secret to use.") do |v|
options[:oauth_token_secret] = v
end
opts.on("--timestamp TIMESTAMP", "Specifies the timestamp to use.") do |v|
options[:oauth_timestamp] = v
end
opts.on("--realm REALM", "Specifies the realm to use.") do |v|
options[:realm] = v
end
opts.on("--uri URI", "Specifies the URI to use when signing.") do |v|
options[:uri] = v
end
opts.on("--version [VERSION]", "Specifies the OAuth version to use.") do |v|
options[:oauth_version] = v
end
opts.on("--no-version", "Omit oauth_version.") do
options[:oauth_version] = nil
end
opts.on("--xmpp", "Generate XMPP stanzas.") do
options[:xmpp] = true
options[:method] ||= "iq"
end
opts.on("-v", "--verbose", "Be verbose.") do
options[:verbose] = true
end
end
def _option_parser_authorization(opts)
opts.separator("\n options for authorization")
opts.on("--access-token-url URL", "Specifies the access token URL.") do |v|
options[:access_token_url] = v
end
opts.on("--authorize-url URL", "Specifies the authorization URL.") do |v|
options[:authorize_url] = v
end
opts.on("--callback-url URL", "Specifies a callback URL.") do |v|
options[:oauth_callback] = v
end
opts.on("--request-token-url URL", "Specifies the request token URL.") do |v|
options[:request_token_url] = v
end
opts.on("--scope SCOPE", "Specifies the scope (Google-specific).") do |v|
options[:scope] = v
end
end
end
end
|