comparison src/memory/memory.py @ 1695:5a93f13c1e76

core (memory): asyncCreateProfile fix: a fake session is created to set general password param
author Goffi <goffi@goffi.org>
date Fri, 27 Nov 2015 16:52:14 +0100
parents 3c608d660f0b
children 292f9c2712f2
comparison
equal deleted inserted replaced
1694:3c608d660f0b 1695:5a93f13c1e76
457 # we want to be sure that the profile exists 457 # we want to be sure that the profile exists
458 profile = self.getProfileName(profile) 458 profile = self.getProfileName(profile)
459 459
460 self.memory_data['Profile_default'] = profile 460 self.memory_data['Profile_default'] = profile
461 461
462 def asyncCreateProfile(self, name, password=''): 462 def asyncCreateProfile(self, name, password):
463 """Create a new profile 463 """Create a new profile
464 @param name: profile name 464 @param name (unicode): profile name
465 @param password: profile password 465 @param password (unicode): profile password
466 Can be empty to disable password
466 @return: Deferred 467 @return: Deferred
467 """ 468 """
468 if not name: 469 if not name:
469 raise ValueError("Empty profile name") 470 raise ValueError("Empty profile name")
470 if name[0] == '@': 471 if name[0] == '@':
471 raise ValueError("A profile name can't start with a '@'") 472 raise ValueError("A profile name can't start with a '@'")
472 473
474 if name in self._entities_cache:
475 raise exceptions.ConflictError(u"A session for this profile exists")
476
473 d = self.params.asyncCreateProfile(name) 477 d = self.params.asyncCreateProfile(name)
474 478
475 def initPersonalKey(dummy): 479 def initPersonalKey(dummy):
476 # be sure to call this after checking that the profile doesn't exist yet 480 # be sure to call this after checking that the profile doesn't exist yet
477 personal_key = BlockCipher.getRandomKey(base64=True) # generated once for all and saved in a PersistentDict 481 personal_key = BlockCipher.getRandomKey(base64=True) # generated once for all and saved in a PersistentDict
478 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=name) # will be encrypted by setParam 482 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=name) # will be encrypted by setParam
479 483
484 def startFakeSession(dummy):
485 # avoid ProfileNotConnected exception in setParam
486 self._entities_cache[name] = None
487 self.params.loadIndParams(name)
488
489 def stopFakeSession(dummy):
490 del self._entities_cache[name]
491 self.params.purgeProfile(name)
492
480 d.addCallback(initPersonalKey) 493 d.addCallback(initPersonalKey)
494 d.addCallback(startFakeSession)
481 d.addCallback(lambda dummy: self.setParam(C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name)) 495 d.addCallback(lambda dummy: self.setParam(C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name))
496 d.addCallback(stopFakeSession)
497 d.addCallback(lambda dummy: self.auth_sessions.profileDelUnique(name))
482 return d 498 return d
483 499
484 def asyncDeleteProfile(self, name, force=False): 500 def asyncDeleteProfile(self, name, force=False):
485 """Delete an existing profile 501 """Delete an existing profile
486 @param name: Name of the profile 502 @param name: Name of the profile