Class: Lux::Current::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Session

Returns a new instance of Session.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lux/current/lib/session.rb', line 14

def initialize request
  # how long will session last if BROWSER or IP change
  Lux.config[:session_forced_validity] ||= 10.minutes.to_i
  Lux.config[:session_cookie_max_age]  ||= 1.week.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
  @hash        = JSON.parse(Crypt.decrypt(request.cookies[@cookie_name] || '{}')) rescue {}

  security_check
end

Instance Attribute Details

Returns the value of attribute cookie_name.



12
13
14
# File 'lib/lux/current/lib/session.rb', line 12

def cookie_name
  @cookie_name
end

#hashObject (readonly)

Returns the value of attribute hash.



12
13
14
# File 'lib/lux/current/lib/session.rb', line 12

def hash
  @hash
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



31
32
33
# File 'lib/lux/current/lib/session.rb', line 31

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

#delete(key) ⇒ Object



35
36
37
# File 'lib/lux/current/lib/session.rb', line 35

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


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lux/current/lib/session.rb', line 39

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

  if @request.cookies[@cookie_name] != encrypted
    cookie_domain = Lux.current.var[:lux_cookie_domain] || Lux.current.nav.domain

    cookie = []
    cookie.push [@cookie_name, encrypted].join('=')
    cookie.push 'Max-Age=%s' % (Lux.config.session_cookie_max_age)
    cookie.push "Path=/"
    cookie.push "Domain=#{cookie_domain}"
    cookie.push "secure" if Lux.current.request.url.start_with?('https:')
    cookie.push "HttpOnly"
    cookie.push "SameSite=Lax"

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

#keysObject



64
65
66
# File 'lib/lux/current/lib/session.rb', line 64

def keys
  @hash.keys
end

#merge!(hash = {}) ⇒ Object



60
61
62
# File 'lib/lux/current/lib/session.rb', line 60

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

#to_hObject



68
69
70
# File 'lib/lux/current/lib/session.rb', line 68

def to_h
  @hash
end