Sha256: 42f5e86bd473f494a5928bbf7748db3f489148c174a4f1c61b4c89e2dc86f7f9

Contents?: true

Size: 1.06 KB

Versions: 48

Compression:

Stored size: 1.06 KB

Contents

module Twilio
  module Util
    class RequestValidator

      def initialize(auth_token = nil)
        @auth_token = auth_token || Twilio.auth_token
        raise ArgumentError, 'Auth token is required' if @auth_token.nil?
      end

      def validate(url, params, signature)
        expected = build_signature_for url, params
        secure_compare(expected, signature)
      end

      def build_signature_for(url, params)
        data = url + params.sort.join
        digest = OpenSSL::Digest.new('sha1')
        Base64.encode64(OpenSSL::HMAC.digest(digest, @auth_token, data)).strip
      end

      private

      # Compares two strings in constant time to avoid timing attacks.
      # Borrowed from ActiveSupport::MessageVerifier.
      # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/message_verifier.rb
      def secure_compare(a, b)
        return false unless a.bytesize == b.bytesize

        l = a.unpack("C#{a.bytesize}")

        res = 0
        b.each_byte { |byte| res |= byte ^ l.shift }
        res == 0
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
twilio-ruby-4.13.0 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.12.1 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc10 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc9 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc8 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc7 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc5 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc4 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc3 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.11.1 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.11.0 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-5.0.0.rc2 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.10.0.edge lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.10.0 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.9.1 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.9.1.edge lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.9.0 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.9.0.edge lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.8.0 lib/twilio-ruby/util/request_validator.rb
twilio-ruby-4.7.0 lib/twilio-ruby/util/request_validator.rb