2006-10-06 01:48:54 +02:00
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Gustavo J. A. M. Carneiro <gjcarneiro@gmail.com>
|
|
|
|
## Copyright (C) 2006 Nikos Kouremenos <kourem@gmail.com>
|
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
|
|
|
__all__ = ['get_password', 'save_password']
|
|
|
|
|
|
|
|
from common import gajim
|
|
|
|
|
2006-12-29 19:30:18 +01:00
|
|
|
USER_HAS_GNOMEKEYRING = False
|
|
|
|
USER_USES_GNOMEKEYRING = False
|
2007-01-21 16:13:57 +01:00
|
|
|
gnomekeyring = None
|
2006-10-06 01:48:54 +02:00
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
class PasswordStorage(object):
|
|
|
|
def get_password(self, account_name):
|
|
|
|
raise NotImplementedError
|
|
|
|
def save_password(self, account_name, password):
|
|
|
|
raise NotImplementedError
|
2006-11-01 17:49:49 +01:00
|
|
|
|
2006-10-06 01:48:54 +02:00
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
class SimplePasswordStorage(PasswordStorage):
|
2006-10-06 01:48:54 +02:00
|
|
|
def get_password(self, account_name):
|
2006-12-27 22:13:42 +01:00
|
|
|
passwd = gajim.config.get_per('accounts', account_name, 'password')
|
2006-12-28 20:03:55 +01:00
|
|
|
if passwd and passwd.startswith('gnomekeyring:'):
|
2006-12-27 22:13:42 +01:00
|
|
|
return None # this is not a real password, it's a gnome keyring token
|
|
|
|
else:
|
|
|
|
return passwd
|
2006-10-06 01:48:54 +02:00
|
|
|
|
|
|
|
def save_password(self, account_name, password):
|
2006-10-09 12:17:53 +02:00
|
|
|
gajim.config.set_per('accounts', account_name, 'password', password)
|
2006-10-06 01:48:54 +02:00
|
|
|
gajim.connections[account_name].password = password
|
|
|
|
|
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
class GnomePasswordStorage(PasswordStorage):
|
2006-12-27 22:13:42 +01:00
|
|
|
def __init__(self):
|
|
|
|
# self.keyring = gnomekeyring.get_default_keyring_sync()
|
|
|
|
|
|
|
|
## above line commented and code below inserted as workaround
|
|
|
|
## for the bug http://bugzilla.gnome.org/show_bug.cgi?id=363019
|
|
|
|
self.keyring = "default"
|
|
|
|
try:
|
|
|
|
gnomekeyring.create_sync(self.keyring, None)
|
|
|
|
except gnomekeyring.AlreadyExistsError:
|
2006-11-01 17:49:49 +01:00
|
|
|
pass
|
2006-10-06 01:48:54 +02:00
|
|
|
|
|
|
|
def get_password(self, account_name):
|
|
|
|
conf = gajim.config.get_per('accounts', account_name, 'password')
|
2006-10-07 13:35:44 +02:00
|
|
|
if conf is None:
|
|
|
|
return None
|
2006-10-06 01:48:54 +02:00
|
|
|
try:
|
2006-10-06 01:54:37 +02:00
|
|
|
unused, auth_token = conf.split('gnomekeyring:')
|
2006-10-06 01:48:54 +02:00
|
|
|
auth_token = int(auth_token)
|
|
|
|
except ValueError:
|
|
|
|
password = conf
|
|
|
|
## migrate the password over to keyring
|
2006-10-11 22:38:23 +02:00
|
|
|
try:
|
|
|
|
self.save_password(account_name, password, update=False)
|
|
|
|
except gnomekeyring.NoKeyringDaemonError:
|
|
|
|
## no keyring daemon: in the future, stop using it
|
|
|
|
set_storage(SimplePasswordStorage())
|
2006-10-06 01:48:54 +02:00
|
|
|
return password
|
|
|
|
try:
|
2006-10-06 01:54:37 +02:00
|
|
|
return gnomekeyring.item_get_info_sync(self.keyring,
|
|
|
|
auth_token).get_secret()
|
2006-10-06 01:48:54 +02:00
|
|
|
except gnomekeyring.DeniedError:
|
|
|
|
return None
|
2006-10-11 22:38:23 +02:00
|
|
|
except gnomekeyring.NoKeyringDaemonError:
|
|
|
|
## no keyring daemon: in the future, stop using it
|
|
|
|
set_storage(SimplePasswordStorage())
|
|
|
|
return None
|
2006-11-01 17:49:49 +01:00
|
|
|
|
2006-10-06 01:48:54 +02:00
|
|
|
def save_password(self, account_name, password, update=True):
|
2006-10-06 01:54:37 +02:00
|
|
|
display_name = _('Gajim account %s') % account_name
|
2006-10-06 01:48:54 +02:00
|
|
|
attributes = dict(account_name=str(account_name), gajim=1)
|
|
|
|
auth_token = gnomekeyring.item_create_sync(
|
|
|
|
self.keyring, gnomekeyring.ITEM_GENERIC_SECRET,
|
|
|
|
display_name, attributes, password, update)
|
2006-10-06 02:04:07 +02:00
|
|
|
token = 'gnomekeyring:%i' % auth_token
|
2006-10-06 01:48:54 +02:00
|
|
|
gajim.config.set_per('accounts', account_name, 'password', token)
|
|
|
|
|
|
|
|
|
|
|
|
storage = None
|
|
|
|
def get_storage():
|
|
|
|
global storage
|
2006-10-06 02:04:07 +02:00
|
|
|
if storage is None: # None is only in first time get_storage is called
|
2007-01-21 16:13:57 +01:00
|
|
|
if gajim.config.get('use_gnomekeyring'):
|
|
|
|
global gnomekeyring
|
|
|
|
try:
|
|
|
|
import gnomekeyring
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
global USER_HAS_GNOMEKEYRING
|
|
|
|
global USER_USES_GNOMEKEYRING
|
|
|
|
USER_HAS_GNOMEKEYRING = True
|
|
|
|
if gnomekeyring.is_available():
|
|
|
|
USER_USES_GNOMEKEYRING = True
|
|
|
|
else:
|
|
|
|
USER_USES_GNOMEKEYRING = False
|
2006-10-20 12:14:15 +02:00
|
|
|
if USER_USES_GNOMEKEYRING:
|
2006-10-11 22:38:23 +02:00
|
|
|
try:
|
|
|
|
storage = GnomePasswordStorage()
|
|
|
|
except gnomekeyring.NoKeyringDaemonError:
|
|
|
|
storage = SimplePasswordStorage()
|
2006-10-06 01:48:54 +02:00
|
|
|
else:
|
|
|
|
storage = SimplePasswordStorage()
|
|
|
|
return storage
|
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
def set_storage(storage_):
|
|
|
|
global storage
|
|
|
|
storage = storage_
|
|
|
|
|
|
|
|
|
2006-10-06 01:48:54 +02:00
|
|
|
def get_password(account_name):
|
|
|
|
return get_storage().get_password(account_name)
|
|
|
|
|
|
|
|
def save_password(account_name, password):
|
|
|
|
return get_storage().save_password(account_name, password)
|