Sha256: 12a4c3456357130fbe21bfd8346e3b605f526243fec014d47613892f778dac7b

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

require "forwardable"
require "akamai_ccu/secret"

module AkamaiCCU
  class Signer
    extend Forwardable

    POST = "POST"
    TAB = "\t"
    HEADER_NAME = "signature"
    HEADER_KEY = "Authorization"

    def_delegators :@request, :body, :request_body_permitted?, :path, :method
    def_delegators :@secret, :max_body, :auth_header, :signed_key

    attr_reader :request

    def initialize(request, secret, headers = [])
      @request = request
      @secret = secret
      @headers = Array(headers)
      @url = URI(path)
    end

    def call!
      @request[HEADER_KEY] = signed_headers
    end

    private def canonical_headers
      @headers.map do |header|
        next unless @request.key?(header)
        value = @request[header].strip.gsub(/\s+/, " ")
        "#{header.downcase}:#{value}"
      end.compact
    end

    private def body?
      body && request_body_permitted?
    end

    private def signed_body
      return "" unless body?
      truncated = body[0...max_body]
      @secret.class.sign(truncated)
    end

    private def signature_data
      @signature_data ||= [].tap do |data|
        data << method
        data << @url.scheme
        data << @request.fetch("host") { @url.host }
        data << @url.request_uri
        data << canonical_headers.join(TAB)
        data << signed_body
        data << auth_header
      end
    end

    private def signature
      @secret.class.sign_HMAC(signed_key, signature_data.join(TAB))
    end

    def signed_header
      "#{HEADER_NAME}=#{signature}"
    end

    private def signed_headers
      @signed_headers ||= auth_header << signed_header
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
akamai_ccu-1.6.0 lib/akamai_ccu/signer.rb
akamai_ccu-1.5.9 lib/akamai_ccu/signer.rb
akamai_ccu-1.5.8 lib/akamai_ccu/signer.rb
akamai_ccu-1.5.7 lib/akamai_ccu/signer.rb
akamai_ccu-1.5.6 lib/akamai_ccu/signer.rb
akamai_ccu-1.5.5 lib/akamai_ccu/signer.rb