Sha256: da8a04b44af213362033d9588ad4fcb0e6621d8ef99f61dce07618944072b9c2

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'rack'

class Ey::Hmac::Adapter::Rack < Ey::Hmac::Adapter
  def initialize(request, options)
    super
    @request = request.is_a?(Hash) ? ::Rack::Request.new(request) : request
  end

  def method
    request.request_method.to_s.upcase
  end

  def content_type
    request.content_type
  end

  def content_digest
    request.env['HTTP_CONTENT_DIGEST']
  end

  def set_content_digest
    if body
      request.env['HTTP_CONTENT_DIGEST'] = Digest::MD5.hexdigest(body)
    end
  end

  def body
    if request.env["rack.input"]
      request.env["rack.input"].rewind
      body = request.env["rack.input"].read
      request.env["rack.input"].rewind
      body == "" ? nil : body
    else nil
    end
  end

  def date
    request.env['HTTP_DATE']
  end

  def set_date
    request.env['HTTP_DATE'] = Time.now.httpdate
  end

  def path
    request.path
  end

  def sign!(key_id, key_secret)
    set_date
    set_content_digest

    if options[:version]
      request.env['HTTP_X_SIGNATURE_VERSION'] = options[:version]
    end

    request.env["HTTP_#{authorization_header.to_s.upcase}"] = authorization(key_id, key_secret)
  end

  def authorization_signature
    request.env["HTTP_#{authorization_header.to_s.upcase}"]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ey-hmac-2.3.0 lib/ey-hmac/adapter/rack.rb
ey-hmac-2.2.0 lib/ey-hmac/adapter/rack.rb