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.



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

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.



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

def cookie_expires
  return @server_obj.session_cookie_expires
end

Get the path for the session cookie.



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

def cookie_path
  return @server_obj.session_cookie_path
end

#decode_decrypt(data) ⇒ Object

Decode and decrypt the session data.



116
117
118
119
120
121
# File 'lib/gloo/web_svr/session.rb', line 116

def decode_decrypt( data )
  return nil unless data && key && iv

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

#encrypt_encode(data) ⇒ Object

Encrypt and encode the session data.



109
110
111
# File 'lib/gloo/web_svr/session.rb', line 109

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

#ivObject

Get the initialization vector for the cipher.



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

def iv
  return @server_obj.encryption_iv
end

#keyObject

Get the key for the encryption cipher.



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

def key
  return @server_obj.encryption_key
end

#secure_cookie?Boolean

Should the session cookie be secure?

Returns:

  • (Boolean)


161
162
163
# File 'lib/gloo/web_svr/session.rb', line 161

def secure_cookie?
  return @server_obj.session_cookie_secure
end

#session_nameObject

Get the session cookie name.



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

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
60
61
62
63
# File 'lib/gloo/web_svr/session.rb', line 43

def set_session_data_for_request( env )
  begin
    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 ) 
        return unless data
        
        data.each do |key, value|
          @server_obj.set_session_var( key, value )
        end
      end
    end
  rescue => e
    @engine.log_exception e
  end
end