Sha256: 213564d56bfc06beacf0cd0db2ca4aa2f066336b572d00422eef1a1ea1db0fb0

Contents?: true

Size: 1.71 KB

Versions: 10

Compression:

Stored size: 1.71 KB

Contents

module Ramaze
  class Session

    # The Session::Hash acts as the wrapper for a simple Hash
    #
    # Its purpose is to notify the underlying cache, in which the sessions
    # are stored, about updates.
    class Hash

      # Sets @hash to an empty Hash

      def initialize(session)
        @session = session
        @hash = {}
      end

      # relays all the methods to the @hash and updates the session_cache in
      # Session.current.sessions if anything changes.

      def method_missing(*args, &block)
        result = @hash.send(*args, &block)
        Cache.sessions[@session.session_id] = self
        result
      end

      # Calls #inspect on the wrapped @hash

      def inspect
        @hash.inspect
      end

      # Use client side session variables

      def client
        Request.current['session.client'] ||= unmarshal(Request.current.cookies["#{Session::SESSION_KEY}-client"]) || {}
      end

      private

      # Marshal a session hash into safe cookie data. Include an integrity hash.
      def marshal(session)
        data = [ Marshal.dump(session) ].pack('m').chop
        "#{data}--#{generate_digest(data)}"
      end

      # Unmarshal cookie data to a hash and verify its integrity.
      def unmarshal(cookie)
        return unless cookie
        data, digest = cookie.split('--')
        return nil unless digest == generate_digest(data)
        Marshal.load(data.unpack('m').first)
      end

      # Generate the inline SHA512 message digest. Larger (128 bytes) than SHA256
      # (64 bytes) or RMD160 (40 bytes), but small relative to the 4096 byte
      # max cookie size.
      def generate_digest(data)
        Digest::SHA512.hexdigest "#{data}#{Session.trait[:secret]}"
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 4 rubygems

Version Path
Pistos-ramaze-2008.12 lib/ramaze/current/session/hash.rb
Pistos-ramaze-2009.01 lib/ramaze/current/session/hash.rb
Pistos-ramaze-2009.02 lib/ramaze/current/session/hash.rb
manveru-ramaze-2008.12 lib/ramaze/current/session/hash.rb
manveru-ramaze-2009.01 lib/ramaze/current/session/hash.rb
ptomato-ramaze-2009.02.1 lib/ramaze/current/session/hash.rb
ptomato-ramaze-2009.02 lib/ramaze/current/session/hash.rb
ramaze-2009.01 lib/ramaze/current/session/hash.rb
ramaze-2009.02 lib/ramaze/current/session/hash.rb
ramaze-2009.03 lib/ramaze/current/session/hash.rb