From 37d61d0415f6cc96a7a9abe057e1ae0f89fd977e Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Mon, 6 Nov 2017 10:04:48 +0000 Subject: Import py-macaroon-bakery_0.0.5.orig.tar.gz --- macaroonbakery/httpbakery/keyring.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'macaroonbakery/httpbakery/keyring.py') diff --git a/macaroonbakery/httpbakery/keyring.py b/macaroonbakery/httpbakery/keyring.py index f4e93f7..01a4349 100644 --- a/macaroonbakery/httpbakery/keyring.py +++ b/macaroonbakery/httpbakery/keyring.py @@ -3,10 +3,11 @@ from six.moves.urllib.parse import urlparse import requests -import macaroonbakery +import macaroonbakery as bakery +from macaroonbakery.httpbakery.error import BAKERY_PROTOCOL_HEADER -class ThirdPartyLocator(macaroonbakery.ThirdPartyLocator): +class ThirdPartyLocator(bakery.ThirdPartyLocator): ''' Implements macaroonbakery.ThirdPartyLocator by first looking in the backing cache and, if that fails, making an HTTP request to find the information associated with the given discharge location. @@ -23,33 +24,36 @@ class ThirdPartyLocator(macaroonbakery.ThirdPartyLocator): def third_party_info(self, loc): u = urlparse(loc) if u.scheme != 'https' and not self._allow_insecure: - raise macaroonbakery.ThirdPartyInfoNotFound( + raise bakery.ThirdPartyInfoNotFound( 'untrusted discharge URL {}'.format(loc)) loc = loc.rstrip('/') info = self._cache.get(loc) if info is not None: return info url_endpoint = '/discharge/info' - resp = requests.get(loc + url_endpoint) + headers = { + BAKERY_PROTOCOL_HEADER: str(bakery.LATEST_VERSION) + } + resp = requests.get(url=loc + url_endpoint, headers=headers) status_code = resp.status_code if status_code == 404: url_endpoint = '/publickey' - resp = requests.get(loc + url_endpoint) + resp = requests.get(url=loc + url_endpoint, headers=headers) status_code = resp.status_code if status_code != 200: - raise macaroonbakery.ThirdPartyInfoNotFound( + raise bakery.ThirdPartyInfoNotFound( 'unable to get info from {}'.format(url_endpoint)) json_resp = resp.json() if json_resp is None: - raise macaroonbakery.ThirdPartyInfoNotFound( + raise bakery.ThirdPartyInfoNotFound( 'no response from /discharge/info') pk = json_resp.get('PublicKey') if pk is None: - raise macaroonbakery.ThirdPartyInfoNotFound( + raise bakery.ThirdPartyInfoNotFound( 'no public key found in /discharge/info') - idm_pk = macaroonbakery.PublicKey.deserialize(pk) - version = json_resp.get('Version', macaroonbakery.BAKERY_V1) - self._cache[loc] = macaroonbakery.ThirdPartyInfo( + idm_pk = bakery.PublicKey.deserialize(pk) + version = json_resp.get('Version', bakery.VERSION_1) + self._cache[loc] = bakery.ThirdPartyInfo( version=version, public_key=idm_pk ) -- cgit v1.2.3