2 ## src/common/passwords.py
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
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
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
):
57 self
.keyring
= gnomekeyring
.get_default_keyring_sync()
58 if self
.keyring
is None:
59 self
.keyring
= 'default'
61 gnomekeyring
.create_sync(self
.keyring
, None)
62 except gnomekeyring
.AlreadyExistsError
:
65 def get_password(self
, account_name
):
66 conf
= gajim
.config
.get_per('accounts', account_name
, 'password')
70 unused
, auth_token
= conf
.split('gnomekeyring:')
71 auth_token
= int(auth_token
)
74 ## migrate the password over to keyring
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())
82 return gnomekeyring
.item_get_info_sync(self
.keyring
,
83 auth_token
).get_secret()
84 except gnomekeyring
.DeniedError
:
86 except gnomekeyring
.NoKeyringDaemonError
:
87 ## no keyring daemon: in the future, stop using it
88 set_storage(SimplePasswordStorage())
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)
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
)
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
110 if storage
is None: # None is only in first time get_storage is called
111 if gajim
.config
.get('use_gnomekeyring'):
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
124 USER_USES_GNOMEKEYRING
= False
125 if USER_USES_GNOMEKEYRING
:
127 storage
= GnomePasswordStorage()
128 except gnomekeyring
.NoKeyringDaemonError
:
129 storage
= SimplePasswordStorage()
130 except gnomekeyring
.DeniedError
:
131 storage
= SimplePasswordStorage()
133 storage
= SimplePasswordStorage()
136 def set_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
)