Class: Lux::Current::Session
Instance Attribute Summary collapse
-
#cookie_name ⇒ Object
readonly
Returns the value of attribute cookie_name.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #generate_cookie ⇒ Object
-
#initialize(request) ⇒ Session
constructor
A new instance of Session.
- #keys ⇒ Object
- #merge!(hash = {}) ⇒ Object
- #to_h ⇒ Object
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 = Lux.config[:session_cookie_name] ||= 'lux_' + Crypt.sha1(Lux.config.secret)[0,4].downcase @request = request @hash = JSON.parse(Crypt.decrypt(request.[] || '{}')) rescue {} security_check end |
Instance Attribute Details
#cookie_name ⇒ Object (readonly)
Returns the value of attribute cookie_name.
12 13 14 |
# File 'lib/lux/current/lib/session.rb', line 12 def end |
#hash ⇒ Object (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 |
#generate_cookie ⇒ Object
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 encrypted = Crypt.encrypt(@hash.to_json) if @request.[] != encrypted = Lux.current.var[:lux_cookie_domain] || Lux.current.nav.domain = [] .push [, encrypted].join('=') .push 'Max-Age=%s' % (Lux.config.) .push "Path=/" .push "Domain=#{cookie_domain}" .push "secure" if Lux.current.request.url.start_with?('https:') .push "HttpOnly" .push "SameSite=Lax" .join('; ') else nil end end |
#keys ⇒ Object
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_h ⇒ Object
68 69 70 |
# File 'lib/lux/current/lib/session.rb', line 68 def to_h @hash end |