Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User can use their secret key to make Infura calls #1299

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions tests/core/providers/test_auto_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 3 additions & 1 deletion web3/auto/infura/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
14 changes: 13 additions & 1 deletion web3/auto/infura/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion web3/auto/infura/kovan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 3 additions & 1 deletion web3/auto/infura/mainnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 3 additions & 1 deletion web3/auto/infura/rinkeby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 3 additions & 1 deletion web3/auto/infura/ropsten.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 2 additions & 2 deletions web3/providers/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down