Skip to content

Commit 3b8cca4

Browse files
authored
Merge pull request #32 from jpslopes/master
Add support to pass additional parameters to the KeyBundle's HTTP client
2 parents d867388 + d049e7c commit 3b8cca4

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/cryptojwt/key_bundle.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class KeyBundle:
152152

153153
def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
154154
fileformat="jwks", keytype="RSA", keyusage=None, kid='',
155-
httpc=None):
155+
httpc=None, httpc_params=None):
156156
"""
157157
Contains a set of keys that have a common origin.
158158
The sources can be serveral:
@@ -171,6 +171,8 @@ def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
171171
:param keyusage: What the key loaded from file should be used for.
172172
Only applicable for DER files
173173
:param httpc: A HTTP client function
174+
:param httpc_params: Additional parameters to pass to the HTTP client
175+
function
174176
"""
175177

176178
self._keys = []
@@ -193,6 +195,7 @@ def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
193195
else:
194196
self.httpc = requests.request
195197
self.verify_ssl = verify_ssl
198+
self.httpc_params = httpc_params or {}
196199

197200
if keys:
198201
self.source = None
@@ -314,13 +317,11 @@ def do_remote(self):
314317
:return: True or False if load was successful
315318
"""
316319
if self.verify_ssl is not None:
317-
args = {"verify": self.verify_ssl}
318-
else:
319-
args = {}
320+
self.httpc_params["verify"] = self.verify_ssl
320321

321322
try:
322323
LOGGER.debug('KeyBundle fetch keys from: %s', self.source)
323-
_http_resp = self.httpc('GET', self.source, **args)
324+
_http_resp = self.httpc('GET', self.source, **self.httpc_params)
324325
except Exception as err:
325326
LOGGER.error(err)
326327
raise UpdateFailed(

tests/test_03_key_bundle.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import time
66

77
import pytest
8+
import requests
9+
import responses
810
from cryptography.hazmat.primitives.asymmetric import rsa
911
from cryptojwt.jwk.ec import new_ec_key
1012
from cryptojwt.jwk.hmac import SYMKey
@@ -471,6 +473,31 @@ def test_local_jwk_copy():
471473
# assert len(kb.get('oct')) == 1
472474

473475

476+
@pytest.fixture()
477+
def mocked_jwks_response():
478+
with responses.RequestsMock() as rsps:
479+
yield rsps
480+
481+
482+
def test_httpc_params_1():
483+
source = 'https://login.salesforce.com/id/keys' # From test_jwks_url()
484+
# Mock response
485+
responses.add(method=responses.GET, url=source, json=JWKS_DICT, status=200)
486+
httpc_params = {'timeout': (2, 2)} # connect, read timeouts in seconds
487+
kb = KeyBundle(source=source, httpc=requests.request,
488+
httpc_params=httpc_params)
489+
assert kb.do_remote()
490+
491+
492+
def test_httpc_params_2():
493+
httpc_params = {'timeout': 0}
494+
kb = KeyBundle(source='https://login.salesforce.com/id/keys',
495+
httpc=requests.request, httpc_params=httpc_params)
496+
# Will always fail to fetch the JWKS because the timeout cannot be set
497+
# to 0s
498+
assert not kb.update()
499+
500+
474501
def test_update_2():
475502
rsa_key = new_rsa_key()
476503
_jwks = {"keys": [rsa_key.serialize()]}

0 commit comments

Comments
 (0)