Skip to content

Commit f5d6026

Browse files
issue #254 Clear cache when authenticate
1 parent e106045 commit f5d6026

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
@@ -402,6 +402,7 @@ def authenticate_basic(self, username: Optional[str] = None, password: Optional[
402402
if username is None:
403403
raise OpenEoClientException("No username/password given or found.")
404404

405+
self._capabilities_cache.clear()
405406
resp = self.get(
406407
'/credentials/basic',
407408
# /credentials/basic is the only endpoint that expects a Basic HTTP auth
@@ -470,6 +471,7 @@ def _get_oidc_provider(
470471
f"No OIDC provider given. Using first provider {provider_id!r} as advertised by backend."
471472
)
472473

474+
self._capabilities_cache.clear()
473475
provider_info = OidcProviderInfo.from_dict(provider) if parse_info else None
474476

475477
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
@@ -513,6 +513,39 @@ def test_capabilities_caching(requests_mock):
513513
assert con.capabilities().api_version() == "1.0.0"
514514
assert m.call_count == 1
515515

516+
def test_capabilities_caching_after_authenticate_basic(requests_mock):
517+
user, pwd = "john262", "J0hndo3"
518+
requests_mock.get(API_URL, json={"api_version": "1.0.0", "endpoints": BASIC_ENDPOINTS})
519+
requests_mock.get(API_URL + 'credentials/basic', text=_credentials_basic_handler(user, pwd))
520+
521+
with mock.patch('openeo.rest.connection.AuthConfig') as AuthConfig:
522+
conn = Connection(API_URL)
523+
conn._capabilities_cache._cache={"test":"test1"}
524+
assert conn._capabilities_cache._cache != {}
525+
AuthConfig.return_value.get_basic_auth.return_value = (user, pwd)
526+
conn.authenticate_basic(user, pwd)
527+
assert conn._capabilities_cache._cache == {}
528+
529+
530+
def test_capabilities_caching_after_authenticate_oidc(requests_mock):
531+
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
532+
client_id = "myclient"
533+
requests_mock.get(API_URL + 'credentials/oidc', json={
534+
"providers": [{"id": "fauth", "issuer": "https://fauth.test", "title": "Foo Auth", "scopes": ["openid", "im"]}]
535+
})
536+
oidc_mock = OidcMock(
537+
requests_mock=requests_mock,
538+
expected_grant_type="authorization_code",
539+
expected_client_id=client_id,
540+
expected_fields={"scope": "im openid"},
541+
oidc_issuer="https://fauth.test",
542+
scopes_supported=["openid", "im"],
543+
)
544+
conn = Connection(API_URL)
545+
conn._capabilities_cache._cache = {"test": "test1"}
546+
conn.authenticate_oidc_authorization_code(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open)
547+
assert conn._capabilities_cache._cache == {}
548+
516549

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

0 commit comments

Comments
 (0)