1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 try:
21 from hashlib import sha1
22 sha1 = sha1
23 except ImportError:
24 from sha import new as sha1
25 from time import time
26 from random import random
27 from datetime import datetime, timedelta
28 import os
30 if hasattr(os, 'urandom'):
31 return os.urandom(30)
32 return random()
33
35 return sha1('%s%s' % (time(), _urandom())).hexdigest()
36
37
39 """I'm a user's session. Contains information about a user's session
40 a user can have several session
41 a session is associated with a cookie
42 """
43 user = ""
44 infos = {}
49
51
52 self.expiration = datetime.now()+ timedelta(1)
53 return self.expiration
54
56 return datetime.now() > self.expiration
57
59 return ('%(fullName)s [<a href="mailto:%(email)s">%(email)s</a>]' %
60 (self.infos))
61
63 delim = '-'
64 d = self.expiration.utctimetuple()
65 return '%s, %02d%s%s%s%s %02d:%02d:%02d GMT' % (
66 ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')[d.tm_wday],
67 d.tm_mday, delim,
68 ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
69 'Oct', 'Nov', 'Dec')[d.tm_mon - 1],
70 delim, str(d.tm_year), d.tm_hour, d.tm_min, d.tm_sec
71 )
72
74 """I'm the session manager. Holding the current sessions
75 managing cookies, and their expiration
76
77 KISS version for the moment:
78
79 The sessions are stored in RAM so that you have to relogin after buildbot
80 reboot
81
82 Old sessions are searched at every connection, which is not very good for
83 scaling
84
85 """
86
87
88 __shared_state = dict(sessions={},users={})
89
92
93 - def new(self, user, infos):
98
100 """remove old cookies"""
101 expired = []
102 for cookie in self.sessions:
103 s = self.sessions[cookie]
104 if s.expired():
105 expired.append(cookie)
106 for cookie in expired:
107 del self.sessions[cookie]
108
109 - def get(self, cookie):
110 self.gc()
111 if cookie in self.sessions:
112 return self.sessions[cookie]
113 return None
114
116 if cookie in self.sessions:
117 del self.sessions[cookie]
118
121