Sha256: 03f0cc88335d530c3df7baa5b2c194bf7b8db8a55761c39ad1745ee36096ed74

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module Trupanion
  class AuthorizationHeaders
    def initialize(client, request)
      @client = client
      @request = request
    end

    def call
      return default_headers unless request.body

      default_headers.merge(
        "Content-Md5" => md5,
      )
    end

    private

    attr_reader :request, :client

    def default_headers
      @_default_headers ||= {
        "TimeStamp" => timestamp,
        "Content-Type" => "application/json",
        "Content-Length" => "0",
        "Authorization" => authorization_header,
      }
    end

    def md5
      return unless request.body
      @_md5 ||= Digest::MD5.base64digest(request.body.to_json)
    end

    def timestamp
      @_timestamp ||= Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT")
    end

    def http_verb
      @_http_verb ||= request.method.to_s.upcase
    end

    def authorization_header
      "TruID #{client.api_key}:#{authorization_hash}"
    end

    def authorization_hash
      Base64.strict_encode64(
        OpenSSL::HMAC.digest(
          "sha256", client.api_secret, authorization_message
        ),
      )
    end

    def authorization_message
      "#{http_verb}\n#{md5}\napplication/json\n#{timestamp}\n#{request.path}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trupanion-0.1.2 lib/trupanion/authentication_headers.rb
trupanion-0.1.1 lib/trupanion/authentication_headers.rb
trupanion-0.1.0 lib/trupanion/authentication_headers.rb