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

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

Parameters:

  • body (String, nil)
  • algorithm (String) (defaults to: "SHA1")

Returns:

  • (String)


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

Parameters:

  • body (String, nil) (defaults to: nil)
  • signature_method (String) (defaults to: DEFAULT_SIGNATURE_METHOD)

Returns:

  • (Header::oauth_options)


22
23
24
25
26
27
28
29
# File 'lib/simple_oauth/header/class_methods.rb', line 22

def default_options(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

Parameters:

Returns:

  • (Boolean)


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

Parameters:

  • form (String, _ToS, nil)

Returns:

  • (Array[[String, String]])


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

Parameters:

  • request (Header::_Request)
  • oauth (Header::oauth_options, String) (defaults to: {})

Returns:



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_nonceString

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

Returns:

  • (String)


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

Parameters:

Returns:

  • (String)


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

Parameters:

  • header (String, _ToS)

Returns:

  • (Header::oauth_options)


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

Examples:

SimpleOAuth::Header.parse_form_body('oauth_consumer_key=key&oauth_signature=sig&status=hello')
# => {consumer_key: "key", signature: "sig"}

Parse the credentials from a query string

SimpleOAuth::Header.parse_query('oauth_consumer_key=key&status=hello')
# => {consumer_key: "key"}

Parameters:

  • body (String, #to_s)

    the form-encoded request body

Returns:

  • (Hash)

    parsed OAuth attributes with symbol keys (only valid OAuth keys)

Raises:



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