Sha256: 7a7d9fc1bf903edb2bce464ba92b648a60773859e6a37d7cbbbfc40ce81a294e

Contents?: true

Size: 1.29 KB

Versions: 12

Compression:

Stored size: 1.29 KB

Contents

require "base64"
require "cgi"
require "openssl"

module Rapa
  class Signer
    # @param host [String]
    # @param http_method [String]
    # @param key [String]
    # @param path [String]
    # @param query_string [String]
    def initialize(host:, http_method:, key:, path:, query_string:)
      @host = host
      @http_method = http_method
      @key = key
      @path = path
      @query_string = query_string
    end

    # @return [String]
    def sign
      ::CGI.escape(
        ::Base64.encode64(
          ::OpenSSL::HMAC.digest(
            digest,
            key,
            source,
          ),
        ).chomp
      )
    end

    private

    # @private
    # @return [OpenSSL::Digest]
    def digest
      ::OpenSSL::Digest.new("sha256")
    end

    # @private
    # @return [String]
    def host
      @host
    end

    # @private
    # @return [String]
    def http_method
      @http_method
    end

    # @private
    # @return [String]
    def key
      @key
    end

    # @private
    # @return [String]
    def path
      @path
    end

    # @private
    # @return [String]
    def query_string
      @query_string
    end

    # @private
    # @return [String]
    def source
      [
        http_method,
        host,
        path,
        query_string,
      ].join("\n")
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rapa-0.6.0 lib/rapa/signer.rb
rapa-0.5.7 lib/rapa/signer.rb
rapa-0.5.6 lib/rapa/signer.rb
rapa-0.5.5 lib/rapa/signer.rb
rapa-0.5.4 lib/rapa/signer.rb
rapa-0.5.3 lib/rapa/signer.rb
rapa-0.5.2 lib/rapa/signer.rb
rapa-0.5.1 lib/rapa/signer.rb
rapa-0.5.0 lib/rapa/signer.rb
rapa-0.4.3 lib/rapa/signer.rb
rapa-0.4.2 lib/rapa/signer.rb
rapa-0.4.1 lib/rapa/signer.rb