def setLogin(self, url, realm, username, password): """ Public method to set the login credentials. @param url URL to set the credentials for (QUrl) @param realm realm to set the credentials for (string or QString) @param username username for the login (string or QString) @param password password for the login (string or QString) """ if not self.__loaded: self.__load() key = self.__createKey(url, realm) self.__logins[key] = (unicode(username), Utilities.pwEncode(password)) self.emit(SIGNAL("changed()"))
def post(self, request, data): """ Public method to check, if the data to be sent contains login data. @param request reference to the network request (QNetworkRequest) @param data data to be sent (QByteArray) """ # shall passwords be saved? if not Preferences.getHelp("SavePasswords"): return # observe privacy if QWebSettings.globalSettings().testAttribute( QWebSettings.PrivateBrowsingEnabled): return if not self.__loaded: self.__load() # determine the url refererHeader = request.rawHeader("Referer") if refererHeader.isEmpty(): return url = QUrl.fromEncoded(refererHeader) url = self.__stripUrl(url) # check that url isn't in __never if unicode(url.toString()) in self.__never: return # check the request type v = request.attribute(QNetworkRequest.User + 101) if not v.isValid(): return navType = v.toInt()[0] if navType != QWebPage.NavigationTypeFormSubmitted: return # determine the QWebPage v = request.attribute(QNetworkRequest.User + 100) webPage = v.toPyObject() if webPage == NotImplemented or webPage is None: return # determine the requests content type contentTypeHeader = request.rawHeader("Content-Type") if contentTypeHeader.isEmpty(): return multipart = contentTypeHeader.startsWith("multipart/form-data") if multipart: boundary = contentTypeHeader.split(" ")[1].split("=")[1] else: boundary = None # find the matching form on the web page form = self.__findForm(webPage, data, boundary = boundary) if not form.isValid(): return form.url = QUrl(url) # check, if the form has a password if not form.hasAPassword: return # prompt, if the form has never be seen key = self.__createKey(url, "") if key not in self.__loginForms: mb = QMessageBox() mb.setText(self.trUtf8( """<b>Would you like to save this password?</b><br/>""" """To review passwords you have saved and remove them, """ """use the password management dialog of the Settings menu.""" )) neverButton = mb.addButton( self.trUtf8("Never for this site"), QMessageBox.DestructiveRole) noButton = mb.addButton(self.trUtf8("Not now"), QMessageBox.RejectRole) yesButton = mb.addButton(QMessageBox.Yes) mb.exec_() if mb.clickedButton() == neverButton: self.__never.append(unicode(url.toString())) return elif mb.clickedButton() == noButton: return # extract user name and password user = "" password = "" for index in range(len(form.elements)): element = form.elements[index] name = element[0].toLower() type_ = form.elementTypes[unicode(element[0])] if user == "" and \ type_ == "text": user = element[1] elif password == "" and \ type_ == "password": password = element[1] form.elements[index] = (element[0], QString("--PASSWORD--")) if user and password: self.__logins[key] = (unicode(user), Utilities.pwEncode(password)) self.__loginForms[key] = form self.emit(SIGNAL("changed()"))