Class: OAuth::CLI
Constant Summary collapse
- SUPPORTED_COMMANDS =
{ "authorize" => "Obtain an access token and secret for a user", "debug" => "Verbosely generate an OAuth signature", "query" => "Query a protected resource", "sign" => "Generate an OAuth signature", "version" => "Display the current version of the library" }
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
- #execute(stdout, stdin, stderr, arguments = []) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
22 23 24 25 26 27 28 29 |
# File 'lib/oauth/cli.rb', line 22 def initialize = {} # don't dump a backtrace on a ^C trap(:INT) { exit } end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
14 15 16 |
# File 'lib/oauth/cli.rb', line 14 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/oauth/cli.rb', line 15 def end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
16 17 18 |
# File 'lib/oauth/cli.rb', line 16 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
16 17 18 |
# File 'lib/oauth/cli.rb', line 16 def stdout @stdout end |
Class Method Details
.execute(stdout, stdin, stderr, arguments = []) ⇒ Object
18 19 20 |
# File 'lib/oauth/cli.rb', line 18 def self.execute(stdout, stdin, stderr, arguments = []) self.new.execute(stdout, stdin, stderr, arguments) end |
Instance Method Details
#execute(stdout, stdin, stderr, arguments = []) ⇒ Object
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 |
# File 'lib/oauth/cli.rb', line 31 def execute(stdout, stdin, stderr, arguments = []) @stdout = stdout @stdin = stdin @stderr = stderr (arguments) if && valid_command? if command == "debug" @command = "sign" [:verbose] = true end case command # TODO move command logic elsewhere when "authorize" begin consumer = OAuth::Consumer.new \ [:oauth_consumer_key], [:oauth_consumer_secret], :access_token_url => [:access_token_url], :authorize_url => [:authorize_url], :request_token_url => [:request_token_url], :scheme => [:scheme], :http_method => [:method].to_s.downcase.to_sym # parameters for OAuth 1.0a oauth_verifier = nil # get a request token request_token = consumer.get_request_token({ :oauth_callback => [:oauth_callback] }, { "scope" => [:scope] }) if request_token.callback_confirmed? stdout.puts "Server appears to support OAuth 1.0a; enabling support." [:version] = "1.0a" end stdout.puts "Please visit this url to authorize:" stdout.puts request_token. if [:version] == "1.0a" stdout.puts "Please enter the verification code provided by the SP (oauth_verifier):" oauth_verifier = stdin.gets.chomp else stdout.puts "Press return to continue..." stdin.gets end begin # get an access token access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier) stdout.puts "Response:" access_token.params.each do |k,v| stdout.puts " #{k}: #{v}" unless k.is_a?(Symbol) end rescue OAuth:: => e stderr.puts "A problem occurred while attempting to obtain an access token:" stderr.puts e stderr.puts e.request.body end rescue OAuth:: => e stderr.puts "A problem occurred while attempting to authorize:" stderr.puts e stderr.puts e.request.body end when "query" consumer = OAuth::Consumer.new \ [:oauth_consumer_key], [:oauth_consumer_secret], :scheme => [:scheme] access_token = OAuth::AccessToken.new(consumer, [:oauth_token], [:oauth_token_secret]) # append params to the URL uri = URI.parse([:uri]) params = prepare_parameters.map { |k,v| v.map { |v2| "#{URI.encode(k)}=#{URI.encode(v2)}" } * "&" } uri.query = [uri.query, *params].reject { |x| x.nil? } * "&" p uri.to_s response = access_token.request([:method].downcase.to_sym, uri.to_s) puts "#{response.code} #{response.message}" puts response.body when "sign" parameters = prepare_parameters request = OAuth::RequestProxy.proxy \ "method" => [:method], "uri" => [:uri], "parameters" => parameters if verbose? stdout.puts "OAuth parameters:" request.oauth_parameters.each do |k,v| stdout.puts " " + [k, v] * ": " end stdout.puts if request.non_oauth_parameters.any? stdout.puts "Parameters:" request.non_oauth_parameters.each do |k,v| stdout.puts " " + [k, v] * ": " end stdout.puts end end request.sign! \ :consumer_secret => [:oauth_consumer_secret], :token_secret => [:oauth_token_secret] if verbose? stdout.puts "Method: #{request.method}" stdout.puts "URI: #{request.uri}" stdout.puts "Normalized params: #{request.normalized_parameters}" unless [:xmpp] stdout.puts "Signature base string: #{request.signature_base_string}" if [:xmpp] stdout.puts stdout.puts "XMPP Stanza:" stdout.puts " <oauth xmlns='urn:xmpp:oauth:0'>\n<oauth_consumer_key>\#{request.oauth_consumer_key}</oauth_consumer_key>\n<oauth_token>\#{request.oauth_token}</oauth_token>\n<oauth_signature_method>\#{request.oauth_signature_method}</oauth_signature_method>\n<oauth_signature>\#{request.oauth_signature}</oauth_signature>\n<oauth_timestamp>\#{request.oauth_timestamp}</oauth_timestamp>\n<oauth_nonce>\#{request.oauth_nonce}</oauth_nonce>\n<oauth_version>\#{request.oauth_version}</oauth_version>\n </oauth>\n EOS\n stdout.puts\n stdout.puts \"Note: You may want to use bare JIDs in your URI.\"\n stdout.puts\n else\n stdout.puts \"OAuth Request URI: \#{request.signed_uri}\"\n stdout.puts \"Request URI: \#{request.signed_uri(false)}\"\n stdout.puts \"Authorization header: \#{request.oauth_header(:realm => options[:realm])}\"\n end\n stdout.puts \"Signature: \#{request.oauth_signature}\"\n stdout.puts \"Escaped signature: \#{OAuth::Helper.escape(request.oauth_signature)}\"\n else\n stdout.puts request.oauth_signature\n end\n when \"version\"\n puts \"OAuth for Ruby \#{OAuth::VERSION}\"\n end\n else\n usage\n end\nend\n" |