Module: Signet

Defined in:
lib/signet.rb,
lib/signet/errors.rb,
lib/signet/version.rb,
lib/signet/oauth_2.rb,
lib/signet/oauth_1.rb,
lib/signet/oauth_2/client.rb,
lib/signet/oauth_1/server.rb,
lib/signet/oauth_1/client.rb,
lib/signet/oauth_1/credential.rb,
lib/signet/oauth_1/signature_methods/rsa_sha1.rb,
lib/signet/oauth_1/signature_methods/hmac_sha1.rb,
lib/signet/oauth_1/signature_methods/plaintext.rb

Overview

:nodoc:

Defined Under Namespace

Modules: OAuth1, OAuth2, VERSION Classes: AuthorizationError, MalformedAuthorizationError, ParseError, UnsafeOperationError

Class Method Summary collapse

Class Method Details

.parse_auth_param_list(auth_param_string) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/signet.rb', line 18

def self.parse_auth_param_list(auth_param_string)
  # Production rules from:
  # http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-12
  token = /[-!#$\%&'*+.^_`|~0-9a-zA-Z]+/
  d_qdtext = /[\s\x21\x23-\x5B\x5D-\x7E\x80-\xFF]/n
  d_quoted_pair = /\\[\s\x21-\x7E\x80-\xFF]/n
  d_qs = /"(?:#{d_qdtext}|#{d_quoted_pair})*"/
  # Production rules that allow for more liberal parsing, i.e. single quotes
  s_qdtext = /[\s\x21-\x26\x28-\x5B\x5D-\x7E\x80-\xFF]/n
  s_quoted_pair = /\\[\s\x21-\x7E\x80-\xFF]/n
  s_qs = /'(?:#{s_qdtext}|#{s_quoted_pair})*'/
  # Combine the above production rules to find valid auth-param pairs.
  auth_param = /((?:#{token})\s*=\s*(?:#{d_qs}|#{s_qs}|#{token}))/
  auth_param_pairs = []
  last_match = nil
  remainder = auth_param_string
  # Iterate over the string, consuming pair matches as we go.  Verify that
  # pre-matches and post-matches contain only allowable characters.
  #
  # This would be way easier in Ruby 1.9, but we want backwards
  # compatibility.
  while (match = remainder.match(auth_param))
    if match.pre_match && match.pre_match !~ /^[\s,]*$/
      raise ParseError,
        "Unexpected auth param format: '#{auth_param_string}'."
    end
    auth_param_pairs << match.captures[0] # Appending pair
    remainder = match.post_match
    last_match = match
  end
  if last_match.post_match && last_match.post_match !~ /^[\s,]*$/
    raise ParseError,
      "Unexpected auth param format: '#{auth_param_string}'."
  end
  # Now parse the auth-param pair strings & turn them into key-value pairs.
  return (auth_param_pairs.inject([]) do |accu, pair|
    name, value = pair.split('=', 2)
    if value =~ /^".*"$/
      value = value.gsub(/^"(.*)"$/, '\1').gsub(/\\(.)/, '\1')
    elsif value =~ /^'.*'$/
      value = value.gsub(/^'(.*)'$/, '\1').gsub(/\\(.)/, '\1')
    elsif value =~ /[\(\)<>@,;:\\\"\/\[\]?={}]/
      # Certain special characters are not allowed
      raise ParseError, (
        "Unexpected characters in auth param " +
        "list: '#{auth_param_string}'."
      )
    end
    accu << [name, value]
    accu
  end)
end