def test_find_items(self): '''find_items_sync()''' search_attrs = GnomeKeyring.Attribute.list_new() # no attributes, finds everything (result, items) = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, search_attrs) self.assertEqual(result, GnomeKeyring.Result.OK) print('(no attributes: %i matches) ' % len(items), end='', file=sys.stderr) for item in items: self.assertNotEqual(item.keyring, '') for attr in GnomeKeyring.Attribute.list_to_glist(item.attributes): self.assertTrue( attr.type in (GnomeKeyring.AttributeType.STRING, GnomeKeyring.AttributeType.UINT32)) self.assertEqual(type(attr.name), type('')) self.assertGreater(len(attr.name), 0) # check that we can get the value if attr.type == GnomeKeyring.AttributeType.STRING: self.assertEqual(type(attr.get_string()), type('')) else: self.assertTrue(isinstance(attr.get_uint32()), long) # search for unknown attribute, should have no results GnomeKeyring.Attribute.list_append_string(search_attrs, 'unknown!_attr', '') (result, items) = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, search_attrs) self.assertEqual(result, GnomeKeyring.Result.NO_MATCH) self.assertEqual(len(items), 0)
def test_find_items(self): '''find_items_sync()''' search_attrs = GnomeKeyring.Attribute.list_new() # no attributes, finds everything (result, items) = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, search_attrs) self.assertEqual(result, GnomeKeyring.Result.OK) print('(no attributes: %i matches) ' % len(items), end='', file=sys.stderr) for item in items: self.assertNotEqual(item.keyring, '') for attr in GnomeKeyring.Attribute.list_to_glist(item.attributes): self.assertTrue(attr.type in (GnomeKeyring.AttributeType.STRING, GnomeKeyring.AttributeType.UINT32)) self.assertEqual(type(attr.name), type('')) self.assertGreater(len(attr.name), 0) # check that we can get the value if attr.type == GnomeKeyring.AttributeType.STRING: self.assertEqual(type(attr.get_string()), type('')) else: self.assertTrue(isinstance(attr.get_uint32()), long) # search for unknown attribute, should have no results GnomeKeyring.Attribute.list_append_string(search_attrs, 'unknown!_attr', '') (result, items) = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, search_attrs) self.assertEqual(result, GnomeKeyring.Result.NO_MATCH) self.assertEqual(len(items), 0)
def find_keyring_items(self): attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'rhythmbox-plugin', 'pandora') result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if (result is None or result is GnomeKeyring.Result.OK) and len(items) != 0: # Got list of search results self.__keyring_data['id'] = items[0].item_id result, item = GnomeKeyring.item_get_info_sync(None, self.__keyring_data['id']) if result is None or result is GnomeKeyring.Result.OK: # Item retrieved successfully self.__keyring_data['item'] = item self.fill_account_details() else: print("Couldn't retrieve keyring item: " + str(result)) elif result == GnomeKeyring.Result.NO_MATCH or len(items) == 0: # No items were found, so we'll create one result, id = GnomeKeyring.item_create_sync( None, GnomeKeyring.ItemType.GENERIC_SECRET, "Rhythmbox: Pandora account information", attrs, "", # Empty secret for now True) if result is None or result is GnomeKeyring.Result.OK: # Item successfully created self.__keyring_data['id'] = id result, item = GnomeKeyring.item_get_info_sync(None, id) if result is None: # Item retrieved successfully self.__keyring_data['item'] = item self.fill_account_details() else: print("Couldn't retrieve keyring item: " + str(result)) else: print("Couldn't create keyring item: " + str(result)) else: # Some other error occurred print("Couldn't access keyring: " + str(result))
def get_credentials(self, acc): self.lock.acquire() try: logger.debug( "Getting credentials with gnome keyring for account %s" % (acc.get_name())) attrs = gk.Attribute.list_new() gk.Attribute.list_append_string(attrs, 'account_name', acc.get_name()) try: (result, items) = gk.find_items_sync(gk.ItemType.NETWORK_PASSWORD, attrs) except gk.NoMatchError, e: items = list() if len(items) < 1: raise KeyringException( "Cannot find the keyring data for the account %s" % (acc.get_name())) logger.debug("items ok") username = '' for attr in gk.Attribute.list_to_glist(items[0].attributes): if attr.name == 'username': username = attr.get_string() return Credentials(username, items[0].secret)
def fetch_secret_code(secret_code): attr = GK.Attribute.list_new() GK.Attribute.list_append_string(attr, 'id', secret_code) result, value = GK.find_items_sync(GK.ItemType.GENERIC_SECRET, attr) if result == GK.Result.OK: return value[0].secret else: return None
def remove(self, key): attrs = self._get_attrs(key) result, items = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: GnomeKeyring.item_delete_sync(self._defaultKeyring, items[0].item_id)
def get(self, key): attrs = self._get_attrs(key) result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: return items[0].secret else: return ''
def delete_credentials(self, user): attrs = attributes({ "user": user, "server": self._server, "protocol": self._protocol }) result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) for item in items: GnomeKeyring.item_delete_sync(self._keyring, item.item_id)
def find_folder(self,epath): attributes = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attributes, 'appid', APPID) GnomeKeyring.Attribute.list_append_string(attributes, 'encfs-path', epath) ff = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attributes)[1] if len(ff)>0: return ff[0].item_id, ff[0].secret else: return None
def get(self, key): attrs = self._get_attrs(key) result, items = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: return items[0].secret else: return ''
def _migrate_keyring(self): attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'application', 'Mailnag') result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: for i in items: result, info = GnomeKeyring.item_get_info_sync(self._defaultKeyring, i.item_id) self.set(info.get_display_name(), i.secret) GnomeKeyring.item_delete_sync(self._defaultKeyring, i.item_id)
def get_all_users(self): attrs = attributes({"server": self._server, "protocol": self._protocol}) result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) if len(items) == 0: raise KeyringException("Credentials not found") users_list = [] for item in items: d = dict_from_attributes(item.attributes) users_list.append(d["user"]) return users_list
def get_credentials(self, user): attrs = attributes({ "user" : user, "server": self._server, "protocol": self._protocol }) result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) if len(items) == 0: raise KeyringException("Credentials not found") d = dict_from_attributes(items[0].attributes) return Credentials(d["user"], items[0].secret)
def get_from_keyring(acctid, name): """Get the entry from the keyring for @acctid with @name.""" attributes = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attributes, 'id', str("%s/%s" % (acctid, name))) result, value = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, attributes) if result == GnomeKeyring.Result.OK: return value[0].secret return None
def do_query(self, id): # Fetch secret by id attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'id', id) result, value = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result != GnomeKeyring.Result.OK: return #print 'password %s = %s' % (id, value[0].secret) #print 'password id = %s' % value[0].item_id username, password = value[0].secret.split('@@@') Liferea.auth_info_from_store(id, username, password)
def _get_value_from_gnomekeyring(self, name): """Get a locked secret for threaded/multiprocessing use.""" value = "" attrlist = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrlist, "id", str(name)) result, found = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrlist) if result == GnomeKeyring.Result.OK: value = found[0].secret mlock(value) return value elif result == GnomeKeyring.Result.NO_MATCH: raise (NoPasswordFound()) raise (GnomeKeyringLocked())
def gkr_get(self, id): attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'id', id) result, value = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: return value[0].secret elif result == GnomeKeyring.Result.NO_MATCH: # Key doesn't exist, create again print(_("Key doesn't exist in GNOME Keyring. Creating again...")) self.gkr_store(GUIFINETLOGINKEYNAME, ':') return '' else: return None
def _migrate_keyring(self): attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'application', 'Mailnag') result, items = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: for i in items: result, info = GnomeKeyring.item_get_info_sync( self._defaultKeyring, i.item_id) self.set(info.get_display_name(), i.secret) GnomeKeyring.item_delete_sync(self._defaultKeyring, i.item_id)
def get_keyring_password(): GnomeKeyring.unlock_sync("login", None) attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, "id", "resnet") result, value = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: return value[0].secret elif result == GnomeKeyring.Result.NO_MATCH: # Password not found, prompt to add new one print("Password not found in keyring") password = getpass.getpass("OSU Login Password: "******"login", GnomeKeyring.ItemType.GENERIC_SECRET, "resnet", attrs, password, True) return password elif result != GnomeKeyring.Result.OK: return None
def get_secret(id): """Get a locked secret for threaded/multiprocessing use.""" value = "" attrlist = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrlist, 'id', str(id)) result, found = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, attrlist) if result == GnomeKeyring.Result.OK: value = found[0].secret mlock(value) return value elif result == GnomeKeyring.Result.NO_MATCH: print "FAILED: " + id return None
def delete_password(self, service, username): """Delete the password for the username of the service. """ from gi.repository import GnomeKeyring attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'username', username) GnomeKeyring.Attribute.list_append_string(attrs, 'domain', service) result, items = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) if result == GnomeKeyring.Result.NO_MATCH: raise PasswordDeleteError("Password not found") for current in items: result = GnomeKeyring.item_delete_sync(current.keyring, current.item_id) if result == GnomeKeyring.Result.CANCELLED: raise PasswordDeleteError("Cancelled by user")
def set(self, key, secret): if secret == '': return attrs = self._get_attrs(key) existing_secret = '' result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: existing_secret = items[0].secret if existing_secret != secret: GnomeKeyring.item_create_sync(self._defaultKeyring, \ GnomeKeyring.ItemType.GENERIC_SECRET, key, \ attrs, secret, True)
def set(self, key, secret): if secret == '': return attrs = self._get_attrs(key) existing_secret = '' result, items = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: existing_secret = items[0].secret if existing_secret != secret: GnomeKeyring.item_create_sync(self._defaultKeyring, \ GnomeKeyring.ItemType.GENERIC_SECRET, key, \ attrs, secret, True)
def __init__(self): self.keyring_item = None self.keyring_attributes = GnomeKeyring.attribute_list_new() GnomeKeyring.attribute_list_append_string(self.keyring_attributes, "rhythmbox-plugin", "vkontakte") (result, items) = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, self.keyring_attributes) if result == GnomeKeyring.Result.OK and len(items) != 0: (result, item) = GnomeKeyring.item_get_info_sync(None, items[0].item_id) if result == GnomeKeyring.Result.OK: self.keyring_item = item else: print "Couldn't get keyring item: " + GnomeKeyring.result_to_message(result) else: print "couldn't search keyring items: " + GnomeKeyring.result_to_message(result)
def _find_keyring_item_id(self): cached_value = getattr(self, '_cached_item_id', None) if cached_value: # only 1 keyring item is needed, so it's safe to cache logger.debug("Retrieved cached keyring item id from cache: %s" % cached_value) return self._cached_item_id logger.debug("No keyring item id cached, so need to create the item") result, value = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.GENERIC_SECRET, self.item_attrs) if result != GnomeKeyring.Result.OK: logger.debug("There was an error finding the keyring item: %s" % result) return None self._cached_item_id = value[0].item_id logger.debug("Successfully found keyring item id: %s" % self._cached_item_id) return self._cached_item_id
def get_pandora_account_info(self): print "Getting account details..." error_message = "Account details are needed before you can connect. Check your settings." attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'rhythmbox-plugin', 'pandora') result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.NO_MATCH or len(items) == 0: print "Pandora Account Info not found." raise AccountNotSetException(error_message) secret = items[0].secret if secret is "": print "Pandora Account Info empty." raise AccountNotSetException(error_message) return tuple(secret.split('\n'))
def _find_passwords(self, service, username, deleting=False): """Get password of the username for the service """ passwords = [] service = self._safe_string(service) username = self._safe_string(username) for attrs_tuple in (("username", "service"), ("user", "domain")): attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, attrs_tuple[0], username) GnomeKeyring.Attribute.list_append_string(attrs, attrs_tuple[1], service) result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) if result == GnomeKeyring.Result.OK: passwords += items elif deleting: if result == GnomeKeyring.Result.CANCELLED: raise PasswordDeleteError("Cancelled by user") elif result != GnomeKeyring.Result.NO_MATCH: raise PasswordDeleteError(result.value_name) return passwords
def get_credentials(self, acc): self.lock.acquire() try: logger.debug("Getting credentials with gnome keyring for account %s" % (acc.get_name())) attrs = gk.Attribute.list_new() gk.Attribute.list_append_string(attrs, 'account_name', acc.get_name()) try: (result, items) = gk.find_items_sync(gk.ItemType.NETWORK_PASSWORD, attrs) except gk.NoMatchError, e: items = list() if len(items) < 1: raise KeyringException("Cannot find the keyring data for the account %s" % (acc.get_name())) logger.debug("items ok") username = '' for attr in gk.Attribute.list_to_glist(items[0].attributes): if attr.name == 'username': username = attr.get_string() return Credentials (username, items[0].secret)
def get_item(keyring_name, id): try: item = chk(GnomeKeyring.item_get_info_sync(keyring_name, id)) except BadResult as e: sys.stderr.write("Could not examine item (%s, %s): %s\n" % (keyring_name, id, e)) return None attrs = { attr.name: attr.get_string() for item in chk( GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, GnomeKeyring.Attribute.list_new())) for attr in GnomeKeyring.Attribute.list_to_glist(item.attributes) if item.keyring == keyring_name and item.item_id == id } return { 'display_name': item.get_display_name(), 'secret': item.get_secret(), 'mtime': item.get_mtime(), 'ctime': item.get_ctime(), 'attributes': attrs, }
def _find_passwords(self, service, username, deleting=False): """Get password of the username for the service""" passwords = [] service = self._safe_string(service) username = self._safe_string(username) for attrs_tuple in (('username', 'service'), ('user', 'domain')): attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, attrs_tuple[0], username) GnomeKeyring.Attribute.list_append_string(attrs, attrs_tuple[1], service) result, items = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) if result == GnomeKeyring.Result.OK: passwords += items elif deleting: if result == GnomeKeyring.Result.CANCELLED: raise PasswordDeleteError("Cancelled by user") elif result != GnomeKeyring.Result.NO_MATCH: raise PasswordDeleteError(result.value_name) return passwords
def get_password(self, service, username): """Get password of the username for the service """ from gi.repository import GnomeKeyring service = self._safe_string(service) username = self._safe_string(username) attrs = GnomeKeyring.Attribute.list_new() GnomeKeyring.Attribute.list_append_string(attrs, 'username', username) GnomeKeyring.Attribute.list_append_string(attrs, 'domain', service) result, items = GnomeKeyring.find_items_sync( GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) if result == GnomeKeyring.Result.IO_ERROR: return None if result == GnomeKeyring.Result.NO_MATCH: return None if result == GnomeKeyring.Result.CANCELLED: # The user pressed "Cancel" when prompted to unlock their keyring. return None assert len(items) == 1, 'no more than one entry should ever match' secret = items[0].secret return secret if isinstance(secret, unicode) else secret.decode('utf-8')
def remove(self, key): attrs = self._get_attrs(key) result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs) if result == GnomeKeyring.Result.OK: GnomeKeyring.item_delete_sync(self._defaultKeyring, items[0].item_id)
def has_any_credentials(self): attrs = attributes({"server": self._server, "protocol": self._protocol}) result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs) if result in (GnomeKeyring.Result.NO_MATCH, GnomeKeyring.Result.DENIED): return False return len(items) > 0