Sha256: c9a8361bcd3c6e96fcfdd0625927c1a1f801e00fdc79b882ea4ce4915aad556c

Contents?: true

Size: 1.74 KB

Versions: 55

Compression:

Stored size: 1.74 KB

Contents

require 'active_support/base64'
require 'active_support/core_ext/object/blank'

module ActiveSupport
  # +MessageVerifier+ makes it easy to generate and verify messages which are signed
  # to prevent tampering.
  #
  # This is useful for cases like remember-me tokens and auto-unsubscribe links where the
  # session store isn't suitable or available.
  #
  # Remember Me:
  #   cookies[:remember_me] = @verifier.generate([@user.id, 2.weeks.from_now])
  #
  # In the authentication filter:
  #
  #   id, time = @verifier.verify(cookies[:remember_me])
  #   if time < Time.now
  #     self.current_user = User.find(id)
  #   end
  #
  class MessageVerifier
    class InvalidSignature < StandardError; end

    def initialize(secret, digest = 'SHA1')
      @secret = secret
      @digest = digest
    end

    def verify(signed_message)
      raise InvalidSignature if signed_message.blank?

      data, digest = signed_message.split("--")
      if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
        Marshal.load(ActiveSupport::Base64.decode64(data))
      else
        raise InvalidSignature
      end
    end

    def generate(value)
      data = ActiveSupport::Base64.encode64s(Marshal.dump(value))
      "#{data}--#{generate_digest(data)}"
    end

    private
      # constant-time comparison algorithm to prevent timing attacks
      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

      def generate_digest(data)
        require 'openssl' unless defined?(OpenSSL)
        OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@digest).new, @secret, data)
      end
  end
end

Version data entries

55 entries across 55 versions & 2 rubygems

Version Path
activesupport-3.1.12 lib/active_support/message_verifier.rb
activesupport-3.1.11 lib/active_support/message_verifier.rb
activesupport-3.1.10 lib/active_support/message_verifier.rb
activesupport-3.1.9 lib/active_support/message_verifier.rb
dirty_history-0.7.3 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.7.2 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.7.1 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.7.0 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.6.7 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.6.6 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.6.5 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.6.4 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
activesupport-3.1.8 lib/active_support/message_verifier.rb
dirty_history-0.6.3 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
activesupport-3.1.7 lib/active_support/message_verifier.rb
dirty_history-0.6.2 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.6.1 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
dirty_history-0.6.0 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb
activesupport-3.1.6 lib/active_support/message_verifier.rb
dirty_history-0.5.4 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/message_verifier.rb