Commit 753554e2 authored by jo-so's avatar jo-so

Rework password classes

Now, there's a new class `ConfigPasswordStorage` that handles the storing
of the password in Gajim's config. The `PasswordStorageManager` maintains
a list of password storages and schedules the requests between them. This
makes it easier to extend to list for another password storage.
parent e4cd9205
Pipeline #1462 failed with stages
in 0 seconds
......@@ -68,6 +68,19 @@ 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.
......@@ -75,46 +88,31 @@ class PasswordStorageManager(PasswordStorage):
def __init__(self):
# TODO: handle disappearing backends
if app.config.get('use_keyring') and keyring:
self.secret = SecretPasswordStorage()
else:
self.secret = None
self.backends = []
if keyring and app.config.get('use_keyring'):
self.backends.append(SecretPasswordStorage())
if self.secret:
self.preferred_backend = self.secret
else:
self.preferred_backend = None
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
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)
return True
app.config.set_per('accounts', account_name, 'password', password)
if account_name in app.connections:
app.connections[account_name].password = password
return True
return False
passwordStorageManager = None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment