Class: OAuth::CLI
- Inherits:
-
Object
- Object
- OAuth::CLI
- Defined in:
- lib/oauth/cli.rb
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" }
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.
21 22 23 24 25 26 27 28 |
# File 'lib/oauth/cli.rb', line 21 def initialize @options = {} # don't dump a backtrace on a ^C trap(:INT) { exit } end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
13 14 15 |
# File 'lib/oauth/cli.rb', line 13 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/oauth/cli.rb', line 14 def @options end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
15 16 17 |
# File 'lib/oauth/cli.rb', line 15 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
15 16 17 |
# File 'lib/oauth/cli.rb', line 15 def stdout @stdout end |
Class Method Details
.execute(stdout, stdin, stderr, arguments = []) ⇒ Object
17 18 19 |
# File 'lib/oauth/cli.rb', line 17 def self.execute(stdout, stdin, stderr, arguments = []) self.new.execute(stdout, stdin, stderr, arguments) end |
Instance Method Details
#execute(stdout, stdin, stderr, arguments = []) ⇒ Object
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 |
# File 'lib/oauth/cli.rb', line 30 def execute(stdout, stdin, stderr, arguments = []) @stdout = stdout @stdin = stdin @stderr = stderr (arguments) if && valid_command? if command == "debug" @command = "sign" @options[:verbose] = true end case command # TODO move command logic elsewhere when "authorize" 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] # parameters for OAuth 1.0a oauth_verifier = nil # get a request token request_token = consumer.get_request_token(:oauth_callback => [:oauth_callback]) 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::Unauthorized => e stderr.puts "A problem occurred while attempting to obtain an access token:" stderr.puts e 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]) response = access_token.request([:method].downcase.to_sym, [:uri]) puts "#{response.code} #{response.}" 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 <<-EOS <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_nonce>#{request.oauth_nonce}</oauth_nonce> <oauth_version>#{request.oauth_version}</oauth_version> </oauth> EOS stdout.puts stdout.puts "Note: You may want to use bare JIDs in your URI." stdout.puts else stdout.puts "OAuth Request URI: #{request.signed_uri}" stdout.puts "Request URI: #{request.signed_uri(false)}" stdout.puts "Authorization header: #{request.oauth_header(:realm => [:realm])}" end stdout.puts "Signature: #{request.oauth_signature}" stdout.puts "Escaped signature: #{OAuth::Helper.escape(request.oauth_signature)}" else stdout.puts request.oauth_signature end end else usage end end |