def set_smtp_password(server, username, password): schema = Secret.get_schema(Secret.SchemaType.COMPAT_NETWORK) attrs = dict(user=username, server=server, protocol='smtp') label = '{user}@{server}'.format_map(attrs) def callback(source, result): if not Secret.password_store_finish(result): log.error(_("Failed to store SMTP password in the keyring.")) else: log.debug("SMTP password stored in the keyring.") log.debug("Storing the SMTP password for %s in the keyring.", label) Secret.password_store(schema, attrs, collection=Secret.COLLECTION_DEFAULT, label=label, password=password, cancellable=None, callback=callback)
def start_smtp_password_lookup(server, username, callback): schema = Secret.get_schema(Secret.SchemaType.COMPAT_NETWORK) attrs = dict(user=username, server=server, protocol='smtp') def password_callback(source, result): password = Secret.password_lookup_finish(result) if password: log.debug("Found the SMTP password in the keyring.") else: log.debug("Did not find the SMTP password in the keyring.") callback(password or '') log.debug("Looking up the SMTP password for %s@%s in the keyring.", username, server) Secret.password_lookup(schema, attrs, cancellable=None, callback=password_callback)
def save_to_keyring(self, uri, username, password): schema = Secret.get_schema(Secret.SchemaType.COMPAT_NETWORK) attrs = dict(server=uri.get_host(), protocol=uri.get_scheme(), port=str(uri.get_port()), user=username) label = '{user}@{server}:{port}'.format_map(attrs) def password_stored_callback(source, result): if not Secret.password_store_finish(result): log.error(_("Failed to store HTTP password in the keyring.")) else: log.debug("HTTP password stored in the keyring.") log.debug("Storing the HTTP password for %s in the keyring.", label) Secret.password_store( schema, attrs, collection=Secret.COLLECTION_DEFAULT, label=label, password=password, cancellable=None, callback=password_stored_callback, )
def _find_in_keyring(self, uri, callback, source, result): service = Secret.Service.get_finish(result) schema = Secret.get_schema(Secret.SchemaType.COMPAT_NETWORK) attrs = dict(server=uri.get_host(), protocol=uri.get_scheme(), port=str(uri.get_port())) flags = (Secret.SearchFlags.UNLOCK | Secret.SearchFlags.LOAD_SECRETS) def search_callback(source, result): items = service.search_finish(result) if items: # Note: the search will give us the most recently used password # if several ones match (e.g. the user tried several different # usernames). This is good because if the wrong username gets # picked and is rejected by the server, we'll ask the user # again and then remember the more recently provided answer. # This is bad only if multiple sets of credentials are valid # but cause different data to be returned -- the user will # be forced to launch Seahorse and remove the saved credentials # manually to log in into a different account. log.debug("Found the HTTP password for %s in the keyring.", items[0].get_label()) username = items[0].get_attributes()['user'] password = items[0].get_secret().get_text() if len(items) > 1: # This will never happen. If you want this to happen, # add Secret.SearchFlags.ALL to the search flags. log.debug("Ignoring the other %d found passwords.", len(items) - 1) else: log.debug("Did not find any HTTP passwords for %s://%s:%s" " in the keyring.", uri.get_scheme(), uri.get_host(), uri.get_port()) username = self.username password = self.password callback(username, password) service.search(schema, attrs, flags, cancellable=None, callback=search_callback)