Class: SimpleOAuth::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_oauth/parser.rb,
sig/simple_oauth/parser.rbs

Overview

Parses OAuth Authorization headers

Constant Summary collapse

PARAM_PATTERN =

Pattern to match OAuth key-value pairs

Returns:

  • (Regexp)
/(\w+)="([^"]*)"\s*(,?)\s*/
OAUTH_PREFIX =

OAuth scheme prefix pattern

Returns:

  • (Regexp)
/OAuth\s+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new Parser for the given header string

Parameters:

  • header (String, #to_s)

    the OAuth Authorization header string



31
32
33
34
# File 'lib/simple_oauth/parser.rb', line 31

def initialize(header)
  @scanner = StringScanner.new(header.to_s)
  @attributes = {}
end

Instance Attribute Details

#attributesHash{Symbol => String} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parsed OAuth attributes

Returns:

  • (Hash{Symbol => String})

    the parsed attributes



25
26
27
# File 'lib/simple_oauth/parser.rb', line 25

def attributes
  @attributes
end

#scannerStringScanner (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The StringScanner instance for parsing the header

Returns:



20
21
22
# File 'lib/simple_oauth/parser.rb', line 20

def scanner
  @scanner
end

Instance Method Details

#parse(valid_keys) ⇒ Hash{Symbol => String}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses the OAuth Authorization header

Parameters:

  • valid_keys (Array<Symbol>)

    the valid OAuth parameter keys

Returns:

  • (Hash{Symbol => String})

    the parsed attributes

Raises:



41
42
43
44
45
46
# File 'lib/simple_oauth/parser.rb', line 41

def parse(valid_keys)
  scan_oauth_prefix
  scan_params(valid_keys)
  verify_complete
  attributes
end

#scan_oauth_prefixvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Scans and validates the OAuth prefix

Raises:



54
55
56
57
58
# File 'lib/simple_oauth/parser.rb', line 54

def scan_oauth_prefix
  return if scanner.scan(OAUTH_PREFIX)

  raise ParseError, "Authorization header must start with 'OAuth '"
end

#scan_params(valid_keys) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Scans all key-value parameters from the header

Parameters:

  • valid_keys (Array<Symbol>)

    the valid OAuth parameter keys



64
65
66
67
68
69
70
71
72
# File 'lib/simple_oauth/parser.rb', line 64

def scan_params(valid_keys)
  while scanner.scan(PARAM_PATTERN)
    key = scanner[1] #: String
    value = scanner[2] #: String
    comma = scanner[3] #: String
    validate_comma_separator(key, comma)
    store_if_valid(key, value, valid_keys)
  end
end

#store_if_valid(key, value, valid_keys) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Stores the parameter if it's a valid OAuth key

Parameters:

  • key (String)

    the raw parameter key (e.g., "oauth_consumer_key")

  • value (String)

    the parameter value

  • valid_keys (Array<Symbol>)

    the valid OAuth parameter keys

Raises:



94
95
96
97
98
99
100
# File 'lib/simple_oauth/parser.rb', line 94

def store_if_valid(key, value, valid_keys)
  parsed_key = valid_keys.find { |k| "oauth_#{k}".eql?(key) }
  return if parsed_key.nil?
  raise ParseError, "Duplicate protocol parameter: #{key}" if attributes.key?(parsed_key)

  attributes[parsed_key] = Header.unescape(value)
end

#validate_comma_separator(key, comma) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validates that a comma separator exists between parameters

Parameters:

  • key (String)

    the parameter key for error messages

  • comma (String)

    the comma separator (empty string if missing)

Raises:



80
81
82
83
84
85
# File 'lib/simple_oauth/parser.rb', line 80

def validate_comma_separator(key, comma)
  return if !comma.empty? || scanner.eos?

  raise ParseError,
    "Expected comma after '#{key}' parameter at position #{scanner.pos}: #{scanner.rest.inspect}"
end

#verify_completevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Verifies that the entire header was parsed

Raises:



106
107
108
109
110
111
# File 'lib/simple_oauth/parser.rb', line 106

def verify_complete
  return if scanner.eos?

  raise ParseError,
    "Could not parse parameter at position #{scanner.pos}: #{scanner.rest.inspect}"
end