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
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
#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
23 24 25 |
# File 'lib/simple_oauth/parser.rb', line 23 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
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
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_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
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
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
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
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_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
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 |