Sha256: ec58a49a3fb20696631272107fd6beb424ea84055b7e4ff561860b1bd3074281

Contents?: true

Size: 1.97 KB

Versions: 7

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require "base64"

require "pakyow/support/indifferentize"
require "pakyow/support/message_verifier"

require "pakyow/application/connection/session/base"

module Pakyow
  class Application
    class Connection
      module Session
        class Cookie < Base
          def initialize(connection, options)
            if (cookie = connection.cookies[options.name]) && cookie.is_a?(Support::IndifferentHash)
              super(connection, options, Support::IndifferentHash.new(cookie[:value].to_h))
              connection.cookies[options.name][:value] = self
            else
              super(connection, options, deserialize(connection, options))
              connection.cookies[options.name] = Support::IndifferentHash.new(
                domain: options.domain,
                path: options.path,
                max_age: options.max_age,
                expires: options.expires,
                secure: options.secure,
                http_only: options.http_only,
                same_site: options.same_site,
                value: self
              )

              # Update the original cookie value so we can compare for changes.
              #
              connection.update_request_cookie(options.name, self.dup)
            end
          end

          def to_s
            Base64.urlsafe_encode64(
              Pakyow.verifier.sign(
                Marshal.dump(to_h)
              )
            )
          end

          private

          def deserialize(connection, options)
            if value = connection.cookies[options.name]
              Support::IndifferentHash.deep(
                Marshal.load(
                  Pakyow.verifier.verify(
                    Base64.urlsafe_decode64(value)
                  )
                )
              )
            else
              Support::IndifferentHash.new
            end
          rescue StandardError
            Support::IndifferentHash.new
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pakyow-core-1.0.6 lib/pakyow/application/connection/session/cookie.rb
pakyow-core-1.0.5 lib/pakyow/application/connection/session/cookie.rb
pakyow-core-1.0.4 lib/pakyow/application/connection/session/cookie.rb
pakyow-core-1.0.3 lib/pakyow/application/connection/session/cookie.rb
pakyow-core-1.0.2 lib/pakyow/application/connection/session/cookie.rb
pakyow-core-1.0.1 lib/pakyow/application/connection/session/cookie.rb
pakyow-core-1.0.0 lib/pakyow/application/connection/session/cookie.rb