revert [10593]. see #4449
[gajim.git] / src / common / passwords.py
blobe6e66b59355fe5faf70218fe0eb7d44e188990a0
1 # -*- coding:utf-8 -*-
2 ## src/common/passwords.py
3 ##
4 ## Copyright (C) 2006 Gustavo J. A. M. Carneiro <gjcarneiro AT gmail.com>
5 ## Nikos Kouremenos <kourem AT gmail.com>
6 ## Copyright (C) 2006-2008 Yann Leboulanger <asterix AT lagaule.org>
7 ## Copyright (C) 2007 Jean-Marie Traissard <jim AT lapin.org>
8 ## Julien Pivotto <roidelapluie AT gmail.com>
9 ## Copyright (C) 2008 Stephan Erb <steve-e AT h3c.de>
11 ## This file is part of Gajim.
13 ## Gajim is free software; you can redistribute it and/or modify
14 ## it under the terms of the GNU General Public License as published
15 ## by the Free Software Foundation; version 3 only.
17 ## Gajim is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
22 ## You should have received a copy of the GNU General Public License
23 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
26 __all__ = ['get_password', 'save_password']
28 from common import gajim
30 USER_HAS_GNOMEKEYRING = False
31 USER_USES_GNOMEKEYRING = False
32 gnomekeyring = None
34 class PasswordStorage(object):
35 def get_password(self, account_name):
36 raise NotImplementedError
37 def save_password(self, account_name, password):
38 raise NotImplementedError
41 class SimplePasswordStorage(PasswordStorage):
42 def get_password(self, account_name):
43 passwd = gajim.config.get_per('accounts', account_name, 'password')
44 if passwd and passwd.startswith('gnomekeyring:'):
45 return None # this is not a real password, it's a gnome keyring token
46 else:
47 return passwd
49 def save_password(self, account_name, password):
50 gajim.config.set_per('accounts', account_name, 'password', password)
51 if account_name in gajim.connections:
52 gajim.connections[account_name].password = password
55 class GnomePasswordStorage(PasswordStorage):
56 def __init__(self):
57 self.keyring = gnomekeyring.get_default_keyring_sync()
58 if self.keyring is None:
59 self.keyring = 'default'
60 try:
61 gnomekeyring.create_sync(self.keyring, None)
62 except gnomekeyring.AlreadyExistsError:
63 pass
65 def get_password(self, account_name):
66 conf = gajim.config.get_per('accounts', account_name, 'password')
67 if conf is None:
68 return None
69 try:
70 unused, auth_token = conf.split('gnomekeyring:')
71 auth_token = int(auth_token)
72 except ValueError:
73 password = conf
74 ## migrate the password over to keyring
75 try:
76 self.save_password(account_name, password, update=False)
77 except gnomekeyring.NoKeyringDaemonError:
78 ## no keyring daemon: in the future, stop using it
79 set_storage(SimplePasswordStorage())
80 return password
81 try:
82 return gnomekeyring.item_get_info_sync(self.keyring,
83 auth_token).get_secret()
84 except gnomekeyring.DeniedError:
85 return None
86 except gnomekeyring.NoKeyringDaemonError:
87 ## no keyring daemon: in the future, stop using it
88 set_storage(SimplePasswordStorage())
89 return None
91 def save_password(self, account_name, password, update=True):
92 display_name = _('Gajim account %s') % account_name
93 attributes = dict(account_name=str(account_name), gajim=1)
94 try:
95 auth_token = gnomekeyring.item_create_sync(
96 self.keyring, gnomekeyring.ITEM_GENERIC_SECRET,
97 display_name, attributes, password, update)
98 except gnomekeyring.DeniedError:
99 set_storage(SimplePasswordStorage())
100 storage.save_password(account_name, password)
101 return
102 token = 'gnomekeyring:%i' % auth_token
103 gajim.config.set_per('accounts', account_name, 'password', token)
104 if account_name in gajim.connections:
105 gajim.connections[account_name].password = password
107 storage = None
108 def get_storage():
109 global storage
110 if storage is None: # None is only in first time get_storage is called
111 if gajim.config.get('use_gnomekeyring'):
112 global gnomekeyring
113 try:
114 import gnomekeyring
115 except ImportError:
116 pass
117 else:
118 global USER_HAS_GNOMEKEYRING
119 global USER_USES_GNOMEKEYRING
120 USER_HAS_GNOMEKEYRING = True
121 if gnomekeyring.is_available():
122 USER_USES_GNOMEKEYRING = True
123 else:
124 USER_USES_GNOMEKEYRING = False
125 if USER_USES_GNOMEKEYRING:
126 try:
127 storage = GnomePasswordStorage()
128 except gnomekeyring.NoKeyringDaemonError:
129 storage = SimplePasswordStorage()
130 except gnomekeyring.DeniedError:
131 storage = SimplePasswordStorage()
132 else:
133 storage = SimplePasswordStorage()
134 return storage
136 def set_storage(storage_):
137 global storage
138 storage = storage_
141 def get_password(account_name):
142 return get_storage().get_password(account_name)
144 def save_password(account_name, password):
145 return get_storage().save_password(account_name, password)
147 # vim: se ts=3: