Commit 8247b23d authored by André's avatar André

Passwords: Make SecretPasswordStorage static

parent 863ac751
Pipeline #5260 passed with stages
in 3 minutes and 6 seconds
......@@ -34,6 +34,9 @@ backends = keyring.backend.get_all_keyring()
for backend in backends:
log.info('Found keyring backend: %s', backend)
keyring_backend = keyring.get_keyring()
log.info('Select %s backend', keyring_backend)
KEYRING_AVAILABLE = any(keyring.core.recommended(backend)
for backend in backends)
......@@ -43,41 +46,40 @@ class SecretPasswordStorage:
Store password using Keyring
"""
def __init__(self):
self.keyring = keyring.get_keyring()
log.info('Select %s backend', self.keyring)
def save_password(self, account_name, password):
@staticmethod
def save_password(account_name, password):
if not KEYRING_AVAILABLE:
log.warning('No recommended keyring backend available.'
'Passwords cannot be stored.')
return True
try:
log.info('Save password to keyring')
self.keyring.set_password('gajim', account_name, password)
keyring_backend.set_password('gajim', account_name, password)
return True
except Exception:
log.exception('Save password failed')
return False
def get_password(self, account_name):
@staticmethod
def get_password(account_name):
log.info('Request password from keyring')
if not KEYRING_AVAILABLE:
return
try:
# For security reasons remove clear-text password
ConfigPasswordStorage.delete_password(account_name)
return self.keyring.get_password('gajim', account_name)
return keyring_backend.get_password('gajim', account_name)
except Exception:
log.exception('Request password failed')
return
def delete_password(self, account_name):
@staticmethod
def delete_password(account_name):
log.info('Remove password from keyring')
if not KEYRING_AVAILABLE:
return
try:
return self.keyring.delete_password('gajim', account_name)
return keyring_backend.delete_password('gajim', account_name)
except keyring.errors.PasswordDeleteError as error:
log.warning('Removing password failed: %s', error)
except Exception:
......@@ -104,12 +106,9 @@ class ConfigPasswordStorage:
return True
secretPasswordStorage = SecretPasswordStorage()
def get_password(account_name):
if app.config.get('use_keyring'):
return secretPasswordStorage.get_password(account_name)
return SecretPasswordStorage.get_password(account_name)
return ConfigPasswordStorage.get_password(account_name)
......@@ -121,7 +120,7 @@ def save_password(account_name, password):
return True
if app.config.get('use_keyring'):
return secretPasswordStorage.save_password(account_name, password)
return SecretPasswordStorage.save_password(account_name, password)
return ConfigPasswordStorage.save_password(account_name, password)
......@@ -130,5 +129,5 @@ def delete_password(account_name):
app.connections[account_name].password = None
if app.config.get('use_keyring'):
return secretPasswordStorage.delete_password(account_name)
return SecretPasswordStorage.delete_password(account_name)
return ConfigPasswordStorage.delete_password(account_name)
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