/[glsr]/trunk/harmonious/_sessionutils.py
Gentoo

Contents of /trunk/harmonious/_sessionutils.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 97 - (show annotations) (download) (as text)
Sat Jul 30 09:29:15 2005 UTC (9 years ago) by port001
File MIME type: text/x-python
File size: 4288 byte(s)
Minor fixes
1 __id__ = '$Id$'
2 __modulename__ = '_sessionutils'
3
4 import sha
5 import hmac
6 import threading
7 from time import time
8 from random import Random
9 from types import BooleanType
10
11 from harmonious import server
12
13 class Session:
14
15 def __init__(self):
16
17 self.__id = ''
18 self.attrs = {}
19 self.__persistent = False
20 self.expires = ''
21 self.__removed = False
22
23 def _set_id(self, id):
24
25 self.__id = id
26
27 def get_id(self):
28
29 return self.__id
30
31 def set_persistent(self, flag):
32
33 if type(flag) == BooleanType:
34
35 self.__persistent = flag
36
37 def get_persistent(self):
38
39 return self.__persistent
40
41 def set_removed(self):
42
43 self.__removed = True
44
45 server._sessionStore.remove_session(self.__id)
46
47 def get_removed(self):
48
49 return self.__removed
50
51 class SessionStorage:
52
53 def __init__(self, type):
54
55 # type is ['ram', 'file', 'cookie']
56 # TODO: Implement locking. An instance of this class with be shared across all threads
57 # TODO: We need a cleanup method called every n houres
58
59 self.__type = str(type).lower()
60 self.__lock = threading.Lock()
61
62 if self.__type == 'ram':
63
64 self.__store = {}
65
66 def has_expired(self, sessionId):
67
68 if not self.has_session(sessionId):
69
70 return True
71
72 self.__lock.acquire()
73 if self.__store[sessionId]['expirationDate'] <= int(time()) and self.__store[sessionId]['persistent'] == False:
74
75 self.__lock.release()
76 return True
77
78 self.__lock.release()
79 return False
80
81 def has_session(self, sessionId):
82
83 self.__lock.acquire()
84 if self.__store.has_key(sessionId):
85
86 self.__lock.release()
87 return True
88
89 self.__lock.release()
90 return False
91
92 def remove_session(self, sessionId):
93
94 if self.has_session(sessionId):
95
96 self.__lock.acquire()
97 self.__store.pop(sessionId)
98 self.__lock.release()
99
100 def store_session(self, sessionId=None, expirationDate=None):
101
102 self.__lock.acquire()
103 self.__store[sessionId] = {'expirationDate': 0, 'attrs': {}, 'persistent': False}
104 self.__store[sessionId]['expirationDate'] = int(time() + (expirationDate * 60))
105 self.__lock.release()
106
107 if self.__type == 'file':
108
109 pass
110 # self.__flush_file()
111
112 def set_persistent(self, sessionId, flag):
113
114 if self.has_session(sessionId):
115 self.__lock.acquire()
116 self.__store[sessionId]['persistent'] = flag
117 self.__lock.release()
118
119 def load_session(self, sessionId):
120
121 if self.__type == 'ram':
122
123 self.__lock.acquire()
124 if self.__store.has_key(sessionId):
125
126 self.__lock.release()
127 if self.has_expired(sessionId):
128
129 self.__lock.acquire()
130 self.__store.pop(sessionId)
131 self.__lock.release()
132 return None
133
134 self.__lock.acquire()
135 x = self.__store[sessionId]
136 self.__lock.release()
137 return x
138
139 else:
140
141 self.__lock.release()
142 return None
143
144 def update_session(self, sessionId, **args):
145
146 self.__lock.acquire()
147
148 if sessionId in self.__store.keys():
149
150 for key in args.keys():
151
152 if key in self.__store[sessionId].keys():
153
154 self.__store[sessionId][key] = args[key]
155
156 elif key in self.__store[sessionId]['attrs'].keys():
157
158 self.__store[sessionId]['attrs'][key] = args[key]
159
160 else:
161
162 self.__store[sessionId]['attrs'][key] = args[key]
163
164 self.__lock.release()
165
166 def gen_hash(sid):
167
168 return hmac.new(server.config.server['SecretKey'], sid, sha).hexdigest()[:10]
169
170 def _gen_sid(client_addr, host):
171
172 ran = Random()
173
174 return sha.new(str(time() * ran.randint(0, 10000000000000000000000000)) + \
175 str(client_addr) + str(host)).hexdigest()
176
177 # TODO: Just using the client IP addr isn't great.
178
179 def gen_session_id(client_addr, host):
180
181 sid = _gen_sid(client_addr, host)
182
183 return sid + gen_hash(sid)

Properties

Name Value
svn:keyword Id
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20