def handleAuthenticationRequired(self, requestUrl: QUrl, auth: QAuthenticator): mainWindow = self.view().window() dialog = QDialog(mainWindow, Qt.WindowFlags() & ~Qt.WindowContextHelpButtonHint) dialog.setModal(True) # dialog.setWindowFlags(dialog.windowFlags() & ~Qt.WindowContextHelpButtonHint) passwordDialog = Ui_PasswordDialog() passwordDialog.setupUi(dialog) passwordDialog.m_iconLabel.setText('') icon = QIcon(mainWindow.style().standardIcon( QStyle.SP_MessageBoxQuestion, widget=mainWindow)) passwordDialog.m_iconLabel.setPixmap(icon.pixmap(32, 32)) introMessage = self.tr( "Enter username and password for \"{}\" at {}").format( # auth.realm(), requestUrl.toString().toHtmlEscaped()) auth.realm(), requestUrl.toString()) # TODO: toHtmlEscaped()? passwordDialog.m_infoLabel.setText(introMessage) passwordDialog.m_infoLabel.setWordWrap(True) if dialog.exec() == QDialog.Accepted: auth.setUser(passwordDialog.m_userNameLineEdit.text()) auth.setPassword(passwordDialog.m_passwordLineEdit.text()) else: # Set authenticator null if dialog is cancelled # auth = QAuthenticator() self.setHtml('auth canceled') # TODO: proper workaround?
def _on_proxy_authentication_required(self, url, authenticator, proxy_host): """Called when a proxy needs authentication.""" msg = "<b>{}</b> requires a username and password.".format( html_utils.escape(proxy_host)) answer = message.ask(title="Proxy authentication required", text=msg, mode=usertypes.PromptMode.user_pwd, abort_on=[self.shutting_down, self.load_started]) if answer is not None: authenticator.setUser(answer.user) authenticator.setPassword(answer.password) else: try: # pylint: disable=no-member, useless-suppression sip.assign(authenticator, QAuthenticator()) except AttributeError: url_string = url.toDisplayString() error_page = jinja.render( 'error.html', title="Error loading page: {}".format(url_string), url=url_string, error="Proxy authentication required", icon='') self.set_html(error_page)
def _on_authentication_required(self, url, authenticator): # FIXME:qtwebengine support .netrc answer = shared.authentication_required( url, authenticator, abort_on=[self.shutting_down, self.load_started]) if answer is None: try: # pylint: disable=no-member, useless-suppression sip.assign(authenticator, QAuthenticator()) except AttributeError: # WORKAROUND for # https://www.riverbankcomputing.com/pipermail/pyqt/2016-December/038400.html url_string = url.toDisplayString() error_page = jinja.render( 'error.html', title="Error loading page: {}".format(url_string), url=url_string, error="Authentication required", icon='') self.set_html(error_page)
def _on_authentication_required(self, url, authenticator): netrc_success = False if not self.data.netrc_used: self.data.netrc_used = True netrc_success = shared.netrc_authentication(url, authenticator) if not netrc_success: abort_on = [self.shutting_down, self.load_started] answer = shared.authentication_required(url, authenticator, abort_on) if not netrc_success and answer is None: try: # pylint: disable=no-member, useless-suppression sip.assign(authenticator, QAuthenticator()) # pylint: enable=no-member, useless-suppression except AttributeError: # WORKAROUND for # https://www.riverbankcomputing.com/pipermail/pyqt/2016-December/038400.html self._show_error_page(url, "Authentication required")
def _on_proxy_authentication_required(self, url, authenticator, proxy_host): """Called when a proxy needs authentication.""" msg = "<b>{}</b> requires a username and password.".format( html_utils.escape(proxy_host)) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) answer = message.ask( title="Proxy authentication required", text=msg, mode=usertypes.PromptMode.user_pwd, abort_on=[self.shutting_down, self.load_started], url=urlstr) if answer is not None: authenticator.setUser(answer.user) authenticator.setPassword(answer.password) else: try: # pylint: disable=no-member, useless-suppression sip.assign(authenticator, QAuthenticator()) # pylint: enable=no-member, useless-suppression except AttributeError: self._show_error_page(url, "Proxy authentication required")
def _on_authentication_required(self, url, authenticator): netrc_success = False if not self.data.netrc_used: self.data.netrc_used = True netrc_success = shared.netrc_authentication(url, authenticator) if not netrc_success: abort_on = [self.shutting_down, self.load_started] answer = shared.authentication_required(url, authenticator, abort_on) if not netrc_success and answer is None: try: # pylint: disable=no-member, useless-suppression sip.assign(authenticator, QAuthenticator()) # pylint: enable=no-member, useless-suppression except AttributeError: # WORKAROUND for # https://www.riverbankcomputing.com/pipermail/pyqt/2016-December/038400.html url_string = url.toDisplayString() error_page = jinja.render( 'error.html', title="Error loading page: {}".format(url_string), url=url_string, error="Authentication required") self.set_html(error_page)
def webAuthenticationRequired(self, uri: QUrl, cred: QAuthenticator): print("Authentication Request from " + uri.toString()) if (args.enableAutoLogon): print("Using autologin credentials") cred.setUser(args.autoLogonUser) cred.setPassword(args.autoLogonPassword)
def reject(self): from PyQt5.QtNetwork import QAuthenticator import sip sip.assign(auth, QAuthenticator()) return super().reject()
def __doFtpLogin(self, username, password, byAuth=False): """ Private method to do the FTP login with asking for a username and password, if the login fails with an error 530. @param username user name to use for the login (string) @param password password to use for the login (string) @param byAuth flag indicating that the login data was provided by an authenticator (boolean) @return tuple of two flags indicating a successful login and if the login should be retried (boolean, boolean) """ try: self.__ftp.login(username, password) return True, False except E5FtpProxyError as err: code = str(err)[:3] if code[1] == "5": # could be a 530, check second line lines = str(err).splitlines() if lines[1][:3] == "530": if "usage" in "\n".join(lines[1:].lower()): # found a not supported proxy self.setError( QNetworkReply.ProxyConnectionRefusedError, self.tr("The proxy type seems to be wrong." " If it is not in the list of" " supported proxy types please report" " it with the instructions given by" " the proxy.\n{0}").format("\n".join( lines[1:]))) self.error.emit( QNetworkReply.ProxyConnectionRefusedError) return False, False else: from UI.AuthenticationDialog import \ AuthenticationDialog info = self.tr( "<b>Connect to proxy '{0}' using:</b>")\ .format(Utilities.html_encode( Preferences.getUI("ProxyHost/Ftp"))) dlg = AuthenticationDialog( info, Preferences.getUI("ProxyUser/Ftp"), True) dlg.setData(Preferences.getUI("ProxyUser/Ftp"), Preferences.getUI("ProxyPassword/Ftp")) if dlg.exec_() == QDialog.Accepted: username, password = dlg.getData() if dlg.shallSave(): Preferences.setUI("ProxyUser/Ftp", username) Preferences.setUI("ProxyPassword/Ftp", password) self.__ftp.setProxyAuthentication( username, password) return False, True return False, False except (ftplib.error_perm, ftplib.error_temp) as err: code = err.args[0].strip()[:3] if code in ["530", "421"]: # error 530 -> Login incorrect # error 421 -> Login may be incorrect (reported by some # proxies) if byAuth: self.__handler.setAuthenticator(self.url().host(), None) auth = None else: auth = self.__handler.getAuthenticator(self.url().host()) if not auth or auth.isNull() or not auth.user(): auth = QAuthenticator() self.__manager.authenticationRequired.emit(self, auth) if not auth.isNull(): if auth.user(): self.__handler.setAuthenticator( self.url().host(), auth) return False, True return False, False return False, True else: raise
def __doFtpLogin(self, username, password, byAuth=False): """ Private method to do the FTP login with asking for a username and password, if the login fails with an error 530. @param username user name to use for the login (string) @param password password to use for the login (string) @param byAuth flag indicating that the login data was provided by an authenticator (boolean) @return tuple of two flags indicating a successful login and if the login should be retried (boolean, boolean) """ try: self.__ftp.login(username, password) return True, False except E5FtpProxyError as err: code = str(err)[:3] if code[1] == "5": # could be a 530, check second line lines = str(err).splitlines() if lines[1][:3] == "530": if "usage" in "\n".join(lines[1:].lower()): # found a not supported proxy self.setError( QNetworkReply.ProxyConnectionRefusedError, self.tr("The proxy type seems to be wrong." " If it is not in the list of" " supported proxy types please report" " it with the instructions given by" " the proxy.\n{0}").format( "\n".join(lines[1:]))) self.error.emit( QNetworkReply.ProxyConnectionRefusedError) return False, False else: from UI.AuthenticationDialog import \ AuthenticationDialog info = self.tr( "<b>Connect to proxy '{0}' using:</b>")\ .format(Utilities.html_encode( Preferences.getUI("ProxyHost/Ftp"))) dlg = AuthenticationDialog( info, Preferences.getUI("ProxyUser/Ftp"), True) dlg.setData(Preferences.getUI("ProxyUser/Ftp"), Preferences.getUI("ProxyPassword/Ftp")) if dlg.exec_() == QDialog.Accepted: username, password = dlg.getData() if dlg.shallSave(): Preferences.setUI("ProxyUser/Ftp", username) Preferences.setUI( "ProxyPassword/Ftp", password) self.__ftp.setProxyAuthentication(username, password) return False, True return False, False except (ftplib.error_perm, ftplib.error_temp) as err: code = err.args[0].strip()[:3] if code in ["530", "421"]: # error 530 -> Login incorrect # error 421 -> Login may be incorrect (reported by some # proxies) if byAuth: self.__handler.setAuthenticator(self.url().host(), None) auth = None else: auth = self.__handler.getAuthenticator(self.url().host()) if not auth or auth.isNull() or not auth.user(): auth = QAuthenticator() self.__manager.authenticationRequired.emit(self, auth) if not auth.isNull(): if auth.user(): self.__handler.setAuthenticator(self.url().host(), auth) return False, True return False, False return False, True else: raise