class Trusona::Api::HashedMessage

A HMAC message suitable for authorization to the Trusona API

Public Class Methods

new(params = {}) click to toggle source
# File lib/trusona/api/hashed_message.rb, line 10
def initialize(params = {})
  validate(params)

  @method       = params[:method]
  @body         = params[:body]
  @content_type = params[:content_type]
  @path         = params[:path]
  @date         = params[:date]
  @secret       = Trusona.config.secret
  @token        = Trusona.config.token
end

Public Instance Methods

auth_header() click to toggle source
# File lib/trusona/api/hashed_message.rb, line 22
def auth_header
  "TRUSONA #{@token}:#{signature}"
end
signature() click to toggle source
# File lib/trusona/api/hashed_message.rb, line 26
def signature
  Base64.strict_encode64(
    OpenSSL::HMAC.hexdigest(
      OpenSSL::Digest::SHA256.new, @secret, prepare_data
    )
  )
end

Private Instance Methods

body_digest() click to toggle source
# File lib/trusona/api/hashed_message.rb, line 36
def body_digest
  digestable_body = ''
  digestable_body = @body unless @body.nil? || @body.empty?

  OpenSSL::Digest::MD5.new.hexdigest(digestable_body)
end
invalid_method?(method) click to toggle source
# File lib/trusona/api/hashed_message.rb, line 47
def invalid_method?(method)
  http_methods = %w[GET POST DELETE PATCH PUT]
  return true if invalid_param?(method)
  return true unless http_methods.include?(method.strip.upcase)
end
invalid_param?(param) click to toggle source
# File lib/trusona/api/hashed_message.rb, line 43
def invalid_param?(param)
  param.nil?
end
prepare_data() click to toggle source
# File lib/trusona/api/hashed_message.rb, line 53
def prepare_data
  data = [
    @method.to_s,
    body_digest,
    @content_type,
    @date,
    @path
  ]

  data.join("\n")
end
validate(params) click to toggle source
# File lib/trusona/api/hashed_message.rb, line 65
def validate(params)
  raise ArgumentError if invalid_method?(params[:method])
  raise ArgumentError if invalid_param?(params[:path])
end