rsence 2.0.0.6.pre → 2.0.0.7.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,485 +7,476 @@
7
7
  ##
8
8
 
9
9
 
10
- require 'rubygems'
11
- require 'json'
10
+ module RSence
12
11
 
13
- ## Shared messaging-object:
14
- require 'session/msg'
12
+ require 'rubygems'
13
+ require 'json'
15
14
 
16
- ## Unique random number generator:
17
- require 'randgen'
15
+ ## Shared messaging-object:
16
+ require 'session/msg'
18
17
 
19
- ## SessionStorage is the superclass of SessionManager
20
- require 'session/sessionstorage'
18
+ ## Unique random number generator:
19
+ require 'randgen'
21
20
 
22
- module RSence
21
+ ## SessionStorage is the superclass of SessionManager
22
+ require 'session/sessionstorage'
23
23
 
24
- require 'digest/sha1'
24
+ require 'digest/sha1'
25
25
 
26
- =begin
27
- SessionManager does session creation, validation, expiration and storage duties.
28
- It's quite transparent.
29
- =end
30
- class SessionManager < SessionStorage
26
+ # SessionManager does session creation, validation, expiration and storage duties.
27
+ class SessionManager < SessionStorage
31
28
 
32
- include Digest
29
+ include Digest
33
30
 
34
- attr_reader :randgen
31
+ attr_reader :randgen
35
32
 
36
- ## Makes everything ready to run
37
- def initialize( transporter )
33
+ ## Makes everything ready to run
34
+ def initialize( transporter )
38
35
 
39
- super()
36
+ super()
40
37
 
41
- @transporter = transporter
38
+ @transporter = transporter
42
39
 
43
- @valuemanager = @transporter.valuemanager
40
+ @valuemanager = @transporter.valuemanager
44
41
 
45
- @plugins = @transporter.plugins
42
+ @plugins = @transporter.plugins
46
43
 
47
- ## 'Unique' Random String generator for ses_key:s and cookie_key:s
48
- @randgen = RandGen.new( @config[:key_length] )
44
+ ## 'Unique' Random String generator for ses_key:s and cookie_key:s
45
+ @randgen = RandGen.new( @config[:key_length] )
49
46
 
50
- # regex to match ipv4 addresses
51
- @ipv4_reg = /^([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[1-9])\.([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[0-9])$/
47
+ # regex to match ipv4 addresses
48
+ @ipv4_reg = /^([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[1-9])\.([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]|[2][0-5][0-9]|[1-9][0-9]|[0-9])$/
52
49
 
53
- end
50
+ end
54
51
 
55
- ### Creates a new session
56
- def init_ses( msg, ses_seed )
52
+ ### Creates a new session
53
+ def init_ses( msg, ses_seed )
57
54
 
58
- ## Assigns new timeout for the session
59
- time_now = Time.now.to_i # seconds since epoch
60
- timeout = time_now + @config[:timeout_secs]
55
+ ## Assigns new timeout for the session
56
+ time_now = Time.now.to_i # seconds since epoch
57
+ timeout = time_now + @config[:timeout_secs]
61
58
 
62
- ## Creates a new session key
63
- ses_key = @randgen.gen
59
+ ## Creates a new session key
60
+ ses_key = @randgen.gen
64
61
 
65
- ## Creates a new cookie key
66
- cookie_key = @randgen.gen_many(@config[:cookie_key_multiplier]).join('')
62
+ ## Creates a new cookie key
63
+ cookie_key = @randgen.gen_many(@config[:cookie_key_multiplier]).join('')
67
64
 
68
- ## Makes a new database row for the session, returns its id
69
- ses_id = new_ses_id( cookie_key, ses_key, timeout )
65
+ ## Makes a new database row for the session, returns its id
66
+ ses_id = new_ses_id( cookie_key, ses_key, timeout )
70
67
 
71
- ses_sha = SHA1.hexdigest(ses_key+ses_seed)
68
+ ses_sha = SHA1.hexdigest(ses_key+ses_seed)
72
69
 
73
- ### Default session data structure,
74
- ### Please don't mess with it, unless you know exactly what you are doing.
75
- ses_data = {
70
+ ### Default session data structure,
71
+ ### Please don't mess with it, unless you know exactly what you are doing.
72
+ ses_data = {
76
73
 
77
- # the time, when the session will time out
78
- :timeout => timeout,
74
+ # the time, when the session will time out
75
+ :timeout => timeout,
79
76
 
80
- # session id, used internally
81
- :ses_id => ses_id,
77
+ # session id, used internally
78
+ :ses_id => ses_id,
82
79
 
83
- # session key, used externally (client xhr)
84
- :ses_key => ses_sha,
80
+ # session key, used externally (client xhr)
81
+ :ses_key => ses_sha,
85
82
 
86
- # session key, used externally (client cookies)
87
- :cookie_key => cookie_key,
83
+ # session key, used externally (client cookies)
84
+ :cookie_key => cookie_key,
88
85
 
89
- # user id, map to your own user management code
90
- :user_id => 0,
86
+ # user id, map to your own user management code
87
+ :user_id => 0,
91
88
 
92
- # valuemanager data
93
- :values => {
94
- :sync => [], # value id's to sync to client
95
- :check => [], # value id's to validate in server (from client)
96
- :by_id => {} # values by id
89
+ # valuemanager data
90
+ :values => {
91
+ :sync => [], # value id's to sync to client
92
+ :check => [], # value id's to validate in server (from client)
93
+ :by_id => {} # values by id
94
+ }
97
95
  }
98
- }
99
96
 
100
- # bind the session data to @sessions by its id
101
- @sessions[ ses_id ] = ses_data
97
+ # bind the session data to @sessions by its id
98
+ @sessions[ ses_id ] = ses_data
102
99
 
103
- # map the key back to the id
104
- @session_keys[ ses_sha ] = ses_id
100
+ # map the key back to the id
101
+ @session_keys[ ses_sha ] = ses_id
105
102
 
106
- # map the ses_id to cookie key
107
- @session_cookie_keys[ cookie_key ] = ses_id
103
+ # map the ses_id to cookie key
104
+ @session_cookie_keys[ cookie_key ] = ses_id
108
105
 
109
- ### Tell the client what the new key is
110
- msg.ses_key = ses_key
106
+ ### Tell the client what the new key is
107
+ msg.ses_key = ses_key
111
108
 
112
- ### Set the session data and id to the message object
113
- msg.session = ses_data
109
+ ### Set the session data and id to the message object
110
+ msg.session = ses_data
114
111
 
115
- # Flag the session as new, so associated
116
- # plugins know when to create new data
117
- msg.new_session = true
112
+ # Flag the session as new, so associated
113
+ # plugins know when to create new data
114
+ msg.new_session = true
118
115
 
119
- # Returns the cookie key, so it can be sent in the response header
120
- return cookie_key
116
+ # Returns the cookie key, so it can be sent in the response header
117
+ return cookie_key
121
118
 
122
- end
119
+ end
123
120
 
124
- def refresh_ses( msg, ses_data, ses_id, ses_key, ses_seed )
125
- # new time-out
126
- ses_data[:timeout] = Time.now.to_i + @config[:timeout_secs]
121
+ def refresh_ses( msg, ses_data, ses_id, ses_key, ses_seed )
122
+ # new time-out
123
+ ses_data[:timeout] = Time.now.to_i + @config[:timeout_secs]
127
124
 
128
- # re-generates the ses_key for each xhr
129
- if @config[:disposable_keys]
125
+ # re-generates the ses_key for each xhr
126
+ if @config[:disposable_keys]
130
127
 
131
- # disposes the old (current) ses_key:
132
- @session_keys.delete( ses_key )
128
+ # disposes the old (current) ses_key:
129
+ @session_keys.delete( ses_key )
133
130
 
134
- unless ses_seed
135
- ses_seed = ses_key
136
- end
131
+ unless ses_seed
132
+ ses_seed = ses_key
133
+ end
137
134
 
138
- # gets a new ses_key:
139
- ses_key = @randgen.gen
135
+ # gets a new ses_key:
136
+ ses_key = @randgen.gen
140
137
 
141
- ses_sha = SHA1.hexdigest(ses_key+ses_seed)
138
+ ses_sha = SHA1.hexdigest(ses_key+ses_seed)
142
139
 
143
- # re-maps the session id to the new key
144
- @session_keys[ses_sha] = ses_id
140
+ # re-maps the session id to the new key
141
+ @session_keys[ses_sha] = ses_id
145
142
 
146
- # changes the session key in the session data
147
- ses_data[:ses_key] = ses_sha
143
+ # changes the session key in the session data
144
+ ses_data[:ses_key] = ses_sha
148
145
 
149
- # tell the client what its new session key is
150
- msg.ses_key = ses_key
151
- end
146
+ # tell the client what its new session key is
147
+ msg.ses_key = ses_key
148
+ end
152
149
 
153
- if @config[:clone_cookie_sessions] and @clone_targets.has_key? ses_id
154
- targets = []
155
- @clone_targets[ ses_id ].length.times do |n|
156
- target_id = @clone_targets[ ses_id ].shift
157
- # warn "target_id: #{target_id}"
158
- target_ses = @sessions[ target_id ]
159
- if @sessions.has_key?( target_id ) and @sessions[ target_id ].class == Hash
160
- targets.push( target_ses )
150
+ if @config[:clone_cookie_sessions] and @clone_targets.has_key? ses_id
151
+ targets = []
152
+ @clone_targets[ ses_id ].length.times do |n|
153
+ target_id = @clone_targets[ ses_id ].shift
154
+ # warn "target_id: #{target_id}"
155
+ target_ses = @sessions[ target_id ]
156
+ if @sessions.has_key?( target_id ) and @sessions[ target_id ].class == Hash
157
+ targets.push( target_ses )
158
+ end
161
159
  end
160
+ @clone_targets.delete( ses_id ) if @clone_targets[ ses_id ].empty?
161
+ msg.cloned_targets = targets unless targets.empty?
162
162
  end
163
- @clone_targets.delete( ses_id ) if @clone_targets[ ses_id ].empty?
164
- msg.cloned_targets = targets unless targets.empty?
165
- end
166
163
 
167
- ### Bind the session data and id to the message object
168
- msg.session = ses_data
164
+ ### Bind the session data and id to the message object
165
+ msg.session = ses_data
169
166
 
170
- end
167
+ end
171
168
 
172
- def clone_ses( msg, old_data, old_id, old_key, ses_seed )
173
- ses_data = Marshal.restore( Marshal.dump( old_data ) )
174
- old_data[:timeout] = Time.now.to_i + @config[:cloned_session_expires_in]
175
- timeout = Time.now.to_i + @config[:timeout_secs]
176
- cookie_key = @randgen.gen_many(@config[:cookie_key_multiplier]).join('')
177
- ses_key = @randgen.gen
178
- ses_sha = SHA1.hexdigest(ses_key+ses_seed)
179
- ses_data[:timeout] = timeout
180
- ses_data[:ses_key] = ses_key
181
- ses_data[:cookie_key] = cookie_key
182
- ses_id = new_ses_id( cookie_key, ses_key, timeout )
183
- ses_data[:ses_id] = ses_id
184
- @sessions[ ses_id ] = ses_data
185
- @session_keys[ ses_sha ] = ses_id
186
- @session_cookie_keys.delete( old_data[:cookie_key] )
187
- @session_cookie_keys[ cookie_key ] = ses_id
188
- msg.ses_key = ses_key
189
- msg.session = ses_data
190
- if @clone_targets.has_key? old_id
191
- @clone_targets[ old_id ].push( ses_id )
192
- else
193
- @clone_targets[ old_id ] = [ ses_id ]
169
+ def clone_ses( msg, old_data, old_id, old_key, ses_seed )
170
+ ses_data = Marshal.restore( Marshal.dump( old_data ) )
171
+ old_data[:timeout] = Time.now.to_i + @config[:cloned_session_expires_in]
172
+ timeout = Time.now.to_i + @config[:timeout_secs]
173
+ cookie_key = @randgen.gen_many(@config[:cookie_key_multiplier]).join('')
174
+ ses_key = @randgen.gen
175
+ ses_sha = SHA1.hexdigest(ses_key+ses_seed)
176
+ ses_data[:timeout] = timeout
177
+ ses_data[:ses_key] = ses_key
178
+ ses_data[:cookie_key] = cookie_key
179
+ ses_id = new_ses_id( cookie_key, ses_key, timeout )
180
+ ses_data[:ses_id] = ses_id
181
+ @sessions[ ses_id ] = ses_data
182
+ @session_keys[ ses_sha ] = ses_id
183
+ @session_cookie_keys.delete( old_data[:cookie_key] )
184
+ @session_cookie_keys[ cookie_key ] = ses_id
185
+ msg.ses_key = ses_key
186
+ msg.session = ses_data
187
+ if @clone_targets.has_key? old_id
188
+ @clone_targets[ old_id ].push( ses_id )
189
+ else
190
+ @clone_targets[ old_id ] = [ ses_id ]
191
+ end
192
+ @clone_sources[ ses_id ] = old_id
193
+ msg.cloned_source = old_data
194
+ msg.new_session = false
195
+ msg.restored_session = true
194
196
  end
195
- @clone_sources[ ses_id ] = old_id
196
- msg.cloned_source = old_data
197
- msg.new_session = false
198
- msg.restored_session = true
199
- end
200
197
 
201
- ### Returns the current session data, if the session is valid.
202
- ### Otherwise stops the client and returns false.
203
- def check_ses( msg, ses_key, ses_seed=false )
198
+ ### Returns the current session data, if the session is valid.
199
+ ### Otherwise stops the client and returns false.
200
+ def check_ses( msg, ses_key, ses_seed=false )
204
201
 
205
- # first, check if the session key exists (xhr)
206
- if @session_keys.has_key?( ses_key )
202
+ # first, check if the session key exists (xhr)
203
+ if @session_keys.has_key?( ses_key )
207
204
 
208
- # get the session's id based on its key
209
- ses_id = @session_keys[ ses_key ]
205
+ # get the session's id based on its key
206
+ ses_id = @session_keys[ ses_key ]
210
207
 
211
- # get the session's data based on its id
212
- ses_data = @sessions[ ses_id ]
208
+ # get the session's data based on its id
209
+ ses_data = @sessions[ ses_id ]
210
+
211
+ if @config[:clone_cookie_sessions] and ses_seed
212
+ clone_ses( msg, ses_data, ses_id, ses_key, ses_seed )
213
+ return [true, true]
214
+ else
215
+ refresh_ses( msg, ses_data, ses_id, ses_key, ses_seed )
216
+ return [true, false]
217
+ end
213
218
 
214
- if @config[:clone_cookie_sessions] and ses_seed
215
- clone_ses( msg, ses_data, ses_id, ses_key, ses_seed )
216
- return [true, true]
219
+
220
+ ## The session was either faked or expired:
217
221
  else
218
- refresh_ses( msg, ses_data, ses_id, ses_key, ses_seed )
219
- return [true, false]
222
+ ### Tells the client to stop connecting with its session key and reload instead to get a new one.
223
+ stop_client_with_message( msg,
224
+ @config[:messages][:invalid_session][:title],
225
+ @config[:messages][:invalid_session][:descr],
226
+ @config[:messages][:invalid_session][:uri]
227
+ )
228
+
229
+ ## Return failure
230
+ return [false, false]
220
231
  end
221
-
222
232
 
223
- ## The session was either faked or expired:
224
- else
225
- ### Tells the client to stop connecting with its session key and reload instead to get a new one.
226
- stop_client_with_message( msg,
227
- @config[:messages][:invalid_session][:title],
228
- @config[:messages][:invalid_session][:descr],
229
- @config[:messages][:invalid_session][:uri]
230
- )
231
-
232
- ## Return failure
233
- return [false, false]
234
233
  end
235
-
236
- end
237
234
 
238
- def js_str( str )
239
- return str.to_json.gsub('<','&lt;').gsub('>','&gt;').gsub(/\[\[(.*?)\]\]/,'<\1>')
240
- end
235
+ def js_str( str )
236
+ return str.to_json.gsub('<','&lt;').gsub('>','&gt;').gsub(/\[\[(.*?)\]\]/,'<\1>')
237
+ end
241
238
 
242
- ## Displays error message and stops the client
243
- def stop_client_with_message( msg,
244
- title = 'Unknown Issue',
245
- descr = 'No issue description given.',
246
- uri = ::RSence.config[:index_html][:respond_address] )
247
- msg.error_msg( [
248
- "jsLoader.load('default_theme');",
249
- "jsLoader.load('controls');",
250
- "jsLoader.load('servermessage');",
251
- "ReloadApp.nu( #{js_str(title)}, #{js_str(descr)}, #{js_str(uri)} );"
252
- ] )
253
- end
239
+ ## Displays error message and stops the client
240
+ def stop_client_with_message( msg,
241
+ title = 'Unknown Issue',
242
+ descr = 'No issue description given.',
243
+ uri = ::RSence.config[:index_html][:respond_address] )
244
+ msg.error_msg( [
245
+ "jsLoader.load('default_theme');",
246
+ "jsLoader.load('controls');",
247
+ "jsLoader.load('servermessage');",
248
+ "ReloadApp.nu( #{js_str(title)}, #{js_str(descr)}, #{js_str(uri)} );"
249
+ ] )
250
+ end
254
251
 
255
- ### Checks / Sets cookies
256
- def check_cookie( msg, ses_seed )
252
+ ### Checks / Sets cookies
253
+ def check_cookie( msg, ses_seed )
257
254
 
258
- # default to no cookie key found:
259
- cookie_key = false
255
+ # default to no cookie key found:
256
+ cookie_key = false
260
257
 
261
- # gets the cookie array from the request object
262
- cookie_raw = msg.request.cookies
258
+ # gets the cookie array from the request object
259
+ cookie_raw = msg.request.cookies
263
260
 
264
- # checks, if a cookie named 'ses_key' is found
265
- if cookie_raw.has_key?('ses_key')
261
+ # checks, if a cookie named 'ses_key' is found
262
+ if cookie_raw.has_key?('ses_key')
266
263
 
267
- # gets just the data itself (discards comment, domain, exipiry etc)
268
- cookie_key = cookie_raw['ses_key'].split(';')[0]
264
+ # gets just the data itself (discards comment, domain, exipiry etc)
265
+ cookie_key = cookie_raw['ses_key'].split(';')[0]
269
266
 
270
- end
267
+ end
271
268
 
272
- # if a cookie key is found (non-false), checks if it's valid
273
- if cookie_key
269
+ # if a cookie key is found (non-false), checks if it's valid
270
+ if cookie_key
274
271
 
275
- # checks for validity by looking the key up in @session_cookie_keys
276
- cookie_key_exist = @session_cookie_keys.has_key?( cookie_key )
272
+ # checks for validity by looking the key up in @session_cookie_keys
273
+ cookie_key_exist = @session_cookie_keys.has_key?( cookie_key )
277
274
 
278
- # sets the cookie key to false, if it doesn't exist
279
- cookie_key = false unless cookie_key_exist
275
+ # sets the cookie key to false, if it doesn't exist
276
+ cookie_key = false unless cookie_key_exist
280
277
 
281
- end
278
+ end
282
279
 
283
- # at this point, the cookie key seems valid:
284
- if cookie_key and cookie_key_exist
280
+ # at this point, the cookie key seems valid:
281
+ if cookie_key and cookie_key_exist
285
282
 
286
- # get the session identifier
287
- ses_id = @session_cookie_keys[ cookie_key ]
283
+ # get the session identifier
284
+ ses_id = @session_cookie_keys[ cookie_key ]
288
285
 
289
- # get the last session key from session data
290
- ses_key = @sessions[ses_id][:ses_key]
286
+ # get the last session key from session data
287
+ ses_key = @sessions[ses_id][:ses_key]
291
288
 
292
- # make additional checks on the session validity (expiry etc)
293
- (ses_status, ses_cloned) = check_ses( msg, ses_key, ses_seed )
289
+ # make additional checks on the session validity (expiry etc)
290
+ (ses_status, ses_cloned) = check_ses( msg, ses_key, ses_seed )
294
291
 
295
- if ses_status and ses_cloned
296
- ses_id = msg.ses_id
297
- ses_key = msg.session[:ses_key]
298
- cookie_key = msg.session[:cookie_key]
299
- @valuemanager.resend_session_values( msg )
300
- elsif ses_status
301
- # delete the old cookie key:
302
- @session_cookie_keys.delete( cookie_key )
292
+ if ses_status and ses_cloned
293
+ ses_id = msg.ses_id
294
+ ses_key = msg.session[:ses_key]
295
+ cookie_key = msg.session[:cookie_key]
296
+ @valuemanager.resend_session_values( msg )
297
+ elsif ses_status
298
+ # delete the old cookie key:
299
+ @session_cookie_keys.delete( cookie_key )
303
300
 
304
- # get a new cookie key
305
- cookie_key = @randgen.gen_many(@config[:cookie_key_multiplier]).join('')
301
+ # get a new cookie key
302
+ cookie_key = @randgen.gen_many(@config[:cookie_key_multiplier]).join('')
306
303
 
307
- # map the new cookie key to the old session identifier
308
- @session_cookie_keys[ cookie_key ] = ses_id
304
+ # map the new cookie key to the old session identifier
305
+ @session_cookie_keys[ cookie_key ] = ses_id
309
306
 
310
- # binds the new cookie key to the old session data
311
- @sessions[ses_id][:cookie_key] = cookie_key
307
+ # binds the new cookie key to the old session data
308
+ @sessions[ses_id][:cookie_key] = cookie_key
312
309
 
313
- # Sets the restored_session flag of msg to true
314
- # It signals plugins to re-set data
315
- msg.restored_session = true
310
+ # Sets the restored_session flag of msg to true
311
+ # It signals plugins to re-set data
312
+ msg.restored_session = true
316
313
 
317
- # Sets the new_session flag of msg to false
318
- # It signals plugins to not create new server-side values
319
- msg.new_session = false
314
+ # Sets the new_session flag of msg to false
315
+ # It signals plugins to not create new server-side values
316
+ msg.new_session = false
320
317
 
321
- # tells ValueManager to re-send client-side HValue objects
322
- # with data to the client
323
- @valuemanager.resend_session_values( msg )
318
+ # tells ValueManager to re-send client-side HValue objects
319
+ # with data to the client
320
+ @valuemanager.resend_session_values( msg )
324
321
 
325
- # if the session is not valid, make sure to mark the
326
- # cookie key as invalid (false)
327
- else
328
- cookie_key = false
322
+ # if the session is not valid, make sure to mark the
323
+ # cookie key as invalid (false)
324
+ else
325
+ cookie_key = false
326
+ end
329
327
  end
330
- end
331
328
 
332
- # if the cookie key failed validation in the
333
- # tests above, create a new session instead
334
- unless cookie_key
335
- cookie_key = init_ses( msg, ses_seed )
336
- ses_status = true
337
- end
329
+ # if the cookie key failed validation in the
330
+ # tests above, create a new session instead
331
+ unless cookie_key
332
+ cookie_key = init_ses( msg, ses_seed )
333
+ ses_status = true
334
+ end
338
335
 
339
- renew_cookie( msg, cookie_key )
336
+ renew_cookie( msg, cookie_key )
340
337
 
341
- ## Return the session status. Actually,
342
- ## the value is always true, but future
343
- ## versions might not accept invalid
344
- ## cookies as new sessions.
345
- return ses_status
346
- end
347
-
348
- def renew_cookie( msg, cookie_key )
349
- # Uses a cookie comment to tell the user what the
350
- # cookie is for, change it to anything valid in the
351
- # configuration.
352
- ses_cookie_comment = @config[:ses_cookie_comment]
353
-
354
- ## mod_rewrite changes the host header to x-forwarded-host:
355
- if msg.request.header.has_key?('x-forwarded-host')
356
- domain = msg.request.header['x-forwarded-host']
357
-
358
- ## direct access just uses host (at least mongrel
359
- ## does mod_rewrite header translation):
360
- else
361
- domain = msg.request.host
338
+ ## Return the session status. Actually,
339
+ ## the value is always true, but future
340
+ ## versions might not accept invalid
341
+ ## cookies as new sessions.
342
+ return ses_status
362
343
  end
344
+
345
+ def renew_cookie( msg, cookie_key )
346
+ # Uses a cookie comment to tell the user what the
347
+ # cookie is for, change it to anything valid in the
348
+ # configuration.
349
+ ses_cookie_comment = @config[:ses_cookie_comment]
350
+
351
+ ## mod_rewrite changes the host header to x-forwarded-host:
352
+ if msg.request.header.has_key?('x-forwarded-host')
353
+ domain = msg.request.header['x-forwarded-host']
354
+
355
+ ## direct access just uses host (at least mongrel
356
+ ## does mod_rewrite header translation):
357
+ else
358
+ domain = msg.request.host
359
+ end
363
360
 
364
- if domain == 'localhost'
365
- warn "Warning: Cookies won't be set for 'localhost'. Use '127.0.0.1' instead." if RSence.args[:debug]
366
- return
367
- end
361
+ if domain == 'localhost'
362
+ warn "Warning: Cookies won't be set for 'localhost'. Use '127.0.0.1' instead." if RSence.args[:debug]
363
+ return
364
+ end
368
365
 
369
- server_port = msg.request.port
370
-
371
- ## if the host address is a real domain
372
- ## (not just hostname or 'localhost'),
373
- ## but not an ip-address, prepend it with
374
- ## a dot to accept wildcards (useful for
375
- ## dns-load-balanced server configurations)
376
- if not @ipv4_reg.match(domain) and domain.include?('.')
377
- ses_cookie_domain = ".#{domain}"
378
- ## Otherwise, use the domain as-is
379
- else
380
- ses_cookie_domain = domain
381
- end
366
+ server_port = msg.request.port
382
367
 
383
- ## uses the timeout to declare the max age
384
- ## of the cookie, allows the browser to delete
385
- ## it, when it expires.
386
- ses_cookie_max_age = @config[:timeout_secs]
387
-
388
- ## Only match the handshaking address of rsence,
389
- ## prevents unneccessary cookie-juggling in xhr's
390
- if @config[:trust_cookies]
391
- ses_cookie_path = '/'
392
- else
393
- ses_cookie_path = ::RSence.config[:broker_urls][:hello]
394
- end
368
+ ## if the host address is a real domain
369
+ ## (not just hostname or 'localhost'),
370
+ ## but not an ip-address, prepend it with
371
+ ## a dot to accept wildcards (useful for
372
+ ## dns-load-balanced server configurations)
373
+ if not @ipv4_reg.match(domain) and domain.include?('.')
374
+ ses_cookie_domain = ".#{domain}"
375
+ ## Otherwise, use the domain as-is
376
+ else
377
+ ses_cookie_domain = domain
378
+ end
395
379
 
396
- ## Formats the cookie to string
397
- ## (through array, to keep it readable in the source)
398
- ses_cookie_arr = [
399
- "ses_key=#{cookie_key}",
400
- "Path=#{ses_cookie_path}",
401
- "Port=#{server_port}",
402
- "Max-Age=#{ses_cookie_max_age}",
403
- "Comment=#{ses_cookie_comment}",
404
- "Domain=#{ses_cookie_domain}"
405
- ]
406
-
407
- ### Sets the set-cookie header
408
- msg.response['Set-Cookie'] = ses_cookie_arr.join('; ')
409
- end
410
-
411
- ### Creates a message and checks the session
412
- def init_msg( request, response, options = { :cookies => false, :servlet => false } )
380
+ ## uses the timeout to declare the max age
381
+ ## of the cookie, allows the browser to delete
382
+ ## it, when it expires.
383
+ ses_cookie_max_age = @config[:timeout_secs]
413
384
 
414
- cookies = options[:cookies]
385
+ ## Only match the handshaking address of rsence,
386
+ ## prevents unneccessary cookie-juggling in xhr's
387
+ if @config[:trust_cookies]
388
+ ses_cookie_path = '/'
389
+ else
390
+ ses_cookie_path = ::RSence.config[:broker_urls][:hello]
391
+ end
415
392
 
416
- if options.has_key?(:query)
417
- query = options[:query]
418
- else
419
- query = request.query
393
+ ## Formats the cookie to string
394
+ ## (through array, to keep it readable in the source)
395
+ ses_cookie_arr = [
396
+ "ses_key=#{cookie_key}",
397
+ "Path=#{ses_cookie_path}",
398
+ "Port=#{server_port}",
399
+ "Max-Age=#{ses_cookie_max_age}",
400
+ "Comment=#{ses_cookie_comment}",
401
+ "Domain=#{ses_cookie_domain}"
402
+ ]
403
+
404
+ ### Sets the set-cookie header
405
+ msg.response['Set-Cookie'] = ses_cookie_arr.join('; ')
420
406
  end
407
+
408
+ ### Creates a message and checks the session
409
+ def init_msg( request, response, options = { :cookies => false, :servlet => false } )
410
+
411
+ cookies = options[:cookies]
421
412
 
422
- ## Perform old-session cleanup on all xhr:s
423
- expire_sessions
424
-
425
- ## The 'ses_id' request query key is required.
426
- ## The client defaults to '0', which means the
427
- ## client needs to be initialized.
428
- ## The client's ses_id is the server's ses_key.
429
- if not query.has_key?( 'ses_key' )
430
- return Message.new( @transporter, request, response, options )
431
- else
432
-
433
- ## get the ses_key from the request query:
434
- ses_key = query[ 'ses_key' ]
435
- # puts "ses key: #{ses_key}"
436
- ## The message object binds request, response
437
- ## and all user/session -related data to one
438
- ## object, which is passed around where
439
- ## request/response/user/session -related
440
- ## data is needed.
441
- msg = Message.new( @transporter, request, response, options )
442
-
443
- ## The client tells that its ses_key is '0',
444
- ## until the server tells it otherwise.
445
- (req_num, ses_seed) = ses_key.split(':.o.:')
446
-
447
- if req_num == '0'
413
+ if options.has_key?(:query)
414
+ query = options[:query]
415
+ else
416
+ query = request.query
417
+ end
418
+
419
+ ## Perform old-session cleanup on all xhr:s
420
+ expire_sessions
421
+
422
+ ## The 'ses_id' request query key is required.
423
+ ## The client defaults to '0', which means the
424
+ ## client needs to be initialized.
425
+ ## The client's ses_id is the server's ses_key.
426
+ if not query.has_key?( 'ses_key' )
427
+ return Message.new( @transporter, request, response, options )
428
+ else
429
+
430
+ ## get the ses_key from the request query:
431
+ ses_key = query[ 'ses_key' ]
432
+ # puts "ses key: #{ses_key}"
433
+ ## The message object binds request, response
434
+ ## and all user/session -related data to one
435
+ ## object, which is passed around where
436
+ ## request/response/user/session -related
437
+ ## data is needed.
438
+ msg = Message.new( @transporter, request, response, options )
439
+
440
+ ## The client tells that its ses_key is '0',
441
+ ## until the server tells it otherwise.
442
+ (req_num, ses_seed) = ses_key.split(':.o.:')
443
+
444
+ if req_num == '0'
448
445
 
449
- # If Broker encounters a '/hello' request, it
450
- # sets cookies to true.
451
- #
452
- # It means that a session should have its cookies
453
- # checked.
454
- #
455
- if cookies
456
- ses_status = check_cookie( msg, ses_seed )
457
- # Otherwise, a new session is created:
446
+ # If Broker encounters a '/hello' request, it
447
+ # sets cookies to true.
448
+ #
449
+ # It means that a session should have its cookies
450
+ # checked.
451
+ #
452
+ if cookies
453
+ ses_status = check_cookie( msg, ses_seed )
454
+ # Otherwise, a new session is created:
455
+ else
456
+ init_ses( msg, ses_seed )
457
+ ses_status = true
458
+ end
459
+
460
+ # for non-'0' ses_keys:
458
461
  else
459
- init_ses( msg, ses_seed )
460
- ses_status = true
461
- end
462
-
463
- # for non-'0' ses_keys:
464
- else
465
462
 
466
- ## Validate the session key
467
- ses_status = check_ses( msg, ses_seed )[0]
463
+ ## Validate the session key
464
+ ses_status = check_ses( msg, ses_seed )[0]
468
465
 
469
- ## Renew the cookie even when the request is a "x" (not "hello")
470
- if @config[:session_cookies] and ses_status
471
- renew_cookie( msg, msg.session[:cookie_key] )
472
- end
466
+ ## Renew the cookie even when the request is a "x" (not "hello")
467
+ if @config[:session_cookies] and ses_status
468
+ renew_cookie( msg, msg.session[:cookie_key] )
469
+ end
473
470
 
474
- end # /ses_key
475
-
476
- ## msg.ses_valid is false by default, meaning
477
- ## it's not valid or hasn't been initialized.
478
- msg.ses_valid = ses_status
471
+ end # /ses_key
479
472
 
480
- return msg
473
+ ## msg.ses_valid is false by default, meaning
474
+ ## it's not valid or hasn't been initialized.
475
+ msg.ses_valid = ses_status
481
476
 
482
- end # /ses_key
483
-
484
- end # /init_msg
485
-
486
-
487
- end
488
-
489
-
477
+ return msg
478
+ end # /ses_key
479
+ end # /init_msg
480
+ end
490
481
  end
491
482