-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.py
449 lines (348 loc) · 14.6 KB
/
sessions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# -*- coding: utf-8 -*-
import time
import datetime
import random
import cgi
from openid.store import memstore
import openid.store.filestore
import openid.fetchers
from tekio.utils import quoteattr
from tekio.utils import tuple_cookie
from tekio.objets.base import create_element
class My_Session():
def __init__(self, log, config, expire, glob, database):
self.config=config
self.log=log
self.expire=expire
self.initdatas()
if config.flag_server.has_key('ACTIVE_USER'):
if config.flag_server['ACTIVE_USER']:
self.set_user(config.flag_server['ACTIVE_USER'],glob,database)
def initdatas(self):
self.user=""
self.id_user=-1
self.openid=None
self.action_openid=None
self.dico={}
#todo add : preference user.
self.groupes=[]
self.responsabilites=[]
self.is_admin=False
self.is_edit=False
self.new_url=''
self.futur_new_url=''
self.langues=self.config.langues
self.langues_interface=self.config.langues_interface
self.langue=self.langues[0]
self.langue_interface=self.langues_interface[0]
def set_new_url(self,u):
self.new_url=u
def set_futur_new_url(self,u):
self.new_url=u
def expired(self):
""" verifie la date d'expiration de la session. """
if datetime.datetime.now()>self.expire:
return True
return False
def set_expire(self,d):
self.expire=d
def deconnecte(self):
self.initdatas()
def set_user(self,user,glob,database):
self.openidinit=user
self.openidtekiolang=None
for l in ['fr','es','en']:
if user.find('http://%s.openid.tekio.org/id/' % l)==0:
dec=user.split('http://%s.openid.tekio.org/id/' % l,1)
user='http://openid.tekio.org/id/%s' % dec[1]
self.openidtekiolang=l
self.user=user
self.groupes=[]
self.responsabilites=[]
self.is_admin=False
self.is_edit=False
if self.user=='':
# ?? cas possible ??
return
self.id_user=glob.openids.get(self.user,-2)
if self.id_user==-2:
#TODO option pour activer/desactiver autocreate user
create_element(glob,database,40,0,{},{'openid':self.user,})
glob.get_population(database)
self.id_user=glob.openids.get(self.user,-2)
self.infos_utilisateur=glob.objets[self.id_user]
self.groupes=self.infos_utilisateur['groupes']
self.responsabilites=self.infos_utilisateur['responsabilites']
if glob.groupes["admin"] in self.groupes:
self.is_admin=True
if glob.groupes["editor"] in self.groupes:
self.is_edit=True
self.proprietes=self.infos_utilisateur['proprietes']
l=self.proprietes.get('langue',None)
self.change_langue_interface(l)
return
def change_langue_interface(self,l):
if l:
if l in self.langues_interface:
self.langue_interface=l
self.langues_interface.remove(l)
self.langues_interface.insert(0,l)
return self.action_openid
def get_action_openid(self):
return self.action_openid
def get_process_openid_url(self):
return self.process_openid_url
def get_openid(self):
return self.openid
def get_user(self):
return self.user
def get_new_url(self):
return self.new_url
def get_futur_new_url(self):
return self.futur_new_url
def get_langue_interface(self):
return self.langue_interface
def get_langue(self):
return self.langue
def set_action_openid(self,value):
self.action_openid=value
def set_process_openid_url(self,value):
self.process_openid_url=value
def set_openid(self,value):
self.openid=value
class Sessions:
def __init__(self, log, config):
#intialisation des sessions
# TODO BEST stoquer dans une base sql pour ne pas perdre les sessions au redemarrage
self.config=config
self.log=log
self.sessions={}
self.sessions_openid={}
#intialisation base de donnee open id
#TEST CON MEMORY STORE..
self.openid_store = openid.store.filestore.FileOpenIDStore(config.path["openid_consumer"])
#self.openid_store = memstore.MemoryStore()
# je ne sais pas a quoi correspond les fetchers, lie a weakssl !?!
#openid.fetchers.setDefaultFetcher(openid.fetchers.Urllib2Fetcher())
def get_session(self,interfaces):
""" recupere la session courrant dans la base de session du serveur,
crée un session, si pas de session courrante """
socket = interfaces[0]
sessions = interfaces[1]
traduct = interfaces[2]
database = interfaces[3]
fs_svn = interfaces[4]
log = interfaces[5]
config = interfaces[6]
datas = interfaces[7]
glob = interfaces[8]
cookies=socket.get_cookies()
# date previsionnelle d'expiration du cookie
d=datetime.datetime.now()+datetime.timedelta(0,self.config.params['session_secondes'])
cs=None
if cookies:
#recuperation du cookie
cs=cookies.get(self.config.cookies['session'],None)
my_session=None
if cs:
alea=cs.value
if self.sessions.has_key(alea):
my_session=self.sessions[alea]
if my_session.expired():
#detruit la session si elle a expiré.
self.sessions[alea]=None
else:
my_session.set_expire(d)
# Si pas de session actuelement, creation d'une session.
if not my_session:
alea=str(random.randrange(1000000000))
alea+=str(random.randrange(1000000000))
my_session=My_Session(self.log,self.config,d,glob,database)
self.sessions[alea]=my_session
if my_session.user=='':
if cookies:
cs=cookies.get(self.config.cookies['session_auto_openid'],None)
if cs:
userautoopenid=cs.value
if userautoopenid!='':
self.envoie_cookie_session(socket,alea,d)
self.openid_step_init_verify(interfaces,userautoopenid,my_session)
my_session.set_futur_new_url(socket.path)
return None
action_openid=my_session.get_action_openid()
if action_openid=='process':
self.openid_step_process(interfaces,my_session)
else:
self.envoie_cookie_session(socket,alea,d)
return my_session
def envoie_cookie_session(self,socket,value,date):
# Actualisation du cookie de session.
(k,v)=tuple_cookie(self.config.cookies['session'],
value,
date=date,
path='/')
socket.add_header(k,v)
def getConsumer(self,my_session,socket,cookies):
return openid.consumer.consumer.Consumer(self.getSessionOpenId(my_session,socket,cookies), self.openid_store)
def getSessionOpenId(self,my_session,socket,cookies):
myopenid=my_session.get_openid()
if myopenid is not None:
return myopenid
name_cookie=self.config.cookies['session_openid']
sid=None
if cookies:
cmsid=cookies.get(name_cookie,None)
if cmsid:
sid=cmsid.value
# If a session id was not set, create a new one
if sid is None:
sid = openid.cryptutil.randomString(16, '0123456789abcdef')
session = None
else:
try:
session = self.sessions_openid.get(sid)
except:
session = None
# If no session exists for this session ID, create one
if session is None:
session = {'id': sid }
my_session.set_openid (session)
(k,v)=tuple_cookie(name_cookie,
sid,
path='/')
socket.add_header(k,v)
self.sessions_openid[sid] = session
return session
def crea_autoconnect(self,socket,value):
(k,v)=tuple_cookie(self.config.cookies['session_auto_openid'],
value,
path='/')
socket.add_header(k,v)
def annule_autoconnect(self,socket,config):
(k,v)=tuple_cookie(config.cookies['session_auto_openid'],
'',
path='/')
socket.add_header(k,v)
def openid_step_init_verify(self,interfaces,userautoopenid,my_session):
socket = interfaces[0]
sessions = interfaces[1]
traduct = interfaces[2]
database = interfaces[3]
fs_svn = interfaces[4]
log = interfaces[5]
config = interfaces[6]
datas = interfaces[7]
glob = interfaces[8]
cookies=socket.get_cookies()
oidconsumer = self.getConsumer(my_session,socket,cookies)
auto=True
if userautoopenid:
openid_url=userautoopenid
else:
#passage par le formulaire de connexion !
auto=False
openid_url = socket.input_text_value('openid')
if openid_url.find('/')==-1:
lang=config.langues[0]
openid_url='http://%s.openid.tekio.org/id/%s' % (lang,openid_url)
if openid_url.find('//')==-1:
openid_url='http://'+openid_url
autoconnect = socket.checkbox_value('autoconnect')
if autoconnect=='yes':
self.crea_autoconnect(socket,openid_url)
my_session.set_futur_new_url('')
try:
requestoid = oidconsumer.begin(openid_url)
except openid.consumer.consumer.DiscoveryFailure, exc:
self.annule_autoconnect(socket,config)
socket.send_datas('Erreur de connexion sur openid 1 : %s ' % openid_url)
socket.send_datas('Autoconnect openid detruit. ')
return
if not requestoid:
if userautoopenid:
self.annule_autoconnect(socket,config)
socket.send_datas('Erreur de connexion sur openid 2 : %s ' % openid_url)
socket.send_datas('Autoconnect openid detruit. ' % openid_url)
return
else:
self.annule_autoconnect(socket,config)
socket.send_datas('Identifiant openid invalide ou inactif : %s ' % openid_url)
socket.send_datas('Autoconnect openid detruit. ' % openid_url)
return
# ?? DATA ? PAPE
#self.requestRegistrationData(requestoid)
#self.requestPAPEDetails(requestoid)
#immediate=True
immediate=False
my_session.set_action_openid('process')
my_session.set_process_openid_url(openid_url)
trust_root = self.config.http['url_base']+'/'
return_to = datas.url_base+my_session.get_futur_new_url()+'/'
if not requestoid.shouldSendRedirect():
print 'NE DOIT PAS REDURECTURL !!! je le fait quand meme ?!!'
direct_url = requestoid.redirectURL(trust_root, return_to, immediate=immediate)
socket.redirection_http(direct_url)
def openid_step_process(self,interfaces,my_session):
socket = interfaces[0]
sessions = interfaces[1]
traduct = interfaces[2]
database = interfaces[3]
fs_svn = interfaces[4]
log = interfaces[5]
config = interfaces[6]
datas = interfaces[7]
glob = interfaces[8]
cookies=socket.get_cookies()
my_session.set_action_openid(None)
oidconsumer = self.getConsumer(my_session,socket,cookies)
url = datas.url_base+my_session.get_futur_new_url()+'/'
info = oidconsumer.complete(socket.get_args(), url)
if info.status == openid.consumer.consumer.FAILURE and info.identity_url:
self.annule_autoconnect(socket,config)
fmt = "Verification of %s failed: %s"
message = fmt % (cgi.escape(info.identity_url),
info.message)
elif info.status == openid.consumer.consumer.SUCCESS:
openidfromform=my_session.get_process_openid_url()
if cgi.escape(info.identity_url)!=openidfromform:
self.annule_autoconnect(socket,config)
message = "retour id incorrectu du server !!!"
return
fmt = "You have successfully verified %s as your identity."
message = fmt % (cgi.escape(info.identity_url),)
if info.endpoint.canonicalID:
message += (" This is an i-name, and its persistent ID is %s"
% (cgi.escape(info.endpoint.canonicalID),))
my_session.set_user(openidfromform,glob,database)
return
elif info.status == openid.consumer.consumer.CANCEL:
# cancelled
self.annule_autoconnect(socket,config)
message = 'Verification cancelled'
elif info.status == openid.consumer.consumer.SETUP_NEEDED:
my_session.set_action_openid('process')
OK=2
if info.setup_url:
message = '<a href=%s>%s</a>' % ( quoteattr(info.setup_url),
'Vous devez faire aller sur le serveur d\'identification')
socket.redirection_http(info.setup_url, message)
return
else:
# todo non-immediate mode.
message = 'Setup needed'
else:
self.annule_autoconnect(socket,config)
message = 'Verification failed. Dont know why.'
socket.redirection_http("/" , message)
def deconnecte(self,my_session,socket,config):
my_session.deconnecte()
self.annule_autoconnect(socket,config)
def stop_sessions(self):
for alea in self.sessions.keys():
self.sessions[alea]=None
#TODO CLEAN SESSION pour virer toutes les sessions trop vielles...
def affiche_user_openid(useropenid):
tmp=useropenid.split('/')
site='/'.join(tmp[:-1])
name=tmp[-1]
return '<div class="papabull"><div class="infobull">%s</div><a href="/user" >%s</a></div>' % (site,name)