Module: SimpleOAuth::Header::ClassMethods Private
- Included in:
- SimpleOAuth::Header
- Defined in:
- lib/simple_oauth/header/class_methods.rb,
sig/simple_oauth/header/class_methods.rbs
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Class methods for Header - parsing, defaults, and body hashing
Instance Method Summary collapse
-
#body_hash(body, algorithm = "SHA1") ⇒ String
private
Computes the oauth_body_hash for a request body.
-
#default_options(body = nil, signature_method = DEFAULT_SIGNATURE_METHOD) ⇒ Header::oauth_options
private
Returns default OAuth options with generated nonce and timestamp.
-
#form_encoded?(request) ⇒ Boolean
private
Checks whether a request carries a form-encoded body.
-
#form_pairs(form) ⇒ Array[[String, String]]
private
Parses a form-encoded query string or body into parameter pairs.
-
#from_request(request, oauth = {}) ⇒ Header
private
Parses OAuth parameters from a form-encoded POST body Builds a header for an HTTP request.
-
#generate_nonce ⇒ String
private
Generates a random nonce for OAuth requests.
-
#media_type(request) ⇒ String
private
Extracts the media type from a request, without its parameters.
-
#parse(header) ⇒ Header::oauth_options
private
Parses an OAuth Authorization header string into a hash.
-
#parse_form_body(body) ⇒ Hash
(also: #parse_query)
Parses OAuth parameters from a form-encoded POST body.
Instance Method Details
#body_hash(body, algorithm = "SHA1") ⇒ 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.
Computes the oauth_body_hash for a request body
40 41 42 |
# File 'lib/simple_oauth/header/class_methods.rb', line 40 def body_hash(body, algorithm = "SHA1") Signature.encode_base64(OpenSSL::Digest.digest(algorithm, body || "")) end |
#default_options(body = nil, signature_method = DEFAULT_SIGNATURE_METHOD) ⇒ Header::oauth_options
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.
Returns default OAuth options with generated nonce and timestamp
22 23 24 25 26 27 28 29 |
# File 'lib/simple_oauth/header/class_methods.rb', line 22 def (body = nil, signature_method = DEFAULT_SIGNATURE_METHOD) { nonce: generate_nonce, signature_method: signature_method, timestamp: Integer(Time.now).to_s, version: OAUTH_VERSION }.tap { |opts| opts[:body_hash] = body_hash(body, Signature.digest(signature_method)) if body } end |
#form_encoded?(request) ⇒ Boolean
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.
Checks whether a request carries a form-encoded body
147 148 149 |
# File 'lib/simple_oauth/header/class_methods.rb', line 147 def form_encoded?(request) media_type(request).eql?(FORM_CONTENT_TYPE) end |
#form_pairs(form) ⇒ Array[[String, 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 a form-encoded query string or body into parameter pairs
136 137 138 |
# File 'lib/simple_oauth/header/class_methods.rb', line 136 def form_pairs(form) URI.decode_www_form(form.to_s).reject { |key, value| key.empty? && value.empty? } end |
#from_request(request, oauth = {}) ⇒ Header
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 OAuth parameters from a form-encoded POST body Builds a header for an HTTP request
72 73 74 75 76 77 78 79 |
# File 'lib/simple_oauth/header/class_methods.rb', line 72 def from_request(request, oauth = {}) uri = request.uri || raise(ArgumentError, "The request has no URI") body = request.body return new(request.method, uri, form_pairs(body), oauth) if form_encoded?(request) no_params = {} #: Header::request_params new(request.method, uri, no_params, oauth, body) end |
#generate_nonce ⇒ 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.
Generates a random nonce for OAuth requests
167 168 169 |
# File 'lib/simple_oauth/header/class_methods.rb', line 167 def generate_nonce SecureRandom.hex end |
#media_type(request) ⇒ 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.
Extracts the media type from a request, without its parameters
159 160 161 |
# File 'lib/simple_oauth/header/class_methods.rb', line 159 def media_type(request) request["Content-Type"].to_s.split(";").first.to_s.strip.downcase end |
#parse(header) ⇒ Header::oauth_options
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 an OAuth Authorization header string into a hash
53 54 55 |
# File 'lib/simple_oauth/header/class_methods.rb', line 53 def parse(header) Parser.new(header).parse(PARSE_KEYS) end |
#parse_form_body(body) ⇒ Hash Also known as: parse_query
Parses OAuth parameters from a form-encoded POST body
OAuth 1.0 allows credentials to be transmitted in the request body for POST requests with Content-Type: application/x-www-form-urlencoded
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/simple_oauth/header/class_methods.rb', line 96 def parse_form_body(body) valid_keys = PARSE_KEYS.map(&:to_s) result = {} #: Hash[Symbol, String] form_pairs(body).each do |key, value| next unless key.start_with?(OAUTH_PREFIX) parsed_key = key.delete_prefix(OAUTH_PREFIX) next unless valid_keys.include?(parsed_key) raise ParseError, "Duplicate protocol parameter: #{key}" if result.key?(parsed_key.to_sym) result[parsed_key.to_sym] = value end result end |