def proxyAuthenticationRequired(proxy, auth):
    """
    Module slot to handle a proxy authentication request.
    
    @param proxy reference to the proxy object (QNetworkProxy)
    @param auth reference to the authenticator object (QAuthenticator)
    """
    info = QCoreApplication.translate(
        "E5NetworkProxyFactory",
        "<b>Connect to proxy '{0}' using:</b>")\
        .format(Utilities.html_encode(proxy.hostName()))

    from UI.AuthenticationDialog import AuthenticationDialog
    dlg = AuthenticationDialog(info, proxy.user(), True)
    dlg.setData(proxy.user(), proxy.password())
    if dlg.exec_() == QDialog.Accepted:
        username, password = dlg.getData()
        auth.setUser(username)
        auth.setPassword(password)
        if dlg.shallSave():
            scheme = schemeFromProxyType(proxy.type())
            if scheme and scheme != "NoProxy":
                Preferences.setUI("ProxyUser/{0}".format(scheme), username)
                Preferences.setUI("ProxyPassword/{0}".format(scheme), password)
            proxy.setUser(username)
            proxy.setPassword(password)
def proxyAuthenticationRequired(proxy, auth):
    """
    Module slot to handle a proxy authentication request.
    
    @param proxy reference to the proxy object (QNetworkProxy)
    @param auth reference to the authenticator object (QAuthenticator)
    """
    info = QCoreApplication.translate("E5NetworkProxyFactory", "<b>Connect to proxy '{0}' using:</b>").format(
        Utilities.html_encode(proxy.hostName())
    )

    from UI.AuthenticationDialog import AuthenticationDialog

    dlg = AuthenticationDialog(info, proxy.user(), True)
    dlg.setData(proxy.user(), proxy.password())
    if dlg.exec_() == QDialog.Accepted:
        username, password = dlg.getData()
        auth.setUser(username)
        auth.setPassword(password)
        if dlg.shallSave():
            scheme = schemeFromProxyType(proxy.type())
            if scheme and scheme != "NoProxy":
                Preferences.setUI("ProxyUser/{0}".format(scheme), username)
                Preferences.setUI("ProxyPassword/{0}".format(scheme), password)
            proxy.setUser(username)
            proxy.setPassword(password)
Exemple #3
0
 def authentication(self, url, auth, page=None):
     """
     Public slot to handle an authentication request.
     
     @param url URL requesting authentication
     @type QUrl
     @param auth reference to the authenticator object
     @type QAuthenticator
     @param page reference to the web page
     @type QWebEnginePage or None
     """
     urlRoot = (
         "{0}://{1}".format(url.scheme(), url.authority())
     )
     realm = auth.realm()
     if not realm and 'realm' in auth.options():
         realm = auth.option("realm")
     if realm:
         info = self.tr(
             "<b>Enter username and password for '{0}', realm '{1}'</b>"
         ).format(urlRoot, realm)
     else:
         info = self.tr(
             "<b>Enter username and password for '{0}'</b>"
         ).format(urlRoot)
     
     from UI.AuthenticationDialog import AuthenticationDialog
     import WebBrowser.WebBrowserWindow
     
     dlg = AuthenticationDialog(info, auth.user(),
                                Preferences.getUser("SavePasswords"),
                                Preferences.getUser("SavePasswords"))
     if Preferences.getUser("SavePasswords"):
         username, password = (
             WebBrowser.WebBrowserWindow.WebBrowserWindow
             .passwordManager().getLogin(url, realm)
         )
         if username:
             dlg.setData(username, password)
     if dlg.exec_() == QDialog.Accepted:
         username, password = dlg.getData()
         auth.setUser(username)
         auth.setPassword(password)
         if Preferences.getUser("SavePasswords") and dlg.shallSave():
             (
                 WebBrowser.WebBrowserWindow.WebBrowserWindow
                 .passwordManager().setLogin(
                     url, realm, username, password)
             )
     else:
         if page is not None:
             self.__showAuthenticationErrorPage(page, url)
 def __proxyAuthenticationRequired(self, proxy, auth):
     """
     Private slot to handle a proxy authentication request.
     
     @param proxy reference to the proxy object (QNetworkProxy)
     @param auth reference to the authenticator object (QAuthenticator)
     """
     info = self.trUtf8("<b>Connect to proxy '%1' using:</b>")\
         .arg(Qt.escape(proxy.hostName()))
     
     dlg = AuthenticationDialog(info, proxy.user(), True)
     dlg.setData(proxy.user(), proxy.password())
     if dlg.exec_() == QDialog.Accepted:
         username, password = dlg.getData()
         auth.setUser(username)
         auth.setPassword(password)
         if dlg.shallSave():
             Preferences.setUI("ProxyUser", username)
             Preferences.setUI("ProxyPassword", password)
Exemple #5
0
 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
Exemple #6
0
 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