コード例 #1
0
ファイル: test-gi.py プロジェクト: Bchelz/libgnome-keyring
    def test_result_str(self):
        '''result_to_message()'''

        self.assertEqual(GnomeKeyring.result_to_message(GnomeKeyring.Result.OK),
                '')
        self.assertEqual(
                type(GnomeKeyring.result_to_message(GnomeKeyring.Result.NO_SUCH_KEYRING)),
                type(''))
コード例 #2
0
    def test_result_str(self):
        '''result_to_message()'''

        self.assertEqual(
            GnomeKeyring.result_to_message(GnomeKeyring.Result.OK), '')
        self.assertEqual(
            type(
                GnomeKeyring.result_to_message(
                    GnomeKeyring.Result.NO_SUCH_KEYRING)), type(''))
コード例 #3
0
def verify(result, action):
    ok = None
    return_if_ok = None
    if isinstance(result, tuple) and isinstance(result[0], GnomeKeyringResult):
        # <result> is a tuple containing a return code and an actual return
        # value. This is the convention for pygobject3 bindings on methods like
        # get_info_sync() and item_get_info_full_sync().
        ok = result[0]
        return_if_ok = result[1]
    elif isinstance(result, GnomeKeyringResult):
        # <result> is a bare return code. This is the convention for pygobject3
        # bindings on methods like item_delete_sync() and item_set_info_sync().
        ok = result
        return_if_ok = result
    else:
        # <result> is the return value of a successful method invocation, sans
        # return code. This is the convention for pygobject2 bindings.
        return result
    if ok == gk.Result.OK:
        return return_if_ok
    if ok == gk.Result.NO_SUCH_KEYRING:
        raise NoSuchKeyringError
    msg = gk.result_to_message(ok)
    if ok == gk.Result.IO_ERROR:
        msg += '; '
        msg += DBUS_ADVICE
    err('Cannot %s: %s' % (action, msg))
コード例 #4
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)
コード例 #5
0
def setup_gnome_keyring():
    """
    Provide clean login Gnome keyring (removes the previous one
    beforehand, if there is a one).
    """
    try:
        # Delete originally stored password
        (response, keyring) = GnomeKeyring.get_default_keyring_sync()
        log.debug('get_info default: %s, %s' % (response, keyring))
        if response == GnomeKeyring.Result.OK:
            if keyring is not None:
                delete_response = GnomeKeyring.delete_sync(keyring)
                log.debug('delete default: %s' % delete_response)
                assert delete_response == GnomeKeyring.Result.OK, \
                    "Delete failed: %s" % delete_response
            response, keyring = GnomeKeyring.get_info_sync('login')
            if response == GnomeKeyring.Result.OK:
                if keyring is not None:
                    delete_response = GnomeKeyring.delete_sync('login')
                    log.debug('delete login: %s' % delete_response)
                    assert delete_response == GnomeKeyring.Result.OK, \
                        "Delete failed: %s" % delete_response
            elif response != GnomeKeyring.Result.NO_SUCH_KEYRING:
                raise IOError(
                    'Unexpected error when manipulating login keyring')

            # This is result of the underlying DBus error:
            # CKR_WRAPPED_KEY_INVALID, CKR_WRAPPED_KEY_LEN_RANGE,
            # CKR_MECHANISM_PARAM_INVALID
            # So, failed either
            # * egg_padding_pkcs7_unpad
            #   (gnome-keyring/egg/egg-padding.c)
            # * gkm_aes_mechanism_unwrap
            #   (gnome-keyring/pkcs11/gkm/gkm-aes-mechanism.c)
            # * gkm_dh_mechanism_derive
            #   (gnome-keyring/pkcs11/gkm/gkm-dh-mechanism.c)
            # * gkm_null_mechanism_unwrap or gkm_null_mechanism_wrap
            #   (gnome-keyring/pkcs11/gkm/gkm-null-mechanism.c)
            create_response = GnomeKeyring.create_sync('login', 'redhat')
            log.debug('create login: %s' % create_response)
            if create_response != GnomeKeyring.Result.OK:
                raise IOError(
                    'Create failed: %s\n%s' %
                    (create_response,
                     GnomeKeyring.result_to_message(create_response)))

            set_default_response = \
                GnomeKeyring.set_default_keyring_sync('login')
            assert set_default_response == GnomeKeyring.Result.OK, \
                "Set default failed: %s" % set_default_response
        unlock_response = GnomeKeyring.unlock_sync("login", 'redhat')
        assert unlock_response == GnomeKeyring.Result.OK, \
            "Unlock failed: %s" % unlock_response
    except Exception as e:
        log.error("Exception while unlocking a keyring: %s", e.message)
        raise  # We shouldn’t let this exception evaporate
コード例 #6
0
def setup_gnome_keyring():
    """
    Provide clean login Gnome keyring (removes the previous one
    beforehand, if there is a one).
    """
    try:
        # Delete originally stored password
        (response, keyring) = GnomeKeyring.get_default_keyring_sync()
        log.debug('get_info default: %s, %s' % (response, keyring))
        if response == GnomeKeyring.Result.OK:
            if keyring is not None:
                delete_response = GnomeKeyring.delete_sync(keyring)
                log.debug('delete default: %s' % delete_response)
                assert delete_response == GnomeKeyring.Result.OK, \
                    "Delete failed: %s" % delete_response
            response, keyring = GnomeKeyring.get_info_sync('login')
            if response == GnomeKeyring.Result.OK:
                if keyring is not None:
                    delete_response = GnomeKeyring.delete_sync('login')
                    log.debug('delete login: %s' % delete_response)
                    assert delete_response == GnomeKeyring.Result.OK, \
                        "Delete failed: %s" % delete_response
            elif response != GnomeKeyring.Result.NO_SUCH_KEYRING:
                raise IOError(
                    'Unexpected error when manipulating login keyring')

            # This is result of the underlying DBus error:
            # CKR_WRAPPED_KEY_INVALID, CKR_WRAPPED_KEY_LEN_RANGE,
            # CKR_MECHANISM_PARAM_INVALID
            # So, failed either
            # * egg_padding_pkcs7_unpad
            #   (gnome-keyring/egg/egg-padding.c)
            # * gkm_aes_mechanism_unwrap
            #   (gnome-keyring/pkcs11/gkm/gkm-aes-mechanism.c)
            # * gkm_dh_mechanism_derive
            #   (gnome-keyring/pkcs11/gkm/gkm-dh-mechanism.c)
            # * gkm_null_mechanism_unwrap or gkm_null_mechanism_wrap
            #   (gnome-keyring/pkcs11/gkm/gkm-null-mechanism.c)
            create_response = GnomeKeyring.create_sync('login', 'redhat')
            log.debug('create login: %s' % create_response)
            if create_response != GnomeKeyring.Result.OK:
                raise IOError(
                    'Create failed: %s\n%s' %
                    (create_response,
                     GnomeKeyring.result_to_message(create_response)))

            set_default_response = \
                GnomeKeyring.set_default_keyring_sync('login')
            assert set_default_response == GnomeKeyring.Result.OK, \
                "Set default failed: %s" % set_default_response
        unlock_response = GnomeKeyring.unlock_sync("login", 'redhat')
        assert unlock_response == GnomeKeyring.Result.OK, \
            "Unlock failed: %s" % unlock_response
    except Exception as e:
        log.error("Exception while unlocking a keyring: %s", e.message)
        raise  # We shouldn’t let this exception evaporate
コード例 #7
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)
コード例 #8
0
 def do_activate(self):
     result = GnomeKeyring.unlock_sync("liferea", None)
     if GnomeKeyring.Result.OK != result:
         raise ValueError("Failed to unlock GnomeKeyring, error: " +
                          GnomeKeyring.result_to_message(result))