<commit_before>require 'oauth/client'
require 'oauth/consumer'
require 'oauth/helper'
require 'oauth/token'
require 'oauth/version'
require 'oauth/signature/hmac/sha1'

module OAuth::Client
  class Helper
    include OAuth::Helper

    def initialize(request, options = {})
      @request = request
      @options = options
      @options[:signature_method] ||= 'HMAC-SHA1'
    end

    def options
      @options
    end

    def nonce
      options[:nonce] ||= generate_key
    end

    def timestamp
      options[:timestamp] ||= generate_timestamp
    end

    def oauth_parameters
      {
        'oauth_callback'         => options[:oauth_callback],
        'oauth_consumer_key'     => options[:consumer].key,
        'oauth_token'            => options[:token] ? options[:token].token : '',
        'oauth_signature_method' => options[:signature_method],
        'oauth_timestamp'        => timestamp,
        'oauth_nonce'            => nonce,
        'oauth_verifier'         => options[:oauth_verifier],
        'oauth_version'          => (options[:oauth_version] || '1.0')
      }.reject { |k,v| v.to_s == "" }
    end

    def signature(extra_options = {})
      OAuth::Signature.sign(@request, { :uri      => options[:request_uri],
                                        :consumer => options[:consumer],
                                        :token    => options[:token],
                                        :unsigned_parameters => options[:unsigned_parameters]
      }.merge(extra_options) )
    end

    def signature_base_string(extra_options = {})
      OAuth::Signature.signature_base_string(@request, { :uri        => options[:request_uri],
                                                         :consumer   => options[:consumer],
                                                         :token      => options[:token],
                                                         :parameters => oauth_parameters}.merge(extra_options) )
    end

    def amend_user_agent_header(headers)
      @oauth_ua_string ||= "OAuth gem v#{OAuth::VERSION}"
      if headers['User-Agent']
        headers['User-Agent'] += " (#{@oauth_ua_string})"
      else
        headers['User-Agent'] = @oauth_ua_string
      end
    end

    def header
      parameters = oauth_parameters
      parameters.merge!('oauth_signature' => signature(options.merge(:parameters => parameters)))

      header_params_str = parameters.map { |k,v| "#{k}=\"#{escape(v)}\"" }.join(', ')

      realm = "realm=\"#{options[:realm]}\", " if options[:realm]
      "OAuth #{realm}#{header_params_str}"
    end

    def parameters
      OAuth::RequestProxy.proxy(@request).parameters
    end

    def parameters_with_oauth
      oauth_parameters.merge(parameters)
    end
  end
end
<commit_msg>Have OAuth::Client::Helper sort its query params -- defends against ill-behaved/lazy oauth providers<commit_after>require 'oauth/client'
require 'oauth/consumer'
require 'oauth/helper'
require 'oauth/token'
require 'oauth/version'
require 'oauth/signature/hmac/sha1'

module OAuth::Client
  class Helper
    include OAuth::Helper

    def initialize(request, options = {})
      @request = request
      @options = options
      @options[:signature_method] ||= 'HMAC-SHA1'
    end

    def options
      @options
    end

    def nonce
      options[:nonce] ||= generate_key
    end

    def timestamp
      options[:timestamp] ||= generate_timestamp
    end

    def oauth_parameters
      {
        'oauth_callback'         => options[:oauth_callback],
        'oauth_consumer_key'     => options[:consumer].key,
        'oauth_token'            => options[:token] ? options[:token].token : '',
        'oauth_signature_method' => options[:signature_method],
        'oauth_timestamp'        => timestamp,
        'oauth_nonce'            => nonce,
        'oauth_verifier'         => options[:oauth_verifier],
        'oauth_version'          => (options[:oauth_version] || '1.0')
      }.reject { |k,v| v.to_s == "" }
    end

    def signature(extra_options = {})
      OAuth::Signature.sign(@request, { :uri      => options[:request_uri],
                                        :consumer => options[:consumer],
                                        :token    => options[:token],
                                        :unsigned_parameters => options[:unsigned_parameters]
      }.merge(extra_options) )
    end

    def signature_base_string(extra_options = {})
      OAuth::Signature.signature_base_string(@request, { :uri        => options[:request_uri],
                                                         :consumer   => options[:consumer],
                                                         :token      => options[:token],
                                                         :parameters => oauth_parameters}.merge(extra_options) )
    end

    def amend_user_agent_header(headers)
      @oauth_ua_string ||= "OAuth gem v#{OAuth::VERSION}"
      if headers['User-Agent']
        headers['User-Agent'] += " (#{@oauth_ua_string})"
      else
        headers['User-Agent'] = @oauth_ua_string
      end
    end

    def header
      parameters = oauth_parameters
      parameters.merge!('oauth_signature' => signature(options.merge(:parameters => parameters)))

      header_params_str = parameters.sort.map { |k,v| "#{k}=\"#{escape(v)}\"" }.join(', ')

      realm = "realm=\"#{options[:realm]}\", " if options[:realm]
      "OAuth #{realm}#{header_params_str}"
    end

    def parameters
      OAuth::RequestProxy.proxy(@request).parameters
    end

    def parameters_with_oauth
      oauth_parameters.merge(parameters)
    end
  end
end

{'fraction_non_alphanumeric': 0.12637362637362637, 'fraction_numerical': 0.0014179369018078695, 'mean_word_length': 2.0651819663226507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5294014', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1534, 'n_words': 280}