Exemplo n.º 1
0
	def get(self, protocol, user, server):
		(result, ids) = GnomeKeyring.list_item_ids_sync(self._defaultKeyring)		
		if result == GnomeKeyring.Result.OK:
			displayNameDict = {}
			for identity in ids:
				(result, item) = GnomeKeyring.item_get_info_sync(self._defaultKeyring, identity)
				displayNameDict[item.get_display_name()] = identity

			if self.KEYRING_ITEM_NAME % (protocol, user, server) in displayNameDict:
				(result, item) = GnomeKeyring.item_get_info_sync(self._defaultKeyring, \
					displayNameDict[self.KEYRING_ITEM_NAME % \
					(protocol, user, server)])

				if item.get_secret() != '':
					return item.get_secret()
				else:
					# DEBUG print "Keyring.get(): No Keyring Password for %s://%s@%s." % (protocol, user, server)
					return ''

			else:
				# DEBUG print "Keyring.get(): %s://%s@%s not in Keyring." % (protocol, user, server)
				return ''

		else:
			# DEBUG print "Keyring.get(): Neither default- nor 'login'-Keyring available."
			return ''
Exemplo n.º 2
0
	def set(self, protocol, user, server, password):
		if password != '':
			displayNameDict = {}
			(result, ids) = GnomeKeyring.list_item_ids_sync(self._defaultKeyring)
			for identity in ids:
				(result, item) = GnomeKeyring.item_get_info_sync(self._defaultKeyring, identity)
				displayNameDict[item.get_display_name()] = identity

			attrs = GnomeKeyring.Attribute.list_new()
			GnomeKeyring.Attribute.list_append_string(attrs, 'application',	'Mailnag')
			GnomeKeyring.Attribute.list_append_string(attrs, 'protocol',	protocol)
			GnomeKeyring.Attribute.list_append_string(attrs, 'user',		user)
			GnomeKeyring.Attribute.list_append_string(attrs, 'server',		server)
			
			if self.KEYRING_ITEM_NAME % (protocol, user, server) in displayNameDict:
				(result, item) = GnomeKeyring.item_get_info_sync(self._defaultKeyring, \
					displayNameDict[self.KEYRING_ITEM_NAME % \
					(protocol, user, server)])
				
				if password != item.get_secret():
					GnomeKeyring.item_create_sync(self._defaultKeyring, \
						GnomeKeyring.ItemType.GENERIC_SECRET, \
						self.KEYRING_ITEM_NAME % (protocol, user, server), \
						attrs, password, True)
			else:
				GnomeKeyring.item_create_sync(self._defaultKeyring, \
					GnomeKeyring.ItemType.GENERIC_SECRET, \
					self.KEYRING_ITEM_NAME % (protocol, user, server), \
					attrs, password, True)
    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))
Exemplo n.º 4
0
def get_master_key():
    ids = GnomeKeyring.list_item_ids_sync(KEYRING)[1]
    for id in ids:
        info = GnomeKeyring.item_get_info_sync('login', id)[1]
        if info.get_display_name() == 'gpwg:masterkey':
            return info.get_secret()
    return None
Exemplo n.º 5
0
 def remove_by_id(self, uid):
     """
         Remove an account by uid
         :param uid: (int) account uid
         :return:
     """
     secret_code = self.get_secret_code(uid)
     if secret_code:
         found = False
         (result, ids) = GK.list_item_ids_sync("TwoFactorAuth")
         for gid in ids:
             (result, item) = GK.item_get_info_sync("TwoFactorAuth", gid)
             if result == GK.Result.OK:
                 if item.get_display_name().strip("'") == secret_code:
                     found = True
                     break
         if found:
             GK.item_delete_sync("TwoFactorAuth", gid)
     query = "DELETE FROM accounts WHERE uid=?"
     try:
         self.conn.execute(query, (uid, ))
         self.conn.commit()
     except Exception as e:
         logging.error(
             "SQL: Couldn't remove account by uid : %s with error : %s" %
             (uid, str(e)))
Exemplo n.º 6
0
 def _get_first_key(self, keyring):
     item_keys = gk.list_item_ids_sync(keyring)
     if len(item_keys[1]) > 0:
         item_info = gk.item_get_info_sync(keyring, item_keys[1][0])
         return (item_info[1].get_display_name(), item_info[1].get_secret())
     else:
         return False
Exemplo n.º 7
0
 def remove_by_id(self, uid):
     """
         Remove an account by uid
         :param uid: (int) account uid
         :return:
     """
     secret_code = self.get_secret_code(uid)
     if secret_code:
         found = False
         (result, ids) = GK.list_item_ids_sync("TwoFactorAuth")
         for gid in ids:
             (result, item) = GK.item_get_info_sync("TwoFactorAuth", gid)
             if result == GK.Result.OK:
                 if item.get_display_name().strip("'") == secret_code:
                     found = True
                     break
         if found:
             GK.item_delete_sync("TwoFactorAuth", gid)
     query = "DELETE FROM accounts WHERE uid=?"
     try:
         self.conn.execute(query, (uid,))
         self.conn.commit()
     except Exception as e:
         logging.error(
             "SQL: Couldn't remove account by uid : %s with error : %s" % (uid, str(e)))
Exemplo n.º 8
0
    def test_item_create_info(self):
        '''item_create_sync(),  item_get_info_sync(), list_item_ids_sync()'''

        self.assertEqual(GnomeKeyring.create_sync(TEST_KEYRING, TEST_PWD),
                         GnomeKeyring.Result.OK)
        self.assertEqual(
            GnomeKeyring.get_info_sync(TEST_KEYRING)[0],
            GnomeKeyring.Result.OK)

        attrs = GnomeKeyring.Attribute.list_new()
        GnomeKeyring.Attribute.list_append_string(attrs, 'context',
                                                  'testsuite')
        GnomeKeyring.Attribute.list_append_uint32(attrs, 'answer', 42)

        (result, id) = GnomeKeyring.item_create_sync(
            TEST_KEYRING, GnomeKeyring.ItemType.GENERIC_SECRET, 'my_password',
            attrs, 'my_secret', False)
        self.assertEqual(result, GnomeKeyring.Result.OK)

        # now query for it
        (result, info) = GnomeKeyring.item_get_info_sync(TEST_KEYRING, id)
        self.assertEqual(result, GnomeKeyring.Result.OK)
        self.assertEqual(info.get_display_name(), 'my_password')
        self.assertEqual(info.get_secret(), 'my_secret')

        # list_item_ids_sync()
        (result, items) = GnomeKeyring.list_item_ids_sync(TEST_KEYRING)
        self.assertEqual(result, GnomeKeyring.Result.OK)
        self.assertEqual(items, [id])
Exemplo n.º 9
0
 def _find(self, displayName, keyring):
     item_keys = gk.list_item_ids_sync(keyring)
     for k in item_keys[1]:
         item_info = gk.item_get_info_sync(keyring, k)
         if item_info.get_display_name() == displayName:
             return (k, item_info.get_display_name(), item_info.get_secret())
     return False
Exemplo n.º 10
0
    def test_item_create_info(self):
        '''item_create_sync(),  item_get_info_sync(), list_item_ids_sync()'''

        self.assertEqual(GnomeKeyring.create_sync(TEST_KEYRING, TEST_PWD),
                GnomeKeyring.Result.OK)
        self.assertEqual(GnomeKeyring.get_info_sync(TEST_KEYRING)[0], GnomeKeyring.Result.OK)

        attrs = GnomeKeyring.Attribute.list_new()
        GnomeKeyring.Attribute.list_append_string(attrs, 'context', 'testsuite')
        GnomeKeyring.Attribute.list_append_uint32(attrs, 'answer', 42)

        (result, id) = GnomeKeyring.item_create_sync(TEST_KEYRING,
                GnomeKeyring.ItemType.GENERIC_SECRET, 'my_password', attrs,
                'my_secret', False)
        self.assertEqual(result, GnomeKeyring.Result.OK)

        # now query for it
        (result, info) = GnomeKeyring.item_get_info_sync(TEST_KEYRING, id)
        self.assertEqual(result, GnomeKeyring.Result.OK)
        self.assertEqual(info.get_display_name(), 'my_password')
        self.assertEqual(info.get_secret(), 'my_secret')

        # list_item_ids_sync()
        (result, items) = GnomeKeyring.list_item_ids_sync(TEST_KEYRING)
        self.assertEqual(result, GnomeKeyring.Result.OK)
        self.assertEqual(items, [id])
Exemplo n.º 11
0
 def get_password(self, item_id):
     result, item_info = GnomeKeyring.item_get_info_sync(
         self.keyring, item_id)
     if result == GnomeKeyring.Result.OK:
         return item_info.get_secret()
     else:
         return ""
Exemplo n.º 12
0
 def get_password(self, item_id):
     result, item_info = GnomeKeyring.item_get_info_sync(
         self.keyring, item_id)
     if result == GnomeKeyring.Result.OK:
         return item_info.get_secret()
     else:
         return ""
Exemplo n.º 13
0
def get_master_key():
    ids = GnomeKeyring.list_item_ids_sync(KEYRING)[1]
    for id in ids:
        info = GnomeKeyring.item_get_info_sync('login', id)[1]
        if info.get_display_name() == 'gpwg:masterkey':
            return info.get_secret()
    return None
 def _get_keyring_item_info(self, item_id):
     result, value = GnomeKeyring.item_get_info_sync(
                         GnomeCredentialStore.KEYRING_NAME,
                         item_id)
     if result != GnomeKeyring.Result.OK:
         logger.debug("Failed to get keyring info for item_id %s: %s" % (item_id, result))
         return None
     logger.debug("Successfully got keyring item info for item_id %s" % item_id)
     return value
Exemplo n.º 15
0
	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)
Exemplo n.º 16
0
	def __get_entry_id(self, entry):
		"""Get entry ID"""
		result = None

		for item_id in keyring.list_item_ids_sync(self.KEYRING_NAME)[1]:
			info = keyring.item_get_info_sync(self.KEYRING_NAME, item_id)[1]

			if info.get_display_name() == entry:
				result = item_id
				break

		return result
Exemplo n.º 17
0
    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)
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
	def get_entries(self):
		"""Return list of tuples containing entry names and description"""
		if not self.keyring_exists():
			raise InvalidKeyringError('Keyring does not exist!')

		result = []

		# if keyring is locked, try to unlock it
		if self.is_locked() and not self.__unlock_keyring():
			return result

		# populate result list
		for item_id in keyring.list_item_ids_sync(self.KEYRING_NAME)[1]:
			info = keyring.item_get_info_sync(self.KEYRING_NAME, item_id)[1]
			result.append((item_id, info.get_display_name(), info.get_mtime()))

		return result
Exemplo n.º 20
0
	def remove(self, accounts):
		# create list of all valid accounts
		valid_accounts = []
		for acc in accounts:
			protocol = 'imap' if acc.imap else 'pop'
			valid_accounts.append(self.KEYRING_ITEM_NAME % \
			(protocol, acc.user, acc.server))

		# find and delete invalid entries
		(result, ids) = GnomeKeyring.list_item_ids_sync(self._defaultKeyring)
		if result == GnomeKeyring.Result.OK:
			displayNameDict = {}
			for identity in ids:
				(result, item) = GnomeKeyring.item_get_info_sync(self._defaultKeyring, identity)
				displayNameDict[item.get_display_name()] = identity
			for key in displayNameDict.keys():
				if key.startswith('Mailnag password for') \
				and key not in valid_accounts:
					GnomeKeyring.item_delete_sync(self._defaultKeyring, displayNameDict[key])
Exemplo n.º 21
0
	def change_secret(self, entry_id, secret):
		"""Change secret for selected entry"""
		if not self.keyring_exists():
			raise InvalidKeyringError('Keyring does not exist!')

		result = False

		# if keyring is locked, try to unlock it
		if self.is_locked() and not self.__unlock_keyring():
			return result

		# get entry information
		info = keyring.item_get_info_sync(self.KEYRING_NAME, entry_id)[1]

		if info is not None:
			info.set_secret(secret)
			keyring.item_set_info_sync(self.KEYRING_NAME, entry_id, info)
			result = True

		return result
Exemplo n.º 22
0
 def dump(self, print_passwords=False):
     """Get all keyring entries (with or without passwords)
     """
     from gi.repository import GnomeKeyring
     dump = ""
     (result, ids) = GnomeKeyring.list_item_ids_sync(self.KEYRING_NAME)
     for id in ids:	
         (result, item) = GnomeKeyring.item_get_info_sync(self.KEYRING_NAME, id)
         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
         dump += item.get_display_name()
         if print_passwords:
             dump += " = " + item.get_secret()
         dump += "\n"
     return dump
Exemplo n.º 23
0
	def rename_entry(self, entry, new_name):
		"""Rename entry"""
		if not self.keyring_exists():
			raise InvalidKeyringError('Keyring does not exist!')

		result = False

		# if keyring is locked, try to unlock it
		if self.is_locked() and not self.__unlock_keyring():
			return result

		# get entry information
		entry_id = self.__get_entry_id(entry)
		info = keyring.item_get_info_sync(self.KEYRING_NAME, entry_id)[1]

		if info is not None:
			info.set_display_name(new_name)
			keyring.item_set_info_sync(self.KEYRING_NAME, entry_id, info)
			result = True

		return result
Exemplo n.º 24
0
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,
    }
Exemplo n.º 25
0
    def update(self, username, password):
        secret = '\n'.join((username, password))
        if self.keyring_item is not None:
            if secret == self.keyring_item.get_secret():
                print "account details not changed"
                return

        (result, id) = GnomeKeyring.item_create_sync(None,
                                 GnomeKeyring.ItemType.GENERIC_SECRET,
                                 "Rhythmbox: Vkontakte account information",
                                 self.keyring_attributes,
                                 secret,
                                 True)
        if result == GnomeKeyring.Result.OK:
            if self.keyring_item is None:
                (result, item) = GnomeKeyring.item_get_info_sync(None, id)
                if result == GnomeKeyring.Result.OK:
                    self.keyring_item = item
                else:
                    print "couldn't fetch keyring item: " + GnomeKeyring.result_to_message(result)
        else:
            print "couldn't create keyring item: " + GnomeKeyring.result_to_message(result)
Exemplo n.º 26
0
import sys
from gi.repository import GnomeKeyring as gk

if len(sys.argv) < 3:
    print >> sys.stderr, "invalid arguments\n    python gnomekeyring.py keyring itemname"
    exit(1)

ringname = sys.argv[1]
keyname = sys.argv[2]

(result, keyrings) = gk.list_keyring_names_sync()
if not ringname in keyrings:
    print >> sys.stderr, "keyring '%s' not found" % ringname
    exit(2)


result = gk.unlock_sync(ringname, None)
if not result == gk.Result.OK:
    print >> sys.stderr, "keyring '%s' is locked" % ringname
    exit(3)

(result, ids) = gk.list_item_ids_sync(ringname)
for id in ids:
    (result, info) = gk.item_get_info_sync(ringname, id)
    if info.get_display_name() == keyname:
        print info.get_secret()
        exit(0)

print >> sys.stderr, "keyname '%s' in '%s' not found" % (keyname, ringname)
exit(4)
Exemplo n.º 27
0
 def get_item(krname, item_id):
     return gk.item_get_info_sync(krname, item_id)