Class: Lux::Current::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/lux/current/lib/session.rb

Overview

vars Lux.config.session_cookie_name Lux.config.session_cookie_max_age Lux.config.session_cookie_domain

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Session

Returns a new instance of Session.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/lux/current/lib/session.rb', line 7

def initialize request
  # how long will session last if BROWSER or IP change
  Lux.config.session_forced_validity ||= 10.minutes.to_i

  # name of the session cookie
  @cookie_name = Lux.config.session_cookie_name ||= 'lux_' + Crypt.sha1(Lux.config.secret)[0,4].downcase
  @request     = request
  @session     = JSON.parse(Crypt.decrypt(request.cookies[@cookie_name] || '{}')) rescue {}

  security_check
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/lux/current/lib/session.rb', line 19

def [] key
  @session[key.to_s.downcase]
end

#[]=(key, value) ⇒ Object



23
24
25
# File 'lib/lux/current/lib/session.rb', line 23

def []= key, value
  @session[key.to_s.downcase] = value
end

#delete(key) ⇒ Object



27
28
29
# File 'lib/lux/current/lib/session.rb', line 27

def delete key
  @session.delete key.to_s.downcase
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lux/current/lib/session.rb', line 31

def generate_cookie
  encrypted = Crypt.encrypt(@session.to_json)

  if @request.cookies[@cookie_name] != encrypted
    cookie = []
    cookie.push [@cookie_name, encrypted].join('=')
    cookie.push 'Max-Age=%s' % (Lux.config.session_cookie_max_age || 1.week.to_i)
    cookie.push "Path=/"
    cookie.push "Domain=#{Lux.config.session_cookie_domain}" if Lux.config.session_cookie_domain
    cookie.push "secure" if Lux.config.host.include?('https:')
    cookie.push "HttpOnly"

    cookie.join('; ')
  else
    nil
  end
end

#hashObject



53
54
55
# File 'lib/lux/current/lib/session.rb', line 53

def hash
  @session.dup
end

#merge!(hash = {}) ⇒ Object



49
50
51
# File 'lib/lux/current/lib/session.rb', line 49

def merge! hash={}
  hash.keys.each { |k| self[k] = hash[k] }
end