Skip to content

Commit cb6323e

Browse files
committed
use proxy_auth credentials for Authorization header + define custom Plotly-Authorization header for API
1 parent 7207c0c commit cb6323e

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

plotly/plotly/plotly.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import six
2424
import base64
2525
import requests
26-
from requests.auth import HTTPBasicAuth
2726

2827
if sys.version[:1] == '2':
2928
from urlparse import urlparse
@@ -587,9 +586,20 @@ def get(figure_or_data, format='png', width=None, height=None):
587586
credentials = get_credentials()
588587
validate_credentials(credentials)
589588
username, api_key = credentials['username'], credentials['api_key']
589+
encoded_api_auth = base64.b64encode(six.b('{0}:{1}'.format(
590+
username, api_key))).decode('utf8')
591+
592+
# custom enterprise + proxy authentication
593+
proxy_username = credentials['proxy_username']
594+
proxy_passwd = credentials['proxy_passwd']
595+
encoded_proxy_auth = base64.b64encode(six.b('{0}:{1}'.format(
596+
proxy_username, proxy_passwd))).decode('utf8')
597+
590598
headers = {'Plotly-Version': version.__version__,
591599
'Content-Type': 'application/json',
592-
'Plotly-Client-Platform': 'python'}
600+
'Plotly-Client-Platform': 'python',
601+
'Authorization': 'Basic ' + encoded_proxy_auth,
602+
'Plotly-Authorization': 'Basic ' + encoded_api_auth}
593603

594604
payload = {'figure': figure, 'format': format}
595605
if width is not None:
@@ -602,7 +612,6 @@ def get(figure_or_data, format='png', width=None, height=None):
602612
res = requests.post(
603613
url, data=json.dumps(payload, cls=utils.PlotlyJSONEncoder),
604614
headers=headers, verify=get_config()['plotly_ssl_verification'],
605-
auth=HTTPBasicAuth(username, api_key)
606615
)
607616

608617
headers = res.headers
@@ -1200,17 +1209,22 @@ def api_url(cls, resource):
12001209
def headers(cls):
12011210
credentials = get_credentials()
12021211
# todo, validate here?
1203-
un, api_key = credentials['username'], credentials['api_key']
1204-
encoded_un_key_pair = base64.b64encode(
1205-
six.b('{0}:{1}'.format(un, api_key))
1206-
).decode('utf8')
1212+
username, api_key = credentials['username'], credentials['api_key']
1213+
encoded_api_auth = base64.b64encode(six.b('{0}:{1}'.format(
1214+
username, api_key))).decode('utf8')
1215+
1216+
# custom enterprise + proxy authentication
1217+
proxy_username = credentials['proxy_username']
1218+
proxy_passwd = credentials['proxy_passwd']
1219+
encoded_proxy_auth = base64.b64encode(six.b('{0}:{1}'.format(
1220+
proxy_username, proxy_passwd))).decode('utf8')
12071221

12081222
return {
1209-
'authorization': 'Basic ' + encoded_un_key_pair,
1223+
'authorization': 'Basic ' + encoded_proxy_auth,
1224+
'plotly-authorization': 'Basic ' + encoded_api_auth,
12101225
'plotly-client-platform': 'python {0}'.format(version.__version__)
12111226
}
12121227

1213-
12141228
def validate_credentials(credentials):
12151229
"""
12161230
Currently only checks for truthy username and api_key

0 commit comments

Comments
 (0)