Merge "Prevent override of bearer token by .netrc"
This commit is contained in:
commit
685a662e54
@ -17,7 +17,7 @@ from tests.unit import FakeRequestResponse
|
|||||||
|
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
from zuulclient.api import ZuulRESTClient
|
from zuulclient.api import ZuulRESTClient, BearerAuth
|
||||||
from zuulclient.api import ZuulRESTException
|
from zuulclient.api import ZuulRESTException
|
||||||
|
|
||||||
|
|
||||||
@ -42,8 +42,8 @@ class TestApi(BaseTestCase):
|
|||||||
self.assertEqual('https://fake.zuul/', client.url)
|
self.assertEqual('https://fake.zuul/', client.url)
|
||||||
self.assertEqual('https://fake.zuul/api/', client.base_url)
|
self.assertEqual('https://fake.zuul/api/', client.base_url)
|
||||||
self.assertEqual(True, client.session.verify)
|
self.assertEqual(True, client.session.verify)
|
||||||
self.assertEqual('Bearer %s' % token,
|
self.assertTrue(isinstance(client.session.auth, BearerAuth))
|
||||||
client.session.headers.get('Authorization'))
|
self.assertEqual(token, client.session.auth._token)
|
||||||
|
|
||||||
def _test_status_check(self, client, verb, func, *args, **kwargs):
|
def _test_status_check(self, client, verb, func, *args, **kwargs):
|
||||||
# validate request errors
|
# validate request errors
|
||||||
|
@ -21,6 +21,23 @@ class ZuulRESTException(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class BearerAuth(requests.auth.AuthBase):
|
||||||
|
"""Custom authentication helper class.
|
||||||
|
|
||||||
|
Authentication helper class to work around requests' default behavior
|
||||||
|
of using ~/.netrc to authenticate despite having set an explicit
|
||||||
|
authorization header.
|
||||||
|
See also https://github.com/psf/requests/issues/3929
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, token):
|
||||||
|
self._token = token
|
||||||
|
|
||||||
|
def __call__(self, r):
|
||||||
|
r.headers["Authorization"] = 'Bearer %s' % self._token
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
class ZuulRESTClient(object):
|
class ZuulRESTClient(object):
|
||||||
"""Basic client for Zuul's REST API"""
|
"""Basic client for Zuul's REST API"""
|
||||||
def __init__(self, url, verify=False, auth_token=None):
|
def __init__(self, url, verify=False, auth_token=None):
|
||||||
@ -33,8 +50,7 @@ class ZuulRESTClient(object):
|
|||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
self.session.verify = self.verify
|
self.session.verify = self.verify
|
||||||
if self.auth_token:
|
if self.auth_token:
|
||||||
self.session.headers.update(
|
self.session.auth = BearerAuth(self.auth_token)
|
||||||
dict(Authorization='Bearer %s' % self.auth_token))
|
|
||||||
|
|
||||||
def _check_request_status(self, req):
|
def _check_request_status(self, req):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user