Class: Rack::Session::EncryptedCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/session/encryptedcookie.rb

Constant Summary collapse

NOT_FOUND =
[ 404, {}, [ 'Not found' ]].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ EncryptedCookie

Returns a new instance of EncryptedCookie.

Parameters:

  • opts (Hash) (defaults to: {})

    Session options

Options Hash (opts):

  • :cookie_name (String)

    Cookie name

  • :domain (String)

    Domain for the cookie

  • :http_only (Boolean)

    HttpOnly for the cookie

  • :expires (Integer)

    Cookie expiry (in seconds)

  • :cipher (String)

    OpenSSL cipher to use

  • :salt (String)

    Salt for the IV

  • :key (String)

    Encryption key for the data

  • :tag_len (Integer)

    Tag length (for GCM/CCM ciphers)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/session/encryptedcookie.rb', line 29

def initialize(app, opts={})
  @app  = app
  @hash = {}
  @opts = {
    cookie_name: 'rack.session',
    domain:      nil,
    http_only:   false,
    expires:     (15 * 60),
    cipher:      'aes-256-cbc',
    salt:        '3@bG>B@J5vy-FeXJ',
    rounds:      2000,
    key:         'r`*BqnG:c^;AL{k97=KYN!#',
    tag_len:     16
  }.merge(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rack/session/encryptedcookie.rb', line 62

def method_missing(method, *args, &block)
  if @hash.respond_to?(method)
    @hash.send(method, *args, &block)
  else
    raise ArgumentError.new("Method `#{method}` doesn't exist.")
  end
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
# File 'lib/rack/session/encryptedcookie.rb', line 45

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rack/session/encryptedcookie.rb', line 49

def call!(env)
  @cb = env['async.callback']
  env['async.callback'] = method(:save_session) if @cb
  env['rack.session']   = self
  load_session(env)

  if @app
    @cb ? @app.call(env) : save_session(@app.call(env))
  else
    @cb ? @cb.call(NOT_FOUND) : NOT_FOUND
  end
end