Skip to content

Instantly share code, notes, and snippets.

@pklaus
Created February 21, 2010 10:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pklaus/310257 to your computer and use it in GitHub Desktop.
Save pklaus/310257 to your computer and use it in GitHub Desktop.
Use the Gnome Keyring in a Python program
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Gnome Keyring in Python
# Shows how to store credentials of type NETWORK_PASSWORD using python.
# Found on <http://www.rittau.org/blog/20070726-01>
# More on the GNOME Keyring: <http://live.gnome.org/GnomeKeyring>
# alternatively have a look at the new universal and actively developed Python Keyring:
# on <http://pypi.python.org/pypi/keyring> and <http://home.python-keyring.org/>
# another small piece of nice python code: gkeyring (Can retreive all the data stored in the keyring...)
# <http://bazaar.launchpad.net/%7Ekamil.paral/gkeyring/trunk/annotate/head%3A/gkeyring.py>
import gtk # ensure that the application name is correctly set
import gnomekeyring as gkey
class Keyring(object):
def __init__(self, name, server, protocol):
self._name = name
self._server = server
self._protocol = protocol
self._keyring = gkey.get_default_keyring_sync()
def has_credentials(self):
try:
attrs = {"server": self._server, "protocol": self._protocol}
items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
return len(items) > 0
except gkey.DeniedError:
return False
except gkey.NoMatchError:
return False
def get_credentials(self):
attrs = {"server": self._server, "protocol": self._protocol}
items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
return (items[0].attributes["user"], items[0].secret)
def set_credentials(self, (user, pw)):
attrs = {
"user": user,
"server": self._server,
"protocol": self._protocol,
}
gkey.item_create_sync(gkey.get_default_keyring_sync(),
gkey.ITEM_NETWORK_PASSWORD, self._name, attrs, pw, True)
def main():
kr = Keyring('keyring in python', 'someserver.net', 'someprotocol')
if kr.has_credentials():
print("We have credentials stored here.")
else:
print("We have NO credentials stored here.")
print("Storing credentials.")
kr.set_credentials(('admin','krylline'))
print(kr.get_credentials())
if __name__ == "__main__":
main()
#!/usr/bin/env python
# found on <http://michael.susens-schurter.com/blog/2008/10/30/listing-all-passwords-stored-in-gnome-keyring/>
import pygtk
pygtk.require('2.0')
import gtk # sets app name
import gnomekeyring
def print_all():
for keyring in gnomekeyring.list_keyring_names_sync():
try:
for id in gnomekeyring.list_item_ids_sync(keyring):
item = gnomekeyring.item_get_info_sync(keyring, id)
print('[%s] %s = %s' % (keyring, item.get_display_name(), item.get_secret()))
else:
if len(gnomekeyring.list_item_ids_sync(keyring)) == 0:
print('[%s] --empty--' % keyring)
except gnomekeyring.DeniedError:
pass
if __name__ == '__main__':
print_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment