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

Using basic auth for proxy tunnel #1133

Merged
merged 2 commits into from
Aug 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions plotly/plotly/chunked_requests/chunked_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import ssl

from six.moves import http_client
from six.moves.urllib.parse import urlparse
from six.moves.urllib.parse import urlparse, unquote

from plotly.api import utils


class Stream:
Expand Down Expand Up @@ -86,6 +88,9 @@ def _get_proxy_config(self):

proxy_server = None
proxy_port = None
proxy_username = None
proxy_password = None
proxy_auth = None
ssl_enabled = self._ssl_enabled

if ssl_enabled:
Expand All @@ -99,8 +104,15 @@ def _get_proxy_config(self):
p = urlparse(proxy)
proxy_server = p.hostname
proxy_port = p.port
proxy_username = p.username
proxy_password = p.password

if proxy_username and proxy_password:
username = unquote(proxy_username)
password = unquote(proxy_password)
proxy_auth = utils.basic_auth(username, password)

return proxy_server, proxy_port
return proxy_server, proxy_port, proxy_auth

def _get_ssl_context(self):
"""
Expand All @@ -123,7 +135,7 @@ def _connect(self):
port = self._port
headers = self._headers
ssl_enabled = self._ssl_enabled
proxy_server, proxy_port = self._get_proxy_config()
proxy_server, proxy_port, proxy_auth = self._get_proxy_config()

if (proxy_server and proxy_port):
if ssl_enabled:
Expand All @@ -135,7 +147,12 @@ def _connect(self):
self._conn = http_client.HTTPConnection(
proxy_server, proxy_port
)
self._conn.set_tunnel(server, port)

tunnel_headers = None
if proxy_auth:
tunnel_headers = {'Proxy-Authorization': proxy_auth}

self._conn.set_tunnel(server, port, headers=tunnel_headers)
else:
if ssl_enabled:
context = self._get_ssl_context()
Expand Down