Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use netrc for proxy authentication #4151

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion qutebrowser/browser/shared.py
Expand Up @@ -306,9 +306,13 @@ def netrc_authentication(url, authenticator):
# altogether.
return False
user, password = None, None
try:
host = url.host()
except AttributeError:
host = url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of thing definitely isn't the right way to go about this 😉

What about just making all callers pass a host (rather than a URL), and then do url.host() where netrc_authentication is called instead?

try:
net = netrc.netrc(config.val.content.netrc_file)
authenticators = net.authenticators(url.host())
authenticators = net.authenticators(host)
if authenticators is not None:
(user, _account, password) = authenticators
except FileNotFoundError:
Expand Down
41 changes: 24 additions & 17 deletions qutebrowser/browser/webengine/webenginetab.py
Expand Up @@ -1148,23 +1148,30 @@ def _on_history_trigger(self):
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")
netrc_success = False
if not self.data.netrc_used:
self.data.netrc_used = True
urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded)
netrc_success = shared.netrc_authentication(html_utils.escape(proxy_host),
authenticator)
if not netrc_success:
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")

@pyqtSlot(QUrl, 'QAuthenticator*')
def _on_authentication_required(self, url, authenticator):
Expand Down
29 changes: 18 additions & 11 deletions qutebrowser/browser/webkit/network/networkmanager.py
Expand Up @@ -294,17 +294,24 @@ def on_proxy_authentication_required(self, proxy, authenticator):
authenticator.setUser(user)
authenticator.setPassword(password)
else:
msg = '<b>{}</b> says:<br/>{}'.format(
html.escape(proxy.hostName()),
html.escape(authenticator.realm()))
abort_on = self._get_abort_signals()
answer = message.ask(
title="Proxy authentication required", text=msg,
mode=usertypes.PromptMode.user_pwd, abort_on=abort_on)
if answer is not None:
authenticator.setUser(answer.user)
authenticator.setPassword(answer.password)
_proxy_auth_cache[proxy_id] = answer
netrc_success = False
if not self.data.netrc_used:
self.data.netrc_used = True
urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded)
netrc_success = shared.netrc_authentication(html_utils.escape(proxy_host),
authenticator)
if not netrc_success:
msg = '<b>{}</b> says:<br/>{}'.format(
html.escape(proxy.hostName()),
html.escape(authenticator.realm()))
abort_on = self._get_abort_signals()
answer = message.ask(
title="Proxy authentication required", text=msg,
mode=usertypes.PromptMode.user_pwd, abort_on=abort_on)
if answer is not None:
authenticator.setUser(answer.user)
authenticator.setPassword(answer.password)
_proxy_auth_cache[proxy_id] = answer

@pyqtSlot()
def on_adopted_download_destroyed(self):
Expand Down