module ActiveResourceOAuthClient

Monkey-patch ActiveResource to allow us to merge our OAuth headers in. Portions of the below are taken from Active Resource which is MIT licensed; hence this whole file is being licensed under the MIT License to err on the side of safety.

Public Instance Methods

default_header() click to toggle source
# File lib/deadwood/active_resource_oauth_client.rb, line 74
def default_header
  @default_header ||= {}
  config = Deadwood::Katello::Base.config || {}
  @default_header = {'HTTP_KATELLO_USER' => config[:katello_user]}
end
request_with_oauth(method, path, *arguments) click to toggle source
# File lib/deadwood/active_resource_oauth_client.rb, line 29
def request_with_oauth(method, path, *arguments)
  @oauth_config = Deadwood::Katello::Base.config || {}
  # Take care to fall back to the standard request method if we don't have full OAuth credentials
  unless use_oauth_for_url?("#{site.scheme}://#{site.host}:#{site.port}#{path}")
    return request_without_oauth(method, path, *arguments)
  end

  result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
    payload[:method] = method
    payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
    oauth_consumer = OAuth::Consumer.new(
      @oauth_config[:consumer_key],
      @oauth_config[:consumer_secret],
      :site => "#{site.scheme}://#{site.host}:#{site.port}"  )
    token = OAuth::AccessToken.new(oauth_consumer)
    # Katello objects must not include the id attribute on updates
    if method.to_s.include?('put')
      obj = JSON.parse(arguments.first)
      obj[obj.keys.first].delete('id')
      arguments[0] = obj.to_json
    end
    base_request = oauth_consumer.create_signed_request(method, path, token, {}, *arguments)
    payload[:result] = http.request(base_request)
  end
  # Error-handling code from OAuth
  # http://wiki.oauth.net/w/page/12238543/ProblemReporting
  auth_header = result.to_hash['www-authenticate']
  problem_header = auth_header ? auth_header.select{|h| h =~ /^OAuth /}.select{|h| h =~ /oauth_problem/}.first : nil
  if auth_header && problem_header
    params = OAuth::Helper.parse_header(problem_header)
    raise OAuth::Problem.new(params.delete("oauth_problem"), result, params)
  end
  # Error-handling code from ActiveResource
  handle_response(result)
  rescue Timeout::Error => e
    raise TimeoutError.new(e.message)
  rescue OpenSSL::SSL::SSLError => e
    raise SSLError.new(e.message)
end
use_oauth_for_url?(url) click to toggle source
# File lib/deadwood/active_resource_oauth_client.rb, line 69
def use_oauth_for_url?(url)
  Deadwood::Katello::Base.use_oauth? and
    url.include?(Deadwood::Katello::Base.config[:site])
end