...
 
Commits (3)
......@@ -484,7 +484,7 @@ def get_jid_from_account(account_name):
return jid
def get_account_from_jid(jid):
for account in app.config.get_per('accounts'):
for account in config.get_per('accounts'):
if jid == get_jid_from_account(account):
return account
......
......@@ -68,63 +68,51 @@ class SecretPasswordStorage(PasswordStorage):
log.debug('getting password')
return self.keyring.get_password('gajim', account_name)
class ConfigPasswordStorage(PasswordStorage):
""" Store password in Gajim's config """
def get_password(self, account_name):
pw = app.config.get_per('accounts', account_name, 'password')
if pw.startswith(SecretPasswordStorage.identifier):
pw = None
return pw
def save_password(self, account_name, password):
return app.config.set_per('accounts', account_name, 'password', password)
class PasswordStorageManager(PasswordStorage):
"""Access all the implemented password storage backends, knowing which ones
are available and which we prefer to use.
Also implements storing directly in gajim config."""
def __init__(self):
self.preferred_backend = None
self.secret = None
self.connect_backends()
self.set_preferred_backend()
def connect_backends(self):
"""Initialize backend connections, determining which ones are available.
"""
# TODO: handle disappearing backends
self.backends = []
if keyring and app.config.get('use_keyring'):
self.backends.append(SecretPasswordStorage())
if app.config.get('use_keyring') and keyring:
self.secret = SecretPasswordStorage()
self.backends.append(ConfigPasswordStorage())
def get_password(self, account_name):
pw = app.config.get_per('accounts', account_name, 'password')
if not pw:
return pw
if pw.startswith(SecretPasswordStorage.identifier) and self.secret:
backend = self.secret
else:
backend = None
if backend:
pw = backend.get_password(account_name)
if backend != self.preferred_backend:
# migrate password to preferred_backend
self.save_password(account_name, pw)
# TODO: remove from old backend
return pw
for be in self.backends:
pwd = be.get_password(account_name)
if pwd:
return pwd
return None
def save_password(self, account_name, password):
if self.preferred_backend:
if self.preferred_backend.save_password(account_name, password):
app.config.set_per('accounts', account_name, 'password',
self.preferred_backend.identifier)
if account_name in app.connections:
app.connections[account_name].password = password
return True
for be in self.backends:
if be.save_password(account_name, password):
if be != self.backends[-1]:
# remove the password from the ConfigPasswordStorage when
# it's saved in a secure storage
self.backends[-1].save_password(account_name, None)
app.config.set_per('accounts', account_name, 'password', password)
if account_name in app.connections:
app.connections[account_name].password = password
return True
return True
def set_preferred_backend(self):
if self.secret:
self.preferred_backend = self.secret
else:
self.preferred_backend = None
return False
passwordStorageManager = None
......