Example #1
0
    def set_credentials(self, user, pw):
        """
        Store or update username and password for account
        """
        # Note: attrs is used to look up the account for old user credentials
        attrs = {"server": self._host, "protocol": self._protocol}

        LOGGER.debug("Updating credentials for %s" % self._host)

        keyring = matekeyring.get_default_keyring_sync()

        # Look up all accounts registered for this service and realm and delete
        # them. We get weird errors if more than one account is present
        try:
            items = matekeyring.find_items_sync(matekeyring.ITEM_NETWORK_PASSWORD, attrs)
        except matekeyring.NoMatchError:
            LOGGER.debug("No existing accounts for %s" % self._host)
            items = []

        for item in items:
            LOGGER.debug("Purging account %[email protected]%s" % (item.attributes["user"], item.attributes["server"]))
            matekeyring.item_delete_sync(keyring, item.item_id)

        # Add the 'user' attribute to attrs and commit it to the keyring
        LOGGER.debug("Creating new account %[email protected]%s" % (user, self._host))
        attrs["user"] = user
        matekeyring.item_create_sync(keyring, matekeyring.ITEM_NETWORK_PASSWORD, self._realm, attrs, pw, True)

        LOGGER.debug("Credential update success")
Example #2
0
    def __authenticate (self, password=None, save=None):
        if password is not None:
            import mpd
            try:
                self.__mpd.password (password)
            except mpd.CommandError:
                return False

        # Just because the password worked doesn't mean we have all the
        # permissions we need.
        needed_commands = ("status", "currentsong", "play", "previous", "next")
        if len ([c for c in self.__mpd.notcommands () if c in needed_commands]) > 0:
            return False

        if save:
            try:
                import matekeyring
                keyring = matekeyring.get_default_keyring_sync ()
                result = matekeyring.item_create_sync (keyring,
                                                        matekeyring.ITEM_NETWORK_PASSWORD,
                                                        "Password for MPD server at %s:%d" % (self.host, self.port),
                                                        { "server":self.host, "port":self.port, "protocol":"mpd" },
                                                        password,
                                                        True)
            except ImportError:
                print "MPD plugin: unable to save password; matekeyring not found"

        self.__delay = 1
        self.__authenticated = True
        gobject.idle_add (self.__set_connected, True)
        return True
Example #3
0
def get_login_password():
    keyring = matekeyring.get_default_keyring_sync()
    auth_token = mateconf.client_get_default().get_int(MATECONF_AUTH_KEY)
    if auth_token > 0:
        try:
            secret = matekeyring.item_get_info_sync(keyring, auth_token).get_secret()
        except matekeyring.DeniedError:
            login = None
            password = None
            auth_token = 0
        else:
            login, password = secret.split('\n')
    else:
        login = None
        password = None
    
    dialog = gtk.Dialog("Enter login", None, 0,
                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                         gtk.STOCK_OK, gtk.RESPONSE_OK))
    dialog.props.has_separator = False
    dialog.set_default_response(gtk.RESPONSE_OK)

    hbox = gtk.HBox(False, 8)
    hbox.set_border_width(8)
    dialog.vbox.pack_start(hbox, False, False, 0)

    stock = gtk.image_new_from_stock(gtk.STOCK_DIALOG_AUTHENTICATION,
                                     gtk.ICON_SIZE_DIALOG)
    hbox.pack_start(stock, False, False, 0)

    table = gtk.Table(2, 2)
    table.set_row_spacings(4)
    table.set_col_spacings(4)
    hbox.pack_start(table, True, True, 0)

    label = gtk.Label("_Login")
    label.set_use_underline(True)
    table.attach(label, 0, 1, 0, 1)
    local_entry1 = gtk.Entry()
    local_entry1.set_activates_default(True)
    if login is not None:
        local_entry1.set_text(login)
    table.attach(local_entry1, 1, 2, 0, 1)
    label.set_mnemonic_widget(local_entry1)

    label = gtk.Label("_Password")
    label.set_use_underline(True)
    table.attach(label, 0, 1, 1, 2)
    local_entry2 = gtk.Entry()
    local_entry2.set_visibility(False)
    local_entry2.set_activates_default(True)
    if password is not None:
        local_entry2.set_text(password)
    table.attach(local_entry2, 1, 2, 1, 2)
    label.set_mnemonic_widget(local_entry2)

    dialog.show_all()
    while 1:
        response = dialog.run()

        if response == gtk.RESPONSE_OK:
            login = local_entry1.get_text()
            password = local_entry2.get_text()
            if not login or not password:
                continue
            auth_token = matekeyring.item_create_sync(
                keyring,
                matekeyring.ITEM_GENERIC_SECRET,
                "MatePythonDesktop keyring example, login information",
                dict(appname="MatePythonDesktop, sync keyring example"),
                "\n".join((login, password)), True)
            mateconf.client_get_default().set_int(MATECONF_AUTH_KEY, auth_token)
            return login, password
        else:
            raise SystemExit