Exemple #1
0
def get_http_auth_credentials(qurl, authenticator, parent=None):
    qurl = QUrl(qurl)
    qurl.setFragment(None)
    realm = authenticator.realm()
    trealm = (' (%s)' % realm) if realm else ''
    ac = parent and parent.get_login_credentials(qurl.toString())
    d = Credentials(
        _('Please specify a password for {0}{1}').format(
            qurl.toString(), trealm), parent)
    if ac is not None:
        if ac['autologin']:
            authenticator.setUser(ac['username'])
            authenticator.setPassword(ac['password'])
            return
        d.credentials = ac['username'], ac['password']

    if d.exec() == QDialog.DialogCode.Accepted:
        username, password = d.credentials
        authenticator.setUser(username)
        authenticator.setPassword(password)
        if parent is not None:
            parent.on_login_form_submit(qurl.toString(), username, password)
    else:
        if parent is not None:
            parent.setHtml(
                '<p style="font-family:sans-serif">{} {}</p>'.format(
                    _('Authentication required to access: '),
                    '<a href="{0}">{0}</a>'.format(qurl.toDisplayString())))
Exemple #2
0
 def set_message(self, text, color_, bold=False, is_permanent=False):
     from vise.view import certificate_error_domains
     key = (text, color_.name(), bold, is_permanent)
     if key == self.current_key:
         return
     self.current_key = key
     self.is_permanent = is_permanent
     prefix = text.partition(':')[0]
     self.is_address = self.is_permanent and prefix.lower() in {
         'http', 'https', 'vise', 'file'
     }
     self.is_secure = prefix.lower() in {'https', 'vise', 'file'}
     color_ = color_ or self.palette().color(self.palette().WindowText)
     if self.is_address:
         qurl = QUrl(text)
         if self.is_secure and qurl.host() in certificate_error_domains:
             self.is_secure = False
         if qurl.scheme() in {'vise', 'file'}:
             host = qurl.path()
             rest = ''
             sep = ':'
         else:
             host = qurl.host()
             rest = qurl.toDisplayString(
                 QUrl.UrlFormattingOption.RemoveScheme
                 | QUrl.UrlFormattingOption.RemoveAuthority)
             sep = '://'
         self.static_text = QStaticText(
             '<span style="white-space:nowrap; color: {fg}">'
             '<span style="color:{emph}; font-weight:bold">{scheme}</span><span style="color:{dull}">{sep}</span>'
             '<span style="color:{fg}">{host}</span>'
             '<span style="color:{dull}">{rest}</span>'.format(
                 fg=color_.name(),
                 emph='green' if self.is_secure else 'red',
                 scheme=escape(qurl.scheme()),
                 host=escape(host),
                 dull=color('status bar dull foreground', 'gray'),
                 sep=sep,
                 rest=escape(rest)))
     else:
         self.static_text = QStaticText(
             '<span style="color:{}; font-weight: {}; white-space:nowrap">{}</span>'
             .format(color_.name(), ('bold' if bold else 'normal'),
                     escape(text)))
     to = QTextOption(Qt.AlignmentFlag.AlignLeft
                      | Qt.AlignmentFlag.AlignTop)
     to.setWrapMode(QTextOption.WrapMode.NoWrap)
     self.static_text.setTextOption(to)
     self.static_text.prepare(font=self.font())
     self.update()