diff --git a/tests/core/providers/test_auto_provider.py b/tests/core/providers/test_auto_provider.py index f322b68694..09c3f41e2a 100644 --- a/tests/core/providers/test_auto_provider.py +++ b/tests/core/providers/test_auto_provider.py @@ -26,9 +26,11 @@ @pytest.fixture(autouse=True) -def delete_infura_key(monkeypatch): - monkeypatch.delenv('INFURA_API_KEY', raising=False) +def delete_environment_variables(monkeypatch): + monkeypatch.delenv('WEB3_INFURA_PROJECT_ID', raising=False) monkeypatch.delenv('WEB3_INFURA_API_KEY', raising=False) + monkeypatch.delenv('WEB3_INFURA_API_SECRET', raising=False) + monkeypatch.delenv('WEB3_INFURA_SCHEME', raising=False) @pytest.mark.parametrize( @@ -105,7 +107,7 @@ def test_web3_auto_infura_websocket_default(monkeypatch, caplog, environ_name): monkeypatch.setenv('WEB3_INFURA_SCHEME', 'wss') API_KEY = 'aoeuhtns' monkeypatch.setenv(environ_name, API_KEY) - expected_url = 'wss://%s/ws/v3/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY) + expected_url = 'wss://:@%s/ws/v3/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY) importlib.reload(infura) assert len(caplog.record_tuples) == 0 @@ -122,3 +124,32 @@ def test_web3_auto_infura_raises_error_with_nonexistent_scheme(monkeypatch): error_msg = "Cannot connect to Infura with scheme 'not-a-scheme'" with pytest.raises(ValidationError, match=error_msg): importlib.reload(infura) + + +@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID']) +def test_web3_auto_infura_websocket_with_secret(monkeypatch, caplog, environ_name): + monkeypatch.setenv(environ_name, 'test') + monkeypatch.setenv('WEB3_INFURA_API_SECRET', 'secret') + + importlib.reload(infura) + + w3 = infura.w3 + assert isinstance(w3.provider, WebsocketProvider) + expected_url = 'wss://:secret@%s/ws/v3/test' % (infura.INFURA_MAINNET_DOMAIN) + assert getattr(w3.provider, 'endpoint_uri') == expected_url + + +@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID']) +def test_web3_auto_infura_with_secret(monkeypatch, caplog, environ_name): + monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https') + monkeypatch.setenv(environ_name, 'test') + monkeypatch.setenv('WEB3_INFURA_API_SECRET', 'secret') + + importlib.reload(infura) + + w3 = infura.w3 + assert isinstance(w3.provider, HTTPProvider) + expected_url = 'https://%s/v3/test' % (infura.INFURA_MAINNET_DOMAIN) + expected_auth_value = ('', 'secret') + assert getattr(w3.provider, 'endpoint_uri') == expected_url + assert w3.provider.get_request_kwargs()['auth'] == expected_auth_value diff --git a/web3/auto/infura/__init__.py b/web3/auto/infura/__init__.py index aa29978adb..b04e8ba37d 100644 --- a/web3/auto/infura/__init__.py +++ b/web3/auto/infura/__init__.py @@ -5,9 +5,11 @@ from .endpoints import ( INFURA_MAINNET_DOMAIN, + build_http_headers, build_infura_url, ) +_headers = build_http_headers() _infura_url = build_infura_url(INFURA_MAINNET_DOMAIN) -w3 = Web3(load_provider_from_uri(_infura_url)) +w3 = Web3(load_provider_from_uri(_infura_url, _headers)) diff --git a/web3/auto/infura/endpoints.py b/web3/auto/infura/endpoints.py index b7ac9d03c8..62226a04fa 100644 --- a/web3/auto/infura/endpoints.py +++ b/web3/auto/infura/endpoints.py @@ -29,12 +29,24 @@ def load_api_key(): return key +def load_secret(): + return os.environ.get('WEB3_INFURA_API_SECRET', '') + + +def build_http_headers(): + secret = load_secret() + if secret: + headers = {'auth': ('', secret)} + return headers + + def build_infura_url(domain): scheme = os.environ.get('WEB3_INFURA_SCHEME', WEBSOCKET_SCHEME) key = load_api_key() + secret = load_secret() if scheme == WEBSOCKET_SCHEME: - return "%s://%s/ws/v3/%s" % (scheme, domain, key) + return "%s://:%s@%s/ws/v3/%s" % (scheme, secret, domain, key) elif scheme == HTTP_SCHEME: return "%s://%s/v3/%s" % (scheme, domain, key) else: diff --git a/web3/auto/infura/kovan.py b/web3/auto/infura/kovan.py index 3a15bff6f0..e9b12005f4 100644 --- a/web3/auto/infura/kovan.py +++ b/web3/auto/infura/kovan.py @@ -5,9 +5,11 @@ from .endpoints import ( INFURA_KOVAN_DOMAIN, + build_http_headers, build_infura_url, ) +_headers = build_http_headers() _infura_url = build_infura_url(INFURA_KOVAN_DOMAIN) -w3 = Web3(load_provider_from_uri(_infura_url)) +w3 = Web3(load_provider_from_uri(_infura_url, _headers)) diff --git a/web3/auto/infura/mainnet.py b/web3/auto/infura/mainnet.py index aa29978adb..b04e8ba37d 100644 --- a/web3/auto/infura/mainnet.py +++ b/web3/auto/infura/mainnet.py @@ -5,9 +5,11 @@ from .endpoints import ( INFURA_MAINNET_DOMAIN, + build_http_headers, build_infura_url, ) +_headers = build_http_headers() _infura_url = build_infura_url(INFURA_MAINNET_DOMAIN) -w3 = Web3(load_provider_from_uri(_infura_url)) +w3 = Web3(load_provider_from_uri(_infura_url, _headers)) diff --git a/web3/auto/infura/rinkeby.py b/web3/auto/infura/rinkeby.py index 35b870b638..972df83dfb 100644 --- a/web3/auto/infura/rinkeby.py +++ b/web3/auto/infura/rinkeby.py @@ -8,10 +8,12 @@ from .endpoints import ( INFURA_RINKEBY_DOMAIN, + build_http_headers, build_infura_url, ) +_headers = build_http_headers() _infura_url = build_infura_url(INFURA_RINKEBY_DOMAIN) -w3 = Web3(load_provider_from_uri(_infura_url)) +w3 = Web3(load_provider_from_uri(_infura_url, _headers)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) diff --git a/web3/auto/infura/ropsten.py b/web3/auto/infura/ropsten.py index cde15e757b..d82356bcaa 100644 --- a/web3/auto/infura/ropsten.py +++ b/web3/auto/infura/ropsten.py @@ -5,9 +5,11 @@ from .endpoints import ( INFURA_ROPSTEN_DOMAIN, + build_http_headers, build_infura_url, ) +_headers = build_http_headers() _infura_url = build_infura_url(INFURA_ROPSTEN_DOMAIN) -w3 = Web3(load_provider_from_uri(_infura_url)) +w3 = Web3(load_provider_from_uri(_infura_url, _headers)) diff --git a/web3/providers/auto.py b/web3/providers/auto.py index 3c5b33b264..26c9ca3ab6 100644 --- a/web3/providers/auto.py +++ b/web3/providers/auto.py @@ -25,12 +25,12 @@ def load_provider_from_environment(): return load_provider_from_uri(uri_string) -def load_provider_from_uri(uri_string): +def load_provider_from_uri(uri_string, headers=None): uri = urlparse(uri_string) if uri.scheme == 'file': return IPCProvider(uri.path) elif uri.scheme in HTTP_SCHEMES: - return HTTPProvider(uri_string) + return HTTPProvider(uri_string, headers) elif uri.scheme in WS_SCHEMES: return WebsocketProvider(uri_string) else: