comparison src/memory/memory.py @ 1212:628e320eab1f

memory: Sessions.newSession can be called with a forced session ID
author souliane <souliane@mailoo.org>
date Sun, 21 Sep 2014 13:10:55 +0200
parents 96fb74a4714d
children b5928601d7aa
comparison
equal deleted inserted replaced
1211:9355f48f979e 1212:628e320eab1f
45 @param timeout: nb of seconds before session destruction 45 @param timeout: nb of seconds before session destruction
46 """ 46 """
47 self._sessions = dict() 47 self._sessions = dict()
48 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT 48 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT
49 49
50 def newSession(self, session_data=None, profile=None): 50 def newSession(self, session_data=None, session_id=None, profile=None):
51 """ Create a new session 51 """ Create a new session
52 @param session_data: mutable data to use, default to a dict 52 @param session_data: mutable data to use, default to a dict
53 @param session_id (str): force the session_id to the given string
53 @param profile: if set, the session is owned by the profile, 54 @param profile: if set, the session is owned by the profile,
54 and profileGet must be used instead of __getitem__ 55 and profileGet must be used instead of __getitem__
55 @return: session_id, session_data 56 @return: session_id, session_data
56 """ 57 """
57 session_id = str(uuid4()) 58 if session_id is None:
59 session_id = str(uuid4())
58 timer = reactor.callLater(self.timeout, self._purgeSession, session_id) 60 timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
59 if session_data is None: 61 if session_data is None:
60 session_data = {} 62 session_data = {}
61 self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile) 63 self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile)
62 return session_id, session_data 64 return session_id, session_data
266 @param profile: %(doc_profile)s 268 @param profile: %(doc_profile)s
267 @return: a deferred None value 269 @return: a deferred None value
268 """ 270 """
269 def gotPersonalKey(personal_key): 271 def gotPersonalKey(personal_key):
270 """Create the session for this profile and store the personal key""" 272 """Create the session for this profile and store the personal key"""
271 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile) 273 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile)
272 log.debug('auth session created for profile %s' % profile) 274 log.debug('auth session created for profile %s' % profile)
273 275
274 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load() 276 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load()
275 d.addCallback(lambda data: BlockCipher.decrypt(key, data[C.MEMORY_CRYPTO_KEY])) 277 d.addCallback(lambda data: BlockCipher.decrypt(key, data[C.MEMORY_CRYPTO_KEY]))
276 return d.addCallback(gotPersonalKey) 278 return d.addCallback(gotPersonalKey)
317 @param name: profile name 319 @param name: profile name
318 @param password: profile password 320 @param password: profile password
319 @return: Deferred 321 @return: Deferred
320 """ 322 """
321 personal_key = BlockCipher.getRandomKey(base64=True) # generated once for all and saved in a PersistentDict 323 personal_key = BlockCipher.getRandomKey(base64=True) # generated once for all and saved in a PersistentDict
322 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, name) # will be encrypted by setParam 324 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=name) # will be encrypted by setParam
323 d = self.params.asyncCreateProfile(name) 325 d = self.params.asyncCreateProfile(name)
324 d.addCallback(lambda dummy: self.setParam(C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name)) 326 d.addCallback(lambda dummy: self.setParam(C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name))
325 return d 327 return d
326 328
327 def asyncDeleteProfile(self, name, force=False): 329 def asyncDeleteProfile(self, name, force=False):