Get attributes of GnomeKeyring key with Python 2.7

2019-06-14 02:39发布

I am using Linux Mint 17, based on Ubuntu 14.04

I have installed:

python-gnomekeyring 2.3.2 python-gi 3.12 python-gtk2 2.24

I want to use the GnomeKeyring. I save and read a key name an key password, but I annot access the attributes. When I try to access them I always get:

*** Error in `python': double free or corruption (out)

Here is my code, I created this today to work with the GnomeKeyring:

#!/usr/bin/python
# -*- coding: utf8 -*-


import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GnomeKeyring', '1.0')
from gi.repository import Gtk
from gi.repository import GnomeKeyring

import logging
from pprint import pprint


class KeyringManager():

    def __init__(self):
        pass

    def create_key(self, keyring_name, key_name, attributes=None):
        logging.warning("create_key: %s / %s", keyring_name, key_name)
        self.unlock_keyring(keyring_name)
        key_id = self.get_key_id(keyring_name, key_name)
        if key_id:
            logging.warning("key already exists, overwriting now, press return key to continue")
            raw_input()
        attrs = GnomeKeyring.Attribute.list_new()
        for key, value in attributes.items():
            GnomeKeyring.Attribute.list_append_string(attrs, key, value)
        GnomeKeyring.item_create_sync(keyring_name, GnomeKeyring.ItemType.GENERIC_SECRET, key_name, attrs, raw_input("please enter password for key '{}': ".format(key_name)), True)

    def unlock_keyring(self, keyring_name):
        logging.warning("unlock_keyring: %s", keyring_name)
        # check if keyring exists
        (result, info) = GnomeKeyring.get_info_sync(keyring_name)
        if result == GnomeKeyring.Result.NO_SUCH_KEYRING:
            logging.warning("keyring does not exists: %s", keyring_name)
            self.create_keyring(keyring_name)
        result = GnomeKeyring.unlock_sync(keyring_name, None)
        pprint(result.value_name)

    def create_keyring(self, keyring_name):
        logging.warning("create_keyring: %s", keyring_name)
        result = GnomeKeyring.create_sync(keyring_name, raw_input("please enter passwort for new keyring '{}': ".format(keyring_name)))
        pprint(result)

    def get_key_id(self, keyring_name, key_name):
        logging.warning("get_key_id: %s / %s", keyring_name, key_name)
        (result, key_ids) = GnomeKeyring.list_item_ids_sync(keyring_name)
        for key_id in key_ids:
            (result, item) = GnomeKeyring.item_get_info_sync(keyring_name, key_id)
            if item.get_display_name() == key_name:
                return key_id

    def get_key_info(self, keyring_name, key_name):
        logging.warning("get_key_info: %s / %s", keyring_name, key_name)
        self.unlock_keyring(keyring_name)
        key_id = self.get_key_id(keyring_name, key_name)
        GnomeKeyring.item_get_info_sync (keyring_name, key_id)
        (result, item) = GnomeKeyring.item_get_info_sync(keyring_name, key_id)
        self.key_name = item.get_display_name()
        self.secret = item.get_secret()
        self.attrs = GnomeKeyring.Attribute.list_new()
        result = GnomeKeyring.item_get_attributes_sync(keyring_name, key_id, self.attrs)#
        try:
            for attr in GnomeKeyring.Attribute.list_to_glist(self.attrs):
                ## *** Error in `python': double free or corruption (out)
                print '%s = %s ' % (attr.name, attr.get_string())
        except Exception, e:
            logging.error(e.message, exc_info=True)
            exit(e.message)

    def delete_key(self, keyring_name="login", key_name="==THENAME"):
        logging.warning("delete_key: %s / %s", keyring_name, key_name)
        raw_input("press return key to continue")
        logging.error("not implemented yet")
        #GnomeKeyring.item_delete_sync(keyring, id)


#KeyringManager().create_key("my_keyring", "my_key", {"user":"carl", "url":"apple.de", "port":"1212"})
KeyringManager().get_key_info("my_keyring", "my_key")

some links:

https://github.com/lwindolf/liferea/blob/master/plugins/gnome-keyring.py

2条回答
一夜七次
2楼-- · 2019-06-14 03:19

this is working:

def get_attributes(self, keyring_name, key_id):
    attributes = {}
    attrs_list = GnomeKeyring.Attribute.list_new()
    (result, items )= GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs_list)
    for item in items:
        if item.keyring == keyring_name:
            if item.item_id == key_id:
                for attr in GnomeKeyring.Attribute.list_to_glist(item.attributes):
                    if attr.name != "xdg:schema":
                        attributes[attr.name] = attr.get_string()
    return attributes
查看更多
冷血范
3楼-- · 2019-06-14 03:32

I see the main question you have already clarified.

The error was because you used GnomeKeyring.Attribute.list_new() as argument for GnomeKeyring.Attribute.list_to_glist(…) instead of item.attributes of an GnomeKeyring.Found.

查看更多
登录 后发表回答