Class: OAuth::CLI

Inherits:
Object show all
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",
  "version"   => "Display the current version of the library"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



22
23
24
25
26
27
28
29
# File 'lib/oauth/cli.rb', line 22

def initialize
  @options = {}

  # don't dump a backtrace on a ^C
  trap(:INT) {
    exit
  }
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



14
15
16
# File 'lib/oauth/cli.rb', line 14

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/oauth/cli.rb', line 15

def options
  @options
end

#stdinObject (readonly)

Returns the value of attribute stdin.



16
17
18
# File 'lib/oauth/cli.rb', line 16

def stdin
  @stdin
end

#stdoutObject (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
  extract_command_and_parse_options(arguments)

  if sufficient_options? && valid_command?
    if command == "debug"
      @command = "sign"
      @options[:verbose] = true
    end

    case command
    # TODO move command logic elsewhere
    when "authorize"
      begin
        consumer = OAuth::Consumer.new \
          options[:oauth_consumer_key],
          options[:oauth_consumer_secret],
          :access_token_url  => options[:access_token_url],
          :authorize_url     => options[:authorize_url],
          :request_token_url => options[:request_token_url],
          :scheme            => options[:scheme],
          :http_method       => options[: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 => options[:oauth_callback] }, options[:scope] ? { "scope" => options[:scope] } : {})

        if request_token.callback_confirmed?
          stdout.puts "Server appears to support OAuth 1.0a; enabling support."
          options[:version] = "1.0a"
        end

        stdout.puts "Please visit this url to authorize:"
        stdout.puts request_token.authorize_url

        if options[: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
          stderr.puts e.request.body
        end
      rescue OAuth::Unauthorized => 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 \
        options[:oauth_consumer_key],
        options[:oauth_consumer_secret],
        :scheme => options[:scheme]

      access_token = OAuth::AccessToken.new(consumer, options[:oauth_token], options[:oauth_token_secret])

      # append params to the URL
      uri = URI.parse(options[: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(options[:method].to_s.downcase.to_sym, uri.to_s)
      puts "#{response.code} #{response.message}"
      puts response.body
    when "sign"
      parameters = prepare_parameters

      request = OAuth::RequestProxy.proxy \
         "method"     => options[:method],
         "uri"        => options[: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 => options[:oauth_consumer_secret],
        :token_secret    => options[:oauth_token_secret]

      if verbose?
        stdout.puts "Method: #{request.method}"
        stdout.puts "URI: #{request.uri}"
        stdout.puts "Normalized params: #{request.normalized_parameters}" unless options[:xmpp]
        stdout.puts "Signature base string: #{request.signature_base_string}"

        if options[: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_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 => options[: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
    when "version"
      puts "OAuth for Ruby #{OAuth::VERSION}"
    end
  else
    usage
  end
end