Module: OAuth::Helper

### Home Classes Methods

### In Files

- lib/oauth/helper.rb

### Methods

- #escape
- #generate_key
- #generate_nonce
- #normalize
- #normalize_nested_query
- #parse_header
- #stringify_keys
- #unescape

### Files

- LICENSE
- README.rdoc
- TODO

### Class/Module Index

Quicksearch

- OAuth
- OAuth::AccessToken
- OAuth::CLI
- OAuth::Client
- OAuth::Client::Helper
- OAuth::Consumer
- OAuth::ConsumerToken
- OAuth::Error
- OAuth::Helper
- OAuth::OAuthTestHelper
- OAuth::Problem
- OAuth::RequestProxy
- OAuth::RequestProxy::ActionControllerRequest
- OAuth::RequestProxy::Base
- OAuth::RequestProxy::Curl
- OAuth::RequestProxy::Curl::Easy
- OAuth::RequestProxy::EventMachine
- OAuth::RequestProxy::EventMachine::HttpRequest
- OAuth::RequestProxy::JabberRequest
- OAuth::RequestProxy::MockRequest
- OAuth::RequestProxy::Net
- OAuth::RequestProxy::Net::HTTP
- OAuth::RequestProxy::Net::HTTP::HTTPRequest
- OAuth::RequestProxy::RackRequest
- OAuth::RequestProxy::Typhoeus
- OAuth::RequestProxy::Typhoeus::Request
- OAuth::RequestProxy::UnknownRequestType
- OAuth::RequestToken
- OAuth::Server
- OAuth::ServerToken
- OAuth::Signature
- OAuth::Signature::Base
- OAuth::Signature::HMAC
- OAuth::Signature::HMAC::Base
- OAuth::Signature::HMAC::MD5
- OAuth::Signature::HMAC::RMD160
- OAuth::Signature::HMAC::SHA1
- OAuth::Signature::HMAC::SHA2
- OAuth::Signature::MD5
- OAuth::Signature::PLAINTEXT
- OAuth::Signature::RSA
- OAuth::Signature::RSA::SHA1
- OAuth::Signature::SHA1
- OAuth::Signature::UnknownSignatureMethod
- OAuth::Token
- OAuth::Unauthorized
- ActionController
- ActionController::Base
- ActionController::TestRequest
- Digest
- Digest::HMAC
- EventMachine
- EventMachine::HttpClient
- Net
- Net::HTTPGenericRequest
- Object
- String

No matching classes.

# OAuth::Helper

### Public Instance Methods

escape(value) click to toggle source

Escape `value` by URL encoding all non-reserved character.

See Also: OAuth core spec version 1.0, section 5.1

    # File lib/oauth/helper.rb, line 11
    def escape(value)
      URI::escape(value.to_s, OAuth::RESERVED_CHARACTERS)
    rescue ArgumentError
      URI::escape(value.to_s.force_encoding(Encoding::UTF_8), OAuth::RESERVED_CHARACTERS)
    end

generate_key(size=32) click to toggle source

Generate a random key of up to `size` bytes. The value returned is Base64 encoded with non-word characters removed.

    # File lib/oauth/helper.rb, line 19
    def generate_key(size=32)
      Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
    end

Also aliased as: generate_nonce
generate_nonce(size=32) click to toggle source
Alias for: generate_key
normalize(params) click to toggle source

Normalize a `Hash` of parameter values. Parameters are sorted by name, using lexicographical byte value ordering. If two or more parameters share the same name, they are sorted by their value. Parameters are concatenated in their sorted order into a single string. For each parameter, the name is separated from the corresponding value by an "=" character, even if the value is empty. Each name-value pair is separated by an "&" character.

See Also: OAuth core spec version 1.0, section 9.1.1

    # File lib/oauth/helper.rb, line 36
    def normalize(params)
      params.sort.map do |k, values|
        if values.is_a?(Array)
          # make sure the array has an element so we don't lose the key
          values << nil if values.empty?
          # multiple values were provided for a single key
          values.sort.collect do |v|
            [escape(k),escape(v)] * "="
          end
        elsif values.is_a?(Hash)
          normalize_nested_query(values, k)
        else
          [escape(k),escape(values)] * "="
        end
      end * "&"
    end

normalize_nested_query(value, prefix = nil) click to toggle source

Returns a string representation of the Hash like in URL query string

    build_nested_query({:level_1 => {:level_2 => ['value_1','value_2']}}, 'prefix'))
      #=> ["prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_1", "prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_2"]

    # File lib/oauth/helper.rb, line 56
    def normalize_nested_query(value, prefix = nil)
      case value
      when Array
        value.map do |v|
          normalize_nested_query(v, "#{prefix}[]")
        end.flatten.sort
      when Hash
        value.map do |k, v|
          normalize_nested_query(v, prefix ? "#{prefix}[#{k}]" : k)
        end.flatten.sort
      else
        [escape(prefix), escape(value)] * "="
      end
    end

parse_header(header) click to toggle source

Parse an Authorization / WWW-Authenticate header into a hash. Takes care of unescaping and removing surrounding quotes. Raises a OAuth::Problem if the header is not parsable into a valid hash. Does not validate the keys or values.

    hash = parse_header(headers['Authorization'] || headers['WWW-Authenticate'])
    hash['oauth_timestamp']
      #=>"1234567890"

    # File lib/oauth/helper.rb, line 79
    def parse_header(header)
      # decompose
      params = header[6,header.length].split(/[,=&]/)

      # odd number of arguments - must be a malformed header.
      raise OAuth::Problem.new("Invalid authorization header") if params.size % 2 != 0

      params.map! do |v|
        # strip and unescape
        val = unescape(v.strip)
        # strip quotes
        val.sub(/^\"(.*)\"$/, '\1')
      end

      # convert into a Hash
      Hash[*params.flatten]
    end

stringify_keys(hash) click to toggle source

    # File lib/oauth/helper.rb, line 101
    def stringify_keys(hash)
      new_h = {}
      hash.each do |k, v|
        new_h[k.to_s] = v.is_a?(Hash) ? stringify_keys(v) : v
      end
      new_h
    end

unescape(value) click to toggle source

    # File lib/oauth/helper.rb, line 97
    def unescape(value)
      URI.unescape(value.gsub('+', '%2B'))
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.