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



29
30
31
32
# File 'lib/simple_oauth/parser.rb', line 29

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



23
24
25
# File 'lib/simple_oauth/parser.rb', line 23

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:



18
19
20
# File 'lib/simple_oauth/parser.rb', line 18

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:



39
40
41
42
43
44
# File 'lib/simple_oauth/parser.rb', line 39

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:



52
53
54
55
56
# File 'lib/simple_oauth/parser.rb', line 52

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



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

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



91
92
93
94
# File 'lib/simple_oauth/parser.rb', line 91

def store_if_valid(key, value, valid_keys)
  parsed_key = valid_keys.find { |k| "oauth_#{k}".eql?(key) }
  attributes[parsed_key] = Header.unescape(value) if parsed_key
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:



78
79
80
81
82
83
# File 'lib/simple_oauth/parser.rb', line 78

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:



100
101
102
103
104
105
# File 'lib/simple_oauth/parser.rb', line 100

def verify_complete
  return if scanner.eos?

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