Damien Wilson
Damien Wilson

Reputation: 4560

How do I use a custom cookie session serializer in Rack?

I'm currently integrating Warden into a new Rack application I'm building. I'd like to implement a recent patch to Rack that allows me to specify how sessions are serialized; specifically, I'd like to use Rack::Session::Cookie::Identity as the session processor.

Unfortunately, the documentation is a little unclear as to what syntax I should use to configure Rack::Session::Cookie in my rackup file, can anyone here tell me what I'm doing wrong?

config.ru

require 'my_sinatra_app'

app = self

use Rack::Session::Cookie.new(app, Rack::Session::Cookie::Identity.new), {:key => "auth_token"}

use Warden::Manager do |warden| # Must come AFTER Rack::Session
  warden.default_strategies :password
  warden.failure_app Jelli::Auth.run!
end

run MySinatraApp

error message from thin

!! Unexpected error while processing request: undefined method `new' for #<Rack::Session::Cookie:0x00000110124128>

PS: I'm using bundler to manage my gem dependencies and I've likewise included rack's master branch as the desired version.

Update: As suggested in the comments below, I have read the documentation; sadly the suggested syntax in the docs is not working.

Update: Still no luck on my end; offering up a bounty to whoever can help me figure this out.

Upvotes: 2

Views: 1944

Answers (2)

stef
stef

Reputation: 14268

require 'my_sinatra_app'

app = self

session_app = Rack::Session::Cookie.new(app, {
        :coder => Class.new {
                def encode(str); str; end
                def decode(str); str; end
              }.new
      })

use session_app

run MySinatraApp

I had a look at the source code, and Rack::Session::Cookie::Identity is as simple as:

class Identity
  def encode(str); str; end
  def decode(str); str; end
end

then this should suffice. I've run it under thin and I get a hello world app up - that's as far as I went.

Upvotes: 3

shingara
shingara

Reputation: 46914

If you check the documentation of Rack::Session::Cookie ( https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb )

You can see there are no initialize method and it propose to use :

use Rack::Session::Cookie, :key => 'rack.session',
                           :domain => 'foo.com',
                           :path => '/',
                           :expire_after => 2592000,
                           :secret => 'change_me'

Upvotes: 1

Related Questions