Sha256: 53fc69aaf7bd8db1a2a1f102beea64cdf6f3566fb76ac757a4ae20b0c87f9739

Contents?: true

Size: 1.75 KB

Versions: 28

Compression:

Stored size: 1.75 KB

Contents

require 'rest-core'

module RestCore::ClientOauth1
  include RestCore

  def authorize_url! opts={}
    self.data = ParseQuery.parse_query(
      post(request_token_path, {}, {}, {:json_response => false}.merge(opts)))

    authorize_url
  end

  def authorize_url
    url(authorize_path, :oauth_token => oauth_token)
  end

  def authorize! opts={}
    self.data = ParseQuery.parse_query(
      post(access_token_path, {}, {}, {:json_response => false}.merge(opts)))

    data['authorized'] = 'true'
    data
  end

  def authorized?
    !!(oauth_token && oauth_token_secret && data['authorized'])
  end

  def data_json
    Json.encode(data.merge('sig' => calculate_sig))
  end

  def data_json= json
    self.data = check_sig_and_return_data(Json.decode(json))
  rescue Json.const_get(:ParseError)
    self.data = nil
  end

  def oauth_token
    data['oauth_token'] if data.kind_of?(Hash)
  end
  def oauth_token= token
    data['oauth_token'] = token if data.kind_of?(Hash)
  end
  def oauth_token_secret
    data['oauth_token_secret'] if data.kind_of?(Hash)
  end
  def oauth_token_secret= secret
    data['oauth_token_secret'] = secret if data.kind_of?(Hash)
  end
  def oauth_callback
    data['oauth_callback'] if data.kind_of?(Hash)
  end
  def oauth_callback= uri
    data['oauth_callback'] = uri if data.kind_of?(Hash)
  end

  private
  def default_data
    {}
  end

  def check_sig_and_return_data hash
    hash if consumer_secret && hash.kind_of?(Hash) &&
            calculate_sig(hash) == hash['sig']
  end

  def calculate_sig hash=data
    base = hash.reject{ |(k, _)| k == 'sig' }.sort.map{ |(k, v)|
      "#{Middleware.escape(k.to_s)}=#{Middleware.escape(v.to_s)}"
    }.join('&')
    Digest::MD5.hexdigest("#{Middleware.escape(consumer_secret)}&#{base}")
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
rest-core-3.6.0 lib/rest-core/client_oauth1.rb
rest-core-3.5.92 lib/rest-core/client_oauth1.rb
rest-core-3.5.91 lib/rest-core/client_oauth1.rb
rest-core-3.5.9 lib/rest-core/client_oauth1.rb
rest-core-3.5.8 lib/rest-core/client_oauth1.rb
rest-core-3.5.7 lib/rest-core/client_oauth1.rb
rest-core-3.5.6 lib/rest-core/client_oauth1.rb
rest-core-3.5.5 lib/rest-core/client_oauth1.rb
rest-core-3.5.4 lib/rest-core/client_oauth1.rb
rest-core-3.5.3 lib/rest-core/client_oauth1.rb
rest-core-3.5.2 lib/rest-core/client_oauth1.rb
rest-core-3.5.1 lib/rest-core/client_oauth1.rb
rest-core-3.5.0 lib/rest-core/client_oauth1.rb
rest-core-3.4.1 lib/rest-core/client_oauth1.rb
rest-core-3.4.0 lib/rest-core/client_oauth1.rb
rest-core-3.3.3 lib/rest-core/client_oauth1.rb
rest-core-3.3.2 lib/rest-core/client_oauth1.rb
rest-core-3.3.1 lib/rest-core/client_oauth1.rb
rest-core-3.3.0 lib/rest-core/client_oauth1.rb
rest-core-3.2.0 lib/rest-core/client_oauth1.rb