Skip to content

Commit b09416e

Browse files
ElienVandermaesenVITOsoxofaan
authored andcommitted
issue #254 Clear cache when authenticate
1 parent ca1bde1 commit b09416e

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

openeo/rest/connection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ def authenticate_basic(self, username: Optional[str] = None, password: Optional[
404404
if username is None:
405405
raise OpenEoClientException("No username/password given or found.")
406406

407+
self._capabilities_cache.clear()
407408
resp = self.get(
408409
'/credentials/basic',
409410
# /credentials/basic is the only endpoint that expects a Basic HTTP auth
@@ -472,6 +473,7 @@ def _get_oidc_provider(
472473
f"No OIDC provider given. Using first provider {provider_id!r} as advertised by backend."
473474
)
474475

476+
self._capabilities_cache.clear()
475477
provider_info = OidcProviderInfo.from_dict(provider) if parse_info else None
476478

477479
return provider_id, provider_info

openeo/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ def get(self, key: Union[str, tuple], load: Callable[[], Any]):
476476
self._cache[key] = load()
477477
return self._cache[key]
478478

479+
def clear(self):
480+
self._cache = {}
481+
479482

480483
def str_truncate(text: str, width: int = 64, ellipsis: str = "...") -> str:
481484
"""Shorten a string (with an ellipsis) if it is longer than certain length."""

tests/rest/test_connection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,39 @@ def test_capabilities_caching(requests_mock):
551551
assert con.capabilities().api_version() == "1.0.0"
552552
assert m.call_count == 1
553553

554+
def test_capabilities_caching_after_authenticate_basic(requests_mock):
555+
user, pwd = "john262", "J0hndo3"
556+
requests_mock.get(API_URL, json={"api_version": "1.0.0", "endpoints": BASIC_ENDPOINTS})
557+
requests_mock.get(API_URL + 'credentials/basic', text=_credentials_basic_handler(user, pwd))
558+
559+
with mock.patch('openeo.rest.connection.AuthConfig') as AuthConfig:
560+
conn = Connection(API_URL)
561+
conn._capabilities_cache._cache={"test":"test1"}
562+
assert conn._capabilities_cache._cache != {}
563+
AuthConfig.return_value.get_basic_auth.return_value = (user, pwd)
564+
conn.authenticate_basic(user, pwd)
565+
assert conn._capabilities_cache._cache == {}
566+
567+
568+
def test_capabilities_caching_after_authenticate_oidc(requests_mock):
569+
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
570+
client_id = "myclient"
571+
requests_mock.get(API_URL + 'credentials/oidc', json={
572+
"providers": [{"id": "fauth", "issuer": "https://fauth.test", "title": "Foo Auth", "scopes": ["openid", "im"]}]
573+
})
574+
oidc_mock = OidcMock(
575+
requests_mock=requests_mock,
576+
expected_grant_type="authorization_code",
577+
expected_client_id=client_id,
578+
expected_fields={"scope": "im openid"},
579+
oidc_issuer="https://fauth.test",
580+
scopes_supported=["openid", "im"],
581+
)
582+
conn = Connection(API_URL)
583+
conn._capabilities_cache._cache = {"test": "test1"}
584+
conn.authenticate_oidc_authorization_code(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open)
585+
assert conn._capabilities_cache._cache == {}
586+
554587

555588
def test_file_formats(requests_mock):
556589
requests_mock.get("https://oeo.test/", json={"api_version": "1.0.0"})

0 commit comments

Comments
 (0)