Sha256: 6573aefe2226e025139ea5f32bf7ef88de5cb7535b83353139adbf9e74b0c721

Contents?: true

Size: 1.82 KB

Versions: 19

Compression:

Stored size: 1.82 KB

Contents

# Toplevel Pubnub module.
module Pubnub
  # Event class lib/pubnub/event.rb
  class Event
    # Module that holds signature computing code
    module Signature
      private

      def super_admin_signature(http_method, body)
        return unless @app.env[:secret_key]

        # Handle special case with publish operation signing when message sent as POST body.
        http_method = if http_method.upcase == "POST" && current_operation == Pubnub::Constants::OPERATION_PUBLISH
                        "GET"
                      else
                        http_method.upcase
                      end

        message = [
          http_method.upcase,
          @app.env[:publish_key],
          path,
          variables_for_signature.gsub(/[!~'()*]/) { |char| '%' + char.ord.to_s(16).upcase }, # Replace ! ~ * ' ( )
        ]

        if %w[POST PATCH].include?(http_method)
          message.push(body)
        else
          message.push('')
        end

        signature = URI.encode_www_form_component(Base64.encode64(
          OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'),
                               @app.env[:secret_key].to_s, message.join("\n"))
        ).strip.gsub(/\+/, '-').gsub(/\//, '_').gsub(/=+$/, ''))

        "v2.#{signature}"
      end

      def variables_for_signature
        unsorted_params = parameters(true)
        sorted_keys = unsorted_params.sort_by {|k, v| k.to_s}
        sorted_keys.map do |k, v|
          if %w[meta ortt].include?(k.to_s)
            encoded_value = URI.encode_www_form_component(v.to_json).gsub('+', '%20')
            "#{k}=#{encoded_value}"
          elsif %w[t state filter-expr sort filter].include?(k.to_s)
            "#{k}=#{v}"
          else
            "#{k}=#{URI.encode_www_form_component(v.to_s).gsub('+', '%20')}"
          end
        end.join('&')
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
pubnub-5.3.3 lib/pubnub/event/signature.rb
pubnub-5.3.2 lib/pubnub/event/signature.rb
pubnub-5.3.1 lib/pubnub/event/signature.rb
pubnub-5.3.0 lib/pubnub/event/signature.rb
pubnub-5.2.2 lib/pubnub/event/signature.rb
pubnub-5.2.1 lib/pubnub/event/signature.rb
pubnub-5.2.0 lib/pubnub/event/signature.rb
pubnub-5.1.2 lib/pubnub/event/signature.rb
pubnub-5.1.1 lib/pubnub/event/signature.rb
pubnub-5.1.0 lib/pubnub/event/signature.rb
pubnub-5.0.0 lib/pubnub/event/signature.rb
pubnub-4.8.0 lib/pubnub/event/signature.rb
pubnub-4.7.1 lib/pubnub/event/signature.rb
pubnub-4.7.0 lib/pubnub/event/signature.rb
pubnub-4.6.2 lib/pubnub/event/signature.rb
pubnub-4.6.1 lib/pubnub/event/signature.rb
pubnub-4.6.0 lib/pubnub/event/signature.rb
pubnub-4.5.0 lib/pubnub/event/signature.rb
pubnub-4.4.0 lib/pubnub/event/signature.rb