Skip to content

Commit 8932fb0

Browse files
authored
Add context manager on urlopen in request verifier. closes #122 (#123)
1 parent 850cf99 commit 8932fb0

File tree

1 file changed

+12
-6
lines changed
  • ask-sdk-webservice-support/ask_sdk_webservice_support

1 file changed

+12
-6
lines changed

ask-sdk-webservice-support/ask_sdk_webservice_support/verifier.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import base64
2121
import typing
22+
import six
2223

2324
from dateutil import tz
2425
from datetime import datetime
@@ -32,6 +33,7 @@
3233
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
3334
from cryptography.hazmat.primitives.hashes import SHA1
3435
from cryptography.exceptions import InvalidSignature
36+
from contextlib import closing
3537

3638
from .verifier_constants import (
3739
SIGNATURE_CERT_CHAIN_URL_HEADER, SIGNATURE_HEADER,
@@ -285,12 +287,16 @@ def _load_cert_chain(self, cert_url, x509_backend=default_backend()):
285287
if cert_url in self._cert_cache:
286288
return self._cert_cache.get(cert_url)
287289
else:
288-
with urlopen(cert_url) as cert_response:
289-
cert_data = cert_response.read()
290-
x509_certificate = load_pem_x509_certificate(
291-
cert_data, x509_backend)
292-
self._cert_cache[cert_url] = x509_certificate
293-
return x509_certificate
290+
if six.PY2:
291+
with closing(urlopen(cert_url)) as cert_response:
292+
cert_data = cert_response.read()
293+
else:
294+
with urlopen(cert_url) as cert_response:
295+
cert_data = cert_response.read()
296+
x509_certificate = load_pem_x509_certificate(
297+
cert_data, x509_backend)
298+
self._cert_cache[cert_url] = x509_certificate
299+
return x509_certificate
294300
except ValueError as e:
295301
raise VerificationException(
296302
"Unable to load certificate from URL", e)

0 commit comments

Comments
 (0)