Class: SimpleOAuth::Parser
- Inherits:
-
Object
- Object
- SimpleOAuth::Parser
- 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
/(\w+)="([^"]*)"\s*(,?)\s*/- OAUTH_PREFIX =
OAuth scheme prefix pattern
/OAuth\s+/
Instance Attribute Summary collapse
-
#attributes ⇒ Hash{Symbol => String}
readonly
private
The parsed OAuth attributes.
-
#scanner ⇒ StringScanner
readonly
private
The StringScanner instance for parsing the header.
Instance Method Summary collapse
-
#initialize(header) ⇒ Parser
constructor
private
Creates a new Parser for the given header string.
-
#parse(valid_keys) ⇒ Hash{Symbol => String}
private
Parses the OAuth Authorization header.
-
#scan_oauth_prefix ⇒ void
private
Scans and validates the OAuth prefix.
-
#scan_params(valid_keys) ⇒ void
private
Scans all key-value parameters from the header.
-
#store_if_valid(key, value, valid_keys) ⇒ void
private
Stores the parameter if it's a valid OAuth key.
-
#validate_comma_separator(key, comma) ⇒ void
private
Validates that a comma separator exists between parameters.
-
#verify_complete ⇒ void
private
Verifies that the entire header was parsed.
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
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
#attributes ⇒ Hash{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
25 26 27 |
# File 'lib/simple_oauth/parser.rb', line 25 def attributes @attributes end |
#scanner ⇒ StringScanner (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
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
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_prefix ⇒ 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 and validates the OAuth prefix
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
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
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
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_complete ⇒ 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.
Verifies that the entire header was parsed
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 |