Sha256: 727bc48af9df895ce86deec789e8989b61c8a0013142be66d62e67fc8d77b916

Contents?: true

Size: 1.92 KB

Versions: 7

Compression:

Stored size: 1.92 KB

Contents

require 'date'
require 'openssl'

module Hubspot
  module Helpers
    class Signature
      MAX_ALLOWED_TIMESTAMP = 3000
      def is_valid(
        signature: String,
        client_secret: String,
        request_body: String,
        http_uri: nil,
        http_method: 'POST',
        signature_version: 'v2',
        timestamp: nil
      )
        if signature_version == "v3"
          current_time = DateTime.now.strftime("%s").to_i
          if current_time - timestamp.to_i > MAX_ALLOWED_TIMESTAMP
            raise StandardError("Timestamp is invalid, reject request.")
          end
        end
        hashed_signature = get_signature(
          client_secret: client_secret,
          request_body: request_body,
          signature_version: signature_version,
          http_uri: http_uri,
          http_method: http_method,
          timestamp: timestamp
        )

        signature == hashed_signature
      end

      def get_signature(
        client_secret: String,
        request_body: String,
        signature_version: String,
        http_uri: nil,
        http_method: "POST",
        timestamp: nil
      )
        case signature_version
          when "v1"
            source_string = "#{client_secret}#{request_body}"
            hash_result = Digest::SHA2.hexdigest(source_string.encode('utf-8'))
            return hash_result
          when "v2"
            source_string =  "#{client_secret}#{http_method}#{http_uri}#{request_body}"
            hash_result = Digest::SHA2.hexdigest(source_string.encode('utf-8'))
            return hash_result
          when "v3"
            source_string = "#{http_method}#{http_uri}#{request_body}#{timestamp}"
            hash_result =  OpenSSL::HMAC.base64digest('SHA256', client_secret, source_string.encode('utf-8'))
            return hash_result
           else
             raise StandardError("Not supported signature version: #{signature_version}")
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hubspot-api-client-17.0.0.pre.beta.3 lib/hubspot/helpers/signature.rb
hubspot-api-client-17.0.0.pre.beta.2 lib/hubspot/helpers/signature.rb
hubspot-api-client-17.0.0.pre.beta.1 lib/hubspot/helpers/signature.rb
hubspot-api-client-16.4.0 lib/hubspot/helpers/signature.rb
hubspot-api-client-16.3.0 lib/hubspot/helpers/signature.rb
hubspot-api-client-16.2.1 lib/hubspot/helpers/signature.rb
hubspot-api-client-16.2.0 lib/hubspot/helpers/signature.rb