예제 #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 %s@%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 %s@%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")
예제 #2
0
파일: mpd_.py 프로젝트: s1ag/Mate-Extra
    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
예제 #3
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
예제 #4
0
파일: mpd_.py 프로젝트: s1ag/Mate-Extra
    def __try_connect(self):
        import mpd
        try:
            full_key = self.__plugin._conf.resolve_plugin_key(
                self.__plugin, "host")
            host = self.__plugin._conf.client.get_string(full_key)
            if host == "":
                host = "localhost"
                self.host = os.environ.get("MPD_HOST", "localhost")
                if "@" in self.host:
                    self.host = (self.host.split("@", 1))[1]
            else:
                self.host = host

            full_key = self.__plugin._conf.resolve_plugin_key(
                self.__plugin, "port")
            port = self.__plugin._conf.client.get_int(full_key)
            if port <= 0:
                port = 6600
                self.port = int(os.environ.get("MPD_PORT", 6600))
            else:
                self.port = port

            self.__mpd = mpd.MPDClient()
            self.__mpd.connect(host, port)

            if not self.__authenticate():
                try:
                    import matekeyring
                    keyring = matekeyring.get_default_keyring_sync()
                    found = matekeyring.find_items_sync(
                        matekeyring.ITEM_NETWORK_PASSWORD, {
                            "server": self.host,
                            "port": self.port,
                            "protocol": "mpd"
                        })
                    for item in found:
                        if self.__authenticate(item.secret, False):
                            break
                except ImportError:
                    print "MPD plugin: unable to check for saved passwords"
                except matekeyring.DeniedError:
                    print "MPD plugin: no passwords found"
                except AttributeError:
                    print "MPD plugin: password lookup requires matekeyring >= 2.18.0"

            if not self.__authenticated:
                gobject.idle_add(self.__plugin._show_password_dialog)

        except socket.error:
            # Thrown if connect fails.
            self.__mpd = None
예제 #5
0
    def __try_connect (self):
        import mpd
        try:
            full_key = self.__plugin._conf.resolve_plugin_key (self.__plugin, "host")
            host = self.__plugin._conf.client.get_string (full_key)
            if host == "":
                host = "localhost"
                self.host = os.environ.get ("MPD_HOST", "localhost")
                if "@" in self.host:
                    self.host = (self.host.split ("@", 1))[1]
            else:
                self.host = host

            full_key = self.__plugin._conf.resolve_plugin_key (self.__plugin, "port")
            port = self.__plugin._conf.client.get_int (full_key)
            if port <= 0:
                port = 6600
                self.port = int (os.environ.get ("MPD_PORT", 6600))
            else:
                self.port = port

            self.__mpd = mpd.MPDClient ()
            self.__mpd.connect (host, port)

            if not self.__authenticate ():
                try:
                    import matekeyring
                    keyring = matekeyring.get_default_keyring_sync ()
                    found = matekeyring.find_items_sync (matekeyring.ITEM_NETWORK_PASSWORD,
                                                          { "server":self.host, "port":self.port, "protocol":"mpd" })
                    for item in found:
                        if self.__authenticate (item.secret, False):
                            break
                except ImportError:
                    print "MPD plugin: unable to check for saved passwords"
                except matekeyring.DeniedError:
                    print "MPD plugin: no passwords found"
                except AttributeError:
                    print "MPD plugin: password lookup requires matekeyring >= 2.18.0"

            if not self.__authenticated:
                gobject.idle_add (self.__plugin._show_password_dialog)

        except socket.error:
            # Thrown if connect fails.
            self.__mpd = None
예제 #6
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 %s@%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 %s@%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")
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:
        def item_get_info_cb(result, item, data):
            if result is None:
                secret = item.get_secret()
                data.append(secret)
            else:
                print "get_item_info result:", result
            gtk.main_quit()
        data = []
        matekeyring.item_get_info(keyring, auth_token, item_get_info_cb, data)
        gtk.main()
        try:
            secret, = data
        except ValueError:
            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
            def item_create_callback(result, auth_token, user_data):
                assert user_data == "user data"
                if result is None:
                    print "result is ok"
                    mateconf.client_get_default().set_int(MATECONF_AUTH_KEY, auth_token)
                else:
                    print "result:", result
                gtk.main_quit()
            auth_token = matekeyring.item_create(
                keyring,
                matekeyring.ITEM_GENERIC_SECRET,
                "MatePythonDesktop keyring example, login information",
                dict(appname="MatePythonDesktop, sync keyring example"),
                "\n".join((login, password)), True,
                item_create_callback, "user data")
            gtk.main()
            return login, password
        else:
            raise SystemExit
예제 #8
0
 def __init__(self, host, realm):
     self._realm = realm
     self._host = host
     self._protocol = "http"
     self._keyring = matekeyring.get_default_keyring_sync()
예제 #9
0
 def __init__(self, host, realm):
     self._realm = realm
     self._host = host
     self._protocol = "http"
     self._keyring = matekeyring.get_default_keyring_sync()