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

Contents of /trunk/harmonious/_sessionutils.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations) (download) (as text)
Thu Jun 30 10:21:39 2005 UTC (9 years, 4 months ago) by port001
File MIME type: text/x-python
File size: 3038 byte(s)
Use the host string when generating a session ID
1 __id__ = '$Id$'
2 __modulename__ = '_sessionutils'
3
4 import sha
5 import hmac
6 from time import time
7 from random import Random
8
9 from harmonious import harm
10
11 class Session:
12
13 def __init__(self):
14
15 self.__id = ''
16 self.attrs = {}
17 self.persistent = False
18 self.expires = ''
19
20 def _set_id(self, id):
21
22 self.__id = id
23
24 def get_id(self):
25
26 return self.__id
27
28 class SessionStorage:
29
30 def __init__(self, type):
31
32 # type is ['ram', 'file', 'cookie']
33 # TODO: Implement locking. An instance of this class with be shared across all threads
34 # TODO: We need a cleanup method called every n houres
35
36 self.__type = str(type).lower()
37
38 if self.__type == 'ram':
39
40 self.__store = {}
41
42 def has_expired(self, sessionId):
43
44 if not self.has_session(sessionId):
45
46 return True
47
48 if self.__store[sessionId]['expirationDate'] <= int(time()) and self.__store[sessionId]['persistent'] == False:
49
50 return True
51
52 return False
53
54 def has_session(self, sessionId):
55
56 if sessionId in self.__store.keys():
57
58 return True
59
60 return False
61
62 def remove_session(self, sessionId):
63
64 if self.has_session(sessionId):
65
66 self.__store.pop(sessionId)
67
68 def store_session(self, sessionId=None, expirationDate=None):
69
70 self.__store[sessionId] = {'expirationDate': 0, 'attrs': {}, 'persistent': False}
71 self.__store[sessionId]['expirationDate'] = int(time() + (expirationDate * 60))
72
73 if self.__type == 'file':
74
75 pass
76 # self.__flush_file()
77
78 def set_persistent(self, sessionId, flag):
79
80 self.__store[sessionId]['persistent'] = flag
81
82 def load_session(self, sessionId):
83
84 if self.__type == 'ram':
85
86 if self.__store.has_key(sessionId):
87
88 if self.has_expired(sessionId):
89
90 self.__store.pop(sessionId)
91 return None
92
93 return self.__store[sessionId]
94
95 else:
96
97 return None
98
99 def update_session(self, sessionId, **args):
100
101 if sessionId in self.__store.keys():
102
103 for key in args.keys():
104
105 if key in self.__store[sessionId].keys():
106
107 self.__store[sessionId][key] = args[key]
108
109 elif key in self.__store[sessionId]['attrs'].keys():
110
111 self.__store[sessionId]['attrs'][key] = args[key]
112
113 else:
114
115 self.__store[sessionId]['attrs'][key] = args[key]
116
117 def _gen_hash(sid):
118
119 return hmac.new(harm.config.server['SecretKey'], sid, sha).hexdigest()[:10]
120
121 def _gen_sid(client_addr, host):
122
123 ran = Random()
124
125 return sha.new(str(time() * ran.randint(0, 10000000000000000000000000)) + \
126 str(client_addr) + str(host)).hexdigest()[:10]
127
128 # TODO: Just using the client IP addr isn't great.
129
130 def gen_session_id(client_addr, host):
131
132 sid = _gen_sid(client_addr, host)
133
134 return sid + _gen_hash(sid)

Properties

Name Value
svn:keyword Id
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20