lib/signet/oauth_1/client.rb in signet-0.5.0 vs lib/signet/oauth_1/client.rb in signet-0.5.1
- old
+ new
@@ -52,29 +52,66 @@
# 'https://www.google.com/accounts/OAuthGetAccessToken',
# :client_credential_key => 'anonymous',
# :client_credential_secret => 'anonymous'
# )
def initialize(options={})
- self.temporary_credential_uri = options[:temporary_credential_uri]
- self.authorization_uri = options[:authorization_uri]
- self.token_credential_uri = options[:token_credential_uri]
+ self.update!(options)
+ end
+
+ ##
+ # Updates an OAuth 1.0 client.
+ #
+ # @param [Hash] options
+ # The configuration parameters for the client.
+ # - <code>:temporary_credential_uri</code> -
+ # The OAuth temporary credentials URI.
+ # - <code>:authorization_uri</code> -
+ # The OAuth authorization URI.
+ # - <code>:token_credential_uri</code> -
+ # The OAuth token credentials URI.
+ # - <code>:client_credential_key</code> -
+ # The OAuth client credential key.
+ # - <code>:client_credential_secret</code> -
+ # The OAuth client credential secret.
+ # - <code>:callback</code> - The OAuth callback. Defaults to 'oob'.
+ #
+ # @example
+ # client.update!(
+ # :temporary_credential_uri =>
+ # 'https://www.google.com/accounts/OAuthGetRequestToken',
+ # :authorization_uri =>
+ # 'https://www.google.com/accounts/OAuthAuthorizeToken',
+ # :token_credential_uri =>
+ # 'https://www.google.com/accounts/OAuthGetAccessToken',
+ # :client_credential_key => 'anonymous',
+ # :client_credential_secret => 'anonymous'
+ # )
+ #
+ # @see Signet::OAuth1::Client#initialize
+ def update!(options={})
+ # Normalize key to String to allow indifferent access.
+ options = options.inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu }
+ self.temporary_credential_uri = options["temporary_credential_uri"]
+ self.authorization_uri = options["authorization_uri"]
+ self.token_credential_uri = options["token_credential_uri"]
# Technically... this would allow you to pass in a :client key...
# But that would be weird. Don't do that.
self.client_credential_key =
- Signet::OAuth1.extract_credential_key_option(:client, options)
+ Signet::OAuth1.extract_credential_key_option("client", options)
self.client_credential_secret =
- Signet::OAuth1.extract_credential_secret_option(:client, options)
+ Signet::OAuth1.extract_credential_secret_option("client", options)
self.temporary_credential_key =
- Signet::OAuth1.extract_credential_key_option(:temporary, options)
+ Signet::OAuth1.extract_credential_key_option("temporary", options)
self.temporary_credential_secret =
- Signet::OAuth1.extract_credential_secret_option(:temporary, options)
+ Signet::OAuth1.extract_credential_secret_option("temporary", options)
self.token_credential_key =
- Signet::OAuth1.extract_credential_key_option(:token, options)
+ Signet::OAuth1.extract_credential_key_option("token", options)
self.token_credential_secret =
- Signet::OAuth1.extract_credential_secret_option(:token, options)
- self.callback = options[:callback]
- self.two_legged = options[:two_legged] || false
+ Signet::OAuth1.extract_credential_secret_option("token", options)
+ self.callback = options["callback"]
+ self.two_legged = options["two_legged"] || false
+ return self
end
##
# Returns the temporary credentials URI for this client.
#
@@ -125,12 +162,14 @@
#
# @param [Addressable::URI, String, #to_str] new_authorization_uri
# The authorization URI.
def authorization_uri=(new_authorization_uri)
if new_authorization_uri != nil
- new_authorization_uri =
- Addressable::URI.parse(new_authorization_uri)
+ new_authorization_uri = Addressable::URI.send(
+ new_authorization_uri.kind_of?(Hash) ? :new : :parse,
+ new_authorization_uri
+ )
@authorization_uri = new_authorization_uri
else
@authorization_uri = nil
end
end
@@ -145,16 +184,18 @@
alias_method :access_token_uri, :token_credential_uri
##
# Sets the token credential URI for this client.
#
- # @param [Addressable::URI, String, #to_str] new_token_credential_uri
+ # @param [Addressable::URI, Hash, String, #to_str] new_token_credential_uri
# The token credential URI.
def token_credential_uri=(new_token_credential_uri)
if new_token_credential_uri != nil
- new_token_credential_uri =
- Addressable::URI.parse(new_token_credential_uri)
+ new_token_credential_uri = Addressable::URI.send(
+ new_token_credential_uri.kind_of?(Hash) ? :new : :parse,
+ new_token_credential_uri
+ )
@token_credential_uri = new_token_credential_uri
else
@token_credential_uri = nil
end
end
@@ -508,10 +549,32 @@
@two_legged = new_two_legged
end
end
##
+ # Serialize the client object to JSON.
+ #
+ # @note A serialized client contains sensitive information. Persist or transmit with care.
+ #
+ # @return [String] A serialized JSON representation of the client.
+ def to_json
+ return MultiJson.dump({
+ 'temporary_credential_uri' => self.temporary_credential_uri,
+ 'authorization_uri' => self.authorization_uri,
+ 'token_credential_uri' => self.token_credential_uri,
+ 'callback' => self.callback,
+ 'two_legged' => self.two_legged,
+ 'client_credential_key' => self.client_credential_key,
+ 'client_credential_secret' => self.client_credential_secret,
+ 'temporary_credential_key' => self.temporary_credential_key,
+ 'temporary_credential_secret' => self.temporary_credential_secret,
+ 'token_credential_key' => self.token_credential_key,
+ 'token_credential_secret' => self.token_credential_secret
+ })
+ end
+
+ ##
# Generates a request for temporary credentials.
#
# @param [Hash] options
# The configuration parameters for the request.
# - <code>:signature_method</code> -
@@ -857,11 +920,11 @@
options = {
:signature_method => 'HMAC-SHA1',
:realm => nil,
:connection => Faraday.default_connection
}.merge(options)
-
+
if options[:request].kind_of?(Faraday::Request)
request = options[:request]
else
if options[:request].kind_of?(Array)
method, uri, headers, body = options[:request]
@@ -900,20 +963,20 @@
req.url(Addressable::URI.parse(uri).normalize.to_s)
req.headers = Faraday::Utils::Headers.new(headers)
req.body = body
end
end
-
+
parameters = ::Signet::OAuth1.unsigned_resource_parameters(
:client_credential_key => self.client_credential_key,
:token_credential_key => self.token_credential_key,
:signature_method => options[:signature_method],
:two_legged => self.two_legged
)
-
+
env = request.to_env(options[:connection])
-
+
content_type = request['Content-Type'].to_s
content_type = content_type.split(';', 2).first if content_type.index(';')
if request.method == :post && content_type == 'application/x-www-form-urlencoded'
# Serializes the body in case a hash/array was passed. Noop if already string like
encoder = Faraday::Request::UrlEncoded.new(lambda { |env| })
@@ -921,20 +984,20 @@
request.body = env[:body]
post_parameters = Addressable::URI.form_unencode(env[:body])
parameters = parameters.concat(post_parameters)
end
-
+
# No need to attach URI query parameters, the .sign_parameters
# method takes care of that automatically.
signature = ::Signet::OAuth1.sign_parameters(
env[:method],
env[:url],
parameters,
self.client_credential_secret,
self.token_credential_secret
)
-
+
parameters << ['oauth_signature', signature]
request['Authorization'] = ::Signet::OAuth1.generate_authorization_header(
parameters, options[:realm])
request['Cache-Control'] = 'no-store'
return request