class Trusona::Api::SignedRequest

A signed request that can be used to make HMAC'd API calls to Trusona

Attributes

body[R]
uri[R]

Public Class Methods

new(path, body, method, host) click to toggle source
# File lib/trusona/api/signed_request.rb, line 10
def initialize(path, body, method, host)
  @host = host
  @uri  = build_uri(path, body)
  @body = parse_body(body)
  @path = build_path(path)
  @method = method
  @headers = { 'x-date' => nil, 'Authorization' => nil }
  @date = Time.now.httpdate
  validate
  @signature = sign
end

Public Instance Methods

headers() click to toggle source
# File lib/trusona/api/signed_request.rb, line 22
def headers
  @headers.merge(
    'x-date' => @date,
    'Date' => @date,
    'X-Date' => @date,
    'Authorization' => @signature,
    'Content-Type' => determine_content_type
  )
end

Private Instance Methods

build_path(path) click to toggle source
# File lib/trusona/api/signed_request.rb, line 40
def build_path(path)
  return path if @uri.query.nil? || @uri.query.empty?

  [@uri.path, @uri.query].join('?')
end
build_uri(path, body) click to toggle source
# File lib/trusona/api/signed_request.rb, line 46
def build_uri(path, body)
  return build_uri_with_query(URI(path)) if URI(path).query
  return build_uri_with_body_as_query(path, body) if valid_hash_body(body)

  URI::HTTPS.build(host: @host, path: path)
end
build_uri_with_body_as_query(path, body) click to toggle source
# File lib/trusona/api/signed_request.rb, line 83
def build_uri_with_body_as_query(path, body)
  URI::HTTPS.build(
    host: @host,
    path: path,
    query: URI.encode_www_form(body)
  )
end
build_uri_with_query(generic) click to toggle source
# File lib/trusona/api/signed_request.rb, line 75
def build_uri_with_query(generic)
  URI::HTTPS.build(
    host: @host,
    path: generic.path,
    query: generic.query
  )
end
determine_content_type() click to toggle source
# File lib/trusona/api/signed_request.rb, line 34
def determine_content_type
  return '' if @method == 'GET' || @method == 'DELETE'

  Trusona::Api::HTTPClient::CONTENT_TYPE
end
parse_body(body) click to toggle source
# File lib/trusona/api/signed_request.rb, line 57
def parse_body(body)
  body.is_a?(Hash) ? '' : body
end
sign() click to toggle source
# File lib/trusona/api/signed_request.rb, line 61
def sign
  message = Trusona::Api::HashedMessage.new(
    body: @body,
    content_type: determine_content_type,
    path: @path,
    method: @method,
    date: @date
  )

  message.auth_header
rescue ArgumentError
  raise Trusona::SigningError
end
valid_hash_body(body) click to toggle source
# File lib/trusona/api/signed_request.rb, line 53
def valid_hash_body(body)
  body.is_a?(Hash) && !body.empty?
end
validate() click to toggle source
# File lib/trusona/api/signed_request.rb, line 91
def validate
  raise ArgumentError unless @path
  raise ArgumentError unless @body
  raise ArgumentError unless @method
  raise ArgumentError unless @host
end