core: misc improvments:

- added some constants for message types and presence
    - removed profile authentification specific methods in favor of a more generic "validated" key
This commit is contained in:
Goffi 2014-12-10 18:36:11 +01:00
commit 05e5e1448b
4 changed files with 35 additions and 14 deletions

View file

@ -82,6 +82,22 @@ class Const(object):
## Messages ##
MESS_TYPE_INFO = 'info'
MESS_TYPE_CHAT = 'chat'
MESS_TYPE_ERROR = 'error'
MESS_TYPE_GROUPCHAT = 'groupchat'
MESS_TYPE_HEADLINE = 'headline'
MESS_TYPE_NORMAL = 'normal'
## PRESENCE ##
PRESENCE_UNAVAILABLE = 'unavailable'
PRESENCE_SHOW_AWAY = 'away'
PRESENCE_SHOW_CHAT = 'chat'
PRESENCE_SHOW_DND = 'dnd'
PRESENCE_SHOW_XA = 'xa'
PRESENCE_SHOW = 'show'
PRESENCE_STATUSES = 'statuses'
PRESENCE_PRIORITY = 'priority'
## Configuration ##
if BaseDirectory: # skipped when xdg module is not available (should not happen in backend)

View file

@ -332,8 +332,11 @@ class Memory(object):
def getProfileName(self, profile_key, return_profile_keys=False):
"""Return name of profile from keyword
@param profile_key: can be the profile name or a keywork (like @DEFAULT@)
@return: profile name or None if it doesn't exist"""
@param return_profile_keys: if True, return unmanaged profile keys (like "@ALL@"). This keys must be managed by the caller
@return: requested profile name or emptry string if it doesn't exist
"""
return self.params.getProfileName(profile_key, return_profile_keys)
def asyncCreateProfile(self, name, password=''):
@ -342,6 +345,10 @@ class Memory(object):
@param password: profile password
@return: Deferred
"""
if not name:
raise ValueError("Empty profile name")
if name[0] == '@':
raise ValueError("A profile name can't start with a '@'")
personal_key = BlockCipher.getRandomKey(base64=True) # generated once for all and saved in a PersistentDict
self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=name) # will be encrypted by setParam
d = self.params.asyncCreateProfile(name)
@ -579,7 +586,7 @@ class Memory(object):
try:
del self._entities_cache[profile][entity]
except KeyError:
log.debug("Can't delete entity [%s]: not in cache" % entity.full())
log.warning(_("Can't delete entity [{}]: not in cache").format(entity.full()))
def encryptValue(self, value, profile):
"""Encrypt a value for the given profile. The personal key must be loaded

View file

@ -179,7 +179,7 @@ class Params(object):
return defer.fail(Failure(exceptions.ConnectedProfileError))
return self.storage.deleteProfile(profile)
def getProfileName(self, profile_key, return_profile_keys = False):
def getProfileName(self, profile_key, return_profile_keys=False):
"""return profile according to profile_key
@param profile_key: profile name or key which can be

View file

@ -48,20 +48,20 @@ class ProfileManager(object):
def gotProfileCipher(profile_cipher):
if self.host.memory.auth_sessions.profileGetUnique(profile):
# case 1: profile already authenticated
return {'authenticated_profile': profile, 'caller': data['caller']}
return {'validated': C.str(True)}
self.profile_ciphers[profile] = profile_cipher
if 'profile_password' in data:
# case 2: password is provided by the caller
return self._verifyPassword(data, profile)
def check_empty_password(empty_password_result):
if 'authenticated_profile' in empty_password_result:
def check_empty_password(verify_password_result):
validated = C.bool(verify_password_result['validated'])
if validated:
# case 3: there's no password for this profile
return empty_password_result
return verify_password_result
# case 4: prompt the user for a password
def xmlui_cb(data_, profile):
data_['caller'] = data['caller']
return self._verifyPassword(data_, profile)
callback_id = self.host.registerCallback(xmlui_cb, with_data=True, one_shot=True)
@ -69,11 +69,10 @@ class ProfileManager(object):
form_ui.addPassword('profile_password', value='')
return {'xmlui': form_ui.toXml()}
check_empty_data = {'profile_password': '', 'caller': data['caller']}
check_empty_data = {'profile_password': ''}
d = self._verifyPassword(check_empty_data, profile)
return d.addCallback(check_empty_password)
assert(data['caller'])
d = self.host.memory.asyncGetStringParamA(C.PROFILE_PASS_PATH[1], C.PROFILE_PASS_PATH[0], profile_key=profile)
d.addCallback(gotProfileCipher)
d.addErrback(self.getParamError)
@ -82,7 +81,7 @@ class ProfileManager(object):
def getParamError(self, failure):
_dialog = xml_tools.XMLUI('popup', title=D_('Error'))
_dialog.addText(D_("Can't get profile parameter."))
return {'xmlui': _dialog.toXml()}
return {'xmlui': _dialog.toXml(), 'validated': C.str(False)}
@defer.inlineCallbacks
def _verifyPassword(self, data, profile):
@ -93,7 +92,6 @@ class ProfileManager(object):
@return: deferred dict
"""
assert(profile in self.profile_ciphers)
assert(data['caller'])
try:
profile_password = data[xml_tools.formEscape('profile_password')]
@ -103,10 +101,10 @@ class ProfileManager(object):
if not verified:
_dialog = xml_tools.XMLUI('popup', title=D_('Connection error'))
_dialog.addText(D_("The provided profile password doesn't match."))
defer.returnValue({'xmlui': _dialog.toXml()})
defer.returnValue({'xmlui': _dialog.toXml(), 'validated': C.str(False)})
yield self.host.memory.newAuthSession(profile_password, profile)
defer.returnValue({'authenticated_profile': profile, 'caller': data['caller']})
defer.returnValue({'validated': C.str(True)})
def _changeXMPPPassword(self, data, profile):
session_data = self._sessions.profileGetUnique(profile)