Class: Gloo::WebSvr::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/web_svr/session.rb

Constant Summary collapse

SESSION_CONTAINER =
'session'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(engine, server_obj) ⇒ Session

Set up the web server.



27
28
29
30
31
32
# File 'lib/gloo/web_svr/session.rb', line 27

def initialize( engine, server_obj )
  @engine = engine
  @log = @engine.log

  @server_obj = server_obj
end

Instance Method Details

#add_session_for_response(headers) ⇒ Object

If there is session data, encrypt and add it to the response. Once done, clear out the session data.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gloo/web_svr/session.rb', line 70

def add_session_for_response( headers )
  # Are we using sessions?
  if @server_obj.use_session?
    # Build and add encrypted session data
    data = @server_obj.get_session_data
    unless data.empty?
      data = encrypt_encode( data )
      session_hash = { 
        value: data, 
        path: cookie_path, 
        expires: cookie_expires,
        http_only: true }

      if secure_cookie?
        session_hash[ :secure ] = true
      end

      Rack::Utils.set_cookie_header!( headers, session_name, session_hash )
    end

    # Clear out session data
    @server_obj.clear_session_data
  end

  return headers
end

Get the expiration time for the session cookie.



148
149
150
# File 'lib/gloo/web_svr/session.rb', line 148

def cookie_expires
  return @server_obj.session_cookie_expires
end

Get the path for the session cookie.



141
142
143
# File 'lib/gloo/web_svr/session.rb', line 141

def cookie_path
  return @server_obj.session_cookie_path
end

#decode_decrypt(data) ⇒ Object

Decode and decrypt the session data.



112
113
114
115
# File 'lib/gloo/web_svr/session.rb', line 112

def decode_decrypt( data )
  data = Gloo::Objs::Cipher.decrypt( data, key, iv )
  return JSON.parse( data )
end

#encrypt_encode(data) ⇒ Object

Encrypt and encode the session data.



105
106
107
# File 'lib/gloo/web_svr/session.rb', line 105

def encrypt_encode( data )
  return Gloo::Objs::Cipher.encrypt( data.to_json, key, iv )
end

#ivObject

Get the initialization vector for the cipher.



134
135
136
# File 'lib/gloo/web_svr/session.rb', line 134

def iv
  return @server_obj.encryption_iv
end

#keyObject

Get the key for the encryption cipher.



127
128
129
# File 'lib/gloo/web_svr/session.rb', line 127

def key
  return @server_obj.encryption_key
end

#secure_cookie?Boolean

Should the session cookie be secure?

Returns:

  • (Boolean)


155
156
157
# File 'lib/gloo/web_svr/session.rb', line 155

def secure_cookie?
  return @server_obj.session_cookie_secure
end

#session_nameObject

Get the session cookie name.



120
121
122
# File 'lib/gloo/web_svr/session.rb', line 120

def session_name
  return @server_obj.session_name
end

#set_session_data_for_request(env) ⇒ Object

Get the session data from the encrypted cookie. Add it to the session container.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gloo/web_svr/session.rb', line 43

def set_session_data_for_request( env )
  cookie_hash = Rack::Utils.parse_cookies( env )

  # Are we using sessions?
  if @server_obj.use_session?
    data = cookie_hash[ session_name ]

    if data
      data = decode_decrypt( data ) 

      data.each do |key, value|
        @server_obj.set_session_var( key, value )
      end
    end
  end

end