Class: Twurl::OAuthClient
- Inherits:
-
Object
- Object
- Twurl::OAuthClient
- Defined in:
- lib/twurl/oauth_client.rb
Direct Known Subclasses
Constant Summary collapse
- OAUTH_CLIENT_OPTIONS =
%w[username consumer_key consumer_secret token secret]
- METHODS =
{ :post => Net::HTTP::Post, :get => Net::HTTP::Get, :put => Net::HTTP::Put, :delete => Net::HTTP::Delete, :options => Net::HTTP::Options, :head => Net::HTTP::Head, :copy => Net::HTTP::Copy }
Instance Attribute Summary collapse
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
- .has_oauth_options?(options) ⇒ Boolean
- .load_client_for_app_only_auth(options, consumer_key) ⇒ Object
- .load_client_for_non_profile_app_only_auth(options) ⇒ Object
- .load_client_for_username(username) ⇒ Object
- .load_client_for_username_and_consumer_key(username, consumer_key) ⇒ Object
- .load_default_client(options) ⇒ Object
- .load_from_options(options) ⇒ Object
- .load_new_client_from_oauth_options(options) ⇒ Object
- .load_new_client_from_options(options) ⇒ Object
- .rcfile(reload = false) ⇒ Object
Instance Method Summary collapse
- #access_token ⇒ Object
- #authorized? ⇒ Boolean
- #build_request_from_options(options, &block) ⇒ Object
- #configure_http! ⇒ Object
- #consumer ⇒ Object
- #exchange_credentials_for_access_token ⇒ Object
- #fetch_verify_credentials ⇒ Object
- #generate_authorize_url ⇒ Object
-
#initialize(options = {}) ⇒ OAuthClient
constructor
A new instance of OAuthClient.
- #needs_to_authorize? ⇒ Boolean
- #perform_pin_authorize_workflow ⇒ Object
- #perform_request_from_options(options, &block) ⇒ Object
- #pin_auth_parameters ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object
- #user_agent ⇒ Object
- #verify_has_username ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ OAuthClient
Returns a new instance of OAuthClient.
110 111 112 113 114 115 116 117 |
# File 'lib/twurl/oauth_client.rb', line 110 def initialize( = {}) @username = ['username'] @consumer_key = ['consumer_key'] @consumer_secret = ['consumer_secret'] @token = ['token'] @secret = ['secret'] configure_http! end |
Instance Attribute Details
#username ⇒ Object (readonly)
Returns the value of attribute username.
109 110 111 |
# File 'lib/twurl/oauth_client.rb', line 109 def username @username end |
Class Method Details
.has_oauth_options?(options) ⇒ Boolean
29 30 31 |
# File 'lib/twurl/oauth_client.rb', line 29 def () (.consumer_key && .consumer_secret && .access_token && .token_secret) ? true : false end |
.load_client_for_app_only_auth(options, consumer_key) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/twurl/oauth_client.rb', line 66 def load_client_for_app_only_auth(, consumer_key) if .command == 'authorize' AppOnlyOAuthClient.new() else AppOnlyOAuthClient.new( ..merge( 'bearer_token' => rcfile.bearer_tokens.to_hash[consumer_key] ) ) end end |
.load_client_for_non_profile_app_only_auth(options) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/twurl/oauth_client.rb', line 78 def load_client_for_non_profile_app_only_auth() AppOnlyOAuthClient.new( ..merge( 'bearer_token' => rcfile.bearer_tokens.to_hash[.consumer_key] ) ) end |
.load_client_for_username(username) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/twurl/oauth_client.rb', line 42 def load_client_for_username(username) if user_profiles = rcfile[username] if user_profiles.values.size == 1 new(user_profiles.values.first) else raise Exception, "There is more than one consumer key associated with #{username}. Please specify which consumer key you want as well." end else raise Exception, "No profile for #{username}" end end |
.load_client_for_username_and_consumer_key(username, consumer_key) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/twurl/oauth_client.rb', line 33 def load_client_for_username_and_consumer_key(username, consumer_key) user_profiles = rcfile[username] if user_profiles && attributes = user_profiles[consumer_key] new(attributes) else raise Exception, "No profile for #{username}" end end |
.load_default_client(options) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/twurl/oauth_client.rb', line 86 def load_default_client() return if .command == 'bearer_tokens' = "You must authorize first." = "To use --bearer option, you need to authorize (OAuth1.0a) and create at least one user profile (~/.twurlrc):\n\n" \ "twurl authorize -c key -s secret\n" \ "\nor, you can specify issued token's consumer_key directly:\n" \ "(to see your issued tokens: 'twurl bearer_tokens')\n\n" \ "twurl --bearer -c key '/path/to/api'" raise Exception, "#{.app_only ? : }" unless rcfile.default_profile if .app_only raise Exception, "No available bearer token found for consumer_key:#{rcfile.default_profile_consumer_key}" \ unless rcfile.has_bearer_token_for_consumer_key?(rcfile.default_profile_consumer_key) load_client_for_app_only_auth(, rcfile.default_profile_consumer_key) else load_client_for_username_and_consumer_key(*rcfile.default_profile) end end |
.load_from_options(options) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/twurl/oauth_client.rb', line 11 def () if .command == 'request' && () () elsif .command == 'request' && .app_only && .consumer_key load_client_for_non_profile_app_only_auth() elsif rcfile.has_oauth_profile_for_username_with_consumer_key?(.username, .consumer_key) load_client_for_username_and_consumer_key(.username, .consumer_key) elsif .username load_client_for_username(.username) elsif .command == 'authorize' && .app_only load_client_for_app_only_auth(, .consumer_key) elsif .command == 'authorize' () else load_default_client() end end |
.load_new_client_from_oauth_options(options) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/twurl/oauth_client.rb', line 58 def () new(..merge( 'token' => .access_token, 'secret' => .token_secret ) ) end |
.load_new_client_from_options(options) ⇒ Object
54 55 56 |
# File 'lib/twurl/oauth_client.rb', line 54 def () new(.) end |
Instance Method Details
#access_token ⇒ Object
286 287 288 |
# File 'lib/twurl/oauth_client.rb', line 286 def access_token @access_token ||= OAuth::AccessToken.new(consumer, token, secret) end |
#authorized? ⇒ Boolean
236 237 238 239 |
# File 'lib/twurl/oauth_client.rb', line 236 def oauth_response = fetch_verify_credentials oauth_response.class == Net::HTTPOK end |
#build_request_from_options(options, &block) ⇒ Object
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 |
# File 'lib/twurl/oauth_client.rb', line 129 def (, &block) request_class = METHODS.fetch(.request_method.to_sym) request = request_class.new(.path, .headers) if .upload && .upload['file'].count > 0 boundary = "00Twurl" + rand(1000000000000000000).to_s + "lruwT99" multipart_body = [] file_field = .upload['filefield'] ? .upload['filefield'] : 'media[]' .data.each {|key, value| multipart_body << "--#{boundary}\r\n" multipart_body << "Content-Disposition: form-data; name=\"#{key}\"\r\n" multipart_body << "\r\n" multipart_body << value multipart_body << "\r\n" } .upload['file'].each {|filename| multipart_body << "--#{boundary}\r\n" multipart_body << "Content-Disposition: form-data; name=\"#{file_field}\"; filename=\"#{File.basename(filename)}\"\r\n" multipart_body << "Content-Type: application/octet-stream\r\n" multipart_body << "Content-Transfer-Encoding: base64\r\n" if .upload['base64'] multipart_body << "\r\n" if .upload['base64'] enc = Base64.encode64(File.binread(filename)) multipart_body << enc else multipart_body << File.binread(filename) end } multipart_body << "\r\n--#{boundary}--\r\n" request.body = multipart_body.join request.content_type = "multipart/form-data, boundary=\"#{boundary}\"" elsif .json_data request.body = .data elsif .data request.content_type = "application/x-www-form-urlencoded" unless request.content_type if .raw_data request.body = .data else begin request.body = .data.map do |key, value| "#{key}" + (value.nil? ? "" : "=#{CGI.escape(value)}") end.join("&") rescue raise Exception, "ERROR: failed to parse POST request body" end end end request end |
#configure_http! ⇒ Object
267 268 269 270 271 272 273 274 |
# File 'lib/twurl/oauth_client.rb', line 267 def configure_http! consumer.http.set_debug_output(Twurl..debug_output_io) if Twurl..trace consumer.http.read_timeout = consumer.http.open_timeout = Twurl..timeout || 60 consumer.http.open_timeout = Twurl..connection_timeout if Twurl..connection_timeout consumer.http.max_retries = 0 consumer.http.use_ssl = true consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE end |
#consumer ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/twurl/oauth_client.rb', line 276 def consumer @consumer ||= OAuth::Consumer.new( consumer_key, consumer_secret, :site => Twurl..base_url, :proxy => Twurl..proxy ) end |
#exchange_credentials_for_access_token ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/twurl/oauth_client.rb', line 196 def exchange_credentials_for_access_token response = begin consumer.token_request(:post, consumer.access_token_path, nil, {}) rescue OAuth::Unauthorized end @token = response[:oauth_token] @secret = response[:oauth_token_secret] end |
#fetch_verify_credentials ⇒ Object
232 233 234 |
# File 'lib/twurl/oauth_client.rb', line 232 def fetch_verify_credentials access_token.get('/1.1/account/verify_credentials.json?include_entities=false&skip_status=true') end |
#generate_authorize_url ⇒ Object
218 219 220 221 222 223 224 225 226 |
# File 'lib/twurl/oauth_client.rb', line 218 def request = consumer.create_signed_request(:get, consumer., @request_token, pin_auth_parameters) params = request['Authorization'].sub(/^OAuth\s+/, '').split(/,\s+/).map { |p| k, v = p.split('=') v =~ /"(.*?)"/ "#{k}=#{CGI.escape($1)}" }.join('&') "#{Twurl..base_url}#{request.path}?#{params}" end |
#needs_to_authorize? ⇒ Boolean
241 242 243 |
# File 'lib/twurl/oauth_client.rb', line 241 def token.nil? || secret.nil? end |
#perform_pin_authorize_workflow ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/twurl/oauth_client.rb', line 206 def @request_token = consumer.get_request_token CLI.puts("Go to #{} and paste in the supplied PIN") begin pin = STDIN.gets.chomp rescue SystemExit, Interrupt raise Exception, "Operation cancelled" end access_token = @request_token.get_access_token(:oauth_verifier => pin.chomp) {:oauth_token => access_token.token, :oauth_token_secret => access_token.secret} end |
#perform_request_from_options(options, &block) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/twurl/oauth_client.rb', line 184 def (, &block) request = () request.oauth!(consumer.http, consumer, access_token) request['user-agent'] = user_agent consumer.http.request(request, &block) end |
#pin_auth_parameters ⇒ Object
228 229 230 |
# File 'lib/twurl/oauth_client.rb', line 228 def pin_auth_parameters {'oauth_callback' => 'oob'} end |
#save ⇒ Object
245 246 247 248 |
# File 'lib/twurl/oauth_client.rb', line 245 def save verify_has_username self.class.rcfile << self end |
#to_hash ⇒ Object
258 259 260 261 262 263 264 265 |
# File 'lib/twurl/oauth_client.rb', line 258 def to_hash OAUTH_CLIENT_OPTIONS.inject({}) do |hash, attribute| if value = send(attribute) hash[attribute] = value end hash end end |
#user_agent ⇒ Object
191 192 193 194 |
# File 'lib/twurl/oauth_client.rb', line 191 def user_agent "twurl version: #{Version} " \ "platform: #{RUBY_ENGINE} #{RUBY_VERSION} (#{RUBY_PLATFORM})" end |
#verify_has_username ⇒ Object
250 251 252 253 254 255 256 |
# File 'lib/twurl/oauth_client.rb', line 250 def verify_has_username if username.nil? || username == '' oauth_response = fetch_verify_credentials oauth_response.body =~ /"screen_name"\s*:\s*"(.*?)"/ @username = $1 end end |