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
11:     def escape(value)
12:       URI::escape(value.to_s, OAuth::RESERVED_CHARACTERS)
13:     rescue ArgumentError
14:       URI::escape(value.to_s.force_encoding(Encoding::UTF_8), OAuth::RESERVED_CHARACTERS)
15:     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
19:     def generate_key(size=32)
20:       Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
21:     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
36:     def normalize(params)
37:       params.sort.map do |k, values|
38: 
39:         if values.is_a?(Array)
40:           # multiple values were provided for a single key
41:           values.sort.collect do |v|
42:             [escape(k),escape(v)] * "="
43:           end
44:         else
45:           [escape(k),escape(values)] * "="
46:         end
47:       end * "&"
48:     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 58
58:     def parse_header(header)
59:       # decompose
60:       params = header[6,header.length].split(/[,=]/)
61: 
62:       # odd number of arguments - must be a malformed header.
63:       raise OAuth::Problem.new("Invalid authorization header") if params.size % 2 != 0
64: 
65:       params.map! do |v|
66:         # strip and unescape
67:         val = unescape(v.strip)
68:         # strip quotes
69:         val.sub(/^\"(.*)\"$/, '\1')
70:       end
71: 
72:       # convert into a Hash
73:       Hash[*params.flatten]
74:     end
stringify_keys(hash) click to toggle source
    # File lib/oauth/helper.rb, line 80
80:     def stringify_keys(hash)
81:       new_h = {}
82:       hash.each do |k, v| 
83:         new_h[k.to_s] = v.is_a?(Hash) ? stringify_keys(v) : v
84:       end
85:       new_h
86:     end
unescape(value) click to toggle source
    # File lib/oauth/helper.rb, line 76
76:     def unescape(value)
77:       URI.unescape(value.gsub('+', '%2B'))
78:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.